Skip to content
Browse files

render_crumbs now accepts two additional options:

options[:first_class], options[:last_class] which are html classes applied to first and last elements respectively.
  • Loading branch information...
1 parent 2dad5a5 commit 354d7edaea44028be40d46aa4c6a181b8bf95488 @jasiek jasiek committed Dec 6, 2011
Showing with 48 additions and 9 deletions.
  1. +19 −9 lib/crummy/standard_renderer.rb
  2. +29 −0 test/standard_renderer_test.rb
View
28 lib/crummy/standard_renderer.rb
@@ -34,10 +34,13 @@ def render_crumbs(crumbs, options = {})
options[:separator] = "crumb" if options[:format] == :xml
end
options[:links] = true if options[:links] == nil
+ options[:first_class] ||= ''
+ options[:last_class] ||= ''
+
case options[:format]
when :html
crumb_string = crumbs.collect do |crumb|
- crumb_to_html crumb, options[:links]
+ crumb_to_html(crumb, options[:links], options[:first_class], options[:last_class], (crumb == crumbs.first), (crumb == crumbs.last))
end * options[:separator]
crumb_string
when :html_list
@@ -49,13 +52,13 @@ def render_crumbs(crumbs, options = {})
options[:ul_class] = "" if options[:ul_class] == nil
options[:ul_id] = "" if options[:ul_id] == nil
crumb_string = crumbs.collect do |crumb|
- crumb_to_html_list crumb, options[:links], options[:li_class], options[:active_li_class]
+ crumb_to_html_list(crumb, options[:links], options[:li_class], options[:active_li_class], options[:first_class], options[:last_class], (crumb == crumbs.first), (crumb == crumbs.last))
end * options[:separator]
crumb_string = "<ul class=\"#{options[:ul_class]}\" id=\"#{options[:ul_id]}\">" + crumb_string + "</ul>"
crumb_string
when :xml
crumbs.collect do |crumb|
- crumb_to_xml crumb, options[:links], options[:separator]
+ crumb_to_xml(crumb, options[:links], options[:separator], (crumb == crumbs.first), (crumb == crumbs.last))
end * ''
else
raise ArgumentError, "Unknown breadcrumb output format"
@@ -64,17 +67,24 @@ def render_crumbs(crumbs, options = {})
private
- def crumb_to_html(crumb, links)
+ def crumb_to_html(crumb, links, first_class, last_class, is_first, is_last)
+ html_classes = []
+ html_classes << first_class if is_first
+ html_classes << last_class if is_last
name, url = crumb
- url && links ? link_to(name, url) : name
+ url && links ? link_to(name, url, :class => html_classes) : name
end
- def crumb_to_html_list(crumb, links, li_class, active_li_class)
+ def crumb_to_html_list(crumb, links, li_class, active_li_class, first_class, last_class, is_first, is_last)
name, url = crumb
- url && links ? "<li class=\"#{li_class}\"><a href=\"#{url}\">#{name}</a></li>" : "<li class=\"#{active_li_class}\"><span>#{name}</span></li>"
+ html_classes = []
+ html_classes << first_class if is_first
+ html_classes << last_class if is_last
+ html_classes << active_li_class unless url && links
+ url && links ? "<li class=\"#{html_classes.join(' ').strip}\"><a href=\"#{url}\">#{name}</a></li>" : "<li class=\"#{html_classes.join(' ').strip}\"><span>#{name}</span></li>"
end
-
- def crumb_to_xml(crumb, links, separator)
+
+ def crumb_to_xml(crumb, links, separator, is_first, is_last)
name, url = crumb
url && links ? "<#{separator} href=\"#{url}\">#{name}</#{separator}>" : "<#{separator}>#{name}</#{separator}>"
end
View
29 test/standard_renderer_test.rb
@@ -0,0 +1,29 @@
+require 'rubygems'
+require 'bundler'
+Bundler.require(:test)
+require 'test/unit'
+
+require 'action_controller'
+require 'active_support/core_ext/string/output_safety'
+require 'crummy/standard_renderer'
+
+class StandardRendererTest < Test::Unit::TestCase
+ include Crummy
+
+ def test_classes
+ renderer = StandardRenderer.new
+ assert_equal('<a href="url" class="first last">name</a>',
+ renderer.render_crumbs([['name', 'url']], :first_class => 'first', :last_class => 'last', :format => :html))
+ assert_equal('<ul class="" id=""><li class="first last"><a href="url">name</a></li></ul>',
+ renderer.render_crumbs([['name', 'url']], :first_class => 'first', :last_class => 'last', :format => :html_list))
+ assert_equal('<crumb href="url">name</crumb>',
+ renderer.render_crumbs([['name', 'url']], :first_class => 'first', :last_class => 'last', :format => :xml))
+
+ assert_equal('<a href="url1" class="first">name1</a> &raquo; <a href="url2" class="last">name2</a>',
+ renderer.render_crumbs([['name1', 'url1'], ['name2', 'url2']], :first_class => 'first', :last_class => 'last', :format => :html))
+ assert_equal('<ul class="" id=""><li class="first"><a href="url1">name1</a></li><li class="last"><a href="url2">name2</a></li></ul>',
+ renderer.render_crumbs([['name1', 'url1'], ['name2', 'url2']], :first_class => 'first', :last_class => 'last', :format => :html_list))
+ assert_equal('<crumb href="url1">name1</crumb><crumb href="url2">name2</crumb>',
+ renderer.render_crumbs([['name1', 'url1'], ['name2', 'url2']], :first_class => 'first', :last_class => 'last', :format => :xml))
+ end
+end

0 comments on commit 354d7ed

Please sign in to comment.
Something went wrong with that request. Please try again.