OSDN Git Service

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