OSDN Git Service

t#30102#30122:update i18n index
[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_user!, :only => [:index, :show, :comic, :new, :create, :edit, :update, :destroy]
7   end
8   before_filter :authenticate_admin!, :only => [:list, :browse]
9
10   def index
11     @page = Story.page params[:page]
12     @page_size = Story.page_size params[:page_size]
13     @stories = Story.list(@page, @page_size)
14
15     respond_to do |format|
16       format.html # index.html.erb
17       format.json { render :json => @stories.to_json(Story.list_json_opt) }
18     end
19   end
20
21   def show
22     @story = Story.show(params[:id], @author)
23
24     respond_to do |format|
25       format.html # show.html.erb
26       format.json { render json: @story.story_as_json(@author) }
27     end
28   end
29   
30   def comic
31     @comic = Comic.show(params[:id], @author)
32     cnt = Story.count(:conditions => ['comic_id = ?', @comic.id]).to_i
33     @offset = Story.offset cnt, params[:offset]
34     @panel_count = Story.panel_count cnt, params[:count]
35     @stories = Story.play_list(@comic, @author, @offset, @panel_count)
36     respond_to do |format|
37       format.html # index.html.erb
38       format.json {render text: Story.list_as_json_text(@stories, @author)}
39       format.jsonp {
40         render :json => "callback(" + @stories.to_json_list + ");"
41       }
42     end
43   end
44   
45   def list
46     @stories = Story.all
47
48     respond_to do |format|
49       format.html { render layout: 'system' }# index.html.erb
50       format.json { render json: @stories }
51     end
52   end
53
54   def browse
55     @story = Story.find(params[:id])
56
57     respond_to do |format|
58       format.html { render layout: 'system' } # show.html.erb
59       format.json { render json: @story }
60     end
61   end
62   
63   # GET /stories/new
64   # GET /stories/new.js
65   # GET /stories/new.json
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   # GET /stories/1/edit
77   # GET /stories/1.js/edit
78   def edit
79     @story = Story.edit(params[:id], @author)
80     respond_to do |format|
81       format.html 
82       format.js
83     end
84   end
85
86   # POST /stories
87   # POST /stories.json
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)
94     @panel = Panel.show(@story.panel_id, @author)
95     
96     respond_to do |format|
97       if @story.store
98         format.html { redirect_to action: :comic, id: @story.comic_id }
99         format.json { render json: @story.story_as_json(@author) }
100       else
101         format.html { render action: "new" }
102         format.json { render json: @story.errors, status: :unprocessable_entity }
103       end
104     end
105   end
106   
107   # PUT /stories/1
108   # PUT /stories/1.json
109   def update
110     @story = Story.edit(params[:id], @author)
111     ot = @story.t
112     @story.attributes = params[:story]
113     @story.overwrite @author
114     respond_to do |format|
115       if @story.store ot
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   # DELETE /stories/1
126   # DELETE /stories/1.js
127   # DELETE /stories/1.json
128   def destroy
129     @story = Story.edit(params[:id], @author)
130     Story.transaction do
131       @story.destroy_and_shorten
132     end
133     respond_to do |format|
134       format.html { redirect_to story_path(@story.comic) }
135       format.json { head :ok }
136     end
137   end
138 end