Class: ActiveAdmin::Namespace

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

Overview

Namespaces are the basic organizing principle for resources within Active Admin

Each resource is registered into a namespace which defines:

* the namespaceing for routing
* the module to namespace controllers
* the menu which gets displayed (other resources in the same namespace)

For example:

ActiveAdmin.register Post, namespace: :admin

Will register the Post model into the “admin” namespace. This will namespace the urls for the resource to “/admin/posts” and will set the controller to Admin::PostsController

You can also register to the “root” namespace, which is to say no namespace at all.

ActiveAdmin.register Post, namespace: false

This will register the resource to an instantiated namespace called :root. The resource will be accessible from “/posts” and the controller will be PostsController.

Defined Under Namespace

Classes: Store

Constant Summary collapse

RegisterEvent =
'active_admin.namespace.register'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(application, name) ⇒ Namespace

Returns a new instance of Namespace.



38
39
40
41
42
43
44
# File 'lib/active_admin/namespace.rb', line 38

def initialize(application, name)
  @application = application
  @name = name.to_s.underscore
  @resources = ResourceCollection.new
  register_module unless root?
  @menus = MenuCollection.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



58
59
60
# File 'lib/active_admin/namespace.rb', line 58

def method_missing(method, *args)
  settings.respond_to?(method) ? settings.send(method, *args) : super
end

Instance Attribute Details

#applicationObject (readonly)

Returns the value of attribute application.



36
37
38
# File 'lib/active_admin/namespace.rb', line 36

def application
  @application
end

Returns the value of attribute menus.



36
37
38
# File 'lib/active_admin/namespace.rb', line 36

def menus
  @menus
end

#resourcesObject (readonly)

Returns the value of attribute resources.



36
37
38
# File 'lib/active_admin/namespace.rb', line 36

def resources
  @resources
end

Class Method Details

.setting(name, default) ⇒ Object



29
30
31
# File 'lib/active_admin/namespace.rb', line 29

def setting(name, default)
  Deprecation.warn "This method does not do anything and will be removed."
end

Instance Method Details

#add_current_user_to_menu(menu, priority = 10, html_options = {}) ⇒ Object

The default user session menu item

Parameters:

  • menu (ActiveAdmin::MenuItem)

    The menu to add the logout link to

  • priority (Fixnum) (defaults to: 10)

    The numeric priority for the order in which it appears

  • html_options (Hash) (defaults to: {})

    An options hash to pass along to link_to



197
198
199
200
201
202
203
204
# File 'lib/active_admin/namespace.rb', line 197

def add_current_user_to_menu(menu, priority = 10, html_options = {})
  if current_user_method
    menu.add id: 'current_user', priority: priority, html_options: html_options,
      label: -> { display_name current_active_admin_user },
      url:   -> { auto_url_for(current_active_admin_user) },
      if:    :current_active_admin_user?
  end
end

#add_logout_button_to_menu(menu, priority = 20, html_options = {}) ⇒ Object

The default logout menu item

Parameters:

  • menu (ActiveAdmin::MenuItem)

    The menu to add the logout link to

  • priority (Fixnum) (defaults to: 20)

    The numeric priority for the order in which it appears

  • html_options (Hash) (defaults to: {})

    An options hash to pass along to link_to



181
182
183
184
185
186
187
188
189
# File 'lib/active_admin/namespace.rb', line 181

def add_logout_button_to_menu(menu, priority = 20, html_options = {})
  if logout_link_path
    html_options = html_options.reverse_merge(method: logout_link_method || :get)
    menu.add id: 'logout', priority: priority, html_options: html_options,
      label: ->{ I18n.t 'active_admin.logout' },
      url:   ->{ render_or_call_method_or_proc_on self, active_admin_namespace.logout_link_path },
      if:    :current_active_admin_user?
  end
end

#build_default_utility_navObject (protected)

Builds the default utility navigation in top right header with current user & logout button



232
233
234
235
236
237
238
# File 'lib/active_admin/namespace.rb', line 232

