OSDN Git Service

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