class ActiveAdmin::Application

Constants

AfterLoadEvent
BeforeLoadEvent

Event that gets triggered on load of Active Admin

Attributes

namespaces[R]

Public Class Methods

new() click to toggle source
# File lib/active_admin/application.rb, line 19
def initialize
  @namespaces = {}
end

Public Instance Methods

dashboard_section(name, options = {}, &block) click to toggle source

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
files() click to toggle source

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
load!() click to toggle source

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
loaded?() click to toggle source

Whether all configuration files have been loaded

# File lib/active_admin/application.rb, line 152
def loaded?
  @@loaded ||= false
end
namespace(name) { |namespace| ... } click to toggle source

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
prepare!() click to toggle source

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
register(resource, options = {}, &block) click to toggle source

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_page(name, options = {}, &block) click to toggle source

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
router() click to toggle source
# File lib/active_admin/application.rb, line 180
def router
  @router ||= Router.new(self)
end
routes(rails_router) click to toggle source

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
setup!() click to toggle source

Runs before the app's AA initializer

# File lib/active_admin/application.rb, line 104
def setup!
  register_default_assets
end
unload!() click to toggle source

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

attach_reloader() click to toggle source

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
namespace_name(options) click to toggle source

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
register_default_assets() click to toggle source
# 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
remove_active_admin_load_paths_from_rails_autoload() click to toggle source

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