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
Public Class Methods
# File lib/active_admin/namespace.rb, line 39 def initialize(application, name) @application = application @name = name.to_s.underscore @resources = ResourceCollection.new register_module unless root? build_menu_collection end
# File lib/active_admin/namespace.rb, line 30 def setting(name, default) ActiveAdmin.deprecator.warn "This method does not do anything and will be removed." end
Public Instance Methods
# File lib/active_admin/namespace.rb, line 59 def method_missing(method, *args) settings.respond_to?(method) ? settings.send(method, *args) : super end
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 103 def module_name root? ? nil : @name.camelize end
# File lib/active_admin/namespace.rb, line 47 def name @name.to_sym end
Register a resource into this namespace. The preferred 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 66 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.instrument ActiveAdmin::Resource::RegisterEvent, { active_admin_resource: config } # Return the config config end
# File lib/active_admin/namespace.rb, line 81 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
Returns the first registered ActiveAdmin::Resource
instance for a given class
# File lib/active_admin/namespace.rb, line 118 def resource_for(klass) resources[klass] end
# File lib/active_admin/namespace.rb, line 55 def respond_to_missing?(method, include_private = false) settings.respond_to?(method) || super end
# File lib/active_admin/namespace.rb, line 92 def root? name == :root end
# File lib/active_admin/namespace.rb, line 107 def route_prefix root? ? nil : @name end
# File lib/active_admin/namespace.rb, line 51 def settings @settings ||= SettingsNode.build(application.namespace_settings) end
Unload all the registered resources for this namespace
# File lib/active_admin/namespace.rb, line 112 def unload! unload_resources! reset_menu! end
Protected Instance Methods
# File lib/active_admin/namespace.rb, line 203 def build_page(name, options) resources.add Page.new(self, name, options) end
Either returns an existing Resource
instance or builds a new one.
# File lib/active_admin/namespace.rb, line 199 def find_or_build_resource(resource_class, options) resources.add Resource.new(self, resource_class, options) end
# File lib/active_admin/namespace.rb, line 246 def parse_page_registration_block(config, &block) PageDSL.new(config).run_registration_block(&block) end
# File lib/active_admin/namespace.rb, line 241 def parse_registration_block(config, &block) config.dsl = ResourceDSL.new(config) config.dsl.run_registration_block(&block) end
Creates a ruby module to namespace all the classes in if required
# File lib/active_admin/namespace.rb, line 229 def register_module unless Object.const_defined? module_name Object.const_set module_name, Module.new end end
TODO: replace ‘eval` with `Class.new`
# File lib/active_admin/namespace.rb, line 208 def register_page_controller(config) eval "class ::#{config.controller_name} < ActiveAdmin::PageController; end" config.controller.active_admin_config = config end
TODO: replace ‘eval` with `Class.new`
# File lib/active_admin/namespace.rb, line 236 def register_resource_controller(config) eval "class ::#{config.controller_name} < ActiveAdmin::ResourceController; end" config.controller.active_admin_config = config end
# File lib/active_admin/namespace.rb, line 213 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