class ActiveAdmin::Application
Constants
- AfterLoadEvent
- BeforeLoadEvent
Event that gets triggered on load of Active Admin
Attributes
Public Class Methods
# File lib/active_admin/application.rb, line 19 def initialize @namespaces = {} end
Public Instance Methods
Returns ALL the files to be loaded
# File lib/active_admin/application.rb, line 185 def files load_paths.flatten.compact.uniq.map{ |path| Dir["#{path}/**/*.rb"] }.flatten end
# File lib/active_admin/application.rb, line 178 def load(file) super rescue ActiveRecord::StatementInvalid => exception raise DatabaseHitDuringLoad.new exception end
Loads all ruby files that are within the load_paths setting. To reload everything simply call ‘ActiveAdmin.unload!`
# File lib/active_admin/application.rb, line 168 def load! unless loaded? ActiveAdmin::Event.dispatch BeforeLoadEvent, self # before_load hook files.each{ |file| load file } # load files namespace(default_namespace) # init AA resources ActiveAdmin::Event.dispatch AfterLoadEvent, self # after_load hook @@loaded = true end end
Whether all configuration files have been loaded
# File lib/active_admin/application.rb, line 155 def loaded? @@loaded ||= false end
Creates a namespace for the given name
Yields the namespace if a block is given
@returns [Namespace] the new or existing namespace
# File lib/active_admin/application.rb, line 128 def namespace(name) name ||= :root if namespaces[name] namespace = namespaces[name] else namespace = namespaces[name] = Namespace.new(self, name) ActiveAdmin::Event.dispatch ActiveAdmin::Namespace::RegisterEvent, namespace end yield(namespace) if block_given? namespace end
Runs after the app’s AA initializer
# File lib/active_admin/application.rb, line 112 def prepare! remove_active_admin_load_paths_from_rails_autoload_and_eager_load attach_reloader end
Registers a brand new configuration for the given resource.
# File lib/active_admin/application.rb, line 118 def register(resource, options = {}, &block) ns = options.fetch(:namespace){ default_namespace } namespace(ns).register resource, options, &block end
Register a page
@param name [String] The page name @options [Hash] Accepts option :namespace. @&block The registration block.
# File lib/active_admin/application.rb, line 149 def register_page(name, options = {}, &block) ns = options.fetch(:namespace){ default_namespace } namespace(ns).register_page name, options, &block end
# File lib/active_admin/application.rb, line 189 def router @router ||= Router.new(self) end
One-liner called by user’s config/routes.rb file
# File lib/active_admin/application.rb, line 194 def routes(rails_router) load! router.apply(rails_router) end
Runs before the app’s AA initializer
# File lib/active_admin/application.rb, line 107 def setup! register_default_assets end
Removes all defined controllers from memory. Useful in development, where they are reloaded on each request.
# File lib/active_admin/application.rb, line 161 def unload! namespaces.values.each &:unload! @@loaded = false end
Private Instance Methods
Hooks the app/admin directory into our Rails Engine’s watchable_dirs
, so the files are automatically reloaded in your development environment.
If files have changed on disk, we forcibly unload all AA configurations, and tell the host application to redraw routes (triggering AA itself to reload).
# File lib/active_admin/application.rb, line 237 def attach_reloader load_paths.each do |path| ActiveAdmin::Engine.config.watchable_dirs[path] = [:rb] end Rails.application.config.after_initialize do ActionDispatch::Reloader.to_prepare do ActiveAdmin.application.unload! Rails.application.reload_routes! end end end
# File lib/active_admin/application.rb, line 215 def register_default_assets register_stylesheet 'active_admin.css', media: 'screen' register_stylesheet 'active_admin/print.css', media: 'print' register_javascript 'active_admin.js' end
Since app/admin is alphabetically before app/models, we have to remove it from the host app’s autoload_paths
to prevent missing constant errors.
As well, we have to remove it from eager_load_paths
to prevent the files from being loaded twice in production.
# File lib/active_admin/application.rb, line 227 def remove_active_admin_load_paths_from_rails_autoload_and_eager_load ActiveSupport::Dependencies.autoload_paths -= load_paths Rails.application.config.eager_load_paths -= load_paths end