OSDN Git Service

t30350#:fix destroy
[pettanr/pettanr.git] / app / controllers / stories_controller.rb
index 4f41dc1..3832ea5 100644 (file)
@@ -1,23 +1,43 @@
 class StoriesController < ApplicationController
-  layout 'test' if Pettanr::TestLayout
-  if Const.run_mode == 0
+  layout 'test' if MagicNumber['test_layout']
+  if MagicNumber['run_mode'] == 0
     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, :new, :create, :edit, :update, :destroy]
+    before_filter :authenticate_user!, :only => [:index, :show, :comic, :new, :create, :edit, :update, :destroy]
+    before_filter :authenticate_author, :only => [:index, :show, :comic, :new, :create, :edit, :update, :destroy]
   end
   before_filter :authenticate_admin!, :only => [:list, :browse]
 
+  def index
+    @page = Story.page params[:page]
+    @page_size = Story.page_size params[:page_size]
+    @stories = Story.list(@page, @page_size)
+
+    respond_to do |format|
+      format.html # index.html.erb
+      format.json { render :json => @stories.to_json(Story.list_json_opt) }
+    end
+  end
+
   def show
+    @story = Story.show(params[:id], @author)
+
+    respond_to do |format|
+      format.html # show.html.erb
+      format.json { render json: @story.story_as_json(@author) }
+    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.list(@comic, @author, @offset, @panel_count)
+    @stories = Story.play_list(@comic, @author, @offset, @panel_count)
     respond_to do |format|
       format.html # index.html.erb
-      format.json {
-        render :json => @stories.to_json_list
-      }
+      format.json {render text: Story.list_as_json_text(@stories, @author)}
       format.jsonp {
         render :json => "callback(" + @stories.to_json_list + ");"
       }
@@ -42,21 +62,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 }
+      format.json { render json: @story.story_as_json(@author) }
     end
   end
 
-  # GET /stories/1/edit
-  # GET /stories/1.js/edit
   def edit
     @story = Story.edit(params[:id], @author)
     respond_to do |format|
@@ -65,18 +80,19 @@ 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) if @story.comic_id
+    @panel = Panel.show(@story.panel_id, @author) if @story.panel_id
     
     respond_to do |format|
       if @story.store
-        format.html { redirect_to action: :show, id: @story.comic_id }
-        format.json { render json: @story.to_json() }
+        flash[:notice] = I18n.t('flash.notice.created', :model => Story.model_name.human)
+        format.html { redirect_to action: :comic, id: @story.comic_id }
+        format.json { render json: @story.story_as_json(@author) }
       else
         format.html { render action: "new" }
         format.json { render json: @story.errors, status: :unprocessable_entity }
@@ -84,8 +100,6 @@ class StoriesController < ApplicationController
     end
   end
   
-  # PUT /stories/1
-  # PUT /stories/1.json
   def update
     @story = Story.edit(params[:id], @author)
     ot = @story.t
@@ -93,7 +107,8 @@ class StoriesController < ApplicationController
     @story.overwrite @author
     respond_to do |format|
       if @story.store ot
-        format.html { redirect_to action: :show, id: @story.comic_id }
+        flash[:notice] = I18n.t('flash.notice.updated', :model => Story.model_name.human)
+        format.html { redirect_to action: :comic, id: @story.comic_id }
         format.json { head :ok }
       else
         format.html { render action: "edit" }
@@ -102,17 +117,18 @@ class StoriesController < ApplicationController
     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 :controller => 'stories', :action => :comic, :id => @story.comic_id }
+        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