Skip to content

Commit

Permalink
Can customize the current item class for each menu
Browse files Browse the repository at this point in the history
  • Loading branch information
fellix committed Mar 14, 2012
1 parent 1760134 commit 6db077c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -19,3 +19,4 @@ rdoc
pkg

## PROJECT::SPECIFIC
.rvmrc
16 changes: 9 additions & 7 deletions 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
Expand All @@ -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
Expand All @@ -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

Expand Down
26 changes: 26 additions & 0 deletions test/helper_test.rb
Expand Up @@ -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]
Expand All @@ -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]

Expand Down Expand Up @@ -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.