Class: ActiveAdmin::Namespace
- Inherits:
-
Object
- Object
- ActiveAdmin::Namespace
- 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
-
#application ⇒ Object
readonly
Returns the value of attribute application.
-
#menus ⇒ Object
readonly
Returns the value of attribute menus.
-
#resources ⇒ Object
readonly
Returns the value of attribute resources.
Class Method Summary collapse
Instance Method Summary collapse
-
#build_menu(name = DEFAULT_MENU) {|ActiveAdmin::Menu| ... } ⇒ void
Add a callback to be ran when we build the menu.
- #fetch_menu(name) ⇒ Object
-
#initialize(application, name) ⇒ Namespace
constructor
A new instance of Namespace.
- #method_missing(method, *args) ⇒ Object
-
#module_name ⇒ Object
Returns the name of the module if required.
- #name ⇒ Object
-
#register(resource_class, options = {}, &block) ⇒ Object
Register a resource into this namespace.
- #register_page(name, options = {}, &block) ⇒ Object
- #reset_menu! ⇒ Object
-
#resource_for(klass) ⇒ Object
Returns the first registered ActiveAdmin::Resource instance for a given class.
- #respond_to_missing?(method, include_private = false) ⇒ Boolean
- #root? ⇒ Boolean
- #route_prefix ⇒ Object
- #settings ⇒ Object
-
#unload! ⇒ Object
Unload all the registered resources for this namespace.
Constructor Details
#initialize(application, name) ⇒ Namespace
Returns a new instance of Namespace.
39 40 41 42 43 44 45 |
# 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? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
59 60 61 |
# File 'lib/active_admin/namespace.rb', line 59 def method_missing(method, *args) settings.respond_to?(method) ? settings.send(method, *args) : super end |
Instance Attribute Details
#application ⇒ Object (readonly)
Returns the value of attribute application.
37 38 39 |
# File 'lib/active_admin/namespace.rb', line 37 def application @application end |
#menus ⇒ Object (readonly)
Returns the value of attribute menus.
37 38 39 |
# File 'lib/active_admin/namespace.rb', line 37 def @menus end |
#resources ⇒ Object (readonly)
Returns the value of attribute resources.
37 38 39 |
# File 'lib/active_admin/namespace.rb', line 37 def resources @resources end |
Class Method Details
.setting(name, default) ⇒ Object
30 31 32 |
# 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 |
Instance Method Details
#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
136 137 138 139 140 141 142 |
# File 'lib/active_admin/namespace.rb', line 136 def (name = DEFAULT_MENU) @menus.before_build do || . name do || yield end end end |
#fetch_menu(name) ⇒ Object
122 123 124 |
# File 'lib/active_admin/namespace.rb', line 122 def (name) @menus.fetch(name) end |
#module_name ⇒ Object
103 104 105 |
# File 'lib/active_admin/namespace.rb', line 103 def module_name root? ? nil : @name.camelize end |
#name ⇒ Object
47 48 49 |
# File 'lib/active_admin/namespace.rb', line 47 def name @name.to_sym end |
#register(resource_class, options = {}, &block) ⇒ Object
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.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/active_admin/namespace.rb', line 66 def register(resource_class, = {}, &block) config = find_or_build_resource(resource_class, ) # Register the resource register_resource_controller(config) parse_registration_block(config, &block) if block # Dispatch a registration event ActiveSupport::Notifications.instrument ActiveAdmin::Resource::RegisterEvent, { active_admin_resource: config } # Return the config config end |
#register_page(name, options = {}, &block) ⇒ Object
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/active_admin/namespace.rb', line 81 def register_page(name, = {}, &block) config = build_page(name, ) # Register the resource register_page_controller(config) parse_page_registration_block(config, &block) if block config end |
#reset_menu! ⇒ Object
126 127 128 |
# File 'lib/active_admin/namespace.rb', line 126 def @menus.clear! end |
#resource_for(klass) ⇒ Object
Returns the first registered ActiveAdmin::Resource instance for a given class
118 119 120 |
# File 'lib/active_admin/namespace.rb', line 118 def resource_for(klass) resources[klass] end |
#respond_to_missing?(method, include_private = false) ⇒ Boolean
55 56 57 |
# File 'lib/active_admin/namespace.rb', line 55 def respond_to_missing?(method, include_private = false) settings.respond_to?(method) || super end |
#root? ⇒ Boolean
92 93 94 |
# File 'lib/active_admin/namespace.rb', line 92 def root? name == :root end |
#route_prefix ⇒ Object
107 108 109 |
# File 'lib/active_admin/namespace.rb', line 107 def route_prefix root? ? nil : @name end |
#settings ⇒ Object
51 52 53 |
# File 'lib/active_admin/namespace.rb', line 51 def settings @settings ||= SettingsNode.build(application.namespace_settings) end |
#unload! ⇒ Object
Unload all the registered resources for this namespace
112 113 114 115 |
# File 'lib/active_admin/namespace.rb', line 112 def unload! unload_resources! end |