OSDN Git Service

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