OSDN Git Service

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