Class: ActiveAdmin::Menu

Inherits:
Object
  • Object
show all
Defined in:
lib/active_admin/menu.rb

Overview

Each Namespace builds up it’s own menu as the global navigation

To build a new menu:

menu = Menu.new do |m|
  m.add label: 'Dashboard', url: '/'
  m.add label: 'Users',     url: '/users'
end

If you’re interested in configuring a menu item, take a look at the options available in ‘ActiveAdmin::MenuItem`

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil) {|_self| ... } ⇒ Menu

Returns a new instance of Menu.

Yields:

  • (_self)

Yield Parameters:



17
18
19
20
21
# File 'lib/active_admin/menu.rb', line 17

def initialize(parent = nil)
  @parent_item = parent
  @children = {}
  yield(self) if block_given?
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



76
77
78
# File 'lib/active_admin/menu.rb', line 76

def children
  @children
end

#parent_itemObject (readonly)

Returns the value of attribute parent_item.



23
24
25
# File 'lib/active_admin/menu.rb', line 23

def parent_item
  @parent_item
end

Class Method Details

.normalize_id(id) ⇒ Object (protected)



89
90
91
92
93
94
95
96
97
98
# File 'lib/active_admin/menu.rb', line 89

def self.normalize_id(id)
  case id
  when String, Symbol, ActiveModel::Name
    id.to_s.downcase.tr ' ', '_'
  when ActiveAdmin::Resource::Name
    id.param_key
  else
    raise TypeError, "#{id.class} isn't supported as a Menu ID"
  end
end

Instance Method Details

#[](id) ⇒ Object



25
26
27
# File 'lib/active_admin/menu.rb', line 25

def [](id)
  @children[self.class.normalize_id(id)]
end

#[]=(id, child) ⇒ Object



29
30
31
# File 'lib/active_admin/menu.rb', line 29

def []=(id, child)
  @children[self.class.normalize_id(id)] = child
end

#add(options) {|item| ... } ⇒ Object

Recursively builds any given menu items. There are two syntaxes supported, as shown in the below examples. Both create an identical menu structure.

Example 1:

menu = Menu.new
menu.add label: 'Dashboard' do |dash|
  dash.add label: 'My Child Dashboard'
end

Example 2:

menu = Menu.new
menu.add label:  'Dashboard'
menu.add parent: 'Dashboard', label: 'My Child Dashboard'

Yields:

  • (item)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/active_admin/menu.rb', line 47

def add(options, &block)
  parent_id = options.delete(:parent)

  if parent_id
    parent_item = find_or_add_parent_item(parent_id)
    item = parent_item.add(options)
  else
    item = new_item(options)
  end

  yield(item) if block_given?

  item
end

#current?(item) ⇒ Boolean

Used in the UI to visually distinguish which menu item is selected.

Returns:

  • (Boolean)


68
69
70
# File 'lib/active_admin/menu.rb', line 68

def current?(item)
  self == item || include?(item)
end

#find_or_add_parent_item(parent_id) ⇒ Object (protected)

fetch parent or add if not present



101
102
103
# File 'lib/active_admin/menu.rb', line 101

def find_or_add_parent_item(parent_id)
  self[parent_id] || add(label: parent_id)
end

#include?(item) ⇒ Boolean

Whether any children match the given item.

Returns:

  • (Boolean)


63
64
65
# File 'lib/active_admin/menu.rb', line 63

def include?(item)
  @children.values.include? item
end

#itemsObject



72
73
74
# File 'lib/active_admin/menu.rb', line 72

def items
  @children.values
end

#new_item(options) ⇒ Object (protected)

The method that actually adds new menu items. Called by the public method. If this ID is already taken, transfer the children of the existing item to the new item.



82
83
84
85
86
87
# File 'lib/active_admin/menu.rb', line 82

def new_item(options)
  ActiveAdmin::MenuItem.new(options.merge parent: parent_item).tap do |item|
    item.submenu.children = children[item.id].children if children[item.id]
    children[item.id] = item
  end
end