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.



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

def children
  @children
end

Instance Method Details

#[](id) ⇒ Object



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

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

#[]=(id, child) ⇒ Object



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

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)


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

def add(options)
  options = options.dup # Make sure parameter is not modified
  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)


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

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

#include?(item) ⇒ Boolean

Whether any children match the given item.

Returns:

  • (Boolean)


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

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

#initializeObject



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

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.



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

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