Class: ActiveAdmin::Namespace
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.
Constant Summary collapse
- RegisterEvent =
'active_admin.namespace.register'.freeze
Instance Attribute Summary collapse
-
#application ⇒ Object
readonly
Returns the value of attribute application.
-
#menu ⇒ Object
readonly
Returns the value of attribute menu.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#resources ⇒ Object
readonly
Returns the value of attribute resources.
Instance Method Summary collapse
-
#dashboard_controller_name ⇒ Object
Returns the name of the dashboard controller for this namespace.
-
#initialize(application, name) ⇒ Namespace
constructor
A new instance of Namespace.
-
#load_menu! ⇒ Object
The menu gets built by Active Admin once all the resources have been loaded.
-
#module_name ⇒ Object
Returns the name of the module if required.
-
#register(resource, options = {}, &block) ⇒ Object
Register a resource into this namespace.
-
#resource_for(klass) ⇒ Object
Returns the first registered ActiveAdmin::Resource instance for a given class.
- #root? ⇒ Boolean
-
#unload! ⇒ Object
Unload all the registered resources for this namespace.
Constructor Details
#initialize(application, name) ⇒ Namespace
Returns a new instance of Namespace.
33 34 35 36 37 38 39 40 |
# File 'lib/active_admin/namespace.rb', line 33 def initialize(application, name) @application = application @name = name.to_s.underscore.to_sym @resources = {} @menu = Menu.new register_module unless root? generate_dashboard_controller end |
Instance Attribute Details
#application ⇒ Object (readonly)
Returns the value of attribute application.
31 32 33 |
# File 'lib/active_admin/namespace.rb', line 31 def application @application end |
#menu ⇒ Object (readonly)
Returns the value of attribute menu.
31 32 33 |
# File 'lib/active_admin/namespace.rb', line 31 def @menu end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
31 32 33 |
# File 'lib/active_admin/namespace.rb', line 31 def name @name end |
#resources ⇒ Object (readonly)
Returns the value of attribute resources.
31 32 33 |
# File 'lib/active_admin/namespace.rb', line 31 def resources @resources end |
Instance Method Details
#dashboard_controller_name ⇒ Object
Returns the name of the dashboard controller for this namespace
80 81 82 |
# File 'lib/active_admin/namespace.rb', line 80 def dashboard_controller_name [module_name, "DashboardController"].compact.join("::") end |
#load_menu! ⇒ Object
The menu gets built by Active Admin once all the resources have been loaded. This method gets called to register each resource with the menu system.
93 94 95 96 97 98 |
# File 'lib/active_admin/namespace.rb', line 93 def register_dashboard resources.values.each do |config| (config) if config. end end |
#module_name ⇒ Object
74 75 76 77 |
# File 'lib/active_admin/namespace.rb', line 74 def module_name return nil if root? @module_name ||= name.to_s.camelize end |
#register(resource, 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.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/active_admin/namespace.rb', line 45 def register(resource, = {}, &block) config = find_or_build_resource(resource, ) # Register the resource register_resource_controller(config) parse_registration_block(config, &block) if block_given? (config) if config. # Ensure that the dashboard is generated generate_dashboard_controller # Dispatch a registration event ActiveAdmin::Event.dispatch ActiveAdmin::Resource::RegisterEvent, config # Return the config config end |
#resource_for(klass) ⇒ Object
Returns the first registered ActiveAdmin::Resource instance for a given class
101 102 103 |
# File 'lib/active_admin/namespace.rb', line 101 def resource_for(klass) resources.values.find{|config| config.resource == klass } end |
#root? ⇒ Boolean
63 64 65 |
# File 'lib/active_admin/namespace.rb', line 63 def root? name == :root end |
#unload! ⇒ Object
Unload all the registered resources for this namespace
85 86 87 88 89 |
# File 'lib/active_admin/namespace.rb', line 85 def unload! unload_resources! unload_dashboard! end |