OSDN Git Service

mrg
[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.item_name, @my_action.list_name
118   end
119   
120   def filer_list
121     set_list
122     list_result = @list.open(@operators, 
123       {:id => params[:id], :page => params[:page], :page_size => params[:page_size]}
124     )
125     @items = list_result.items 
126     respond_to do |format|
127       format.html {
128         @filer = Locmare::Filer.new @list.item_name, list_result.items, list_result, :default, @operators
129         render @filer.template_name, :locals => {
130           :filer => @filer
131         }
132       }
133       format.json { render json:  @items.map{|i| i.attributes}.to_json }
134       # rails3.2 has problem
135       # y method defined as private
136       # attribute y conflict at PanelPicture, balloon ..etc
137       # use i.attributes[name]
138       # format.json { render json:  @items.to_json }
139       format.atom 
140       format.rss
141     end
142   end
143   
144   def set_play
145     set_list
146     @binder_controller = Manifest.manifest.controllers[params[:controller].to_s]
147     @binder_model = Manifest.manifest.models[@binder_controller.item_name]
148     @binder_model_class = @binder_model.classify
149     @item = @binder_model_class.show(params[:id], @operators)
150   end
151   
152   def play_list
153     @options = {:id => params[:id], :my_play => @item.own?(@operators), 
154       :offset => params[:offset], :count => params[:count], 
155       :page => params[:page], :page_size => params[:page_size]
156     }
157     list_result = @list.open(@operators, @options)
158     @items = list_result.items 
159     @count = list_result.count
160     @pager = list_result.paginate
161   end
162   
163   def set_show
164     set_model
165     @item = @my_model_class.show(params[:id], @operators)
166   end
167   
168   def show_prof_format format
169     format.prof {
170       self.formats = [:html]
171       @profiler = Locmare::Profiler.new @my_model_class.item_name, @item, @operators
172       render @profiler.template_name, :locals => {
173         :profiler => @profiler
174       }
175     }
176   end
177   
178   def show_json_format format
179     format.json { render json: @item.to_json(@my_model_class.show_json_opt) }
180   end
181   
182   def show_json_format_for_root format
183     format.json { render json: @item.to_json(@my_model_class.show_json_opt_for_root) }
184   end
185   
186   def set_new
187     set_model
188     @item = @my_model_class.new
189     @item.boosts 'post'
190     @item.supply_default
191   end
192   
193   def set_edit
194     set_model
195     @item = @my_model_class.edit(params[:id], @operators)
196     @item.boosts 'post'
197   end
198   
199   def render_form
200     respond_to do |format|
201       format.html { 
202         @form = Locmare::Bucket.factory @item.item_name, @item, true, true, @operators
203         render @form.template_name, :locals => {
204           :form => @form
205         }
206       }
207       format.json { render json: @item.to_json }
208     end
209   end
210   
211   def form_new
212     set_new
213     render_form
214   end
215   
216   def form_edit
217     set_edit
218     render_form
219   end
220   
221   def created_html_format format, redirect_url = nil
222     format.html {
223       flash[:notice] = I18n.t('flash.notice.created', :model => @my_model_class.model_name.human)
224       redirect_to (redirect_url ? redirect_url : @item)
225     }
226   end
227   
228   def created_json_format format
229     format.json {
230       render json: @item.to_json(@my_model_class.show_json_opt), status: :created, location: @item
231     }
232   end
233   
234   def not_created_html_format format
235     format.html {
236       flash[:notice] = I18n.t('flash.notice.not_created', :model => @my_model_class.model_name.human)
237       render_form
238     }
239   end
240   
241   def not_created_json_format format
242     format.json {
243       render json: @item.errors, status: :unprocessable_entity
244     }
245   end
246   
247   def render_create redirect_url = nil
248     if @item.save
249       respond_to do |format|
250         created_html_format format, redirect_url
251         created_json_format format
252       end
253     else
254       respond_to do |format|
255         not_created_html_format format
256         not_created_json_format format
257       end
258     end
259   end
260   
261   def leaf_created_html_format format, redirect_url
262     format.html {
263       flash[:notice] = I18n.t('flash.notice.created', :model => @my_model_class.model_name.human)
264       redirect_to redirect_url
265     }
266   end
267   
268   def leaf_not_created_html_format format, redirect_url
269     format.html {
270       flash[:notice] = I18n.t('flash.notice.not_created', :model => @my_model_class.model_name.human)
271       redirect_to redirect_url
272     }
273   end
274   
275   def leaf_render_create redirect_url
276     if @item.store @operators
277       respond_to do |format|
278         leaf_created_html_format format, redirect_url
279         created_json_format format
280       end
281     else
282       respond_to do |format|
283         leaf_not_created_html_format format, redirect_url
284         not_created_json_format format
285       end
286     end
287   end
288   
289   def updated_html_format format, redirect_url = nil
290     format.html {
291       flash[:notice] = I18n.t('flash.notice.updated', :model => @my_model_class.model_name.human)
292       redirect_to (redirect_url ? redirect_url : @item)
293     }
294   end
295   
296   def updated_json_format format
297     format.json {
298       head :ok
299     }
300   end
301   
302   def not_updated_html_format format
303     format.html {
304       flash[:notice] = I18n.t('flash.notice.not_updated', :model => @my_model_class.model_name.human)
305       render_form
306     }
307   end
308   
309   def not_updated_json_format format
310     format.json {
311       render json: @item.errors, status: :unprocessable_entity
312     }
313   end
314   
315   def render_update redirect_url = nil
316     if @item.save
317       respond_to do |format|
318         updated_html_format format, redirect_url
319         updated_json_format format
320       end
321     else
322       respond_to do |format|
323         not_updated_html_format format
324         not_updated_json_format format
325       end
326     end
327   end
328   
329   def leaf_updated_html_format format, redirect_url
330     format.html {
331       flash[:notice] = I18n.t('flash.notice.updated', :model => @my_model_class.model_name.human)
332       redirect_to redirect_url
333     }
334   end
335   
336   def leaf_not_updated_html_format format, redirect_url
337     format.html {
338       flash[:notice] = I18n.t('flash.notice.not_updated', :model => @my_model_class.model_name.human)
339       redirect_to redirect_url
340     }
341   end
342   
343   def leaf_render_update ot, redirect_url
344     if @item.store @operators, ot
345       respond_to do |format|
346         leaf_updated_html_format format, redirect_url
347         updated_json_format format
348       end
349     else
350       respond_to do |format|
351         leaf_not_updated_html_format format, redirect_url
352         not_updated_json_format format
353       end
354     end
355   end
356   
357   def destroyed_html_format format, redirect_url
358     format.html {
359       flash[:notice] = I18n.t('flash.notice.destroyed', :model => @my_model_class.model_name.human)
360       redirect_to redirect_url
361     }
362   end
363   
364   def destroyed_json_format format
365     format.json {
366       head :ok
367     }
368   end
369   
370   def not_destroyed_html_format format
371     format.html {
372       flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => @my_model_class.model_name.human)
373       redirect_to @item
374     }
375   end
376   
377   def not_destroyed_json_format format
378     format.json {
379       render json: @item.errors, status: :unprocessable_entity
380     }
381   end
382   
383   def render_destroy redirect_url = nil
384     if @item.destroy
385       respond_to do |format|
386         destroyed_html_format format, redirect_url
387         destroyed_json_format format
388       end
389     else
390       respond_to do |format|
391         not_destroyed_html_format format
392         not_destroyed_json_format format
393       end
394     end
395   end
396   
397   def list_count
398     set_list
399     j = {:count => @list.count(@operators, {:id => params[:id]})}
400     respond_to do |format|
401       format.json { render json: j.to_json }
402     end
403   end
404   
405   def format_filer format
406     format.html {
407       @paginate = @@model.list_paginate(@page, @page_size)
408       render :template => 'system/filer', :locals => {
409         :items => @items, :model => @@model, 
410         :operators => @operators, :pager => @paginate
411       }
412     }
413   end
414   
415   def format_prof format
416     format.prof { 
417       @profiler = self.class.profiler_manager.open(@item, @operators)
418       render :template => 'system/prof', :locals => {
419         :profiler => @profiler
420       }
421     }
422   end
423   
424   def assist_items item_name, list_name
425     list = Locmare::ListGroup.list item_name, list_name
426     list_result = list.open(@operators, {:id => @item.id, :page => 1, :page_size => 5})
427     list_result.items
428   end
429   
430   def set_image(file)
431     if file.respond_to?(:read)
432       file.read
433     else
434       Base64.decode64(file.to_s.gsub(' ', '+'))
435     end
436   end
437   
438   def ymd_to_time ymd_str
439     return nil if ymd_str.blank?
440     date = nil
441     begin
442       date = Time.parse(ymd_str[0..3] + '/' + ymd_str[4..5] + '/' + ymd_str[6..7])
443     rescue
444       date = nil
445     end
446     date
447   end
448   
449   def export_url demander_url, action, token, date
450     u = demander_url + (demander_url[-1] == '/' ? '' : '/')
451     prm = '?auth_token=' + token
452     prm = prm + '&date=' + date.strftime("%Y%m%d") unless date.blank?
453     u = URI.join(u, action + '.json' + prm)
454     u.to_s
455   end
456   
457   def export_from_provider url
458     res = nil
459     begin
460       json = RestClient.get url
461       res = JSON.parse json
462     rescue
463     end
464     res
465   end
466   
467   def export_by action, provider_status, ymd
468     t = ymd_to_time ymd
469     url = export_url provider_status.provider.demander_url, action, provider_status.token, t
470     export_from_provider(url)
471   end
472   
473     rescue_from Pettanr::NotWork, :with => :render_not_work
474     def render_not_work(exception = nil)
475       if exception
476         logger.info "Rendering , :: #{exception.message}"
477       end
478       respond_to do |format|
479         format.html { 
480           render :file => "#{Rails.root}/public/not_work.html", :layout => false
481         }
482         format.json { 
483           render :text => "400 Not work", :status => 400
484         }
485       end
486     end
487     
488   if Rails.env == 'production'
489     rescue_from ActiveRecord::RecordNotFound, :with => :render_404
490     rescue_from ActiveRecord::Forbidden, :with => :render_403
491     
492     private
493     def render_404(exception = nil)
494       if exception
495         logger.info "Rendering 404: #{exception.message}"
496       end
497       respond_to do |format|
498         format.html { 
499           render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
500         }
501         format.json { 
502           render :text => "404 Not found", :status => 404
503         }
504       end
505     end
506     
507     def render_403(exception = nil)
508       if exception
509         logger.info "Rendering 403: #{exception.message}"
510       end
511       respond_to do |format|
512         format.html { 
513           render :file => "#{Rails.root}/public/403.html", :status => 404, :layout => false
514         }
515         format.json { 
516           render :text => "403 Forbidden", :status => 403
517         }
518       end
519     end
520   end
521 end