def build_default_utility_nav
  return if @menus.exists? :utility_navigation
  @menus.menu :utility_navigation do |menu|
    add_current_user_to_menu menu
    add_logout_button_to_menu menu
  end
end

#build_menu(name = DEFAULT_MENU) {|ActiveAdmin::Menu| ... } ⇒ void

This method returns an undefined value.

Add a callback to be ran when we build the menu

Parameters:

  • name (Symbol) (defaults to: DEFAULT_MENU)

    The name of the menu. Default: :default

Yields:



171
172
173
# File 'lib/active_admin/namespace.rb', line 171

def build_menu(name = DEFAULT_MENU, &block)
  extra_menus[name] = block
end

#build_menus!Object (protected)



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/active_admin/namespace.rb', line 212

def build_menus!
  # reset if any resources registered since last build
  @menus.clear! if resources.any? { |r| r.include_in_menu? && !r.menu_item }

  return if @menus.built?

  @menus.build_default_menu

  extra_menus.each_pair do |key, block|
    @menus.menu(key, &block)
  end

  build_default_utility_nav

  resources.each do |resource|
    resource.add_to_menu(@menus)
  end
end

#build_page(name, options) ⇒ Object (protected)



245
246
247
# File 'lib/active_admin/namespace.rb', line 245

def build_page(name, options)
  resources.add Page.new(self, name, options)
end

#configure_page(name, options = {}) {|config| ... } ⇒ Resource

Add page configuration options.

Assumes corresponding Rails Controller class already exists. Block is evaluated within config, not parsed as DSL.

Parameters:

  • name. (String)

Yields:

  • (config)

Returns:



118
119
120
121
122
123
124
125
126
127
# File 'lib/active_admin/namespace.rb', line 118

def configure_page(name, options = {}, &block)
  config = build_page(name, options)

  raise "#{config.controller_name} not found" unless Object.const_defined?(config.controller_name)
  config.controller.active_admin_config = config

  yield(config) if block_given?

  config
end

#configure_resource(resource_class, options = {}) {|config| ... } ⇒ Resource

Add or update resource configuration options.

Assumes corresponding Rails Controller class already exists. Block is evaluated within config, not parsed as DSL.

Parameters:

  • resource_class. (Class)

    ActiveRecord model.

Yields:

  • (config)

Returns:



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/active_admin/namespace.rb', line 87

def configure_resource(resource_class, options = {}, &block)
  config = find_or_build_resource(resource_class, options)

  raise "#{config.controller_name} not found" unless Object.const_defined?(config.controller_name)
  config.controller.active_admin_config = config

  yield(config) if block_given?

  ActiveSupport::Notifications.publish ActiveAdmin::Resource::RegisterEvent, config

  config
end

#extra_menusObject (protected)



208
209
210
# File 'lib/active_admin/namespace.rb', line 208

def extra_menus
  @extra_menus ||= {}
end

#fetch_menu(name) ⇒ Object



159
160
161
162
163
# File 'lib/active_admin/namespace.rb', line 159

def fetch_menu(name)
  build_menus!

  @menus.fetch(name)
end

#find_or_build_resource(resource_class, options) ⇒ Object (protected)

Either returns an existing Resource instance or builds a new one.



241
242
243
# File 'lib/active_admin/namespace.rb', line 241

def find_or_build_resource(resource_class, options)
  resources.add Resource.new(self, resource_class, options)
end

#module_nameObject

Returns the name of the module if required. Will be nil if none is required.

eg:

Namespace.new(:admin).module_name # => 'Admin'
Namespace.new(:root).module_name # => nil


140
141
142
# File 'lib/active_admin/namespace.rb', line 140

def module_name
  root? ? nil : @name.camelize
end

#nameObject



46
47
48
# File 'lib/active_admin/namespace.rb', line 46

def name
  @name.to_sym
end

#parse_page_registration_block(config, &block) ⇒ Object (protected)



304
305
306
# File 'lib/active_admin/namespace.rb', line 304

def parse_page_registration_block(config, &block)
  PageDSL.new(config).run_registration_block(&block)
end

#parse_registration_block(config, &block) ⇒ Object (protected)



