OSDN Git Service

Merge branch 'v05dev' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v06
[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 {
41         if @author
42           @new_panels = Panel.mylist(@author, 0, 5)
43         end
44       }
45       format.json {render text: Story.list_as_json_text(@stories, @author)}
46       format.jsonp {
47         render :json => "callback(" + @stories.to_json_list + ");"
48       }
49     end
50   end
51   
52   def list
53     @stories = Story.all
54
55     respond_to do |format|
56       format.html { render layout: 'system' }# index.html.erb
57       format.json { render json: @stories }
58     end
59   end
60
61   def browse
62     @story = Story.find(params[:id])
63
64     respond_to do |format|
65       format.html { render layout: 'system' } # show.html.erb
66       format.json { render json: @story }
67     end
68   end
69   
70   def new
71     @story = Story.new 
72     @story.supply_default
73     respond_to do |format|
74       format.html # new.html.erb
75       format.js
76       format.json { render json: @story.story_as_json(@author) }
77     end
78   end
79
80   def edit
81     @story = Story.edit(params[:id], @author)
82     respond_to do |format|
83       format.html 
84       format.js
85     end
86   end
87
88   def create
89     @story = Story.new 
90     @story.supply_default
91     @story.attributes = params[:story]
92     @story.overwrite @author
93     @comic = Comic.edit(@story.comic_id, @author) if @story.comic_id
94     @panel = Panel.show(@story.panel_id, @author) if @story.panel_id
95     
96     respond_to do |format|
97       if @story.store
98         flash[:notice] = I18n.t('flash.notice.created', :model => Story.model_name.human)
99         format.html { redirect_to action: :comic, id: @story.comic_id }
100         format.json { render json: @story.story_as_json(@author) }
101       else
102         format.html { render action: "new" }
103         format.json { render json: @story.errors, status: :unprocessable_entity }
104       end
105     end
106   end
107   
108   def update
109     @story = Story.edit(params[:id], @author)
110     ot = @story.t
111     @story.attributes = params[:story]
112     @story.overwrite @author
113     respond_to do |format|
114       if @story.store ot
115         flash[:notice] = I18n.t('flash.notice.updated', :model => Story.model_name.human)
116         format.html { redirect_to action: :comic, id: @story.comic_id }
117         format.json { head :ok }
118       else
119         format.html { render action: "edit" }
120         format.json { render json: @story.errors, status: :unprocessable_entity }
121       end
122     end
123   end
124
125   def destroy
126     @story = Story.edit(params[:id], @author)
127     respond_to do |format|
128       if @story.destroy_and_shorten
129         flash[:notice] = I18n.t('flash.notice.destroyed', :model => Story.model_name.human)
130         format.html { redirect_to :controller => 'stories', :action => :comic, :id => @story.comic_id }
131         format.json { head :ok }
132       else
133         flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => Story.model_name.human)
134         format.html { redirect_to story_path(@story) }
135         format.json { render json: @story.errors, status: :unprocessable_entity }
136       end
137     end
138   end
139 end