OSDN Git Service

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