299
300
301
302
# File 'lib/active_admin/namespace.rb', line 299

def parse_registration_block(config, &block)
  config.dsl = ResourceDSL.new(config)
  config.dsl.run_registration_block(&block)
end

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

Register a resource into this namespace. The preffered method to access this is to use the global registration ActiveAdmin.register which delegates to the proper namespace instance.



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/active_admin/namespace.rb', line 65

def register(resource_class, options = {}, &block)
  config = find_or_build_resource(resource_class, options)

  # Register the resource
  register_resource_controller(config)
  parse_registration_block(config, &block) if block_given?

  # Dispatch a registration event
  ActiveSupport::Notifications.publish ActiveAdmin::Resource::RegisterEvent, config

  # Return the config
  config
end

#register_moduleObject (protected)

Creates a ruby module to namespace all the classes in if required



281
282
283
284
285
# File 'lib/active_admin/namespace.rb', line 281

def register_module
  unless Object.const_defined? module_name
    Object.const_set module_name, Module.new
  end
end

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



100
101
102
103
104
105
106
107
108
# File 'lib/active_admin/namespace.rb', line 100

def register_page(name, options = {}, &block)
  config = build_page(name, options)

  # Register the resource
  register_page_controller(config)
  parse_page_registration_block(config, &block) if block_given?

  config
end

#register_page_controller(config) ⇒ Object (protected)



249
250
251
252
253
254
255
256
257
258
259
# File 'lib/active_admin/namespace.rb', line 249

def register_page_controller(config)
  if Object.const_defined?("::#{config.controller_name}")
    unless config.controller.ancestors.include?(ActiveAdmin::PageController)
      raise "#{config.controller_name} not a PageController"
    end
  else
    eval "class ::#{config.controller_name} < ActiveAdmin::PageController; end"
    config.controller.const_set('DISPOSABLE', true)
  end
  config.controller.active_admin_config = config
end

#register_resource_controller(config) ⇒ Object (protected)



287
288
289
290
291
292
293
294
295
296
297
# File 'lib/active_admin/namespace.rb', line 287

def register_resource_controller(config)
  if Object.const_defined?("::#{config.controller_name}")
    unless config.controller.ancestors.include?(ActiveAdmin::ResourceController)
      raise "#{config.controller_name} not a ResourceController"
    end
  else
    eval "class ::#{config.controller_name} < ActiveAdmin::ResourceController; end"
    config.controller.const_set(:DISPOSABLE, true)
  end
  config.controller.active_admin_config = config
end

#resource_for(klass) ⇒ Object

Returns the first registered ActiveAdmin::Resource instance for a given class



155
156
157
# File 'lib/active_admin/namespace.rb', line 155

def resource_for(klass)
  resources[klass]
end

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

Returns:

  • (Boolean)


54
55
56
# File 'lib/active_admin/namespace.rb', line 54

def respond_to_missing?(method, include_private = false)
  settings.respond_to?(method) || super
end

#root?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/active_admin/namespace.rb', line 129

def root?
  name == :root
end

#route_prefixObject



144
145
146
# File 'lib/active_admin/namespace.rb', line 144

def route_prefix
  root? ? nil : @name
end

#settingsObject



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

def settings
  @settings ||= SettingsNode.build(application.namespace_settings)
end

#unload!Object

Unload all the registered resources for this namespace



149
150
151
152
# File 'lib/active_admin/namespace.rb', line 149

def unload!
  @menus.clear!
  unload_resources!
end

#unload_resources!Object (protected)



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/active_admin/namespace.rb', line 261

def unload_resources!
  resources.each do |resource|
    parent = (module_name || 'Object').constantize
    name   = resource.controller_name.split('::').last
    if parent.const_defined?(name, false)
      if resource.controller.const_defined?(:DISPOSABLE)
        parent.send(:remove_const, name)
      end
    end

    # Remove circular references
    resource.controller.active_admin_config = nil
    if resource.is_a?(Resource) && resource.dsl
      resource.dsl.run_registration_block { @config = nil }
    end
  end
  @resources = ResourceCollection.new
end