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 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.
Instance Method Summary collapse
-
#add_current_user_to_menu(menu, priority = 10, html_options = {}) ⇒ Object
The default user session menu item.
-
#add_logout_button_to_menu(menu, priority = 20, html_options = {}) ⇒ Object
The default logout menu item.
-
#build_default_utility_nav ⇒ Object
protected
Builds the default utility navigation in top right header with current user & logout button.
-
#build_menu(name = DEFAULT_MENU) {|ActiveAdmin::Menu| ... } ⇒ void
Add a callback to be ran when we build the menu.
- #build_menu_collection ⇒ Object protected
- #build_page(name, options) ⇒ Object protected
- #fetch_menu(name) ⇒ Object
-
#find_or_build_resource(resource_class, options) ⇒ Object
protected
Either returns an existing Resource instance or builds a new one.
-
#initialize(application, name) ⇒ Namespace
constructor
A new instance of Namespace.
-
#module_name ⇒ Object
Returns the name of the module if required.
- #name ⇒ Object
- #parse_page_registration_block(config, &block) ⇒ Object protected
- #parse_registration_block(config, &block) ⇒ Object protected
-
#read_default_setting(name) ⇒ Object
Override from ActiveAdmin::Settings to inherit default attributes from the application.
-
#register(resource_class, options = {}, &block) ⇒ Object
Register a resource into this namespace.
-
#register_module ⇒ Object
protected
Creates a ruby module to namespace all the classes in if required.
- #register_page(name, options = {}, &block) ⇒ Object
-
#register_page_controller(config) ⇒ Object
protected
TODO: replace ‘eval` with `Class.new`.
-
#register_resource_controller(config) ⇒ Object
protected
TODO: replace ‘eval` with `Class.new`.
- #reset_menu! ⇒ Object
-
#resource_for(klass) ⇒ Object
Returns the first registered ActiveAdmin::Resource instance for a given class.
- #root? ⇒ Boolean
- #route_prefix ⇒ Object
-
#unload! ⇒ Object
Unload all the registered resources for this namespace.
- #unload_resources! ⇒ Object protected
Constructor Details
#initialize(application, name) ⇒ Namespace
Returns a new instance of Namespace.
32 33 34 35 36 37 38 |
# File 'lib/active_admin/namespace.rb', line 32 def initialize(application, name) @application = application @name = name.to_s.underscore @resources = ResourceCollection.new register_module unless root? end |
Instance Attribute Details
#application ⇒ Object (readonly)
Returns the value of attribute application.
30 31 32 |
# File 'lib/active_admin/namespace.rb', line 30 def application @application end |
#menus ⇒ Object (readonly)
Returns the value of attribute menus.
30 31 32 |
# File 'lib/active_admin/namespace.rb', line 30 def end |
#resources ⇒ Object (readonly)
Returns the value of attribute resources.
30 31 32 |
# File 'lib/active_admin/namespace.rb', line 30 def resources @resources end |
Instance Method Details
#add_current_user_to_menu(menu, priority = 10, html_options = {}) ⇒ Object
The default user session menu item
153 154 155 156 157 158 159 160 |
# File 'lib/active_admin/namespace.rb', line 153 def (, priority = 10, = {}) if current_user_method .add id: 'current_user', priority: priority, 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
137 138 139 140 141 142 143 144 145 |
# File 'lib/active_admin/namespace.rb', line 137 def (, priority = 20, = {}) if logout_link_path = .reverse_merge(method: logout_link_method || :get) .add id: 'logout', priority: priority, 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_nav ⇒ Object (protected)
Builds the default utility navigation in top right header with current user & logout button
177 178 179 180 181 182 183 |
# File 'lib/active_admin/namespace.rb', line 177 def build_default_utility_nav return if .exists? :utility_navigation . :utility_navigation do || 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
123 124 125 126 127 128 129 |
# File 'lib/active_admin/namespace.rb', line 123 def (name = DEFAULT_MENU) .before_build do || . name do || yield end end end |
#build_menu_collection ⇒ Object (protected)
164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/active_admin/namespace.rb', line 164 def = MenuCollection.new .on_build do build_default_utility_nav resources.each do |resource| resource.() end end end |
#build_page(name, options) ⇒ Object (protected)
190 191 192 |
# File 'lib/active_admin/namespace.rb', line 190 def build_page(name, ) resources.add Page.new(self, name, ) end |
#fetch_menu(name) ⇒ Object
109 110 111 |
# File 'lib/active_admin/namespace.rb', line 109 def (name) .fetch(name) end |
#find_or_build_resource(resource_class, options) ⇒ Object (protected)
Either returns an existing Resource instance or builds a new one.
186 187 188 |
# File 'lib/active_admin/namespace.rb', line 186 def find_or_build_resource(resource_class, ) resources.add Resource.new(self, resource_class, ) end |
#module_name ⇒ Object
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
84 85 86 |
# File 'lib/active_admin/namespace.rb', line 84 def module_name root? ? nil : @name.camelize end |
#name ⇒ Object
40 41 42 |
# File 'lib/active_admin/namespace.rb', line 40 def name @name.to_sym end |
#parse_page_registration_block(config, &block) ⇒ Object (protected)
233 234 235 |
# File 'lib/active_admin/namespace.rb', line 233 def parse_page_registration_block(config, &block) PageDSL.new(config).run_registration_block(&block) end |
#parse_registration_block(config, &block) ⇒ Object (protected)
228 229 230 231 |
# File 'lib/active_admin/namespace.rb', line 228 def parse_registration_block(config, &block) config.dsl = ResourceDSL.new(config) config.dsl.run_registration_block(&block) end |
#read_default_setting(name) ⇒ Object
Override from ActiveAdmin::Settings to inherit default attributes from the application
105 106 107 |
# File 'lib/active_admin/namespace.rb', line 105 def read_default_setting(name) application.public_send name 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.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/active_admin/namespace.rb', line 47 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_given? # Dispatch a registration event ActiveSupport::Notifications.publish ActiveAdmin::Resource::RegisterEvent, config # Return the config config end |
#register_module ⇒ Object (protected)
Creates a ruby module to namespace all the classes in if required
216 217 218 219 220 |
# File 'lib/active_admin/namespace.rb', line 216 def register_module unless Object.const_defined? module_name Object.const_set module_name, Module.new end end |
#register_page(name, options = {}, &block) ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/active_admin/namespace.rb', line 62 def register_page(name, = {}, &block) config = build_page(name, ) # Register the resource register_page_controller(config) parse_page_registration_block(config, &block) if block_given? config end |
#register_page_controller(config) ⇒ Object (protected)
TODO: replace ‘eval` with `Class.new`
195 196 197 198 |
# File 'lib/active_admin/namespace.rb', line 195 def register_page_controller(config) eval "class ::#{config.controller_name} < ActiveAdmin::PageController; end" config.controller.active_admin_config = config end |
#register_resource_controller(config) ⇒ Object (protected)
TODO: replace ‘eval` with `Class.new`
223 224 225 226 |
# File 'lib/active_admin/namespace.rb', line 223 def register_resource_controller(config) eval "class ::#{config.controller_name} < ActiveAdmin::ResourceController; end" config.controller.active_admin_config = config end |
#reset_menu! ⇒ Object
113 114 115 |
# File 'lib/active_admin/namespace.rb', line 113 def .clear! end |
#resource_for(klass) ⇒ Object
Returns the first registered ActiveAdmin::Resource instance for a given class
99 100 101 |
# File 'lib/active_admin/namespace.rb', line 99 def resource_for(klass) resources[klass] end |
#root? ⇒ Boolean
73 74 75 |
# File 'lib/active_admin/namespace.rb', line 73 def root? name == :root end |
#route_prefix ⇒ Object
88 89 90 |
# File 'lib/active_admin/namespace.rb', line 88 def route_prefix root? ? nil : @name end |
#unload! ⇒ Object
Unload all the registered resources for this namespace
93 94 95 96 |
# File 'lib/active_admin/namespace.rb', line 93 def unload! unload_resources! end |
#unload_resources! ⇒ Object (protected)
200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/active_admin/namespace.rb', line 200 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 |