Skip to content
Browse files

Moved the resolving of menu items into the view.

Doing this ensures that the full Rails router has been loaded including
any scoping and RackBaseURI configurations.

Fixes #101, #960
  • Loading branch information...
1 parent b27a8e4 commit 3e7ac40a128747a8c2716c4239b6c06e2b7acfdb @gregbell gregbell committed Feb 3, 2012
View
4 CHANGELOG.md
@@ -4,6 +4,10 @@
* Columns component now supports column spans, max and min widths ([@gregbell][])
+### Bug Fixes
+
+* #101: Global nav now works with RackBaseURI ([@gregbell][])
+* #960: Global nav works when scoped in rails routes ([@gregbell][])
## 0.4.0
View
21 lib/active_admin/menu_item.rb
@@ -1,16 +1,6 @@
module ActiveAdmin
class MenuItem
-
- # Generates a route using the rails application url helpers
- #
- # @param [Symbol] named_route
- #
- # @returns [String] The generated route
- def self.generate_url(named_route)
- Rails.application.routes.url_helpers.send(named_route)
- end
-
attr_accessor :name, :url, :priority, :parent, :display_if_block
def initialize(name, url, priority = 10, options = {})
@@ -41,16 +31,6 @@ def dom_id
name.downcase.gsub( " ", '_' ).gsub( /[^a-z0-9_]/, '' )
end
- def url
- case @url
- when Symbol
- generated = self.class.generate_url(@url) # Call the named route
- else
- generated = @url
- end
- @cached_url[@url] ||= generated
- end
-
# Returns an array of the ancestory of this menu item
# The first item is the immediate parent fo the item
def ancestors
@@ -76,6 +56,5 @@ def display_if_block
@display_if_block || lambda { |_| true }
end
-
end
end
View
16 lib/active_admin/views/tabbed_navigation.rb
@@ -42,17 +42,29 @@ def build_menu
def build_menu_item(item)
li :id => item.dom_id do |li_element|
li_element.add_class "current" if current?(item)
+ link_path = url_for_menu_item(item)
if item.children.any?
li_element.add_class "has_nested"
- text_node link_to(item.name, item.url || "#")
+ text_node link_to(item.name, link_path)
render_nested_menu(item)
else
- link_to item.name, item.url
+ link_to item.name, link_path
end
end
end
+ def url_for_menu_item(menu_item)
+ case menu_item.url
+ when Symbol
+ send(menu_item.url)
+ when nil
+ "#"
+ else
+ menu_item.url
+ end
+ end
+
def render_nested_menu(item)
ul do
displayable_items(item.children).each do |child|
View
14 spec/unit/menu_item_spec.rb
@@ -35,20 +35,6 @@ module ActiveAdmin
item.display_if_block.call(self).should == true
end
- describe "url generation and caching" do
- it "should generate a url if it is a symbol" do
- item = MenuItem.new("Posts", :admin_posts_path)
- MenuItem.should_receive(:generate_url).with(:admin_posts_path).
- and_return("/admin/posts")
-
- item.url.should == "/admin/posts"
- end
-
- it "should generate a url if it is a string" do
- MenuItem.new("Posts", "/admin/posts").url.should == "/admin/posts"
- end
- end
-
context "with no children" do
it "should be empty" do
item = MenuItem.new("Blog", "/admin/blog")
View
2 spec/unit/namespace/register_resource_spec.rb
@@ -22,7 +22,7 @@
it "should create a menu item" do
namespace.load_menu!
namespace.menu["Categories"].should be_an_instance_of(ActiveAdmin::MenuItem)
- namespace.menu["Categories"].url.should == "/admin/categories"
+ namespace.menu["Categories"].url.should == :admin_categories_path
end
end # context "with no configuration"
View
4 spec/unit/views/tabbed_navigation_spec.rb
@@ -16,7 +16,7 @@
describe "rendering a menu" do
before do
- menu.add "Blog Posts", "/admin/blog-posts"
+ menu.add "Blog Posts", :admin_posts_path
menu.add "Reports", "/admin/reports"
reports = menu["Reports"]
reports.add "A Sub Reports", "/admin/a-sub-reports"
@@ -39,7 +39,7 @@
end
it "should generate a link for each item" do
- html.should have_tag("a", "Blog Posts", :attributes => { :href => '/admin/blog-posts' })
+ html.should have_tag("a", "Blog Posts", :attributes => { :href => '/admin/posts' })
end
it "should generate a nested list for children" do

0 comments on commit 3e7ac40

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