Class: ActiveAdmin::Application

Inherits:
Object
  • Object
show all
Includes:
AssetRegistration, Settings, Settings::Inheritance
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

Instance Method Summary collapse

Methods included from AssetRegistration

#clear_javascripts!, #clear_stylesheets!, #javascripts, #register_javascript, #register_stylesheet, #stylesheets

Methods included from Settings

#read_default_setting

Constructor Details

#initializeApplication

Returns a new instance of Application.



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

def initialize
  @namespaces = {}
end

Instance Attribute Details

#namespacesObject (readonly)

Returns the value of attribute namespaces.



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

def namespaces
  @namespaces
end

Instance Method Details

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

Helper method to add a dashboard section



204
205
206
# File 'lib/active_admin/application.rb', line 204

def dashboard_section(name, options = {}, &block)
  ActiveAdmin::Dashboards.add_section(name, options, &block)
end

#filesObject

Returns ALL the files to be loaded



176
177
178
# File 'lib/active_admin/application.rb', line 176

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

#load!Object

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



165
166
167
168
169
170
171
172
173
# 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?Boolean

Whether all configuration files have been loaded

Returns:

  • (Boolean)


152
153
154
# File 'lib/active_admin/application.rb', line 152

def loaded?
  @@loaded ||= false
end

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

Creates a namespace for the given name

Yields the namespace if a block is given

Yields:



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# 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!Object

Runs after the app’s AA initializer



109
110
111
112
# 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) ⇒ Object

Registers a brand new configuration for the given resource.



115
116
117
118
# 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) ⇒ Object

Register a page

@&block The registration block.

Parameters:

  • name (String)

    The page name



146
147
148
149
# 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

#routerObject



180
181
182
# File 'lib/active_admin/application.rb', line 180

def router
  @router ||= Router.new(self)
end

#routes(rails_router) ⇒ Object

One-liner called by user’s config/routes.rb file



185
186
187
188
# File 'lib/active_admin/application.rb', line 185

def routes(rails_router)
  load!
  router.apply(rails_router)
end

#setup!Object

Runs before the app’s AA initializer



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

def setup!
  register_default_assets
end

#unload!Object

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



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

def unload!
  namespaces.values.each{ |namespace| namespace.unload! }
  @@loaded = false
end