class ApplicationController < ActionController::Base
protect_from_forgery
+ layout :devise_layout if MagicNumber['test_layout']
before_filter :bf
+ def devise_layout
+ if devise_controller?
+ case resource_name
+ when :admin
+ 'guest'
+ when :user
+ 'guest'
+ when :demand_user
+ 'guest'
+ end
+ else
+ 'application'
+ end
+ end
+
def bf
+ @server_result = {
+ :location => {:controller => params[:controller], :action => params[:action]}
+ }
+ @server_result[:location][:id] = params[:id] if params[:id]
if Admin.count.to_i == 0 or License.count.to_i == 0
if params[:controller] == 'system' and params[:action] == 'start'
else
redirect_to :controller => '/system', :action => 'start'
end
else
- if user_signed_in?
- @user = current_user
- @author = @user.author
- @artist = if @author and @author.artist?
- @author.artist
- else
- Artist.new author_id: @author.id, email: @user.email, name: @author.name, default_license_id: 1
- end
+ user = if user_signed_in?
+ current_user
+ else
+ nil
end
+ author = if user
+ user.author
+ else
+ nil
+ end
+ artist = if user
+ user.artist
+ else
+ nil
+ end
+ admin = if admin_signed_in?
+ current_admin
+ else
+ nil
+ end
+ demand_user = if demand_user_signed_in?
+ current_demand_user
+ else
+ nil
+ end
+ @operators = Operator.new [user, author, artist, admin, demand_user]
end
end
+ def authenticate_reader
+ authenticate_user! unless @operators.reader?
+ end
+
+ def authenticate_user
+ authenticate_user! unless @operators.user?
+ end
+
+ def authenticate_resource_reader
+ authenticate_user! unless @operators.resource_reader?
+ end
+
+ def authenticate_author
+ if @operators.author
+ true
+ else
+ respond_to do |format|
+ format.html { redirect_to main_app.new_author_path, :status => :found }
+ format.js { render "authors/new" }
+ format.json {
+ raise ActiveRecord::Forbidden
+ }
+ end
+ false
+ end
+ end
+
def authenticate_artist
- if @author.artist?
+ if @operators.artist
true
else
respond_to do |format|
- format.html { redirect_to new_artist_path, :status => :found }
+ format.html { redirect_to main_app.new_artist_path, :status => :found }
format.js { render "artists/new" }
format.json {
raise ActiveRecord::Forbidden
end
end
-=begin
- rescue_from ActiveRecord::RecordNotFound, :with => :render_404
-
- private
- def render_404(exception = nil)
- if exception
- logger.info "Rendering 404: #{exception.message}"
+ def self.controller
+ Pettanr::Application::manifest.controller_managers[self.model.item_name]
+ end
+
+ def self.profiler_manager
+ Pettanr::Application::manifest.profiler_managers[self.model.item_name]
+ end
+
+ def public_list
+ action_name = params[:action]
+ @action = self.class.controller.open(action_name, params, @operators)
+ @items = @action.items
+ respond_to do |format|
+ format.html {
+ @filer = @action.filer
+ render :template => 'system/filer', :locals => {
+ :filer => @filer
+ }
+ }
+ format.json { render json: @items.to_json(self.class.model.list_json_opt) }
+ format.atom
+ format.rss
end
+ end
+
+ def my_list params
+ controller_name = params[:controller]
+ action_name = params[:action]
+ controller = Pettanr::Application::manifest.controller_managers[controller_name]
+ @action = controller.open(action_name, params, @operators)
+ @items = @action.items
+
respond_to do |format|
- format.html {
- render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
-}
- format.json {
- render :text => "404 found", :status => 404
-}
+ format.html {
+ @filer = @action.filer
+ render :template => 'system/filer', :locals => {
+ :filer => @filer
+ }
+ }
+ format.json { render json: @items.to_json(Scroll.list_json_opt) }
+ end
+ end
+
+ def format_filer format
+ format.html {
+ @paginate = @@model.list_paginate(@page, @page_size)
+ render :template => 'system/filer', :locals => {
+ :items => @items, :model => @@model,
+ :operators => @operators, :pager => @paginate
+ }
+ }
end
+
+ def format_prof format
+ format.prof {
+ @profiler = self.class.profiler_manager.open(@item, @operators)
+ render :template => 'system/prof', :locals => {
+ :profiler => @profiler
+ }
+ }
+ end
+
+ def set_image(file)
+ if file.respond_to?(:read)
+ file.read
+ else
+ Base64.decode64(file.to_s.gsub(' ', '+'))
+ end
+ end
+
+ def ymd_to_time ymd_str
+ return nil if ymd_str.blank?
+ date = nil
+ begin
+ date = Time.parse(ymd_str[0..3] + '/' + ymd_str[4..5] + '/' + ymd_str[6..7])
+ rescue
+ date = nil
+ end
+ date
+ end
+
+ def export_url demander_url, action, token, date
+ u = demander_url + (demander_url[-1] == '/' ? '' : '/')
+ prm = '?auth_token=' + token
+ prm = prm + '&date=' + date.strftime("%Y%m%d") unless date.blank?
+ u = URI.join(u, action + '.json' + prm)
+ u.to_s
+ end
+
+ def export_from_provider url
+ res = nil
+ begin
+ json = RestClient.get url
+ res = JSON.parse json
+ rescue
+ end
+ res
+ end
+
+ def export_by action, provider_status, ymd
+ t = ymd_to_time ymd
+ url = export_url provider_status.provider.demander_url, action, provider_status.token, t
+ export_from_provider(url)
+ end
+
+ rescue_from Pettanr::NotWork, :with => :render_not_work
+ def render_not_work(exception = nil)
+ if exception
+ logger.info "Rendering , :: #{exception.message}"
+ end
+ respond_to do |format|
+ format.html {
+ render :file => "#{Rails.root}/public/not_work.html", :layout => false
+ }
+ format.json {
+ render :text => "400 Not work", :status => 400
+ }
+ end
+ end
+
+ if Rails.env == 'production'
+ rescue_from ActiveRecord::RecordNotFound, :with => :render_404
+ rescue_from ActiveRecord::Forbidden, :with => :render_403
+
+ private
+ def render_404(exception = nil)
+ if exception
+ logger.info "Rendering 404: #{exception.message}"
+ end
+ respond_to do |format|
+ format.html {
+ render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
+ }
+ format.json {
+ render :text => "404 Not found", :status => 404
+ }
+ end
+ end
+
+ def render_403(exception = nil)
+ if exception
+ logger.info "Rendering 403: #{exception.message}"
+ end
+ respond_to do |format|
+ format.html {
+ render :file => "#{Rails.root}/public/403.html", :status => 404, :layout => false
+ }
+ format.json {
+ render :text => "403 Forbidden", :status => 403
+ }
+ end
+ end
end
-=end
end