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? or request.headers[:email]}
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] or request.headers[:email]))
58 if Devise.secure_compare(user.try(:authentication_token), (params[:token] or request.headers[: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.current_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
164 render json: res.to_json
170 @item = @my_model_class.show(params[:id], @operators)
171 options = if @item.own?(@operators)
172 {finder: :find_private_play, param: [params[:id], @operators]}
174 {finder: :find_play, param: params[:id]}
180 @items = @finder #.map {|sp| sp.root }
181 @count = @finder.total_count
187 @item = @my_model_class.show(params[:id], @operators)
190 def show_prof_format format
192 self.formats = [:html]
193 @profiler = Locmare::Profiler.new @my_model_class.item_name, @item, @operators
194 render @profiler.template_name, :locals => {
195 :profiler => @profiler
200 def show_json_format format
201 format.json { render json: @item.to_json(@my_model_class.show_json_opt) }
204 def show_json_format_for_root format
205 format.json { render json: @item.to_json(@my_model_class.show_json_opt_for_root) }
210 @item = @my_model_class.new
217 @item = @my_model_class.edit(params[:id], @operators)
222 respond_to do |format|
224 @form = Locmare::Bucket.factory @item.item_name, 'default', @item, true, true, @operators
225 render @form.template_name, :locals => {
229 format.json { render json: @item.to_json }
243 def created_html_format format, redirect_url = nil
245 flash[:notice] = I18n.t('flash.notice.created', :model => @my_model_class.model_name.human)
246 redirect_to (redirect_url ? redirect_url : @item)
250 def created_json_format format
252 render json: @item.to_json(@my_model_class.show_json_opt), status: :created, location: @item
256 def not_created_html_format format
258 flash[:notice] = I18n.t('flash.notice.not_created', :model => @my_model_class.model_name.human)
263 def not_created_json_format format
265 render json: @item.errors, status: :unprocessable_entity
269 def render_create redirect_url = nil
271 respond_to do |format|
272 created_html_format format, redirect_url
273 created_json_format format
276 respond_to do |format|
277 not_created_html_format format
278 not_created_json_format format
283 def leaf_created_html_format format, redirect_url
285 flash[:notice] = I18n.t('flash.notice.created', :model => @my_model_class.model_name.human)
286 redirect_to redirect_url
290 def leaf_not_created_html_format format, redirect_url
292 flash[:notice] = I18n.t('flash.notice.not_created', :model => @my_model_class.model_name.human)
293 redirect_to redirect_url
297 def leaf_render_create redirect_url
298 if @item.store @operators
299 respond_to do |format|
300 leaf_created_html_format format, redirect_url
301 created_json_format format
304 respond_to do |format|
305 leaf_not_created_html_format format, redirect_url
306 not_created_json_format format
311 def updated_html_format format, redirect_url = nil
313 flash[:notice] = I18n.t('flash.notice.updated', :model => @my_model_class.model_name.human)
314 redirect_to (redirect_url ? redirect_url : @item)
318 def updated_json_format format
320 render json: '{}', status: :ok
324 def not_updated_html_format format
326 flash[:notice] = I18n.t('flash.notice.not_updated', :model => @my_model_class.model_name.human)
331 def not_updated_json_format format
333 render json: @item.errors, status: :unprocessable_entity
337 def render_update redirect_url = nil
339 respond_to do |format|
340 updated_html_format format, redirect_url
341 updated_json_format format
344 respond_to do |format|
345 not_updated_html_format format
346 not_updated_json_format format
351 def leaf_updated_html_format format, redirect_url
353 flash[:notice] = I18n.t('flash.notice.updated', :model => @my_model_class.model_name.human)
354 redirect_to redirect_url
358 def leaf_not_updated_html_format format, redirect_url
360 flash[:notice] = I18n.t('flash.notice.not_updated', :model => @my_model_class.model_name.human)
361 redirect_to redirect_url
365 def leaf_render_update ot, redirect_url
366 if @item.store @operators, ot
367 respond_to do |format|
368 leaf_updated_html_format format, redirect_url
369 updated_json_format format
372 respond_to do |format|
373 leaf_not_updated_html_format format, redirect_url
374 not_updated_json_format format
379 def destroyed_html_format format, redirect_url
381 flash[:notice] = I18n.t('flash.notice.destroyed', :model => @my_model_class.model_name.human)
382 redirect_to redirect_url
386 def destroyed_json_format format
388 render json: '{}', status: :ok
392 def not_destroyed_html_format format
394 flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => @my_model_class.model_name.human)
399 def not_destroyed_json_format format
401 render json: @item.errors, status: :unprocessable_entity
405 def render_destroy redirect_url
407 respond_to do |format|
408 destroyed_html_format format, redirect_url
409 destroyed_json_format format
412 respond_to do |format|
413 not_destroyed_html_format format
414 not_destroyed_json_format format
419 def render_destroy_by destroy_method_name, redirect_url = nil
420 if @item.__send__(destroy_method_name)
421 respond_to do |format|
422 destroyed_html_format format, redirect_url
423 destroyed_json_format format
426 respond_to do |format|
427 not_destroyed_html_format format
428 not_destroyed_json_format format
433 def format_filer format
435 @paginate = @@model.list_paginate(@page, @page_size)
436 render :template => 'system/filer', :locals => {
437 :items => @items, :model => @@model,
438 :operators => @operators, :pager => @paginate
443 def format_prof format
445 @profiler = self.class.profiler_manager.open(@item, @operators)
446 render :template => 'system/prof', :locals => {
447 :profiler => @profiler
452 def assist_items controller_name, action_name, options = {}
453 controller = Manifest.manifest.controllers[controller_name]
454 action = controller.actions[action_name]
456 options[:page_size] = 5
457 finder = action.find options
462 if file.respond_to?(:read)
465 Base64.decode64(file.to_s.gsub(' ', '+'))
469 def ymd_to_time ymd_str
470 return nil if ymd_str.blank?
473 date = Time.parse(ymd_str[0..3] + '/' + ymd_str[4..5] + '/' + ymd_str[6..7])
480 def export_url demander_url, action, token, date
481 u = demander_url + (demander_url[-1] == '/' ? '' : '/')
482 prm = '?auth_token=' + token
483 prm = prm + '&date=' + date.strftime("%Y%m%d") unless date.blank?
484 u = URI.join(u, action + '.json' + prm)
488 def export_from_provider url
491 json = RestClient.get url
492 res = JSON.parse json
498 def export_by action, provider_status, ymd
500 url = export_url provider_status.provider.demander_url, action, provider_status.token, t
501 export_from_provider(url)
504 rescue_from Pettanr::NotWork, :with => :render_not_work
505 def render_not_work(exception = nil)
507 logger.info "Rendering , :: #{exception.message}"
509 respond_to do |format|
511 render :file => "#{Rails.root}/public/not_work.html", :layout => false
514 render :text => "400 Not work", :status => 400
519 if Rails.env == 'production'
520 rescue_from ActiveRecord::RecordNotFound, :with => :render_404
521 rescue_from ActiveRecord::Forbidden, :with => :render_403
524 def render_404(exception = nil)
526 logger.info "Rendering 404: #{exception.message}"
528 respond_to do |format|
530 render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
533 render :text => "404 Not found", :status => 404
538 def render_403(exception = nil)
540 logger.info "Rendering 403: #{exception.message}"
542 respond_to do |format|
544 render :file => "#{Rails.root}/public/403.html", :status => 404, :layout => false
547 render :text => "403 Forbidden", :status => 403