module ActiveAdmin::Menu::MenuNode

Attributes

children[RW]

Public Class Methods

new() click to toggle source
# File lib/active_admin/menu.rb, line 23
def initialize
  @children = {}
end

Public Instance Methods

[](id) click to toggle source
# File lib/active_admin/menu.rb, line 27
def [](id)
  @children[normalize_id(id)]
end
[]=(id, child) click to toggle source
# File lib/active_admin/menu.rb, line 30
def []=(id, child)
  @children[normalize_id(id)] = child
end
add(options) { |item| ... } click to toggle source

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'
# File lib/active_admin/menu.rb, line 48
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) click to toggle source

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

# File lib/active_admin/menu.rb, line 66
def current?(item)
  self == item || include?(item)
end
include?(item) click to toggle source

Whether any children match the given item.

# File lib/active_admin/menu.rb, line 61
def include?(item)
  @children.values.include? item
end
items(context = nil) click to toggle source

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

# File lib/active_admin/menu.rb, line 72
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

Private Instance Methods

_add(options) click to toggle source

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.

# File lib/active_admin/menu.rb, line 86
def _add(options)
  item = ActiveAdmin::MenuItem.new(options)
  item.send :children=, self[item.id].children if self[item.id]
  self[item.id] = item
end
normalize_id(id) click to toggle source
# File lib/active_admin/menu.rb, line 92
def normalize_id(id)
  case id
  when String, Symbol, ActiveModel::Name
    id.to_s.downcase.gsub ' ', '_'
  when ActiveAdmin::Resource::Name
    id.param_key
  else
    raise TypeError, "#{id.class} isn't supported as a Menu ID"
  end
end