lib/active_admin/menu.rb in activeadmin-0.4.4 vs lib/active_admin/menu.rb in activeadmin-0.5.0.pre
- old
+ new
@@ -1,42 +1,67 @@
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
- @items = []
+ @children = Menu::ItemCollection.new
+
yield(self) if block_given?
- end
-
- def add(*args, &block)
- @items << MenuItem.new(*args, &block)
end
-
- def [](name)
- items.find{ |i| i.name == name }
+
+ # 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
+
+ # @return Sorted [Array] of [MenuItem]
def items
- @items.sort
+ @children.sort
end
-
- def find_by_url(url)
- recursive_find_by_url(items, url)
- end
-
- private
-
- def recursive_find_by_url(collection, url)
- found = nil
- collection.each do |item|
- if item.url == url
- found = item
- break
- else
- found = recursive_find_by_url(item.children, url)
- break if found
- end
+
+ class ItemCollection < Array
+
+ def find_by_id(id)
+ id = MenuItem.generate_item_id(id)
+ find{ |i| i.id == id }
end
- found
+
end
-
+
end
+
end