add taging ability to pluralize #5490

Closed
wants to merge 1 commit into
from
@@ -177,7 +177,8 @@ def excerpt(text, phrase, *args)
# Attempts to pluralize the +singular+ word unless +count+ is 1. If
# +plural+ is supplied, it will use that when count is > 1, otherwise
- # it will use the Inflector to determine the plural form
+ # it will use the Inflector to determine the plural form. It also
+ # supports tagging the count number.
#
# ==== Examples
# pluralize(1, 'person')
@@ -191,8 +192,18 @@ def excerpt(text, phrase, *args)
#
# pluralize(0, 'person')
# # => 0 people
+ #
+ # pluralize(1, 'person'){ |count, word| "#{content_tag(:em, count)} #{word} }
+ # # => <em>1</em> person
+ #
+ # pluralize(3, 'person', 'users'){ |count, word| "#{content_tag(:b, count)} #{content_tag(:span, word)}" }
+ # # => <b>3</b> <span>users</span>
+ #
+ # pluralize(3, 'person', 'users'){ |count, word| "#{content_tag(:b, count, :class => 'count')} #{content_tag(:span, word, :class => 'word')}" }
+ # # => <b class="count">3</b> <span class="word">users</span>
def pluralize(count, singular, plural = nil)
- "#{count || 0} " + ((count == 1 || count =~ /^1(\.0+)?$/) ? singular : (plural || singular.pluralize))
+ word = ((count == 1 || count =~ /^1(\.0+)?$/) ? singular : (plural || singular.pluralize))
+ block_given? ? yield(count, word) : "#{count || 0} #{word}"
end
# Wraps the +text+ into lines no longer than +line_width+ width. This method
@@ -256,6 +256,12 @@ def test_pluralization
assert_equal("10 buffaloes", pluralize(10, "buffalo"))
assert_equal("1 berry", pluralize(1, "berry"))
assert_equal("12 berries", pluralize(12, "berry"))
+ assert_equal("<b>1</b> count", pluralize(1, "count"){ |count, word| "#{content_tag(:b, count)} #{word}"} )
+ assert_equal("<span>2</span> counts", pluralize(2, "count"){ |count, word| "#{content_tag(:span, count)} #{word}" })
+ assert_equal("<p>1</p> <span>count</span>", pluralize(1, "count"){ |count, word| "#{content_tag(:p, count)} #{content_tag(:span, word)}" })
+ assert_equal("1 <span>count</span>", pluralize(1, "count"){ |count, word| "#{count} #{content_tag(:span, word)}" })
+ assert_equal("<b class=\"strong\">1</b> count", pluralize(1, "count"){ |count, word| "#{content_tag(:b, count, :class => 'strong')} #{word}"} )
+ assert_equal("<b class=\"count\">1</b> <span class=\"word\">count</span>", pluralize(1, "count"){ |count, word| "#{content_tag(:b, count, :class => 'count')} #{content_tag(:span, word, :class => 'word')}"} )
end
def test_cycle_class