OSDN Git Service

ef9bb0c9c6a4365ae1f855660f9539e63a7c9a1d
[pettanr/pettanr.git] / app / controllers / scrolls_controller.rb
1 class ScrollsController < 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_reader, :only => [:top, :index, :show, :play]
8     before_filter :authenticate_user, :only => [:new, :create, :edit, :update, :destroy]
9     before_filter :authenticate_author, :only => [:new, :create, :edit, :update, :destroy]
10   end
11   before_filter :authenticate_admin!, :only => [:list, :browse]
12
13   @@model = Scroll
14   @@controller = Pettanr::Application::manifest.controller_managers[@@model.item_name]
15   @@profiler_manager = Pettanr::Application::manifest.profiler_managers[@@model.item_name]
16   
17   def index
18     action_name = params[:action]
19     @action = @@controller.open(action_name, params, @operators)
20     @items = @action.items 
21     respond_to do |format|
22       format.html {
23         @filer = @action.filer
24         render :template => 'system/filer', :locals => {
25           :filer => @filer
26         }
27       }
28       format.json { render json: @items.to_json(@@model.list_json_opt) }
29       format.atom 
30       format.rss
31     end
32   end
33
34   def show
35     @item = Scroll.show(params[:id], @operators)
36
37     respond_to do |format|
38       format.html {
39         @scroll = @item
40         if @operators.author
41           @new_panels = Panel.mylist(@operators.author, 1, 5)
42         end
43       }
44       format.json { render json: @item.to_json(Scroll.show_json_opt) }
45       format.prof { 
46         @profiler = @@profiler_manager.open(@item, @operators)
47         render :template => 'system/prof', :locals => {
48           :profiler => @profiler
49         }
50       }
51       format.atom 
52       format.rss 
53     end
54   end
55
56   def play
57     @scroll = Scroll.show(params[:id], [@user, @admin])
58     cnt = ScrollPanel.count(:conditions => ['scroll_id = ?', @scroll.id]).to_i
59     @offset = ScrollPanel.offset cnt, params[:offset]
60     @panel_count = ScrollPanel.panel_count cnt, params[:count]
61     @scroll_panels = ScrollPanel.play_list(@scroll, @author, @offset, @panel_count)
62     respond_to do |format|
63       format.html {
64         @prev_offset = if @offset > 0
65           if @offset - @panel_count < 0
66             0
67           else
68             @offset - @panel_count
69           end
70         else
71           nil
72         end
73         @next_offset = if @offset + @panel_count > cnt
74           nil
75         else
76           @offset + @panel_count
77         end
78         if @author
79           @new_panels = Panel.mylist(@author, 1, 5)
80         end
81       }
82       format.json {render text: ScrollPanel.list_as_json_text(@scroll_panels, @author)}
83       format.jsonp {
84         render :json => "callback(" + @scroll_panels.to_json_list + ");"
85       }
86     end
87   end
88   
89   def count
90     @scroll = {:count => Scroll.visible_count}
91     respond_to do |format|
92       format.json { render json: @scroll.to_json }
93     end
94   end
95   
96   def list
97     @scrolls = Scroll.all
98
99     respond_to do |format|
100       format.html { render layout: 'system' }# index.html.erb
101       format.json { render json: @scrolls }
102     end
103   end
104
105   def browse
106     @scroll = Scroll.find(params[:id])
107
108     respond_to do |format|
109       format.html { render layout: 'system' } # show.html.erb
110       format.json { render json: @scroll }
111     end
112   end
113   
114   def new
115     @scroll = Scroll.new
116     @scroll.supply_default
117     respond_to do |format|
118       format.html # new.html.erb
119       format.js
120       format.json { render json: @scroll.to_json(Scroll.show_json_opt) }
121     end
122   end
123
124   def edit
125     @scroll = Scroll.edit(params[:id], @author)
126     respond_to do |format|
127       format.html 
128       format.js
129     end
130   end
131
132   def create
133     @scroll = Scroll.new
134     @scroll.supply_default 
135     @scroll.attributes = params[:scroll]
136     @scroll.overwrite @operators
137
138     respond_to do |format|
139       if @scroll.save
140         flash[:notice] = I18n.t('flash.notice.created', :model => Scroll.model_name.human)
141         format.html { redirect_to @scroll }
142         format.json { render json: @scroll.to_json(Scroll.show_json_opt), status: :created, location: @scroll }
143       else
144         flash[:notice] = I18n.t('flash.notice.not_created', :model => Scroll.model_name.human)
145         format.html { render action: "new" }
146         format.json { render json: @scroll.errors, status: :unprocessable_entity }
147       end
148     end
149   end
150
151   def update
152     @scroll = Scroll.edit(params[:id], @author)
153     @scroll.attributes = params[:scroll]
154     @scroll.overwrite @author
155     respond_to do |format|
156       if @scroll.save
157         flash[:notice] = I18n.t('flash.notice.updated', :model => Scroll.model_name.human)
158         format.html { redirect_to @scroll }
159         format.json { head :ok }
160       else
161         flash[:notice] = I18n.t('flash.notice.not_updated', :model => Scroll.model_name.human)
162         format.html { render action: "edit" }
163         format.json { render json: @scroll.errors, status: :unprocessable_entity }
164       end
165     end
166   end
167
168   def destroy
169     @scroll = Scroll.edit(params[:id], @author)
170     respond_to do |format|
171       if @scroll.destroy_with_scroll_panel
172         flash[:notice] = I18n.t('flash.notice.destroyed', :model => Scroll.model_name.human)
173         format.html { redirect_to '/home/scrolls' }
174         format.json { head :ok }
175       else
176         flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => Scroll.model_name.human)
177         format.html { redirect_to @scroll }
178         format.json { render json: @scroll.errors, status: :unprocessable_entity }
179       end
180     end
181   end
182 end