Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force active #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/semantic_menu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class MenuItem
include ActionView::Helpers::TagHelper,
ActionView::Helpers::UrlHelper

attr_accessor :children, :link
attr_accessor :children, :link, :active
cattr_accessor :controller

def initialize(title, link, level, link_opts={})
Expand Down Expand Up @@ -34,7 +34,7 @@ def child_output
end

def active?
children.any?(&:active?) || on_current_page?
self.active || children.any?(&:active?) || on_current_page?
end

def on_current_page?
Expand Down
12 changes: 12 additions & 0 deletions test/semantic_menu_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ def test_menu_item_passes_options_to_link
MenuItem.new("title", "link", 2, :class => 'button').to_s
end

def test_force_active_state
MenuItem.any_instance.stubs(:on_current_page?).returns(false)

menu = SemanticMenu.new nil, :class => 'mymenu' do |root|
root.add 'title', 'link' do |links|
links.active = true
end
end

assert_equal '<ul class="mymenu"><li class="active"><a href="link">title</a></li></ul>', menu.to_s
end

def test_menu_item_with_one_child
MenuItem.any_instance.stubs(:active?).returns(false)
assert_equal '<ul class="mymenu"><li><a href="link">title</a></li></ul>', default_menu.to_s
Expand Down