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