OSDN Git Service

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