Class: ActiveAdmin::Namespace

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(application, name) ⇒ Namespace

Returns a new instance of Namespace.



38
39
40
41
42
43
44
# File 'lib/active_admin/namespace.rb', line 38

def initialize(application, name)
  @application = application
  @name = name.to_s.underscore.to_sym
  @resources = ResourceCollection.new
  register_module unless root?
  generate_dashboard_controller
end

Instance Attribute Details

#applicationObject (readonly)

Returns the value of attribute application.



36
37
38
# File 'lib/active_admin/namespace.rb', line 36

def application
  @application
end

Returns the value of attribute menu.



36
37
38
# File 'lib/active_admin/namespace.rb', line 36

def menu
  @menu
end

#nameObject (readonly)

Returns the value of attribute name.



36
37
38
# File 'lib/active_admin/namespace.rb', line 36

def name
  @name
end

#resourcesObject (readonly)

Returns the value of attribute resources.



36
37
38
# File 'lib/active_admin/namespace.rb', line 36

def resources
  @resources
end

Instance Method Details

#build_page(name, options) ⇒ Object (protected)



133
134
135
# File 'lib/active_admin/namespace.rb', line 133

def build_page(name, options)
  resources.add Page.new(self, name, options)
end

#dashboard_controller_nameObject

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



129
130
131
# File 'lib/active_admin/namespace.rb', line 129

def find_or_build_resource(resource_class, options)
  resources.add Resource.new(self, resource_class, options)
end

#generate_dashboard_controllerObject (protected)

Creates a dashboard controller for this config



183
184
185
186
187
188
189
# File 'lib/active_admin/namespace.rb', line 183

def generate_dashboard_controller
  return unless ActiveAdmin::Dashboards.built?

  eval "class ::#{dashboard_controller_name} < ActiveAdmin::PageController
          include ActiveAdmin::Dashboards::DashboardController
        end"
end

#module_nameObject

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


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

#parse_page_registration_block(config, &block) ⇒ Object (protected)



178
179
180
# File 'lib/active_admin/namespace.rb', line 178

def parse_page_registration_block(config, &block)
  PageDSL.new(config).run_registration_block(&block)
end

#parse_registration_block(config, &block) ⇒ Object (protected)



173
174
175
176
# File 'lib/active_admin/namespace.rb', line 173

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



113
114
115
# File 'lib/active_admin/namespace.rb', line 113

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, 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!

  # 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_moduleObject (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, 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

#register_page_controller(config) ⇒ Object (protected)



137
138
139
140
# File 'lib/active_admin/namespace.rb', line 137

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

#reset_menu!Object



121
122
123
# File 'lib/active_admin/namespace.rb', line 121

def reset_menu!
  @menu = nil
end

#resource_for(klass) ⇒ Object

Returns the first registered ActiveAdmin::Resource instance for a given class



107
108
109
# File 'lib/active_admin/namespace.rb', line 107

def resource_for(klass)
  resources.find_by_resource_class(klass)
end

#root?Boolean

Returns:

  • (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!
  reset_menu!
end

#unload_dashboard!Object (protected)



158
159
160
161
# File 'lib/active_admin/namespace.rb', line 158

def unload_dashboard!
  # TODO: Only clear out my sections
  Dashboards.clear_all_sections!
end

#unload_resources!Object (protected)



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/active_admin/namespace.rb', line 142

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)

    # 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