OSDN Git Service

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