1 class ApplicationController < ActionController::Base
3 protect_from_forgery with: :null_session, if: Proc.new {|c| c.request.format == 'application/json'}
6 before_action :authenticate_user_from_token!, if: -> {params[:email].present?}
25 :location => {:controller => params[:controller], :action => params[:action]}
27 @server_result[:location][:id] = params[:id] if params[:id]
28 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_user_from_token!
57 user = User.find_by(email: params[:email])
58 if Devise.secure_compare(user.try(:authentication_token), params[:auth_token])
59 sign_in user, store: false
64 def authenticate_reader
65 authenticate_user! unless @operators.reader?
69 authenticate_user! unless @operators.user?
72 def authenticate_resource_reader
73 authenticate_user! unless @operators.resource_reader?
76 def authenticate_admin
77 authenticate_admin! unless @operators.admin?
80 def authenticate_author
84 respond_to do |format|
85 format.html { redirect_to main_app.new_author_path, :status => :found }
87 raise ActiveRecord::Forbidden
94 def authenticate_artist
98 respond_to do |format|
99 format.html { redirect_to main_app.new_artist_path, :status => :found }
101 raise ActiveRecord::Forbidden
109 Manifest.manifest.controllers[self.model.item_name]
112 def self.profiler_manager
113 Manifest.manifest.profiler_managers[self.model.item_name]
117 @my_controller = Manifest.manifest.controllers[params[:controller].to_s]
118 @my_action = @my_controller.actions[params[:action].to_s]
119 @my_model = Manifest.manifest.models[@my_action.item_name]
120 @my_model_class = @my_model.classify
123 def set_list options = {}
125 # params merge to options
126 options[:page] ||= params[:page]
127 options[:page_size] ||= params[:page_size]
128 options[:order] ||= params[:order]
129 options[:direction] ||= params[:direction]
130 @finder = @my_action.find options
133 def filer_list options = {}
135 respond_to do |format|
137 @filer = Locmare::Filer.new @my_action.return_item_name, @finder, @finder, @operators
138 render @filer.template_name, :locals => {
142 list_json_format @finder, format
148 def list_json_format list, format
152 :type => :default, :total => @finder.total_count, :total_page => @finder.total_pages,
153 :page => @finder.page, :page_size => @finder.limit_value,
154 :item_name => @my_action.return_item_name
156 # rails3.2 has problem
157 # y method defined as private
158 # attribute y conflict at PanelPicture, balloon ..etc
159 # use i.attributes[name]
160 :list => list.map{|i| i.attributes}
161 # :list => @finder.to_json
163 render json: res.to_json
169 @item = @my_model_class.show(params[:id], @operators)
170 options = if @item.own?(@operators)
171 {finder: :find_private_play, param: [params[:id], @operators]}
173 {finder: :find_play, param: params[:id]}
179 @items = @finder #.map {|sp| sp.root }
180 @count = @finder.total_count
186 @item = @my_model_class.show(params[:id], @operators)
189 def show_prof_format format
191 self.formats = [:html]
192 @profiler = Locmare::Profiler.new @my_model_class.item_name, @item, @operators
193 render @profiler.template_name, :locals => {
194 :profiler => @profiler
199 def show_json_format format
200 format.json { render json: @item.to_json(@my_model_class.show_json_opt) }
203 def show_json_format_for_root format
204 format.json { render json: @item.to_json(@my_model_class.show_json_opt_for_root) }
209 @item = @my_model_class.new
216 @item = @my_model_class.edit(params[:id], @operators)
221 respond_to do |format|
223 @form = Locmare::Bucket.factory @item.item_name, 'default', @item, true, true, @operators
224 render @form.template_name, :locals => {
228 format.json { render json: @item.to_json }
242 def created_html_format format, redirect_url = nil
244 flash[:notice] = I18n.t('flash.notice.created', :model => @my_model_class.model_name.human)
245 redirect_to (redirect_url ? redirect_url : @item)
249 def created_json_format format
251 render json: @item.to_json(@my_model_class.show_json_opt), status: :created, location: @item
255 def not_created_html_format format
257 flash[:notice] = I18n.t('flash.notice.not_created', :model => @my_model_class.model_name.human)
262 def not_created_json_format format
264 render json: @item.errors, status: :unprocessable_entity
268 def render_create redirect_url = nil
270 respond_to do |format|
271 created_html_format format, redirect_url
272 created_json_format format
275 respond_to do |format|
276 not_created_html_format format
277 not_created_json_format format
282 def leaf_created_html_format format, redirect_url
284 flash[:notice] = I18n.t('flash.notice.created', :model => @my_model_class.model_name.human)
285 redirect_to redirect_url
289 def leaf_not_created_html_format format, redirect_url
291 flash[:notice] = I18n.t('flash.notice.not_created', :model => @my_model_class.model_name.human)
292 redirect_to redirect_url
296 def leaf_render_create redirect_url
297 if @item.store @operators
298 respond_to do |format|
299 leaf_created_html_format format, redirect_url
300 created_json_format format
303 respond_to do |format|
304 leaf_not_created_html_format format, redirect_url
305 not_created_json_format format
310 def updated_html_format format, redirect_url = nil
312 flash[:notice] = I18n.t('flash.notice.updated', :model => @my_model_class.model_name.human)
313 redirect_to (redirect_url ? redirect_url : @item)
317 def updated_json_format format
319 render json: '{}', status: :ok
323 def not_updated_html_format format
325 flash[:notice] = I18n.t('flash.notice.not_updated', :model => @my_model_class.model_name.human)
330 def not_updated_json_format format
332 render json: @item.errors, status: :unprocessable_entity
336 def render_update redirect_url = nil
338 respond_to do |format|
339 updated_html_format format, redirect_url
340 updated_json_format format
343 respond_to do |format|
344 not_updated_html_format format
345 not_updated_json_format format
350 def leaf_updated_html_format format, redirect_url
352 flash[:notice] = I18n.t('flash.notice.updated', :model => @my_model_class.model_name.human)
353 redirect_to redirect_url
357 def leaf_not_updated_html_format format, redirect_url
359 flash[:notice] = I18n.t('flash.notice.not_updated', :model => @my_model_class.model_name.human)
360 redirect_to redirect_url
364 def leaf_render_update ot, redirect_url
365 if @item.store @operators, ot
366 respond_to do |format|
367 leaf_updated_html_format format, redirect_url
368 updated_json_format format
371 respond_to do |format|
372 leaf_not_updated_html_format format, redirect_url
373 not_updated_json_format format
378 def destroyed_html_format format, redirect_url
380 flash[:notice] = I18n.t('flash.notice.destroyed', :model => @my_model_class.model_name.human)
381 redirect_to redirect_url
385 def destroyed_json_format format
387 render json: '{}', status: :ok
391 def not_destroyed_html_format format
393 flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => @my_model_class.model_name.human)
398 def not_destroyed_json_format format
400 render json: @item.errors, status: :unprocessable_entity
404 def render_destroy redirect_url
406 respond_to do |format|
407 destroyed_html_format format, redirect_url
408 destroyed_json_format format
411 respond_to do |format|
412 not_destroyed_html_format format
413 not_destroyed_json_format format
418 def render_destroy_by destroy_method_name, redirect_url = nil
419 if @item.__send__(destroy_method_name)
420 respond_to do |format|
421 destroyed_html_format format, redirect_url
422 destroyed_json_format format
425 respond_to do |format|
426 not_destroyed_html_format format
427 not_destroyed_json_format format
432 def format_filer format
434 @paginate = @@model.list_paginate(@page, @page_size)
435 render :template => 'system/filer', :locals => {
436 :items => @items, :model => @@model,
437 :operators => @operators, :pager => @paginate
442 def format_prof format
444 @profiler = self.class.profiler_manager.open(@item, @operators)
445 render :template => 'system/prof', :locals => {
446 :profiler => @profiler
451 def assist_items controller_name, action_name, options = {}
452 controller = Manifest.manifest.controllers[controller_name]
453 action = controller.actions[action_name]
455 options[:page_size] = 5
456 finder = action.find options
461 if file.respond_to?(:read)
464 Base64.decode64(file.to_s.gsub(' ', '+'))
468 def ymd_to_time ymd_str
469 return nil if ymd_str.blank?
472 date = Time.parse(ymd_str[0..3] + '/' + ymd_str[4..5] + '/' + ymd_str[6..7])
479 def export_url demander_url, action, token, date
480 u = demander_url + (demander_url[-1] == '/' ? '' : '/')
481 prm = '?auth_token=' + token
482 prm = prm + '&date=' + date.strftime("%Y%m%d") unless date.blank?
483 u = URI.join(u, action + '.json' + prm)
487 def export_from_provider url
490 json = RestClient.get url
491 res = JSON.parse json
497 def export_by action, provider_status, ymd
499 url = export_url provider_status.provider.demander_url, action, provider_status.token, t
500 export_from_provider(url)
503 rescue_from Pettanr::NotWork, :with => :render_not_work
504 def render_not_work(exception = nil)
506 logger.info "Rendering , :: #{exception.message}"
508 respond_to do |format|
510 render :file => "#{Rails.root}/public/not_work.html", :layout => false
513 render :text => "400 Not work", :status => 400
518 if Rails.env == 'production'
519 rescue_from ActiveRecord::RecordNotFound, :with => :render_404
520 rescue_from ActiveRecord::Forbidden, :with => :render_403
523 def render_404(exception = nil)
525 logger.info "Rendering 404: #{exception.message}"
527 respond_to do |format|
529 render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
532 render :text => "404 Not found", :status => 404
537 def render_403(exception = nil)
539 logger.info "Rendering 403: #{exception.message}"
541 respond_to do |format|
543 render :file => "#{Rails.root}/public/403.html", :status => 404, :layout => false
546 render :text => "403 Forbidden", :status => 403