Skip to content
Browse files

Can customize the current item class for each menu

  • Loading branch information...
1 parent 1760134 commit 6db077c5e1bc817dc85c22d236b2aa88e9f72179 @fellix fellix committed Mar 13, 2012
Showing with 36 additions and 7 deletions.
  1. +1 −0 .gitignore
  2. +9 −7 lib/menu_builder/helper.rb
  3. +26 −0 test/helper_test.rb
View
1 .gitignore
@@ -19,3 +19,4 @@ rdoc
pkg
## PROJECT::SPECIFIC
+.rvmrc
View
16 lib/menu_builder/helper.rb
@@ -1,7 +1,8 @@
module MenuBuilder
module ViewHelpers
def menu(options={}, &block)
- content_tag :ul, Menu.new(self, &block).render, options
+ current_item_class = options.delete :current_item_class
+ content_tag :ul, Menu.new(self, current_item_class, &block).render, options
end
private
@@ -23,11 +24,12 @@ def to_sym
end
class Menu
- def initialize(context, &block)
- @context = context
- @menu_items = context.instance_variable_get('@menu_items')
- @actual_items = []
- @block = block
+ def initialize(context, current_item_class="current", &block)
+ @context = context
+ @current_item_class = current_item_class || "current"
+ @menu_items = context.instance_variable_get('@menu_items')
+ @actual_items = []
+ @block = block
end
def method_missing item, *args, &block
@@ -46,7 +48,7 @@ def render_one item
def html_options_for item
css_classes = []
- css_classes << "current" if included_in_current_items? item
+ css_classes << "#{@current_item_class}" if included_in_current_items? item
css_classes << "first" if first? item
css_classes << "last" if last? item
View
26 test/helper_test.rb
@@ -40,6 +40,17 @@ class HelperTest < ActionView::TestCase
assert_select "li.current", 1
end
+
+ test "customize the current item class" do
+ @menu_items = [:home]
+ concat(menu({:current_item_class => "active"}) { |m|
+ concat m.home "Home", "/"
+ concat m.contact "Store", "/store"
+ })
+
+ assert_select "li.current", 0
+ assert_select "li.active", 1
+ end
test "accept more than one menu item" do
@menu_items = [:settings, :notifications]
@@ -52,7 +63,21 @@ class HelperTest < ActionView::TestCase
assert_select "li.current", 2
end
+
+ test "accept more than one menu item for a custom current item class" do
+ @menu_items = [:settings, :notifications]
+ concat(menu({:current_item_class => "active"}) { |m|
+ concat m.home "Home", "/"
+ concat m.notifications "Notifications", "/notifications"
+ concat m.settings "Settings", "/settings"
+ })
+
+ assert_select "li.current", 0
+ assert_select "li.active", 2
+ end
+
+
test "accept more than one menu" do
@menu_items = [:settings, :notifications]
@@ -95,4 +120,5 @@ class HelperTest < ActionView::TestCase
assert_select "ul > li > a > img"
end
+
end

0 comments on commit 6db077c

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