OSDN Git Service

t#:
[pettanr/pettanr.git] / app / controllers / application_controller.rb
1 class ApplicationController < ActionController::Base
2   protect_from_forgery
3   layout :devise_layout if MagicNumber['test_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_author
74     if @operators.author
75       true
76     else
77       respond_to do |format|
78         format.html { redirect_to main_app.new_author_path, :status => :found }
79         format.js { render "authors/new" }
80         format.json { 
81           raise ActiveRecord::Forbidden
82         }
83       end
84       false
85     end
86   end
87       
88   def authenticate_artist
89     if @operators.artist
90       true
91     else
92       respond_to do |format|
93         format.html { redirect_to main_app.new_artist_path, :status => :found }
94         format.js { render "artists/new" }
95         format.json { 
96           raise ActiveRecord::Forbidden
97         }
98       end
99       false
100     end
101   end
102   
103   def self.controller
104     Pettanr::Application::manifest.controller_managers[self.model.item_name]
105   end
106   
107   def self.profiler_manager
108     Pettanr::Application::manifest.profiler_managers[self.model.item_name]
109   end
110   
111   def public_list
112     action_name = params[:action]
113     @action = self.class.controller.open(action_name, params, @operators)
114     @items = @action.items 
115     respond_to do |format|
116       format.html {
117         @filer = @action.filer
118         render :template => 'system/filer', :locals => {
119           :filer => @filer
120         }
121       }
122       format.json { render json: @items.to_json(self.class.model.list_json_opt) }
123       format.atom 
124       format.rss
125     end
126   end
127   
128   def my_list params
129     controller_name = params[:controller]
130     action_name = params[:action]
131     controller = Pettanr::Application::manifest.controller_managers[controller_name]
132     @action = controller.open(action_name, params, @operators)
133     @items = @action.items 
134
135     respond_to do |format|
136       format.html {
137         @filer = @action.filer
138         render :template => 'system/filer', :locals => {
139           :filer => @filer
140         }
141       }
142       format.json { render json: @items.to_json(Scroll.list_json_opt) }
143     end
144   end
145   
146   def format_filer format
147     format.html {
148       @paginate = @@model.list_paginate(@page, @page_size)
149       render :template => 'system/filer', :locals => {
150         :items => @items, :model => @@model, 
151         :operators => @operators, :pager => @paginate
152       }
153     }
154   end
155   
156   def format_prof format
157     format.prof { 
158       @profiler = self.class.profiler_manager.open(@item, @operators)
159       render :template => 'system/prof', :locals => {
160         :profiler => @profiler
161       }
162     }
163   end
164   
165   def set_image(file)
166     if file.respond_to?(:read)
167       file.read
168     else
169       Base64.decode64(file.to_s.gsub(' ', '+'))
170     end
171   end
172   
173   def ymd_to_time ymd_str
174     return nil if ymd_str.blank?
175     date = nil
176     begin
177       date = Time.parse(ymd_str[0..3] + '/' + ymd_str[4..5] + '/' + ymd_str[6..7])
178     rescue
179       date = nil
180     end
181     date
182   end
183   
184   def export_url demander_url, action, token, date
185     u = demander_url + (demander_url[-1] == '/' ? '' : '/')
186     prm = '?auth_token=' + token
187     prm = prm + '&date=' + date.strftime("%Y%m%d") unless date.blank?
188     u = URI.join(u, action + '.json' + prm)
189     u.to_s
190   end
191   
192   def export_from_provider url
193     res = nil
194     begin
195       json = RestClient.get url
196       res = JSON.parse json
197     rescue
198     end
199     res
200   end
201   
202   def export_by action, provider_status, ymd
203     t = ymd_to_time ymd
204     url = export_url provider_status.provider.demander_url, action, provider_status.token, t
205     export_from_provider(url)
206   end
207   
208     rescue_from Pettanr::NotWork, :with => :render_not_work
209     def render_not_work(exception = nil)
210       if exception
211         logger.info "Rendering , :: #{exception.message}"
212       end
213       respond_to do |format|
214         format.html { 
215           render :file => "#{Rails.root}/public/not_work.html", :layout => false
216         }
217         format.json { 
218           render :text => "400 Not work", :status => 400
219         }
220       end
221     end
222     
223   if Rails.env == 'production'
224     rescue_from ActiveRecord::RecordNotFound, :with => :render_404
225     rescue_from ActiveRecord::Forbidden, :with => :render_403
226     
227     private
228     def render_404(exception = nil)
229       if exception
230         logger.info "Rendering 404: #{exception.message}"
231       end
232       respond_to do |format|
233         format.html { 
234           render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
235         }
236         format.json { 
237           render :text => "404 Not found", :status => 404
238         }
239       end
240     end
241     
242     def render_403(exception = nil)
243       if exception
244         logger.info "Rendering 403: #{exception.message}"
245       end
246       respond_to do |format|
247         format.html { 
248           render :file => "#{Rails.root}/public/403.html", :status => 404, :layout => false
249         }
250         format.json { 
251           render :text => "403 Forbidden", :status => 403
252         }
253       end
254     end
255   end
256 end