OSDN Git Service

t30350#:fix destroy
[pettanr/pettanr.git] / app / controllers / stories_controller.rb
1 class StoriesController < ApplicationController
2   layout 'test' if MagicNumber['test_layout']
3   if MagicNumber['run_mode'] == 0
4     before_filter :authenticate_user!, :only => [:new, :create, :edit, :update, :destroy]
5     before_filter :authenticate_author, :only => [:new, :create, :edit, :update, :destroy]
6   else
7     before_filter :authenticate_user!, :only => [:index, :show, :comic, :new, :create, :edit, :update, :destroy]
8     before_filter :authenticate_author, :only => [:index, :show, :comic, :new, :create, :edit, :update, :destroy]
9   end
10   before_filter :authenticate_admin!, :only => [:list, :browse]
11
12   def index
13     @page = Story.page params[:page]
14     @page_size = Story.page_size params[:page_size]
15     @stories = Story.list(@page, @page_size)
16
17     respond_to do |format|
18       format.html # index.html.erb
19       format.json { render :json => @stories.to_json(Story.list_json_opt) }
20     end
21   end
22
23   def show
24     @story = Story.show(params[:id], @author)
25
26     respond_to do |format|
27       format.html # show.html.erb
28       format.json { render json: @story.story_as_json(@author) }
29     end
30   end
31   
32   def comic
33     @comic = Comic.show(params[:id], @author)
34     cnt = Story.count(:conditions => ['comic_id = ?', @comic.id]).to_i
35     @offset = Story.offset cnt, params[:offset]
36     @panel_count = Story.panel_count cnt, params[:count]
37     @stories = Story.play_list(@comic, @author, @offset, @panel_count)
38     respond_to do |format|
39       format.html # index.html.erb
40       format.json {render text: Story.list_as_json_text(@stories, @author)}
41       format.jsonp {
42         render :json => "callback(" + @stories.to_json_list + ");"
43       }
44     end
45   end
46   
47   def list
48     @stories = Story.all
49
50     respond_to do |format|
51       format.html { render layout: 'system' }# index.html.erb
52       format.json { render json: @stories }
53     end
54   end
55
56   def browse
57     @story = Story.find(params[:id])
58
59     respond_to do |format|
60       format.html { render layout: 'system' } # show.html.erb
61       format.json { render json: @story }
62     end
63   end
64   
65   def new
66     @story = Story.new 
67     @story.supply_default
68     respond_to do |format|
69       format.html # new.html.erb
70       format.js
71       format.json { render json: @story.story_as_json(@author) }
72     end
73   end
74
75   def edit
76     @story = Story.edit(params[:id], @author)
77     respond_to do |format|
78       format.html 
79       format.js
80     end
81   end
82
83   def create
84     @story = Story.new 
85     @story.supply_default
86     @story.attributes = params[:story]
87     @story.overwrite @author
88     @comic = Comic.edit(@story.comic_id, @author) if @story.comic_id
89     @panel = Panel.show(@story.panel_id, @author) if @story.panel_id
90     
91     respond_to do |format|
92       if @story.store
93         flash[:notice] = I18n.t('flash.notice.created', :model => Story.model_name.human)
94         format.html { redirect_to action: :comic, id: @story.comic_id }
95         format.json { render json: @story.story_as_json(@author) }
96       else
97         format.html { render action: "new" }
98         format.json { render json: @story.errors, status: :unprocessable_entity }
99       end
100     end
101   end
102   
103   def update
104     @story = Story.edit(params[:id], @author)
105     ot = @story.t
106     @story.attributes = params[:story]
107     @story.overwrite @author
108     respond_to do |format|
109       if @story.store ot
110         flash[:notice] = I18n.t('flash.notice.updated', :model => Story.model_name.human)
111         format.html { redirect_to action: :comic, id: @story.comic_id }
112         format.json { head :ok }
113       else
114         format.html { render action: "edit" }
115         format.json { render json: @story.errors, status: :unprocessable_entity }
116       end
117     end
118   end
119
120   def destroy
121     @story = Story.edit(params[:id], @author)
122     respond_to do |format|
123       if @story.destroy_and_shorten
124         flash[:notice] = I18n.t('flash.notice.destroyed', :model => Story.model_name.human)
125         format.html { redirect_to :controller => 'stories', :action => :comic, :id => @story.comic_id }
126         format.json { head :ok }
127       else
128         flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => Story.model_name.human)
129         format.html { redirect_to story_path(@story) }
130         format.json { render json: @story.errors, status: :unprocessable_entity }
131       end
132     end
133   end
134 end