1 class ApplicationController < ActionController::Base
23 :location => {:controller => params[:controller], :action => params[:action]}
25 @server_result[:location][:id] = params[:id] if params[:id]
26 user = if user_signed_in?
41 admin = if admin_signed_in?
46 demand_user = if demand_user_signed_in?
51 @operators = Operator.new [user, author, artist, admin, demand_user]
54 def authenticate_reader
55 authenticate_user! unless @operators.reader?
59 authenticate_user! unless @operators.user?
62 def authenticate_resource_reader
63 authenticate_user! unless @operators.resource_reader?
66 def authenticate_admin
67 authenticate_admin! unless @operators.admin?
70 def authenticate_author
74 respond_to do |format|
75 format.html { redirect_to main_app.new_author_path, :status => :found }
77 raise ActiveRecord::Forbidden
84 def authenticate_artist
88 respond_to do |format|
89 format.html { redirect_to main_app.new_artist_path, :status => :found }
91 raise ActiveRecord::Forbidden
99 Manifest.manifest.controllers[self.model.item_name]
102 def self.profiler_manager
103 Manifest.manifest.profiler_managers[self.model.item_name]
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
113 def set_list options = {}
115 # params merge to options
116 options[:page] ||= params[:page]
117 options[:page_size] ||= params[:page_size]
118 options[:order] ||= params[:order]
119 options[:direction] ||= params[:direction]
120 @finder = @my_action.find options
123 def filer_list options = {}
125 respond_to do |format|
127 @filer = Locmare::Filer.new @my_action.return_item_name, @finder, @finder, @operators
128 render @filer.template_name, :locals => {
132 list_json_format @finder, format
138 def list_json_format list, format
142 :type => :default, :total => @finder.total_count, :total_page => @finder.total_pages,
143 :page => @finder.page, :page_size => @finder.limit_value,
144 :item_name => @my_action.return_item_name
146 # rails3.2 has problem
147 # y method defined as private
148 # attribute y conflict at PanelPicture, balloon ..etc
149 # use i.attributes[name]
150 :list => list.map{|i| i.attributes}
151 # :list => @finder.to_json
153 render json: res.to_json
159 @item = @my_model_class.show(params[:id], @operators)
160 options = if @item.own?(@operators)
161 {finder: :find_private_play, param: [params[:id], @operators]}
163 {finder: :find_play, param: params[:id]}
169 @items = @finder #.map {|sp| sp.root }
170 @count = @finder.total_count
176 @item = @my_model_class.show(params[:id], @operators)
179 def show_prof_format format
181 self.formats = [:html]
182 @profiler = Locmare::Profiler.new @my_model_class.item_name, @item, @operators
183 render @profiler.template_name, :locals => {
184 :profiler => @profiler
189 def show_json_format format
190 format.json { render json: @item.to_json(@my_model_class.show_json_opt) }
193 def show_json_format_for_root format
194 format.json { render json: @item.to_json(@my_model_class.show_json_opt_for_root) }
199 @item = @my_model_class.new
206 @item = @my_model_class.edit(params[:id], @operators)
211 respond_to do |format|
213 @form = Locmare::Bucket.factory @item.item_name, 'default', @item, true, true, @operators
214 render @form.template_name, :locals => {
218 format.json { render json: @item.to_json }
232 def created_html_format format, redirect_url = nil
234 flash[:notice] = I18n.t('flash.notice.created', :model => @my_model_class.model_name.human)
235 redirect_to (redirect_url ? redirect_url : @item)
239 def created_json_format format
241 render json: @item.to_json(@my_model_class.show_json_opt), status: :created, location: @item
245 def not_created_html_format format
247 flash[:notice] = I18n.t('flash.notice.not_created', :model => @my_model_class.model_name.human)
252 def not_created_json_format format
254 render json: @item.errors, status: :unprocessable_entity
258 def render_create redirect_url = nil
260 respond_to do |format|
261 created_html_format format, redirect_url
262 created_json_format format
265 respond_to do |format|
266 not_created_html_format format
267 not_created_json_format format
272 def leaf_created_html_format format, redirect_url
274 flash[:notice] = I18n.t('flash.notice.created', :model => @my_model_class.model_name.human)
275 redirect_to redirect_url
279 def leaf_not_created_html_format format, redirect_url
281 flash[:notice] = I18n.t('flash.notice.not_created', :model => @my_model_class.model_name.human)
282 redirect_to redirect_url
286 def leaf_render_create redirect_url
287 if @item.store @operators
288 respond_to do |format|
289 leaf_created_html_format format, redirect_url
290 created_json_format format
293 respond_to do |format|
294 leaf_not_created_html_format format, redirect_url
295 not_created_json_format format
300 def updated_html_format format, redirect_url = nil
302 flash[:notice] = I18n.t('flash.notice.updated', :model => @my_model_class.model_name.human)
303 redirect_to (redirect_url ? redirect_url : @item)
307 def updated_json_format format
309 render json: '{}', status: :ok
313 def not_updated_html_format format
315 flash[:notice] = I18n.t('flash.notice.not_updated', :model => @my_model_class.model_name.human)
320 def not_updated_json_format format
322 render json: @item.errors, status: :unprocessable_entity
326 def render_update redirect_url = nil
328 respond_to do |format|
329 updated_html_format format, redirect_url
330 updated_json_format format
333 respond_to do |format|
334 not_updated_html_format format
335 not_updated_json_format format
340 def leaf_updated_html_format format, redirect_url
342 flash[:notice] = I18n.t('flash.notice.updated', :model => @my_model_class.model_name.human)
343 redirect_to redirect_url
347 def leaf_not_updated_html_format format, redirect_url
349 flash[:notice] = I18n.t('flash.notice.not_updated', :model => @my_model_class.model_name.human)
350 redirect_to redirect_url
354 def leaf_render_update ot, redirect_url
355 if @item.store @operators, ot
356 respond_to do |format|
357 leaf_updated_html_format format, redirect_url
358 updated_json_format format
361 respond_to do |format|
362 leaf_not_updated_html_format format, redirect_url
363 not_updated_json_format format
368 def destroyed_html_format format, redirect_url
370 flash[:notice] = I18n.t('flash.notice.destroyed', :model => @my_model_class.model_name.human)
371 redirect_to redirect_url
375 def destroyed_json_format format
377 render json: '{}', status: :ok
381 def not_destroyed_html_format format
383 flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => @my_model_class.model_name.human)
388 def not_destroyed_json_format format
390 render json: @item.errors, status: :unprocessable_entity
394 def render_destroy redirect_url
396 respond_to do |format|
397 destroyed_html_format format, redirect_url
398 destroyed_json_format format
401 respond_to do |format|
402 not_destroyed_html_format format
403 not_destroyed_json_format format
408 def render_destroy_by destroy_method_name, redirect_url = nil
409 if @item.__send__(destroy_method_name)
410 respond_to do |format|
411 destroyed_html_format format, redirect_url
412 destroyed_json_format format
415 respond_to do |format|
416 not_destroyed_html_format format
417 not_destroyed_json_format format
422 def format_filer format
424 @paginate = @@model.list_paginate(@page, @page_size)
425 render :template => 'system/filer', :locals => {
426 :items => @items, :model => @@model,
427 :operators => @operators, :pager => @paginate
432 def format_prof format
434 @profiler = self.class.profiler_manager.open(@item, @operators)
435 render :template => 'system/prof', :locals => {
436 :profiler => @profiler
441 def assist_items controller_name, action_name, options = {}
442 controller = Manifest.manifest.controllers[controller_name]
443 action = controller.actions[action_name]
445 options[:page_size] = 5
446 finder = action.find options
451 if file.respond_to?(:read)
454 Base64.decode64(file.to_s.gsub(' ', '+'))
458 def ymd_to_time ymd_str
459 return nil if ymd_str.blank?
462 date = Time.parse(ymd_str[0..3] + '/' + ymd_str[4..5] + '/' + ymd_str[6..7])
469 def export_url demander_url, action, token, date
470 u = demander_url + (demander_url[-1] == '/' ? '' : '/')
471 prm = '?auth_token=' + token
472 prm = prm + '&date=' + date.strftime("%Y%m%d") unless date.blank?
473 u = URI.join(u, action + '.json' + prm)
477 def export_from_provider url
480 json = RestClient.get url
481 res = JSON.parse json
487 def export_by action, provider_status, ymd
489 url = export_url provider_status.provider.demander_url, action, provider_status.token, t
490 export_from_provider(url)
493 rescue_from Pettanr::NotWork, :with => :render_not_work
494 def render_not_work(exception = nil)
496 logger.info "Rendering , :: #{exception.message}"
498 respond_to do |format|
500 render :file => "#{Rails.root}/public/not_work.html", :layout => false
503 render :text => "400 Not work", :status => 400
508 if Rails.env == 'production'
509 rescue_from ActiveRecord::RecordNotFound, :with => :render_404
510 rescue_from ActiveRecord::Forbidden, :with => :render_403
513 def render_404(exception = nil)
515 logger.info "Rendering 404: #{exception.message}"
517 respond_to do |format|
519 render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
522 render :text => "404 Not found", :status => 404
527 def render_403(exception = nil)
529 logger.info "Rendering 403: #{exception.message}"
531 respond_to do |format|
533 render :file => "#{Rails.root}/public/403.html", :status => 404, :layout => false
536 render :text => "403 Forbidden", :status => 403