OSDN Git Service

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