Permalink
Browse files

Change ActionView::Helpers::TextHelper#pluralize to return the singul…

…ar form of a word if the count is 0 and the last argument to true.
  • Loading branch information...
1 parent 4ae3da6 commit f3f3ba99e0a494ca7e5e8f5ea025b8896fbb0c4b @GRoguelon committed Jan 11, 2014
Showing with 43 additions and 2 deletions.
  1. +20 −2 actionview/lib/action_view/helpers/text_helper.rb
  2. +23 −0 actionview/test/template/text_helper_test.rb
@@ -195,8 +195,17 @@ def excerpt(text, phrase, options = {})
#
# pluralize(0, 'person')
# # => 0 people
- def pluralize(count, singular, plural = nil)
- word = if (count == 1 || count =~ /^1(\.0+)?$/)
+ #
+ # If you specify +zero_is_singular+ to true and if +count+ is zero, the
+ # method returns the singualar form.
+ #
+ # pluralize(0, 'person', nil, true)
+ # # => 0 person
+ #
+ # pluralize(0, 'person', 'users', true)
+ # # => 0 person
+ def pluralize(count, singular, plural = nil, zero_is_singular = false)
+ word = if singular?(count, zero_is_singular)
singular
else
plural || singular.pluralize
@@ -445,6 +454,15 @@ def cut_excerpt_part(part_position, part, separator, options)
return affix, part.join(separator)
end
+
+ def singular?(count, zero_is_singular)
+ case (count ||= 0)
+ when Integer then count == 1 || zero_is_singular && count == 0
+ when String then count =~ /^#{zero_is_singular ? '0|1' : '1'}(\.0+)?$/
+ else
+ false
+ end
+ end
end
end
end
@@ -359,6 +359,29 @@ 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("0 counts", pluralize(0, "count"))
+ assert_equal("0.0 counts", pluralize('0.0', "count"))
+ assert_equal("0.00 counts", pluralize('0.00', "count"))
+ end
+
+ def test_pluralization_with_zero_singular
+ assert_equal("1 count", pluralize(1, "count", nil, true))
+ assert_equal("2 counts", pluralize(2, "count", nil, true))
+ assert_equal("1 count", pluralize('1', "count", nil, true))
+ assert_equal("2 counts", pluralize('2', "count", nil, true))
+ assert_equal("1,066 counts", pluralize('1,066', "count", nil, true))
+ assert_equal("1.25 counts", pluralize('1.25', "count", nil, true))
+ assert_equal("1.0 count", pluralize('1.0', "count", nil, true))
+ assert_equal("1.00 count", pluralize('1.00', "count", nil, true))
+ assert_equal("2 counters", pluralize(2, "count", "counters", true))
+ assert_equal("0 count", pluralize(nil, "count", "counters", true))
+ assert_equal("2 people", pluralize(2, "person", nil, true))
+ assert_equal("10 buffaloes", pluralize(10, "buffalo", nil, true))
+ assert_equal("1 berry", pluralize(1, "berry", nil, true))
+ assert_equal("12 berries", pluralize(12, "berry", nil, true))
+ assert_equal("0 count", pluralize(0, "count", nil, true))
+ assert_equal("0.0 count", pluralize('0.0', "count", nil, true))
+ assert_equal("0.00 count", pluralize('0.00', "count", nil, true))
end
def test_cycle_class

0 comments on commit f3f3ba9

Please sign in to comment.