OSDN Git Service

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