Skip to content

Commit

Permalink
Return safe html when String#html_safe is available.
Browse files Browse the repository at this point in the history
  • Loading branch information
fnando committed Aug 26, 2010
1 parent 15884a5 commit 2a147d8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
13 changes: 13 additions & 0 deletions README.rdoc
Expand Up @@ -52,6 +52,19 @@ You can set your own separator:
You are here: <%= breadcrumbs.render(:format => :inline, :separator => '|') %>
</p>

You can also define your own formatter. Just create a class that implements a +render+ instance
method and you're good to go.

class Breadcrumbs::Render::Dl
def render
# return breadcrumbs wrapped in a <DL> tag
end
end

To use your new format, just provide the <tt>:format</tt> option.

breadcrumbs.render(:format => :dl)

=== I18n

Breadcrumbs is integrated with I18n. You can set translations like:
Expand Down
10 changes: 6 additions & 4 deletions lib/breadcrumbs.rb
Expand Up @@ -25,6 +25,8 @@ def add(text, url = nil, options = {})
items << [text.to_s, url, options]
end

alias :<< :add

# Render breadcrumbs using the specified format.
# Use HTML lists by default, but can be plain links.
#
Expand All @@ -36,7 +38,7 @@ def add(text, url = nil, options = {})
# breadcrumbs.render(:id => "breadcrumbs")
# breadcrumbs.render(:class => "breadcrumbs")
#
# You can define your own formatter. Just create a class that implements a +render+ instance
# You can also define your own formatter. Just create a class that implements a +render+ instance
# method and you're good to go.
#
# class Breadcrumbs::Render::Dl
Expand All @@ -54,10 +56,10 @@ def render(options = {})

klass_name = options[:format].to_s.classify
klass = Breadcrumbs::Render.const_get(klass_name)
klass.new(self, options).render
end
html = klass.new(self, options).render

alias :<< :add
html.respond_to?(:html_safe) ? html.html_safe : html
end

def translate(scope) # :nodoc:
text = I18n.t(scope, :scope => :breadcrumbs, :raise => true) rescue nil
Expand Down
7 changes: 7 additions & 0 deletions test/breadcrumbs_test.rb
Expand Up @@ -6,6 +6,13 @@ def setup
@inline = Breadcrumbs::Render::Inline.new(@breadcrumbs)
end

def test_return_safe_html
html_mock = mock
html_mock.expects(:html_safe).once
Breadcrumbs::Render::List.any_instance.stubs(:render).returns(html_mock)
@breadcrumbs.render(:format => :list)
end

def test_add_item
@breadcrumbs.add "Home"
assert_equal 1, @breadcrumbs.items.count
Expand Down
2 changes: 2 additions & 0 deletions test/test_helper.rb
@@ -1,8 +1,10 @@
require "rubygems"
gem "test-unit"
require "test/unit"
require "cgi"
require "nokogiri"
require "action_controller"
require "mocha"

require "breadcrumbs"

Expand Down

0 comments on commit 2a147d8

Please sign in to comment.