OSDN Git Service

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