# verify that stylesheets we use are available and if not copy them to public/stylesheets mv_generator_templates_dir = "#{MasterView::ConfigSettings.mv_installation_dir}/generators/masterview/templates" unless File.exist?(mv_generator_templates_dir) # we are in a gem so things are in different directories MasterView::Log.debug{ 'MasterView appears to be installed as a gem...' } mv_generator_dir = MasterView::ConfigSettings.mv_installation_dir.gsub('\\','/').gsub( %r{/masterview-([^/]+)$}, '/masterview_generator-\1' ) mv_generator_templates_dir = "#{mv_generator_dir}/templates" end MasterView::Log.debug{ 'MasterView gem admin stylesheet src dir='+mv_generator_templates_dir } if File.directory?(mv_generator_templates_dir) rails_app_stylesheets_dir = Pathname.for_path(RAILS_ROOT) + 'public/stylesheets/masterview' stylesheet_specs = [ # from/to spec: [ , ] [ 'style.css', 'style.css' ], [ 'sidebox.css', 'sidebox.css' ], [ 'color-scheme.css', 'color-scheme.css' ] ] src_dir_accessor = MasterView::MIO::FileMIOTree.new( mv_generator_templates_dir ) dst_dir_accessor = MasterView::MIO::FileMIOTree.new( rails_app_stylesheets_dir, '.css', :logging => true) stylesheet_specs.each { | from, to | src_file = src_dir_accessor.path(from) dst_file = dst_dir_accessor.path(to) dst_file.write( src_file.read ) unless dst_file.exist? } end # load the admin controller's auth checking mixin admin_auth_module = nil admin_auth_mixin_spec = MasterView::ConfigSettings.admin_auth_mixin rails_root_path = MasterView::ConfigSettings.rails_root_path # ::RAILS_ROOT in normalized form app_mv_dir = "#{rails_root_path}/app/masterview" #File.join(rails_root_path, 'app/masterview') if admin_auth_mixin_spec.nil? # convention is to load admin_auth_mixin.rb from app/masterview dir if available module_path = "#{app_mv_dir}/admin_auth_mixin.rb" if File.exist?(module_path) MasterView::Log.debug { 'Using custom app/masterview/admin_auth mixin for MasterView admin controller' } require module_path admin_auth_module = Object.const_get(:MasterViewAdminAuthMixin) else # use built-in authorization mixin MasterView::Log.debug { 'Using default admin_auth mixin for MasterView admin controller (local requests only)' } require 'masterview/extras/admin_auth_mixin' admin_auth_module = MasterView::Admin::AuthMixin end else MasterView::Log.debug { 'Using custom admin_auth mixin for MasterView admin controller' } # load the app's auth_check module file_ref = admin_auth_mixin_spec.fetch(:file, '/admin_auth_mixin') file_loc = admin_auth_mixin_spec[:file_loc] if file_loc.nil? # default is to load auth mixin from the app/masterview dir module_path = "#{app_mv_dir}/#{file_ref}" elsif file_loc == :RAILS_ROOT # relative file reference from RAILS_ROOT module_path = "#{rails_root_path}/#{file_ref}" else # relative file reference to specified location # #?? use file_loc '' if file_ref is full path? or add checks in file_loc.nil? to handle path case?? module_path = File.join(file_loc, file_ref) end require module_path # resolve the module reference module_ref = admin_auth_mixin_spec.fetch(:module, :MasterViewAdminAuthMixin) #TODO: handle module namespace ref #if module_ref.is_a?( String) # target = ???err, what's the root of the name space universe? # for name in module_ref.split('::'): # target = target.const_get(name) # end # admin_auth_module = target #else # admin_auth_module = const_get(module_ref) #end admin_auth_module = Object.const_get(module_ref) end #assert admin_auth_module.method_defined?(:allow_access?) MasterView.const_set('MasterViewAdminAuthMixin', admin_auth_module) # add our app directories with the masterview_controller to the load path mv_controller_code_dirs = Dir[File.join(MasterView::ConfigSettings.mv_code_base_dir, '/extras/app/controllers')].select { |dir| File.directory?(dir) } if (not mv_controller_code_dirs.empty?) # we have some controller dirs to get into load path mv_controller_code_dirs.each { |dir| $LOAD_PATH.push dir } MasterView::Log.debug{ 'MasterView AdminPages controller directories = '+mv_controller_code_dirs.inspect } # rails 1.2 introduced controller_paths mattr_accessor if( ActionController::Routing.respond_to?(:controller_paths) and defined?(:Dependencies) and Dependencies.respond_to?(:load_paths) ) MasterView::Log.info{ 'Adding MasterView AdminPages controller directories to controller_paths' } ActionController::Routing.class_eval(<<-EOS, __FILE__, __LINE__) class << self alias_method :controller_paths_pre_mv, :controller_paths def controller_paths #:nodoc: # concatenate our controller dirs to original controller_paths controller_paths_pre_mv.concat #{mv_controller_code_dirs.inspect} end end EOS mv_controller_code_dirs.each { |dir| Dependencies.load_paths.push(dir) } # rails 1.1.5+ added additional safe_load_path which we need to add our directories so that controller will be found elsif (ActionController::Routing.const_defined?(:ControllerComponent) and #we have ControllerComponent ActionController::Routing::ControllerComponent.respond_to?(:safe_load_paths)) #we are in rails 1.1.5+ MasterView::Log.info{ 'Adding MasterView AdminPages controller directory to safe_load_path' } ActionController::Routing::ControllerComponent.class_eval(<<-EOS, __FILE__, __LINE__) class << self alias_method :safe_load_paths_pre_mv, :safe_load_paths protected def safe_load_paths #:nodoc: # concatenate our controller dirs to original safe_load_path safe_load_paths_pre_mv.concat #{mv_controller_code_dirs.inspect} end end EOS end end MasterView::Log.info{ 'MasterView Admin pages enabled' }