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