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
Helper method to add a dashboard section
# File lib/active_admin/application.rb, line 204 def dashboard_section(name, options = {}, &block) ActiveAdmin::Dashboards.add_section(name, options, &block) end
Returns ALL the files to be loaded
# File lib/active_admin/application.rb, line 176 def files load_paths.flatten.compact.uniq.map{ |path| Dir["#{path}/**/*.rb"] }.flatten 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 165 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 152 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 125 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 109 def prepare! remove_active_admin_load_paths_from_rails_autoload attach_reloader end
Registers a brand new configuration for the given resource.
# File lib/active_admin/application.rb, line 115 def register(resource, options = {}, &block) ns_name = namespace_name(options) namespace(ns_name).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 146 def register_page(name, options = {}, &block) ns_name = namespace_name(options) namespace(ns_name).register_page name, options, &block end
# File lib/active_admin/application.rb, line 180 def router @router ||= Router.new(self) end
One-liner called by user's config/routes.rb file
# File lib/active_admin/application.rb, line 185 def routes(rails_router) load! router.apply(rails_router) end
Runs before the app's AA initializer
# File lib/active_admin/application.rb, line 104 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 158 def unload! namespaces.values.each{ |namespace| namespace.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 233 def attach_reloader load_paths.each do |path| ActiveAdmin::Engine.config.watchable_dirs[path] = [:rb] end app = self ActionDispatch::Reloader.to_prepare do app.unload! Rails.application.reload_routes! end end
Return either the passed in namespace or the default
# File lib/active_admin/application.rb, line 211 def namespace_name(options) options.fetch(:namespace){ default_namespace } 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 the default load path, app/admin, is alphabetically before app/models, we have to remove it from the host app's autoload_paths
to prevent missing constant errors.
# File lib/active_admin/application.rb, line 224 def remove_active_admin_load_paths_from_rails_autoload ActiveSupport::Dependencies.autoload_paths.reject!{ |path| load_paths.include? path } end