class ActiveAdmin::Namespace

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.

Constants

RegisterEvent

Attributes

application[R]
menus[R]
resources[R]

Public Class Methods

new(application, name) click to toggle source
# 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?
  build_menu_collection
end
setting(name, default) click to toggle source
# 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

Public Instance Methods

add_current_user_to_menu(menu, priority = 10, html_options = {}) click to toggle source

The default user session menu item

@param [ActiveAdmin::MenuItem] menu The menu to add the logout link to @param [Fixnum] priority The numeric priority for the order in which it appears @param [Hash] html_options An options hash to pass along to link_to

# File lib/active_admin/namespace.rb, line 165
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 = {}) click to toggle source

The default logout menu item

@param [ActiveAdmin::MenuItem] menu The menu to add the logout link to @param [Fixnum] priority The numeric priority for the order in which it appears @param [Hash] html_options An options hash to pass along to link_to

# File lib/active_admin/namespace.rb, line 149
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_menu(name = DEFAULT_MENU) { |menu| ... } click to toggle source

Add a callback to be ran when we build the menu

@param [Symbol] name The name of the menu. Default: :default @yield [ActiveAdmin::Menu] The block to be ran when the menu is built

@return [void]

# File lib/active_admin/namespace.rb, line 135
def build_menu(name = DEFAULT_MENU)
  @menus.before_build do |menus|
    menus.menu name do |menu|
      yield menu
    end
  end
end
fetch_menu(name) click to toggle source
# File lib/active_admin/namespace.rb, line 121
def fetch_menu(name)
  @menus.fetch(name)
end
method_missing(method, *args) click to toggle source
Calls superclass method
# File lib/active_admin/namespace.rb, line 58
def method_missing(method, *args)
  settings.respond_to?(method) ? settings.send(method, *args) : super
end
module_name() click to toggle source

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
# File lib/active_admin/namespace.rb, line 102
def module_name
  root? ? nil : @name.camelize
end
name() click to toggle source
# File lib/active_admin/namespace.rb, line 46
def name
  @name.to_sym
end
register(resource_class, options = {}, &block) click to toggle source

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.

# 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?
  reset_menu!

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

  # Return the config
  config
end
register_page(name, options = {}, &block) click to toggle source
# File lib/active_admin/namespace.rb, line 80
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?
  reset_menu!

  config
end
reset_menu!() click to toggle source
# File lib/active_admin/namespace.rb, line 125
def reset_menu!
  @menus.clear!
end
resource_for(klass) click to toggle source

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

# File lib/active_admin/namespace.rb, line 117
def resource_for(klass)
  resources[klass]
end
respond_to_missing?(method, include_private = false) click to toggle source
Calls superclass method
# File lib/active_admin/namespace.rb, line 54
def respond_to_missing?(method, include_private = false)
  settings.respond_to?(method) || super
end
root?() click to toggle source
# File lib/active_admin/namespace.rb, line 91
def root?
  name == :root
end
route_prefix() click to toggle source
# File lib/active_admin/namespace.rb, line 106
def route_prefix
  root? ? nil : @name
end
settings() click to toggle source
# File lib/active_admin/namespace.rb, line 50
def settings
  @settings ||= SettingsNode.build(application.namespace_settings)
end
unload!() click to toggle source

Unload all the registered resources for this namespace

# File lib/active_admin/namespace.rb, line 111
def unload!
  unload_resources!
  reset_menu!
end

Protected Instance Methods

build_default_utility_nav() click to toggle source

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

# File lib/active_admin/namespace.rb, line 189
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_collection() click to toggle source
# File lib/active_admin/namespace.rb, line 176
def build_menu_collection
  @menus = MenuCollection.new

  @menus.on_build do
    build_default_utility_nav

    resources.each do |resource|
      resource.add_to_menu(@menus)
    end
  end
end
build_page(name, options) click to toggle source
# File lib/active_admin/namespace.rb, line 202
def build_page(name, options)
  resources.add Page.new(self, name, options)
end
find_or_build_resource(resource_class, options) click to toggle source

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

# File lib/active_admin/namespace.rb, line 198
def find_or_build_resource(resource_class, options)
  resources.add Resource.new(self, resource_class, options)
end
parse_page_registration_block(config, &block) click to toggle source
# File lib/active_admin/namespace.rb, line 245
def parse_page_registration_block(config, &block)
  PageDSL.new(config).run_registration_block(&block)
end
parse_registration_block(config, &block) click to toggle source
# File lib/active_admin/namespace.rb, line 240
def parse_registration_block(config, &block)
  config.dsl = ResourceDSL.new(config)
  config.dsl.run_registration_block(&block)
end
register_module() click to toggle source

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

# File lib/active_admin/namespace.rb, line 228
def register_module
  unless Object.const_defined? module_name
    Object.const_set module_name, Module.new
  end
end
register_page_controller(config) click to toggle source

TODO: replace `eval` with `Class.new`

# File lib/active_admin/namespace.rb, line 207
def register_page_controller(config)
  eval "class ::#{config.controller_name} < ActiveAdmin::PageController; end"
  config.controller.active_admin_config = config
end
register_resource_controller(config) click to toggle source

TODO: replace `eval` with `Class.new`

# File lib/active_admin/namespace.rb, line 235
def register_resource_controller(config)
  eval "class ::#{config.controller_name} < ActiveAdmin::ResourceController; end"
  config.controller.active_admin_config = config
end
unload_resources!() click to toggle source
# File lib/active_admin/namespace.rb, line 212
def unload_resources!
  resources.each do |resource|
    parent = (module_name || 'Object').constantize
    name   = resource.controller_name.split('::').last
    parent.send(:remove_const, name) if parent.const_defined?(name, false)

    # 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