Class: ActiveAdmin::Namespace
- Inherits:
-
Object
- Object
- ActiveAdmin::Namespace
- Includes:
- Settings
- 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.
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
- #build_page(name, options) ⇒ Object protected
-
#dashboard_controller_name ⇒ Object
Returns the name of the dashboard controller for this namespace.
-
#find_or_build_resource(resource_class, options) ⇒ Object
protected
Either returns an existing Resource instance or builds a new one for the resource and options.
-
#generate_dashboard_controller ⇒ Object
protected
Creates a dashboard controller for this config.
-
#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.
- #page_dsl ⇒ Object protected
- #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_dashboard ⇒ Object
protected
Adds the dashboard to the menu.
-
#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
- #register_resource_controller(config) ⇒ Object protected
-
#register_with_menu(config) ⇒ Object
protected
Does all the work of registernig a config with the menu system.
- #resource_dsl ⇒ Object protected
-
#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.
- #unload_dashboard! ⇒ Object protected
- #unload_menu! ⇒ Object protected
- #unload_resources! ⇒ Object protected
Constructor Details
#initialize(application, name) ⇒ Namespace
Returns a new instance of Namespace.
37 38 39 40 41 42 43 44 |
# File 'lib/active_admin/namespace.rb', line 37 def initialize(application, name) @application = application @name = name.to_s.underscore.to_sym @resources = ResourceCollection.new @menu = Menu.new register_module unless root? generate_dashboard_controller end |
Instance Attribute Details
#application ⇒ Object (readonly)
Returns the value of attribute application.
35 36 37 |
# File 'lib/active_admin/namespace.rb', line 35 def application @application end |
#menu ⇒ Object (readonly)
Returns the value of attribute menu.
35 36 37 |
# File 'lib/active_admin/namespace.rb', line 35 def @menu end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
35 36 37 |
# File 'lib/active_admin/namespace.rb', line 35 def name @name end |
#resources ⇒ Object (readonly)
Returns the value of attribute resources.
35 36 37 |
# File 'lib/active_admin/namespace.rb', line 35 def resources @resources end |
Instance Method Details
#build_page(name, options) ⇒ Object (protected)
134 135 136 |
# File 'lib/active_admin/namespace.rb', line 134 def build_page(name, ) resources.add Page.new(self, name, ) end |
#dashboard_controller_name ⇒ Object
Returns the name of the dashboard controller for this namespace
95 96 97 |
# File 'lib/active_admin/namespace.rb', line 95 def dashboard_controller_name [module_name, "DashboardController"].compact.join("::") end |
#find_or_build_resource(resource_class, options) ⇒ Object (protected)
Either returns an existing Resource instance or builds a new one for the resource and options
130 131 132 |
# File 'lib/active_admin/namespace.rb', line 130 def find_or_build_resource(resource_class, ) resources.add Resource.new(self, resource_class, ) end |
#generate_dashboard_controller ⇒ Object (protected)
Creates a dashboard controller for this config
190 191 192 |
# File 'lib/active_admin/namespace.rb', line 190 def generate_dashboard_controller eval "class ::#{dashboard_controller_name} < ActiveAdmin::Dashboards::DashboardController; end" 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.
108 109 110 111 112 113 |
# File 'lib/active_admin/namespace.rb', line 108 def register_dashboard resources.each do |resource| (resource) if resource. end end |
#module_name ⇒ Object
89 90 91 92 |
# File 'lib/active_admin/namespace.rb', line 89 def module_name return nil if root? @module_name ||= name.to_s.camelize end |
#page_dsl ⇒ Object (protected)
181 182 183 |
# File 'lib/active_admin/namespace.rb', line 181 def page_dsl @page_dsl ||= PageDSL.new end |
#parse_page_registration_block(config, &block) ⇒ Object (protected)
185 186 187 |
# File 'lib/active_admin/namespace.rb', line 185 def parse_page_registration_block(config, &block) page_dsl.run_registration_block(config, &block) end |
#parse_registration_block(config, &block) ⇒ Object (protected)
177 178 179 |
# File 'lib/active_admin/namespace.rb', line 177 def parse_registration_block(config, &block) resource_dsl.run_registration_block(config, &block) end |
#read_default_setting(name) ⇒ Object
Override from ActiveAdmin::Settings to inherit default attributes from the application
122 123 124 |
# File 'lib/active_admin/namespace.rb', line 122 def read_default_setting(name) application.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.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/active_admin/namespace.rb', line 49 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? (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 |
#register_dashboard ⇒ Object (protected)
Adds the dashboard to the menu
195 196 197 198 |
# File 'lib/active_admin/namespace.rb', line 195 def register_dashboard dashboard_path = root? ? :dashboard_path : "#{name}_dashboard_path".to_sym .add(I18n.t("active_admin.dashboard"), dashboard_path, 1) unless [I18n.t("active_admin.dashboard")] end |
#register_module ⇒ Object (protected)
Creates a ruby module to namespace all the classes in if required
164 165 166 |
# File 'lib/active_admin/namespace.rb', line 164 def register_module eval "module ::#{module_name}; end" end |
#register_page(name, options = {}, &block) ⇒ Object
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/active_admin/namespace.rb', line 67 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) if config. config end |
#register_page_controller(config) ⇒ Object (protected)
139 140 141 142 |
# File 'lib/active_admin/namespace.rb', line 139 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)
168 169 170 171 |
# File 'lib/active_admin/namespace.rb', line 168 def register_resource_controller(config) eval "class ::#{config.controller_name} < ActiveAdmin::ResourceController; end" config.controller.active_admin_config = config end |
#register_with_menu(config) ⇒ Object (protected)
Does all the work of registernig a config with the menu system
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/active_admin/namespace.rb', line 201 def (config) # The menu we're going to add this resource to add_to = # Adding as a child if config. # Create the parent if it doesn't exist .add(config., '#') unless [config.] add_to = [config.] end # Check if this menu item has already been created if add_to[config.] # Update the url if it's already been created add_to[config.].url = config.route_collection_path else add_to.add(config., config.route_collection_path, config., { :if => config. }) end end |
#resource_dsl ⇒ Object (protected)
173 174 175 |
# File 'lib/active_admin/namespace.rb', line 173 def resource_dsl @resource_dsl ||= ResourceDSL.new end |
#resource_for(klass) ⇒ Object
Returns the first registered ActiveAdmin::Resource instance for a given class
116 117 118 |
# File 'lib/active_admin/namespace.rb', line 116 def resource_for(klass) resources.find_by_resource_class(klass) end |
#root? ⇒ Boolean
78 79 80 |
# File 'lib/active_admin/namespace.rb', line 78 def root? name == :root end |
#unload! ⇒ Object
Unload all the registered resources for this namespace
100 101 102 103 104 |
# File 'lib/active_admin/namespace.rb', line 100 def unload! unload_resources! unload_dashboard! end |
#unload_dashboard! ⇒ Object (protected)
154 155 156 157 |
# File 'lib/active_admin/namespace.rb', line 154 def unload_dashboard! # TODO: Only clear out my sections Dashboards.clear_all_sections! end |
#unload_menu! ⇒ Object (protected)
159 160 161 |
# File 'lib/active_admin/namespace.rb', line 159 def @menu = Menu.new end |
#unload_resources! ⇒ Object (protected)
144 145 146 147 148 149 150 151 152 |
# File 'lib/active_admin/namespace.rb', line 144 def unload_resources! resources.each do |resource| parent = (module_name || 'Object').constantize const_name = resource.controller_name.split('::').last # Remove the const if its been defined parent.send(:remove_const, const_name) if parent.const_defined?(const_name) end @resources = ResourceCollection.new end |