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