Skip to content

Commit

Permalink
render_crumbs now accepts two additional options:
Browse files Browse the repository at this point in the history
options[:first_class], options[:last_class] which are html classes applied to first and last elements respectively.
  • Loading branch information
jasiek committed Dec 6, 2011
1 parent 2dad5a5 commit 354d7ed
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
28 changes: 19 additions & 9 deletions lib/crummy/standard_renderer.rb
Expand Up @@ -34,10 +34,13 @@ def render_crumbs(crumbs, options = {})
options[:separator] = "crumb" if options[:format] == :xml options[:separator] = "crumb" if options[:format] == :xml
end end
options[:links] = true if options[:links] == nil options[:links] = true if options[:links] == nil
options[:first_class] ||= ''
options[:last_class] ||= ''

case options[:format] case options[:format]
when :html when :html
crumb_string = crumbs.collect do |crumb| 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] end * options[:separator]
crumb_string crumb_string
when :html_list when :html_list
Expand All @@ -49,13 +52,13 @@ def render_crumbs(crumbs, options = {})
options[:ul_class] = "" if options[:ul_class] == nil options[:ul_class] = "" if options[:ul_class] == nil
options[:ul_id] = "" if options[:ul_id] == nil options[:ul_id] = "" if options[:ul_id] == nil
crumb_string = crumbs.collect do |crumb| 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] end * options[:separator]
crumb_string = "<ul class=\"#{options[:ul_class]}\" id=\"#{options[:ul_id]}\">" + crumb_string + "</ul>" crumb_string = "<ul class=\"#{options[:ul_class]}\" id=\"#{options[:ul_id]}\">" + crumb_string + "</ul>"
crumb_string crumb_string
when :xml when :xml
crumbs.collect do |crumb| 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 * '' end * ''
else else
raise ArgumentError, "Unknown breadcrumb output format" raise ArgumentError, "Unknown breadcrumb output format"
Expand All @@ -64,17 +67,24 @@ def render_crumbs(crumbs, options = {})


private 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 name, url = crumb
url && links ? link_to(name, url) : name url && links ? link_to(name, url, :class => html_classes) : name
end 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 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 end

def crumb_to_xml(crumb, links, separator) def crumb_to_xml(crumb, links, separator, is_first, is_last)
name, url = crumb name, url = crumb
url && links ? "<#{separator} href=\"#{url}\">#{name}</#{separator}>" : "<#{separator}>#{name}</#{separator}>" url && links ? "<#{separator} href=\"#{url}\">#{name}</#{separator}>" : "<#{separator}>#{name}</#{separator}>"
end end
Expand Down
29 changes: 29 additions & 0 deletions 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.