diff options
author | José Valim <jose.valim@gmail.com> | 2011-10-17 07:02:39 -0700 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2011-10-17 07:02:39 -0700 |
commit | 59d6df5c695bba671e5e670520f8c5afa5ad193e (patch) | |
tree | 641e67bb986d6d813249b0f2fda7b35a693398e8 /activesupport/lib | |
parent | 8dffc62a9b957c91575f7c014f50806e86d64505 (diff) | |
parent | 4535191c61b496b1a5e9dc57624581753fa71497 (diff) | |
download | rails-59d6df5c695bba671e5e670520f8c5afa5ad193e.tar.gz rails-59d6df5c695bba671e5e670520f8c5afa5ad193e.tar.bz2 rails-59d6df5c695bba671e5e670520f8c5afa5ad193e.zip |
Merge pull request #3151 from zenprogrammer/pluralize_without_count
Added include_count parameter to pluralize.
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/core_ext/string/inflections.rb | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/activesupport/lib/active_support/core_ext/string/inflections.rb b/activesupport/lib/active_support/core_ext/string/inflections.rb index c7ceeb9de4..fd91b3cacb 100644 --- a/activesupport/lib/active_support/core_ext/string/inflections.rb +++ b/activesupport/lib/active_support/core_ext/string/inflections.rb @@ -9,14 +9,25 @@ require 'active_support/inflector/transliterate' class String # Returns the plural form of the word in the string. # + # If the optional parameter +count+ is specified, + # the singular form will be returned if <tt>count == 1</tt>. + # For any other value of +count+ the plural will be returned. + # + # ==== Examples # "post".pluralize # => "posts" # "octopus".pluralize # => "octopi" # "sheep".pluralize # => "sheep" # "words".pluralize # => "words" # "the blue mailman".pluralize # => "the blue mailmen" # "CamelOctopus".pluralize # => "CamelOctopi" - def pluralize - ActiveSupport::Inflector.pluralize(self) + # "apple".pluralize(1) # => "apple" + # "apple".pluralize(2) # => "apples" + def pluralize(count = nil) + if count == 1 + self + else + ActiveSupport::Inflector.pluralize(self) + end end # The reverse of +pluralize+, returns the singular form of a word in a string. @@ -42,7 +53,7 @@ class String def constantize ActiveSupport::Inflector.constantize(self) end - + # +safe_constantize+ tries to find a declared constant with the name specified # in the string. It returns nil when the name is not in CamelCase # or is not initialized. See ActiveSupport::Inflector.safe_constantize |