Skip to content

Commit

Permalink
Merge pull request #32 from mpartel/no_links_if_path_is_nil
Browse files Browse the repository at this point in the history
Don't make links if path = nil
  • Loading branch information
weppos committed Dec 24, 2012
2 parents 71c6eda + d3349cf commit d8eb950
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/breadcrumbs_on_rails/action_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module ActionController

protected

def add_breadcrumb(name, path, options = {})
def add_breadcrumb(name, path = nil, options = {})
self.breadcrumbs << Breadcrumbs::Element.new(name, path, options)
end

Expand Down Expand Up @@ -60,7 +60,7 @@ def self.convert_to_set_of_strings(value, keys)

module ClassMethods

def add_breadcrumb(name, path, filter_options = {})
def add_breadcrumb(name, path = nil, filter_options = {})
# This isn't really nice here
if eval = Utils.convert_to_set_of_strings(filter_options.delete(:eval), %w(name path))
name = Utils.instance_proc(name) if eval.include?("name")
Expand Down
8 changes: 6 additions & 2 deletions lib/breadcrumbs_on_rails/breadcrumbs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ def render
end

def render_element(element)
content = @context.link_to_unless_current(compute_name(element), compute_path(element), element.options)
if element.path == nil
content = compute_name(element)
else
content = @context.link_to_unless_current(compute_name(element), compute_path(element), element.options)
end
if @options[:tag]
@context.content_tag(@options[:tag], content)
else
Expand Down Expand Up @@ -116,7 +120,7 @@ class Element
# @param [Hash] options The element/link URL.
# @return [Element]
#
def initialize(name, path, options = {})
def initialize(name, path = nil, options = {})
self.name = name
self.path = path
self.options = options
Expand Down
10 changes: 7 additions & 3 deletions test/unit/element_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

class ElementTest < ActiveSupport::TestCase

def test_initialize_should_require_name_and_path
def test_initialize_should_require_name
assert_raise(ArgumentError) { BreadcrumbsOnRails::Breadcrumbs::Element.new }
assert_raise(ArgumentError) { BreadcrumbsOnRails::Breadcrumbs::Element.new(nil) }
assert_nothing_raised { BreadcrumbsOnRails::Breadcrumbs::Element.new(nil, nil) }
assert_nothing_raised { BreadcrumbsOnRails::Breadcrumbs::Element.new(nil) }
end

def test_initialize_should_set_name
Expand Down Expand Up @@ -41,5 +40,10 @@ def test_options
element.options = { :title => "Go to the Homepage" }
assert_equal({ :title => "Go to the Homepage" }, element.options)
end

def test_path_is_optional
element = BreadcrumbsOnRails::Breadcrumbs::Element.new(:homepage)
assert_nil element.path
end

end
8 changes: 8 additions & 0 deletions test/unit/simple_builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ def test_render_with_current_page
simplebuilder(@template, generate_elements(2)).render)
end

def test_render_with_no_links
elements = (1..2).collect do |index|
BreadcrumbsOnRails::Breadcrumbs::Element.new("Element #{index}", nil)
end
assert_dom_equal("Element 1 &raquo; Element 2",
simplebuilder(@template, elements).render)
end


protected

Expand Down

0 comments on commit d8eb950

Please sign in to comment.