From 85ab1cde56f69e94b0d600ca6922b3af66f1389f Mon Sep 17 00:00:00 2001 From: Scott Windsor Date: Thu, 15 Apr 2010 14:07:20 -0700 Subject: [PATCH] Adding the ability to pass a block to pluralize. --- actionpack/lib/action_view/helpers/text_helper.rb | 17 ++++++++++++++--- 1 files changed, 14 insertions(+), 3 deletions(-) diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 9279a88..78aaa72 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -166,7 +166,8 @@ module ActionView # 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. If a block + # is given, the count and word are yielded. # # ==== Examples # pluralize(1, 'person') @@ -180,8 +181,18 @@ module ActionView # # pluralize(0, 'person') # # => 0 people - def pluralize(count, singular, plural = nil) - "#{count || 0} " + ((count == 1 || count =~ /^1(\.0+)?$/) ? singular : (plural || singular.pluralize)) + # + # pluralize(2, 'person') do |count, word| + # "*#{count}* #{word}" + # end + # # => *2* people + def pluralize(count, singular, plural = nil, &block) + word = (count == 1 || count == '1') ? singular : (plural || singular.pluralize) + if block_given? + concat(capture((count || 0), word, &block)) + else + "#{count || 0} #{word}" + end end # Wraps the +text+ into lines no longer than +line_width+ width. This method -- 1.6.3.3