OSDN Git Service

diet
[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 @author
51     @form_opt = {}
52     respond_to do |format|
53       format.html # new.html.erb
54       format.js { @form_opt = {:remote => true} ;  render action: "new" }
55       format.json { render json: @story }
56     end
57   end
58
59   # GET /stories/1/edit
60   # GET /stories/1.js/edit
61   def edit
62     @story = Story.show(params[:id], @author)
63     respond_to do |format|
64       format.html 
65       format.js
66     end
67   end
68
69   # POST /stories
70   # POST /stories.json
71   def create
72     @story = Story.new 
73     @story.supply_default @author
74     @story.attributes = params[:story]
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.show(params[:id], @author)
91     @story.author_id = @author.id
92     ot = @story.t
93     @story.attributes = params[:story]
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.show(params[:id], @author)
110     @story.destroy_and_shorten
111     respond_to do |format|
112       format.html { redirect_to story_path(@story.comic) }
113       format.json { head :ok }
114     end
115   end
116 end