Class: ActiveAdmin::Menu
- Inherits:
-
Object
- Object
- ActiveAdmin::Menu
- 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.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
-
#children ⇒ Object
Returns the value of attribute children.
-
#parent_item ⇒ Object
readonly
Returns the value of attribute parent_item.
Class Method Summary collapse
- .normalize_id(id) ⇒ Object protected
Instance Method Summary collapse
- #[](id) ⇒ Object
- #[]=(id, child) ⇒ Object
-
#add(options) {|item| ... } ⇒ Object
Recursively builds any given menu items.
-
#current?(item) ⇒ Boolean
Used in the UI to visually distinguish which menu item is selected.
-
#find_or_add_parent_item(parent_id) ⇒ Object
protected
fetch parent or add if not present.
-
#include?(item) ⇒ Boolean
Whether any children match the given item.
-
#initialize(parent = nil) {|_self| ... } ⇒ Menu
constructor
A new instance of Menu.
- #items ⇒ Object
-
#new_item(options) ⇒ Object
protected
The method that actually adds new menu items.
Constructor Details
#initialize(parent = nil) {|_self| ... } ⇒ Menu
Returns a new instance of Menu.
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
#children ⇒ Object
Returns the value of attribute children.
76 77 78 |
# File 'lib/active_admin/menu.rb', line 76 def children @children end |
#parent_item ⇒ Object (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.new
.add label: 'Dashboard' do |dash|
dash.add label: 'My Child Dashboard'
end
Example 2:
= Menu.new
.add label: 'Dashboard'
.add parent: 'Dashboard', label: 'My Child Dashboard'
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/active_admin/menu.rb', line 47 def add(, &block) parent_id = .delete(:parent) if parent_id parent_item = find_or_add_parent_item(parent_id) item = parent_item.add() else item = new_item() end yield(item) if block_given? item end |
#current?(item) ⇒ Boolean
Used in the UI to visually distinguish which menu item is selected.
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.
63 64 65 |
# File 'lib/active_admin/menu.rb', line 63 def include?(item) @children.values.include? item end |
#items ⇒ Object
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() ActiveAdmin::MenuItem.new(.merge parent: parent_item).tap do |item| item..children = children[item.id].children if children[item.id] children[item.id] = item end end |