Class: ActiveAdmin::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/active_admin/application.rb

Constant Summary collapse

BeforeLoadEvent =

Event that gets triggered on load of Active Admin

"active_admin.application.before_load".freeze
AfterLoadEvent =
"active_admin.application.after_load".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApplication

Returns a new instance of Application.



42
43
44
# File 'lib/active_admin/application.rb', line 42

def initialize
  @namespaces = Namespace::Store.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/active_admin/application.rb', line 31

def method_missing(method, *args)
  if settings.respond_to?(method)
    settings.send(method, *args)
  elsif namespace_settings.respond_to?(method)
    namespace_settings.send(method, *args)
  else
    super
  end
end

Instance Attribute Details

#namespacesObject (readonly)

Returns the value of attribute namespaces.



41
42
43
# File 'lib/active_admin/application.rb', line 41

def namespaces
  @namespaces
end

Class Method Details

.inheritable_setting(name, default) ⇒ Object



14
15
16
# File 'lib/active_admin/application.rb', line 14

def inheritable_setting(name, default)
  NamespaceSettings.register name, default
end

.setting(name, default) ⇒ Object



10
11
12
# File 'lib/active_admin/application.rb', line 10

def setting(name, default)
  ApplicationSettings.register name, default
end

Instance Method Details

#controllers_for_filtersObject



156
157
158
159
160
# File 'lib/active_admin/application.rb', line 156

def controllers_for_filters
  controllers = [BaseController]
  controllers.push *Devise.controllers_for_filters if Dependency.devise?
  controllers
end

#filesObject

Returns ALL the files to be loaded



125
126
127
# File 'lib/active_admin/application.rb', line 125

def files
  load_paths.flatten.compact.uniq.flat_map { |path| Dir["#{path}/**/*.rb"].sort }
end

#load(file) ⇒ Object



120
121
122
# File 'lib/active_admin/application.rb', line 120

def load(file)
  DatabaseHitDuringLoad.capture { super }
end

#load!Object

Loads all ruby files that are within the load_paths setting. To reload everything simply call ‘ActiveAdmin.unload!`



110
111
112
113
114
115
116
117
118
# File 'lib/active_admin/application.rb', line 110

def load!
  unless loaded?
    ActiveSupport::Notifications.instrument BeforeLoadEvent, { active_admin_application: self } # before_load hook
    files.each { |file| load file } # load files
    namespace(default_namespace) # init AA resources
    ActiveSupport::Notifications.instrument AfterLoadEvent, { active_admin_application: self } # after_load hook
    @@loaded = true
  end
end

#loaded?Boolean

Whether all configuration files have been loaded

Returns:

  • (Boolean)


97
98
99
# File 'lib/active_admin/application.rb', line 97

def loaded?
  @@loaded ||= false
end

#namespace(name) {|namespace| ... } ⇒ Namespace

Creates a namespace for the given name

Yields the namespace if a block is given

Yields:

Returns:

  • (Namespace)

    the new or existing namespace



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/active_admin/application.rb', line 71

def namespace(name)
  name ||= :root

  namespace = namespaces[name.to_sym] ||= begin
    namespace = Namespace.new(self, name)
    ActiveSupport::Notifications.instrument ActiveAdmin::Namespace::RegisterEvent, { active_admin_namespace: namespace }
    namespace
  end

  yield(namespace) if block_given?

  namespace
end

#namespace_settingsObject



23
24
25
# File 'lib/active_admin/application.rb', line 23

def namespace_settings
  @namespace_settings ||= SettingsNode.build(NamespaceSettings)
end

#prepare!Object

Runs after the app’s AA initializer



55
56
57
58
# File 'lib/active_admin/application.rb', line 55

def prepare!
  remove_active_admin_load_paths_from_rails_autoload_and_eager_load
  attach_reloader
end

#register(resource, options = {}, &block) ⇒ Object

Registers a brand new configuration for the given resource.



61
62
63
64
# File 'lib/active_admin/application.rb', line 61

def register(resource, options = {}, &block)
  ns = options.fetch(:namespace) { default_namespace }
  namespace(ns).register resource, options, &block
end

#register_page(name, options = {}, &block) ⇒ Object

Register a page

@&block The registration block.

Parameters:

  • name (String)

    The page name

  • [Hash] (Hash)

    a customizable set of options



91
92
93
94
# File 'lib/active_admin/application.rb', line 91

def register_page(name, options = {}, &block)
  ns = options.fetch(:namespace) { default_namespace }
  namespace(ns).register_page name, options, &block
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/active_admin/application.rb', line 27

def respond_to_missing?(method, include_private = false)
  [settings, namespace_settings].any? { |sets| sets.respond_to?(method) } || super
end

#routes(rails_router) ⇒ Object

Creates all the necessary routes for the ActiveAdmin configurations

Use this within the routes.rb file:

Application.routes.draw do |map|
  ActiveAdmin.routes(self)
end

Parameters:

  • rails_router (ActionDispatch::Routing::Mapper)


138
139
140
141
# File 'lib/active_admin/application.rb', line 138

def routes(rails_router)
  load!
  Router.new(router: rails_router, namespaces: namespaces).apply
end

#settingsObject



19
20
21
# File 'lib/active_admin/application.rb', line 19

def settings
  @settings ||= SettingsNode.build(ApplicationSettings)
end

#setup!Object

Runs before the app’s AA initializer



51
52
# File 'lib/active_admin/application.rb', line 51

def setup!
end

#unload!Object

Removes all defined controllers from memory. Useful in development, where they are reloaded on each request.



103
104
105
106
# File 'lib/active_admin/application.rb', line 103

def unload!
  namespaces.each &:unload!
  @@loaded = false
end