forked from activeadmin/activeadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.rb
66 lines (54 loc) · 1.39 KB
/
menu.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
module ActiveAdmin
# Each Namespace builds up it's own menu as the global navigation
#
# To build a new menu:
#
# menu = Menu.new do |m|
# m.add MenuItem.new(:label => "Dashboard", :url => "/")
# m.add MenuItem.new(:label => "Admin", :url => "/admin")
# end
#
# If you're interested in configuring a menu item, take a look at the
# options available in `ActiveAdmin::MenuItem`
#
class Menu
attr_accessor :children
def initialize
@children = Menu::ItemCollection.new
yield(self) if block_given?
end
# Add a new MenuItem to the menu
#
# Example:
# menu = Menu.new
# dash = MenuItem.new :label => "Dashboard"
# menu.add dash
#
# Accepts as many menu items as you wish to add:
#
# menu = Menu.new
# dash = MenuItem.new :label => "Dashboard"
# admin = MenuItem.new :label => "Admin"
# menu.add dash, admin
#
# @param [MenuItem] menu_items Add as many menu items as you pass in
def add(*menu_items)
menu_items.each do |menu_item|
menu_item.parent = nil
@children << menu_item
end
end
def [](id)
@children.find_by_id(id)
end
def items
@children.sort
end
class ItemCollection < Array
def find_by_id(id)
id = MenuItem.generate_item_id(id)
find{ |i| i.id == id }
end
end
end
end