OSDN Git Service

Merge branch 'v04' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v04
[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 {
28         render :json => '[' + @stories.map {|i| i.story_as_json(@author)}.join(',') + ']'
29       }
30       format.jsonp {
31         render :json => "callback(" + @stories.to_json_list + ");"
32       }
33     end
34   end
35   
36   def list
37     @stories = Story.all
38
39     respond_to do |format|
40       format.html { render layout: 'system' }# index.html.erb
41       format.json { render json: @stories }
42     end
43   end
44
45   def browse
46     @story = Story.find(params[:id])
47
48     respond_to do |format|
49       format.html { render layout: 'system' } # show.html.erb
50       format.json { render json: @story }
51     end
52   end
53   
54   # GET /stories/new
55   # GET /stories/new.js
56   # GET /stories/new.json
57   def new
58     @story = Story.new 
59     @story.supply_default
60     respond_to do |format|
61       format.html # new.html.erb
62       format.js
63       format.json { render json: @story.story_as_json(@author) }
64     end
65   end
66
67   # GET /stories/1/edit
68   # GET /stories/1.js/edit
69   def edit
70     @story = Story.edit(params[:id], @author)
71     respond_to do |format|
72       format.html 
73       format.js
74     end
75   end
76
77   # POST /stories
78   # POST /stories.json
79   def create
80     @story = Story.new 
81     @story.supply_default
82     @story.attributes = params[:story]
83     @story.overwrite @author
84     @comic = Comic.edit(@story.comic_id, @author)
85     @panel = Panel.show(@story.panel_id, @author)
86     
87     respond_to do |format|
88       if @story.store
89         format.html { redirect_to action: :comic, id: @story.comic_id }
90         format.json { render json: @story.story_as_json(@author) }
91       else
92         format.html { render action: "new" }
93         format.json { render json: @story.errors, status: :unprocessable_entity }
94       end
95     end
96   end
97   
98   # PUT /stories/1
99   # PUT /stories/1.json
100   def update
101     @story = Story.edit(params[:id], @author)
102     ot = @story.t
103     @story.attributes = params[:story]
104     @story.overwrite @author
105     respond_to do |format|
106       if @story.store ot
107         format.html { redirect_to action: :comic, id: @story.comic_id }
108         format.json { head :ok }
109       else
110         format.html { render action: "edit" }
111         format.json { render json: @story.errors, status: :unprocessable_entity }
112       end
113     end
114   end
115
116   # DELETE /stories/1
117   # DELETE /stories/1.js
118   # DELETE /stories/1.json
119   def destroy
120     @story = Story.edit(params[:id], @author)
121     Story.transaction do
122       @story.destroy_and_shorten
123     end
124     respond_to do |format|
125       format.html { redirect_to story_path(@story.comic) }
126       format.json { head :ok }
127     end
128   end
129 end