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 20
def initialize
  @namespaces = Namespace::Store.new
end

Public Instance Methods

allow_comments=(*) click to toggle source

Deprecated Settings

# File lib/active_admin/application.rb, line 134
def allow_comments=(*)
  raise "`config.allow_comments` is no longer provided in ActiveAdmin 1.x. Use `config.comments` instead."
end
controllers_for_filters() click to toggle source
# File lib/active_admin/application.rb, line 247
def controllers_for_filters
  controllers = [BaseController]
  controllers.push *Devise.controllers_for_filters if Dependency.devise?
  controllers
end
files() click to toggle source

Returns ALL the files to be loaded

# File lib/active_admin/application.rb, line 220
def files
  load_paths.flatten.compact.uniq.flat_map{ |path| Dir["#{path}/**/*.rb"] }
end
load(file) click to toggle source
Calls superclass method
# File lib/active_admin/application.rb, line 215
def load(file)
  DatabaseHitDuringLoad.capture{ super }
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 205
def load!
  unless loaded?
    ActiveSupport::Notifications.publish BeforeLoadEvent, self # before_load hook
    files.each{ |file| load file }                             # load files
    namespace(default_namespace)                               # init AA resources
    ActiveSupport::Notifications.publish 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 192
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

@return [Namespace] the new or existing namespace

# File lib/active_admin/application.rb, line 166
def namespace(name)
  name ||= :root

  namespace = namespaces[name] ||= begin
    namespace = Namespace.new(self, name)
    ActiveSupport::Notifications.publish ActiveAdmin::Namespace::RegisterEvent, namespace
    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 150
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 156
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 @option [Hash] Accepts option :namespace. @&block The registration block.

# File lib/active_admin/application.rb, line 186
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 224
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 229
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 145
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 198
def unload!
  namespaces.each &:unload!
  @@loaded = false
end

Private Instance Methods

attach_reloader() click to toggle source

Hook into the Rails code reloading mechanism so that things are reloaded properly in development mode.

If any of the app files (e.g. models) has changed, we need to reload all the admin files. If the admin files themselves has changed, we need to regenerate the routes as well.

# File lib/active_admin/application.rb, line 278
def attach_reloader
  Rails.application.config.after_initialize do |app|
    unload_active_admin = -> { ActiveAdmin.application.unload! }

    if app.config.reload_classes_only_on_change
      # Rails is about to unload all the app files (e.g. models), so we
      # should first unload the classes generated by Active Admin, otherwise
      # they will contain references to the stale (unloaded) classes.
      Reloader.to_prepare(prepend: true, &unload_active_admin)
    else
      # If the user has configured the app to always reload app files after
      # each request, so we should unload the generated classes too.
      Reloader.to_complete(&unload_active_admin)
    end

    admin_dirs = {}

    load_paths.each do |path|
      admin_dirs[path] = [:rb]
    end

    routes_reloader = app.config.file_watcher.new([], admin_dirs) do
      app.reload_routes!
    end

    app.reloaders << routes_reloader

    Reloader.to_prepare do
      # Rails might have reloaded the routes for other reasons (e.g.
      # routes.rb has changed), in which case Active Admin would have been
      # loaded via the `ActiveAdmin.routes` call in `routes.rb`.
      #
      # Otherwise, we should check if any of the admin files are changed
      # and force the routes to reload if necessary. This would again causes
      # Active Admin to load via `ActiveAdmin.routes`.
      #
      # Finally, if Active Admin is still not loaded at this point, then we
      # would need to load it manually.
      unless ActiveAdmin.application.loaded?
        routes_reloader.execute_if_updated
        ActiveAdmin.application.load!
      end
    end
  end
end
register_default_assets() click to toggle source
# File lib/active_admin/application.rb, line 255
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 267
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