1 class ApplicationController < ActionController::Base
3 protect_from_forgery with: :null_session, if: Proc.new {|c| c.request.format == 'application/json'}
24 :location => {:controller => params[:controller], :action => params[:action]}
26 @server_result[:location][:id] = params[:id] if params[:id]
27 user = if user_signed_in?
43 admin = if admin_signed_in?
48 demand_user = if demand_user_signed_in?
53 @operators = Operator.new [user, author, artist, admin, demand_user]
56 def authenticate_reader
57 authenticate_user! unless @operators.reader?
61 authenticate_user! unless @operators.user?
64 def authenticate_resource_reader
65 authenticate_user! unless @operators.resource_reader?
68 def authenticate_admin
69 authenticate_admin! unless @operators.admin?
72 def authenticate_author
76 respond_to do |format|
77 format.html { redirect_to main_app.new_author_path, :status => :found }
79 raise ActiveRecord::Forbidden
86 def authenticate_artist
90 respond_to do |format|
91 format.html { redirect_to main_app.new_artist_path, :status => :found }
93 raise ActiveRecord::Forbidden
101 Manifest.manifest.controllers[self.model.item_name]
104 def self.profiler_manager
105 Manifest.manifest.profiler_managers[self.model.item_name]
109 @my_controller = Manifest.manifest.controllers[params[:controller].to_s]
110 @my_action = @my_controller.actions[params[:action].to_s]
111 @my_model = Manifest.manifest.models[@my_action.item_name]
112 @my_model_class = @my_model.classify
115 def set_list options = {}
117 # params merge to options
118 options[:page] ||= params[:page]
119 options[:page_size] ||= params[:page_size]
120 options[:order] ||= params[:order]
121 options[:direction] ||= params[:direction]
122 @finder = @my_action.find options
125 def filer_list options = {}
127 respond_to do |format|
129 @filer = Locmare::Filer.new @my_action.return_item_name, @finder, @finder, @operators
130 render @filer.template_name, :locals => {
134 list_json_format @finder, format
140 def list_json_format list, format
144 :type => :default, :total => @finder.total_count, :total_page => @finder.total_pages,
145 :page => @finder.page, :page_size => @finder.limit_value,
146 :item_name => @my_action.return_item_name
148 # rails3.2 has problem
149 # y method defined as private
150 # attribute y conflict at PanelPicture, balloon ..etc
151 # use i.attributes[name]
152 :list => list.map{|i| i.attributes}
153 # :list => @finder.to_json
155 render json: res.to_json
161 @item = @my_model_class.show(params[:id], @operators)
162 options = if @item.own?(@operators)
163 {finder: :find_private_play, param: [params[:id], @operators]}
165 {finder: :find_play, param: params[:id]}
171 @items = @finder #.map {|sp| sp.root }
172 @count = @finder.total_count
178 @item = @my_model_class.show(params[:id], @operators)
181 def show_prof_format format
183 self.formats = [:html]
184 @profiler = Locmare::Profiler.new @my_model_class.item_name, @item, @operators
185 render @profiler.template_name, :locals => {
186 :profiler => @profiler
191 def show_json_format format
192 format.json { render json: @item.to_json(@my_model_class.show_json_opt) }
195 def show_json_format_for_root format
196 format.json { render json: @item.to_json(@my_model_class.show_json_opt_for_root) }
201 @item = @my_model_class.new
208 @item = @my_model_class.edit(params[:id], @operators)
213 respond_to do |format|
215 @form = Locmare::Bucket.factory @item.item_name, 'default', @item, true, true, @operators
216 render @form.template_name, :locals => {
220 format.json { render json: @item.to_json }
234 def created_html_format format, redirect_url = nil
236 flash[:notice] = I18n.t('flash.notice.created', :model => @my_model_class.model_name.human)
237 redirect_to (redirect_url ? redirect_url : @item)
241 def created_json_format format
243 render json: @item.to_json(@my_model_class.show_json_opt), status: :created, location: @item
247 def not_created_html_format format
249 flash[:notice] = I18n.t('flash.notice.not_created', :model => @my_model_class.model_name.human)
254 def not_created_json_format format
256 render json: @item.errors, status: :unprocessable_entity
260 def render_create redirect_url = nil
262 respond_to do |format|
263 created_html_format format, redirect_url
264 created_json_format format
267 respond_to do |format|
268 not_created_html_format format
269 not_created_json_format format
274 def leaf_created_html_format format, redirect_url
276 flash[:notice] = I18n.t('flash.notice.created', :model => @my_model_class.model_name.human)
277 redirect_to redirect_url
281 def leaf_not_created_html_format format, redirect_url
283 flash[:notice] = I18n.t('flash.notice.not_created', :model => @my_model_class.model_name.human)
284 redirect_to redirect_url
288 def leaf_render_create redirect_url
289 if @item.store @operators
290 respond_to do |format|
291 leaf_created_html_format format, redirect_url
292 created_json_format format
295 respond_to do |format|
296 leaf_not_created_html_format format, redirect_url
297 not_created_json_format format
302 def updated_html_format format, redirect_url = nil
304 flash[:notice] = I18n.t('flash.notice.updated', :model => @my_model_class.model_name.human)
305 redirect_to (redirect_url ? redirect_url : @item)
309 def updated_json_format format
311 render json: '{}', status: :ok
315 def not_updated_html_format format
317 flash[:notice] = I18n.t('flash.notice.not_updated', :model => @my_model_class.model_name.human)
322 def not_updated_json_format format
324 render json: @item.errors, status: :unprocessable_entity
328 def render_update redirect_url = nil
330 respond_to do |format|
331 updated_html_format format, redirect_url
332 updated_json_format format
335 respond_to do |format|
336 not_updated_html_format format
337 not_updated_json_format format
342 def leaf_updated_html_format format, redirect_url
344 flash[:notice] = I18n.t('flash.notice.updated', :model => @my_model_class.model_name.human)
345 redirect_to redirect_url
349 def leaf_not_updated_html_format format, redirect_url
351 flash[:notice] = I18n.t('flash.notice.not_updated', :model => @my_model_class.model_name.human)
352 redirect_to redirect_url
356 def leaf_render_update ot, redirect_url
357 if @item.store @operators, ot
358 respond_to do |format|
359 leaf_updated_html_format format, redirect_url
360 updated_json_format format
363 respond_to do |format|
364 leaf_not_updated_html_format format, redirect_url
365 not_updated_json_format format
370 def destroyed_html_format format, redirect_url
372 flash[:notice] = I18n.t('flash.notice.destroyed', :model => @my_model_class.model_name.human)
373 redirect_to redirect_url
377 def destroyed_json_format format
379 render json: '{}', status: :ok
383 def not_destroyed_html_format format
385 flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => @my_model_class.model_name.human)
390 def not_destroyed_json_format format
392 render json: @item.errors, status: :unprocessable_entity
396 def render_destroy redirect_url
398 respond_to do |format|
399 destroyed_html_format format, redirect_url
400 destroyed_json_format format
403 respond_to do |format|
404 not_destroyed_html_format format
405 not_destroyed_json_format format
410 def render_destroy_by destroy_method_name, redirect_url = nil
411 if @item.__send__(destroy_method_name)
412 respond_to do |format|
413 destroyed_html_format format, redirect_url
414 destroyed_json_format format
417 respond_to do |format|
418 not_destroyed_html_format format
419 not_destroyed_json_format format
424 def format_filer format
426 @paginate = @@model.list_paginate(@page, @page_size)
427 render :template => 'system/filer', :locals => {
428 :items => @items, :model => @@model,
429 :operators => @operators, :pager => @paginate
434 def format_prof format
436 @profiler = self.class.profiler_manager.open(@item, @operators)
437 render :template => 'system/prof', :locals => {
438 :profiler => @profiler
443 def assist_items controller_name, action_name, options = {}
444 controller = Manifest.manifest.controllers[controller_name]
445 action = controller.actions[action_name]
447 options[:page_size] = 5
448 finder = action.find options
453 if file.respond_to?(:read)
456 Base64.decode64(file.to_s.gsub(' ', '+'))
460 def ymd_to_time ymd_str
461 return nil if ymd_str.blank?
464 date = Time.parse(ymd_str[0..3] + '/' + ymd_str[4..5] + '/' + ymd_str[6..7])
471 def export_url demander_url, action, token, date
472 u = demander_url + (demander_url[-1] == '/' ? '' : '/')
473 prm = '?auth_token=' + token
474 prm = prm + '&date=' + date.strftime("%Y%m%d") unless date.blank?
475 u = URI.join(u, action + '.json' + prm)
479 def export_from_provider url
482 json = RestClient.get url
483 res = JSON.parse json
489 def export_by action, provider_status, ymd
491 url = export_url provider_status.provider.demander_url, action, provider_status.token, t
492 export_from_provider(url)
495 rescue_from Pettanr::NotWork, :with => :render_not_work
496 def render_not_work(exception = nil)
498 logger.info "Rendering , :: #{exception.message}"
500 respond_to do |format|
502 render :file => "#{Rails.root}/public/not_work.html", :layout => false
505 render :text => "400 Not work", :status => 400
510 if Rails.env == 'production'
511 rescue_from ActiveRecord::RecordNotFound, :with => :render_404
512 rescue_from ActiveRecord::Forbidden, :with => :render_403
515 def render_404(exception = nil)
517 logger.info "Rendering 404: #{exception.message}"
519 respond_to do |format|
521 render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
524 render :text => "404 Not found", :status => 404
529 def render_403(exception = nil)
531 logger.info "Rendering 403: #{exception.message}"
533 respond_to do |format|
535 render :file => "#{Rails.root}/public/403.html", :status => 404, :layout => false
538 render :text => "403 Forbidden", :status => 403