Navigation Menu

Skip to content

Commit

Permalink
allow :highlights_on option to be a Proc as well as a Regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
mjtko committed Jul 10, 2011
1 parent 51c70ea commit 12fc857
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
16 changes: 11 additions & 5 deletions lib/simple_navigation/core/item.rb
Expand Up @@ -45,7 +45,7 @@ def name(options = {})
# * its url matches the url of the current request (auto highlighting)
#
def selected?
@selected = @selected || selected_by_config? || selected_by_subnav? || selected_by_url?
@selected = @selected || selected_by_config? || selected_by_subnav? || selected_by_condition?
end

# Returns the html-options hash for the item, i.e. the options specified for this item in the config-file.
Expand Down Expand Up @@ -76,10 +76,16 @@ def selected_by_config?
end

# Returns true if the item's url matches the request's current url.
def selected_by_url?
def selected_by_condition?
if highlights_on
raise ArgumentError, ':highlights_on must be a regexp' unless highlights_on.instance_of?(Regexp)
SimpleNavigation.request_uri =~ highlights_on
case highlights_on
when Regexp
SimpleNavigation.request_uri =~ highlights_on
when Proc
highlights_on.call
else
raise ArgumentError, ':highlights_on must be a Regexp or Proc'
end
elsif auto_highlight?
!!(root_path_match? || SimpleNavigation.current_page?(url_without_anchor))
else
Expand Down Expand Up @@ -112,4 +118,4 @@ def url_without_anchor
end

end
end
end
34 changes: 17 additions & 17 deletions spec/lib/simple_navigation/core/item_spec.rb
Expand Up @@ -138,7 +138,7 @@
it {@item.should be_selected}
it "should not evaluate the subnav or urls" do
@item.should_not_receive(:selected_by_subnav?)
@item.should_not_receive(:selected_by_url?)
@item.should_not_receive(:selected_by_condition?)
@item.selected?
end
end
Expand All @@ -156,15 +156,15 @@
before(:each) do
@item.stub!(:selected_by_subnav? => false)
end
context 'selected by url' do
context 'selected by condition' do
before(:each) do
@item.stub!(:selected_by_url? => true)
@item.stub!(:selected_by_condition? => true)
end
it {@item.should be_selected}
end
context 'not selected by url' do
context 'not selected by condition' do
before(:each) do
@item.stub!(:selected_by_url? => false)
@item.stub!(:selected_by_condition? => false)
end
it {@item.should_not be_selected}
end
Expand Down Expand Up @@ -300,33 +300,33 @@
end
end

describe 'selected_by_url?' do
describe 'selected_by_condition?' do
context ':highlights_on option is set' do
before(:each) do
@item.stub!(:highlights_on => /^\/current/)
SimpleNavigation.stub!(:request_uri => '/current_url')
end
it "should not check for autohighlighting" do
@item.should_not_receive(:auto_highlight?)
@item.send(:selected_by_url?)
@item.send(:selected_by_condition?)
end
context ':highlights_on is a regexp' do
context 'regexp matches current_url' do
it {@item.send(:selected_by_url?).should be_true}
it {@item.send(:selected_by_condition?).should be_true}
end
context 'regexp does not match current_url' do
before(:each) do
@item.stub!(:highlights_on => /^\/no_match/)
end
it {@item.send(:selected_by_url?).should be_false}
it {@item.send(:selected_by_condition?).should be_false}
end
end
context ':highlights_on is not a regexp' do
before(:each) do
@item.stub!(:highlights_on => "not a regexp")
end
it "should raise an error" do
lambda {@item.send(:selected_by_url?).should raise_error(ArgumentError)}
lambda {@item.send(:selected_by_condition?).should raise_error(ArgumentError)}
end
end
end
Expand All @@ -336,7 +336,7 @@
end
it "should check for autohighlighting" do
@item.should_receive(:auto_highlight?)
@item.send(:selected_by_url?)
@item.send(:selected_by_condition?)
end
end
context 'auto_highlight is turned on' do
Expand All @@ -347,7 +347,7 @@
before(:each) do
@item.stub!(:root_path_match? => true)
end
it {@item.send(:selected_by_url?).should be_true}
it {@item.send(:selected_by_condition?).should be_true}
end
context 'root path does not match' do
before(:each) do
Expand All @@ -359,28 +359,28 @@
end
it "should test with the item's url" do
@adapter.should_receive(:current_page?).with('url')
@item.send(:selected_by_url?)
@item.send(:selected_by_condition?)
end
it "should remove anchors before testing the item's url" do
@item.stub!(:url => 'url#anchor')
@adapter.should_receive(:current_page?).with('url')
@item.send(:selected_by_url?)
@item.send(:selected_by_condition?)
end
it {@item.send(:selected_by_url?).should be_true}
it {@item.send(:selected_by_condition?).should be_true}
end
context 'no match' do
before(:each) do
@adapter.stub!(:current_page? => false)
end
it {@item.send(:selected_by_url?).should be_false}
it {@item.send(:selected_by_condition?).should be_false}
end
end
end
context 'auto_highlight is turned off' do
before(:each) do
@item.stub!(:auto_highlight? => false)
end
it {@item.send(:selected_by_url?).should be_false}
it {@item.send(:selected_by_condition?).should be_false}
end
end

Expand Down

0 comments on commit 12fc857

Please sign in to comment.