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

files() click to toggle source

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
load(file) click to toggle source
Calls superclass method
# File lib/active_admin/application.rb, line 178
def load(file)
  super
rescue ActiveRecord::StatementInvalid => exception
  raise DatabaseHitDuringLoad.new exception
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 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
loaded?() click to toggle source

Whether all configuration files have been loaded

# File lib/active_admin/application.rb, line 155
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 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
prepare!() click to toggle source

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

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_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 149
def register_page(name, options = {}, &block)
  ns = options.fetch(:namespace){ default_namespace }
  namespace(ns).register_page name, options, &block
end
router() click to toggle source
# File lib/active_admin/application.rb, line 189
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 194
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 107
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 161
def unload!
  namespaces.values.each &: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 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
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_and_eager_load() click to toggle source

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