OSDN Git Service

add scroll player
[pettanr/pettanr.git] / app / controllers / application_controller.rb
1 class ApplicationController < ActionController::Base
2   protect_from_forgery
3   layout :devise_layout
4   before_filter :bf
5   
6   def devise_layout
7     if devise_controller?
8       case resource_name
9       when :admin
10         'guest'
11       when :user
12         'guest'
13       when :demand_user
14         'guest'
15       end
16     else
17       'application'
18     end
19   end
20   
21   def bf
22     @server_result = {
23       :location => {:controller => params[:controller], :action => params[:action]}
24     }
25     @server_result[:location][:id] = params[:id] if params[:id]
26     user = if user_signed_in?
27       current_user
28     else
29       nil
30     end
31     author = if user
32       user.author
33     else
34       nil
35     end
36     artist = if user
37       user.artist
38     else
39       nil
40     end
41     admin = if admin_signed_in?
42       current_admin
43     else
44       nil
45     end
46     demand_user = if demand_user_signed_in?
47       current_demand_user
48     else
49       nil
50     end
51     @operators = Operator.new [user, author, artist, admin, demand_user]
52   end
53   
54   def authenticate_reader
55     authenticate_user! unless @operators.reader?
56   end
57   
58   def authenticate_user
59     authenticate_user! unless @operators.user?
60   end
61   
62   def authenticate_resource_reader
63     authenticate_user! unless @operators.resource_reader?
64   end
65   
66   def authenticate_admin
67     authenticate_admin! unless @operators.admin?
68   end
69   
70   def authenticate_author
71     if @operators.author
72       true
73     else
74       respond_to do |format|
75         format.html { redirect_to main_app.new_author_path, :status => :found }
76         format.json { 
77           raise ActiveRecord::Forbidden
78         }
79       end
80       false
81     end
82   end
83       
84   def authenticate_artist
85     if @operators.artist
86       true
87     else
88       respond_to do |format|
89         format.html { redirect_to main_app.new_artist_path, :status => :found }
90         format.json { 
91           raise ActiveRecord::Forbidden
92         }
93       end
94       false
95     end
96   end
97   
98   def self.controller
99     Manifest.manifest.controllers[self.model.item_name]
100   end
101   
102   def self.profiler_manager
103     Manifest.manifest.profiler_managers[self.model.item_name]
104   end
105   
106   def set_model
107     @my_controller = Manifest.manifest.controllers[params[:controller].to_s]
108     @my_action = @my_controller.actions[params[:action].to_s]
109     @my_model = Manifest.manifest.models[@my_action.item_name]
110     @my_model_class = @my_model.classify
111   end
112   
113   def set_list
114     set_model
115     @my_list_model = Manifest.manifest.models[@my_action.item_name]
116     @my_list_model_class = @my_list_model.classify
117     @list = Locmare::ListGroup.list @my_action.controller.name, @my_action.action_name, @operators, params
118   end
119   
120   def filer_list
121     set_list
122     @items = @list.items 
123     respond_to do |format|
124       format.html {
125         @filer = Locmare::Filer.new @list.item_name, @items, @list.page_status, @operators
126         render @filer.template_name, :locals => {
127           :filer => @filer
128         }
129       }
130       list_json_format @list, format
131       format.atom 
132       format.rss
133     end
134   end
135   
136   def list_json_format list, format
137     format.json {
138       res = {
139         :page_status => list.page_status.to_hash,
140         # rails3.2 has problem
141         # y method defined as private
142         # attribute y conflict at PanelPicture, balloon ..etc
143         # use i.attributes[name]
144         :list => list.items.map{|i| i.attributes}
145         # :list => @items.to_json
146       }
147       render json:  res.to_json
148     }
149   end
150   
151   def set_play
152     set_list
153     @binder_action = @my_action.original
154     @binder_controller = @binder_action.controller
155     @binder_model = Manifest.manifest.models[@binder_action.item_name]
156     @binder_model_class = @binder_model.classify
157     @item = @binder_model_class.show(params[:id], @operators)
158   end
159   
160   def play_list
161     @items = @list.items #.map {|sp| sp.root }
162     @count = @list.total
163     @pager = @list.page_status.pager
164   end
165   
166   def set_show
167     set_model
168     @item = @my_model_class.show(params[:id], @operators)
169   end
170   
171   def show_prof_format format
172     format.prof {
173       self.formats = [:html]
174       @profiler = Locmare::Profiler.new @my_model_class.item_name, @item, @operators
175       render @profiler.template_name, :locals => {
176         :profiler => @profiler
177       }
178     }
179   end
180   
181   def show_json_format format
182     format.json { render json: @item.to_json(@my_model_class.show_json_opt) }
183   end
184   
185   def show_json_format_for_root format
186     format.json { render json: @item.to_json(@my_model_class.show_json_opt_for_root) }
187   end
188   
189   def set_new
190     set_model
191     @item = @my_model_class.new
192     @item.boosts 'post'
193     @item.supply_default
194   end
195   
196   def set_edit
197     set_model
198     @item = @my_model_class.edit(params[:id], @operators)
199     @item.boosts 'post'
200   end
201   
202   def render_form
203     respond_to do |format|
204       format.html { 
205         @form = Locmare::Bucket.factory @item.item_name, @item, true, true, @operators
206         render @form.template_name, :locals => {
207           :form => @form
208         }
209       }
210       format.json { render json: @item.to_json }
211     end
212   end
213   
214   def form_new
215     set_new
216     render_form
217   end
218   
219   def form_edit
220     set_edit
221     render_form
222   end
223   
224   def created_html_format format, redirect_url = nil
225     format.html {
226       flash[:notice] = I18n.t('flash.notice.created', :model => @my_model_class.model_name.human)
227       redirect_to (redirect_url ? redirect_url : @item)
228     }
229   end
230   
231   def created_json_format format
232     format.json {
233       render json: @item.to_json(@my_model_class.show_json_opt), status: :created, location: @item
234     }
235   end
236   
237   def not_created_html_format format
238     format.html {
239       flash[:notice] = I18n.t('flash.notice.not_created', :model => @my_model_class.model_name.human)
240       render_form
241     }
242   end
243   
244   def not_created_json_format format
245     format.json {
246       render json: @item.errors, status: :unprocessable_entity
247     }
248   end
249   
250   def render_create redirect_url = nil
251     if @item.save
252       respond_to do |format|
253         created_html_format format, redirect_url
254         created_json_format format
255       end
256     else
257       respond_to do |format|
258         not_created_html_format format
259         not_created_json_format format
260       end
261     end
262   end
263   
264   def leaf_created_html_format format, redirect_url
265     format.html {
266       flash[:notice] = I18n.t('flash.notice.created', :model => @my_model_class.model_name.human)
267       redirect_to redirect_url
268     }
269   end
270   
271   def leaf_not_created_html_format format, redirect_url
272     format.html {
273       flash[:notice] = I18n.t('flash.notice.not_created', :model => @my_model_class.model_name.human)
274       redirect_to redirect_url
275     }
276   end
277   
278   def leaf_render_create redirect_url
279     if @item.store @operators
280       respond_to do |format|
281         leaf_created_html_format format, redirect_url
282         created_json_format format
283       end
284     else
285       respond_to do |format|
286         leaf_not_created_html_format format, redirect_url
287         not_created_json_format format
288       end
289     end
290   end
291   
292   def updated_html_format format, redirect_url = nil
293     format.html {
294       flash[:notice] = I18n.t('flash.notice.updated', :model => @my_model_class.model_name.human)
295       redirect_to (redirect_url ? redirect_url : @item)
296     }
297   end
298   
299   def updated_json_format format
300     format.json {
301       head :ok
302     }
303   end
304   
305   def not_updated_html_format format
306     format.html {
307       flash[:notice] = I18n.t('flash.notice.not_updated', :model => @my_model_class.model_name.human)
308       render_form
309     }
310   end
311   
312   def not_updated_json_format format
313     format.json {
314       render json: @item.errors, status: :unprocessable_entity
315     }
316   end
317   
318   def render_update redirect_url = nil
319     if @item.save
320       respond_to do |format|
321         updated_html_format format, redirect_url
322         updated_json_format format
323       end
324     else
325       respond_to do |format|
326         not_updated_html_format format
327         not_updated_json_format format
328       end
329     end
330   end
331   
332   def leaf_updated_html_format format, redirect_url
333     format.html {
334       flash[:notice] = I18n.t('flash.notice.updated', :model => @my_model_class.model_name.human)
335       redirect_to redirect_url
336     }
337   end
338   
339   def leaf_not_updated_html_format format, redirect_url
340     format.html {
341       flash[:notice] = I18n.t('flash.notice.not_updated', :model => @my_model_class.model_name.human)
342       redirect_to redirect_url
343     }
344   end
345   
346   def leaf_render_update ot, redirect_url
347     if @item.store @operators, ot
348       respond_to do |format|
349         leaf_updated_html_format format, redirect_url
350         updated_json_format format
351       end
352     else
353       respond_to do |format|
354         leaf_not_updated_html_format format, redirect_url
355         not_updated_json_format format
356       end
357     end
358   end
359   
360   def destroyed_html_format format, redirect_url
361     format.html {
362       flash[:notice] = I18n.t('flash.notice.destroyed', :model => @my_model_class.model_name.human)
363       redirect_to redirect_url
364     }
365   end
366   
367   def destroyed_json_format format
368     format.json {
369       head :ok
370     }
371   end
372   
373   def not_destroyed_html_format format
374     format.html {
375       flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => @my_model_class.model_name.human)
376       redirect_to @item
377     }
378   end
379   
380   def not_destroyed_json_format format
381     format.json {
382       render json: @item.errors, status: :unprocessable_entity
383     }
384   end
385   
386   def render_destroy redirect_url = nil
387     if @item.destroy
388       respond_to do |format|
389         destroyed_html_format format, redirect_url
390         destroyed_json_format format
391       end
392     else
393       respond_to do |format|
394         not_destroyed_html_format format
395         not_destroyed_json_format format
396       end
397     end
398   end
399   
400   def format_filer format
401     format.html {
402       @paginate = @@model.list_paginate(@page, @page_size)
403       render :template => 'system/filer', :locals => {
404         :items => @items, :model => @@model, 
405         :operators => @operators, :pager => @paginate
406       }
407     }
408   end
409   
410   def format_prof format
411     format.prof { 
412       @profiler = self.class.profiler_manager.open(@item, @operators)
413       render :template => 'system/prof', :locals => {
414         :profiler => @profiler
415       }
416     }
417   end
418   
419   def assist_items controller_name, action_name
420     list = Locmare::ListGroup.list controller_name, action_name, @operators, {:id => @item.id, :page => 1, :page_size => 5}
421     list.items
422   end
423   
424   def set_image(file)
425     if file.respond_to?(:read)
426       file.read
427     else
428       Base64.decode64(file.to_s.gsub(' ', '+'))
429     end
430   end
431   
432   def ymd_to_time ymd_str
433     return nil if ymd_str.blank?
434     date = nil
435     begin
436       date = Time.parse(ymd_str[0..3] + '/' + ymd_str[4..5] + '/' + ymd_str[6..7])
437     rescue
438       date = nil
439     end
440     date
441   end
442   
443   def export_url demander_url, action, token, date
444     u = demander_url + (demander_url[-1] == '/' ? '' : '/')
445     prm = '?auth_token=' + token
446     prm = prm + '&date=' + date.strftime("%Y%m%d") unless date.blank?
447     u = URI.join(u, action + '.json' + prm)
448     u.to_s
449   end
450   
451   def export_from_provider url
452     res = nil
453     begin
454       json = RestClient.get url
455       res = JSON.parse json
456     rescue
457     end
458     res
459   end
460   
461   def export_by action, provider_status, ymd
462     t = ymd_to_time ymd
463     url = export_url provider_status.provider.demander_url, action, provider_status.token, t
464     export_from_provider(url)
465   end
466   
467     rescue_from Pettanr::NotWork, :with => :render_not_work
468     def render_not_work(exception = nil)
469       if exception
470         logger.info "Rendering , :: #{exception.message}"
471       end
472       respond_to do |format|
473         format.html { 
474           render :file => "#{Rails.root}/public/not_work.html", :layout => false
475         }
476         format.json { 
477           render :text => "400 Not work", :status => 400
478         }
479       end
480     end
481     
482   if Rails.env == 'production'
483     rescue_from ActiveRecord::RecordNotFound, :with => :render_404
484     rescue_from ActiveRecord::Forbidden, :with => :render_403
485     
486     private
487     def render_404(exception = nil)
488       if exception
489         logger.info "Rendering 404: #{exception.message}"
490       end
491       respond_to do |format|
492         format.html { 
493           render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
494         }
495         format.json { 
496           render :text => "404 Not found", :status => 404
497         }
498       end
499     end
500     
501     def render_403(exception = nil)
502       if exception
503         logger.info "Rendering 403: #{exception.message}"
504       end
505       respond_to do |format|
506         format.html { 
507           render :file => "#{Rails.root}/public/403.html", :status => 404, :layout => false
508         }
509         format.json { 
510           render :text => "403 Forbidden", :status => 403
511         }
512       end
513     end
514   end
515 end