Class: ActiveAdmin::Namespace

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



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.to_sym
  @resources = ResourceCollection.new
  register_module unless root?
  build_menu_collection
end

Instance Attribute Details

#applicationObject (readonly)

Returns the value of attribute application.



30
31
32
# File 'lib/active_admin/namespace.rb', line 30

def application
  @application
end

Returns the value of attribute menus.



30
31
32
# File 'lib/active_admin/namespace.rb', line 30

def menus
  @menus
end

#nameObject (readonly)

Returns the value of attribute name.



30
31
32
# File 'lib/active_admin/namespace.rb', line 30

def name
  @name
end

#resourcesObject (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_logout_button_to_menu(menu, priority = 100, html_options = {}) ⇒ Object

Add the default logout button to the menu, using the ActiveAdmin configuration settings

Parameters:

  • menu (ActiveAdmin::MenuItem)

    The menu to add the logout link to

  • priority (Fixnum) (defaults to: 100)

    Override the default priority of 100 to position the logout button where you want

  • html_options (Hash) (defaults to: {})

    An options hash to pass along to link_to



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/active_admin/namespace.rb', line 131

def add_logout_button_to_menu(menu, priority=100, html_options={})
  if logout_link_path
    logout_method = logout_link_method || :get
    menu.add :id           => 'logout',
             :priority     => priority,
             :label        => proc{ I18n.t('active_admin.logout') },
             :html_options => html_options.reverse_merge(:method => logout_method),
             :url          => proc{ render_or_call_method_or_proc_on self, active_admin_namespace.logout_link_path },
             :if           => proc{ current_active_admin_user? }
  end
end

#build_default_utility_navObject (protected)

Builds the default utility navigation in top right header with current user & logout button



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/active_admin/namespace.rb', line 158

def build_default_utility_nav
  return if @menus.exists? :utility_navigation
  @menus.menu :utility_navigation do |menu|
    menu.add  :label  => proc{ display_name current_active_admin_user },
              :url    => proc{ url_for [active_admin_namespace.name, current_active_admin_user] rescue '#' },
              :id     => 'current_user',
              :if     => proc{ current_active_admin_user? }

    add_logout_button_to_menu menu
  end
end

#build_menu(name = DEFAULT_MENU, &block) ⇒ Object

Add a callback to be ran when we build the menu

Parameters:

  • name (Symbol) (defaults to: DEFAULT_MENU)

    The name of the menu. Default: :default

  • block (Proc)

    The block to be ran when the menu is built



116
117
118
119
120
121
122
# File 'lib/active_admin/namespace.rb', line 116

def build_menu(name = DEFAULT_MENU, &block)
  @menus.before_build do |menus|
    menus.menu name do |menu|
      block.call(menu)
    end
  end
end

#build_menu_collectionObject (protected)



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

def build_menu_collection
  @menus = MenuCollection.new

  @menus.on_build do |menus|
    build_default_utility_nav

    resources.each do |resource|
      resource.add_to_menu(@menus)
    end
  end
end

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



176
177
178
# File 'lib/active_admin/namespace.rb', line 176

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

#fetch_menu(name) ⇒ Object



102
103
104
# File 'lib/active_admin/namespace.rb', line 102

def fetch_menu(name)
  @menus.fetch(name)
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



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

def find_or_build_resource(resource_class, options)
  resources.add Resource.new(self, resource_class, options)
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


80
81
82
83
# File 'lib/active_admin/namespace.rb', line 80

def module_name
  return nil if root?
  @module_name ||= name.to_s.camelize
end

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



216
217
218
# File 'lib/active_admin/namespace.rb', line 216

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

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



211
212
213
214
# File 'lib/active_admin/namespace.rb', line 211

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



98
99
100
# File 'lib/active_admin/namespace.rb', line 98

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.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/active_admin/namespace.rb', line 43

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!

  # 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



202
203
204
# File 'lib/active_admin/namespace.rb', line 202

def register_module
  eval "module ::#{module_name}; end"
end

#register_page(name, options = {}, &block) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/active_admin/namespace.rb', line 58

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)



180
181
182
183
# File 'lib/active_admin/namespace.rb', line 180

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)



206
207
208
209
# File 'lib/active_admin/namespace.rb', line 206

def register_resource_controller(config)
  eval "class ::#{config.controller_name} < ActiveAdmin::ResourceController; end"
  config.controller.active_admin_config = config
end

#reset_menu!Object



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

def reset_menu!
  @menus.clear!
end

#resource_for(klass) ⇒ Object

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



92
93
94
# File 'lib/active_admin/namespace.rb', line 92

def resource_for(klass)
  resources[klass]
end

#root?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/active_admin/namespace.rb', line 69

def root?
  name == :root
end

#unload!Object

Unload all the registered resources for this namespace



86
87
88
89
# File 'lib/active_admin/namespace.rb', line 86

def unload!
  unload_resources!
  reset_menu!
end

#unload_resources!Object (protected)



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/active_admin/namespace.rb', line 185

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