OSDN Git Service

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