OSDN Git Service

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