Module: ActiveAdmin::Menu::MenuNode

Included in:
ActiveAdmin::Menu, ActiveAdmin::MenuItem
Defined in:
lib/active_admin/menu.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#childrenObject

Returns the value of attribute children


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

def children
  @children
end

Instance Method Details

#[](id) ⇒ Object


27
28
29
# File 'lib/active_admin/menu.rb', line 27

def [](id)
  @children[normalize_id(id)]
end

#[]=(id, child) ⇒ Object


31
32
33
# File 'lib/active_admin/menu.rb', line 31

def []=(id, child)
  @children[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)

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

def add(options)
  item = if parent = options.delete(:parent)
    (self[parent] || add(label: parent)).add options
  else
    _add options.merge parent: self
  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)

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

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

#include?(item) ⇒ Boolean

Whether any children match the given item.

Returns:

  • (Boolean)

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

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

#initializeObject


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

def initialize
  @children = {}
end

#items(context = nil) ⇒ Object

Returns sorted array of menu items that should be displayed in this context. Sorts by priority first, then alphabetically by label if needed.


73
74
75
76
77
78
79
# File 'lib/active_admin/menu.rb', line 73

def items(context = nil)
  @children.values.select{ |i| i.display?(context) }.sort do |a,b|
    result = a.priority       <=> b.priority
    result = a.label(context) <=> b.label(context) if result == 0
    result
  end
end