From 48722acd229744c23bc413cee4045a4ce82d4706 Mon Sep 17 00:00:00 2001 From: Mathias Meyer Date: Tue, 9 Jun 2009 19:27:44 +0200 Subject: [PATCH] Added support for an unless condition. --- lib/breadcrumb.rb | 9 +++++++-- spec/breadcrumbs_helper_spec.rb | 26 ++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/lib/breadcrumb.rb b/lib/breadcrumb.rb index e9160d2..11b1214 100644 --- a/lib/breadcrumb.rb +++ b/lib/breadcrumb.rb @@ -4,8 +4,13 @@ class Breadcrumb include Singleton Trail = Struct.new(:controller, :action, :trail, :options) do def condition_met?(obj) - condition = options[:if] - condition.nil? or obj.send(condition) + if options[:if] + obj.send(options[:if]) + elsif options[:unless] + !obj.send(options[:unless]) + else + true + end end end diff --git a/spec/breadcrumbs_helper_spec.rb b/spec/breadcrumbs_helper_spec.rb index e5709be..fa1acb2 100644 --- a/spec/breadcrumbs_helper_spec.rb +++ b/spec/breadcrumbs_helper_spec.rb @@ -176,7 +176,7 @@ def its_true! crumbs.should == %Q{Search Results ()} end - it "should not consider a trail when it has a condition and it's not met" do + it "should not consider a trail when it has an :if condition and it's not met" do Breadcrumb.configure do crumb :search_results, 'Search Results', :current trail :search, [:create, :new], [:search_results], :if => :is_it_false? @@ -187,7 +187,7 @@ def its_true! crumbs.should == "" end - it "should consider a trail when it has a condition and it's met" do + it "should consider a trail when it has an :if condition and it's met" do Breadcrumb.configure do crumb :search_results, 'Search Results', :current trail :search, [:create, :new], [:search_results], :if => :its_true! @@ -197,6 +197,28 @@ def its_true! params[:action] = 'new' crumbs.should == %Q{Search Results} end + + it "should not consider a trail when it has an :unless condition and it's not met" do + Breadcrumb.configure do + crumb :search_results, 'Search Results', :current + trail :search, [:create, :new], [:search_results], :unless => :its_true! + end + + params[:controller] = 'search' + params[:action] = 'new' + crumbs.should == "" + end + + it "should consider a trail when it has an :unless condition and it's met" do + Breadcrumb.configure do + crumb :search_results, 'Search Results', :current + trail :search, [:create, :new], [:search_results], :unless => :is_it_false? + end + + params[:controller] = 'search' + params[:action] = 'new' + crumbs.should == %Q{Search Results} + end describe "when fetching parameters" do it "should support nested parameter attributes" do