Skip to content
Browse files

Fixes #1986 - Menu should register child regardless of AA resource lo…

…ad order

* ActiveAdmin::MenuItem#dom_id removed, wasn't needed
* Load order is no longer important in registering menu items
  • Loading branch information...
1 parent 0f51868 commit 7b80902607a221474c66223195dfd66d39c85b0d @macfanatic committed Mar 13, 2013
View
14 features/menu.feature
@@ -37,3 +37,17 @@ Feature: Menu
Then I should see a menu item for "Custom Menu"
When I follow "Custom Menu"
Then I should be on the admin dashboard page
+
+ Scenario: Adding a resource as a sub menu item
+ Given a configuration of:
+ """
+ ActiveAdmin.register User
+ ActiveAdmin.register Post do
+ menu :parent => 'User'
+ end
+ """
+ When I am on the dashboard
+ Then I should see a menu item for "Users"
+ When I follow "Users"
+ Then the "Users" tab should be selected
+ And I should see a nested menu item for "Posts"
View
4 features/step_definitions/menu_steps.rb
@@ -5,3 +5,7 @@
Then /^I should not see a menu item for "([^"]*)"$/ do |name|
page.should_not have_css('#tabs li a', :text => name)
end
+
+Then /^I should see a nested menu item for "([^"]*)"$/ do |name|
+ page.should have_css('#tabs > li > ul > li > a', :text => name)
+end
View
26 lib/active_admin/menu.rb
@@ -61,7 +61,22 @@ def items
def add_without_parent(item_options)
menu_item = ActiveAdmin::MenuItem.new(item_options, self)
+
+ #
+ # Depending on load order, might have an "empty" parent item already
+ # in the menu, that is just a label. We want to allow you to customize that if
+ # you are registering a resource that comes later, such as "Users" parent and
+ # "Posts" child. The "Users" menu is registered after posts, but posts said it
+ # already was a sub menu of users.
+ #
+ # This will correct that by assigning existing children to this new menu item.
+ #
+ if children.has_key? menu_item.id
+ menu_item.send :children=, self[menu_item.id].send(:children)
+ end
+
children[menu_item.id] = menu_item
+
end
def add_with_parent(parent, item_options)
@@ -75,13 +90,18 @@ def children
@children ||= {}
end
+ def children=(kids)
+ @children = kids
+ end
+
def normalize_id(string)
- case string
+ raw_id = case string
when Proc
- string
+ string.call rescue string
else
- string.to_s.downcase.gsub(" ", "_")
+ string.to_s
end
+ raw_id.downcase.gsub( " ", '_' ).gsub( /[^a-z0-9_]/, '' )
end
end
View
9 lib/active_admin/menu_item.rb
@@ -53,15 +53,6 @@ def parent?
!parent.nil?
end
- def dom_id
- case id
- when Proc
- id
- else
- id.to_s.gsub( " ", '_' ).gsub( /[^a-z0-9_]/, '' )
- end
- end
-
# Returns an array of the ancestory of this menu item
# The first item is the immediate parent fo the item
def ancestors
View
6 lib/active_admin/views/tabbed_navigation.rb
@@ -40,11 +40,11 @@ def build_menu
end
def build_menu_item(item)
- dom_id = case item.dom_id
+ dom_id = case item.id
when Proc,Symbol
- normalize_id call_method_or_proc_on(self, item.dom_id)
+ normalize_id call_method_or_proc_on(self, item.id)
else
- item.dom_id
+ item.id
end
li :id => dom_id do |li_element|
View
14 spec/unit/menu_item_spec.rb
@@ -67,6 +67,10 @@ module ActiveAdmin
i
end
+ it "should contain 5 submenu items" do
+ item.items.count.should == 5
+ end
+
it "should give access to the menu item as an array" do
item['Blog'].label.should == 'Blog'
end
@@ -121,7 +125,7 @@ module ActiveAdmin
end # accessing ancestory
- describe ".generate_item_id" do
+ describe "#id" do
it "downcases the id" do
MenuItem.new(:id => "Identifier").id.should == "identifier"
@@ -131,8 +135,12 @@ module ActiveAdmin
MenuItem.new(:id => "An Id").id.should == "an_id"
end
- it "should return a proc if label was a proc" do
- MenuItem.new(:label => proc{ "Dynamic" }).id.should be_an_instance_of(Proc)
+ it "should render a proc if label was a proc" do
+ MenuItem.new(:label => proc{ "Dynamic Id" }).id.should == "dynamic_id"
+ end
+
+ it "should preserve the ID that was set if the label was a proc" do
+ MenuItem.new(:label => proc{ "Dynamic Id" }, :id => "static_id").id.should == "static_id"
end
end
View
10 spec/unit/menu_spec.rb
@@ -50,6 +50,16 @@
menu["Admin"]["Projects"].should be_an_instance_of(ActiveAdmin::MenuItem)
end
+
+ it "should assign children regardless of resource file load order" do
+ menu = Menu.new
+ menu.add :parent => "Users", :label => "Posts"
+ menu.add :label => "Users", :url => '/some/url'
+
+ menu["Users"].url.should == '/some/url'
+ menu["Users"]["Posts"].should be_an_instance_of(ActiveAdmin::MenuItem)
+ end
+
end
end

0 comments on commit 7b80902

Please sign in to comment.
Something went wrong with that request. Please try again.