OSDN Git Service

temp
[pettanr/pettanr.git] / app / controllers / stories_controller.rb
index bcb470e..c828287 100644 (file)
@@ -1,32 +1,61 @@
 class StoriesController < ApplicationController
   layout 'test' if MagicNumber['test_layout']
   if MagicNumber['run_mode'] == 0
-    before_filter :authenticate_user!, :only => [:new, :create, :edit, :update, :destroy]
+    before_filter :authenticate_user, :only => [:new, :create, :edit, :update, :destroy]
+    before_filter :authenticate_author, :only => [:new, :create, :edit, :update, :destroy]
   else
-    before_filter :authenticate_user!, :only => [:index, :show, :comic, :new, :create, :edit, :update, :destroy]
+    before_filter :authenticate_reader, :only => [:index, :show, :play]
+    before_filter :authenticate_user, :only => [:new, :create, :edit, :update, :destroy]
+    before_filter :authenticate_author, :only => [:new, :create, :edit, :update, :destroy]
   end
   before_filter :authenticate_admin!, :only => [:list, :browse]
 
+  @@model = Story
+  def index
+    set_filer
+
+    respond_to do |format|
+      format_filer format
+      format.json { render json: @items.to_json(@@model.list_json_opt) }
+    end
+  end
+
   def show
-    @story = Story.show(params[:id], @author)
+    @item = Story.show(params[:id], @operators)
 
     respond_to do |format|
-      format.html # show.html.erb
-      format.json { render json: @story.story_as_json(@author) }
+      format.html { 
+        @story = @item
+        if @operators.author
+          @new_sheets = Sheet.mylist(@operators.author, 1, 5)
+        end
+      }
+      format_prof format
+      format.json { render json: @item.to_json(Story.show_json_opt) }
     end
   end
   
-  def comic
-    @comic = Comic.show(params[:id], @author)
-    cnt = Story.count(:conditions => ['comic_id = ?', @comic.id]).to_i
-    @offset = Story.offset cnt, params[:offset]
-    @panel_count = Story.panel_count cnt, params[:count]
-    @stories = Story.play_list(@comic, @author, @offset, @panel_count)
+  def play
+    @story = Story.show(params[:id], [@user, @admin])
+    @page = StorySheet.page params[:page]
+    @sheet = StorySheet.play_sheet(@story, @author, @page)
     respond_to do |format|
-      format.html # index.html.erb
-      format.json {render text: Story.list_as_json_text(@stories, @author)}
+      format.html {
+        @paginate = StorySheet.play_paginate(@story, @page)
+        if @author
+          @new_sheets = Sheet.mylist(@author, 1, 5)
+        end
+      }
+      format.json {
+        if @sheet
+          @story_sheets = SheetPanel.play_list @sheet, @author
+          render text: SheetPanel.list_as_json_text(@story_sheets, @author)
+        else
+          render text: ''
+        end
+      }
       format.jsonp {
-        render :json => "callback(" + @stories.to_json_list + ");"
+        render :json => "callback(" + @story_sheets.to_json_list + ");"
       }
     end
   end
@@ -49,21 +78,16 @@ class StoriesController < ApplicationController
     end
   end
   
-  # GET /stories/new
-  # GET /stories/new.js
-  # GET /stories/new.json
   def new
     @story = Story.new 
     @story.supply_default
     respond_to do |format|
       format.html # new.html.erb
       format.js
-      format.json { render json: @story.story_as_json(@author) }
+      format.json { render json: @story.to_json(Story.show_json_opt) }
     end
   end
 
-  # GET /stories/1/edit
-  # GET /stories/1.js/edit
   def edit
     @story = Story.edit(params[:id], @author)
     respond_to do |format|
@@ -72,56 +96,56 @@ class StoriesController < ApplicationController
     end
   end
 
-  # POST /stories
-  # POST /stories.json
   def create
     @story = Story.new 
     @story.supply_default
     @story.attributes = params[:story]
-    @story.overwrite @author
-    @comic = Comic.edit(@story.comic_id, @author)
-    @panel = Panel.show(@story.panel_id, @author)
+    @story.overwrite
+    @comic = Comic.edit(@story.comic_id, @author) if @story.comic_id
     
     respond_to do |format|
-      if @story.store
-        format.html { redirect_to action: :comic, id: @story.comic_id }
-        format.json { render json: @story.story_as_json(@author) }
+      if @story.store @author
+        flash[:notice] = I18n.t('flash.notice.created', :model => Story.model_name.human)
+        format.html { redirect_to play_story_path(@story, :page => @story.t.to_i + 1) }
+        format.json { render json: @story.to_json(Story.show_json_opt) }
       else
+        flash[:notice] = I18n.t('flash.notice.not_created', :model => Story.model_name.human)
         format.html { render action: "new" }
         format.json { render json: @story.errors, status: :unprocessable_entity }
       end
     end
   end
   
-  # PUT /stories/1
-  # PUT /stories/1.json
   def update
     @story = Story.edit(params[:id], @author)
     ot = @story.t
     @story.attributes = params[:story]
-    @story.overwrite @author
+    @story.overwrite
     respond_to do |format|
-      if @story.store ot
-        format.html { redirect_to action: :comic, id: @story.comic_id }
+      if @story.store  @author, ot
+        flash[:notice] = I18n.t('flash.notice.updated', :model => Story.model_name.human)
+        format.html { redirect_to play_story_path(@story, :page => @story.t.to_i + 1) }
         format.json { head :ok }
       else
+        flash[:notice] = I18n.t('flash.notice.not_updated', :model => Story.model_name.human)
         format.html { render action: "edit" }
         format.json { render json: @story.errors, status: :unprocessable_entity }
       end
     end
   end
 
-  # DELETE /stories/1
-  # DELETE /stories/1.js
-  # DELETE /stories/1.json
   def destroy
     @story = Story.edit(params[:id], @author)
-    Story.transaction do
-      @story.destroy_and_shorten
-    end
     respond_to do |format|
-      format.html { redirect_to story_path(@story.comic) }
-      format.json { head :ok }
+      if @story.destroy_and_shorten
+        flash[:notice] = I18n.t('flash.notice.destroyed', :model => Story.model_name.human)
+        format.html { redirect_to comic_path(@story.comic)}
+        format.json { head :ok }
+      else
+        flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => Story.model_name.human)
+        format.html { redirect_to story_path(@story) }
+        format.json { render json: @story.errors, status: :unprocessable_entity }
+      end
     end
   end
 end