OSDN Git Service

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