OSDN Git Service

fix ctl list
[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, :scroll_panels, :panels, :by_author, :by_panel]
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   
12   def self.model
13     Scroll
14   end
15   
16   def index
17     public_list
18   end
19   
20   def scroll_panels
21     has_many_list
22   end
23   
24   def panels
25     has_many_list
26   end
27   
28   def by_author
29     filter_list
30   end
31   
32   def by_panel
33     filter_list
34   end
35   
36   def show
37     @item = Scroll.show(params[:id], @operators)
38
39     respond_to do |format|
40       format.html {
41         if @operators.author
42           @new_panel_items = assist_items('panel', 'private_list')
43           @new_panel_filer = assist_filer 'panel', @new_panel_items
44         end
45       }
46       format.json { render json: @item.to_json(Scroll.show_json_opt) }
47       format_prof format
48       format.atom 
49       format.rss 
50     end
51   end
52
53   def play
54     @scroll = Scroll.show(params[:id], @operators)
55     cnt = ScrollPanel.count(:conditions => ['scroll_id = ?', @scroll.id]).to_i
56     @offset = ScrollPanel.offset cnt, params[:offset]
57     @panel_count = ScrollPanel.panel_count cnt, params[:count]
58     @scroll_panels = ScrollPanel.play_list(@scroll, @author, @offset, @panel_count)
59     respond_to do |format|
60       format.html {
61         @prev_offset = if @offset > 0
62           if @offset - @panel_count < 0
63             0
64           else
65             @offset - @panel_count
66           end
67         else
68           nil
69         end
70         @next_offset = if @offset + @panel_count > cnt
71           nil
72         else
73           @offset + @panel_count
74         end
75         if @author
76           @new_panels = Panel.mylist(@author, 1, 5)
77         end
78       }
79       format.json {render text: ScrollPanel.list_as_json_text(@scroll_panels, @operators)}
80       format.jsonp {
81         render :json => "callback(" + @scroll_panels.to_json_list + ");"
82       }
83     end
84   end
85   
86   def count
87     @scroll = {:count => Scroll.visible_count}
88     respond_to do |format|
89       format.json { render json: @scroll.to_json }
90     end
91   end
92   
93   def new
94     @scroll = Scroll.new
95     @scroll.supply_default
96     respond_to do |format|
97       format.html
98       format.js
99       format.json { render json: @scroll.to_json(Scroll.show_json_opt) }
100     end
101   end
102
103   def edit
104     @scroll = Scroll.edit(params[:id], @operators)
105     respond_to do |format|
106       format.html 
107       format.js
108     end
109   end
110
111   def create
112     @scroll = Scroll.new
113     @scroll.supply_default 
114     @scroll.attributes = params[:scroll]
115     @scroll.overwrite @operators
116
117     respond_to do |format|
118       if @scroll.save
119         flash[:notice] = I18n.t('flash.notice.created', :model => Scroll.model_name.human)
120         format.html { redirect_to @scroll }
121         format.json { render json: @scroll.to_json(Scroll.show_json_opt), status: :created, location: @scroll }
122       else
123         flash[:notice] = I18n.t('flash.notice.not_created', :model => Scroll.model_name.human)
124         format.html { render action: "new" }
125         format.json { render json: @scroll.errors, status: :unprocessable_entity }
126       end
127     end
128   end
129
130   def update
131     @scroll = Scroll.edit(params[:id], @operators)
132     @scroll.attributes = params[:scroll]
133     @scroll.overwrite @operators
134     respond_to do |format|
135       if @scroll.save
136         flash[:notice] = I18n.t('flash.notice.updated', :model => Scroll.model_name.human)
137         format.html { redirect_to @scroll }
138         format.json { head :ok }
139       else
140         flash[:notice] = I18n.t('flash.notice.not_updated', :model => Scroll.model_name.human)
141         format.html { render action: "edit" }
142         format.json { render json: @scroll.errors, status: :unprocessable_entity }
143       end
144     end
145   end
146
147   def destroy
148     @scroll = Scroll.edit(params[:id], @operators)
149     respond_to do |format|
150       if @scroll.destroy_with_scroll_panel
151         flash[:notice] = I18n.t('flash.notice.destroyed', :model => Scroll.model_name.human)
152         format.html { redirect_to '/home/scrolls' }
153         format.json { head :ok }
154       else
155         flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => Scroll.model_name.human)
156         format.html { redirect_to @scroll }
157         format.json { render json: @scroll.errors, status: :unprocessable_entity }
158       end
159     end
160   end
161 end