1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix inconsistent behavior from String#pluralize

Before:
  When calling String#pluralize with count=1 then it returned same
  string, but with count other than 1, returned new string.

After:
  String#pluralize always return a new string.

  => Prevent mutation of a string inadvertently.
This commit is contained in:
Kuldeep Aggarwal 2014-04-19 01:31:07 +05:30
parent 5f72fc6af8
commit 459f7bf38a
2 changed files with 6 additions and 1 deletions

View file

@ -31,7 +31,7 @@ class String
def pluralize(count = nil, locale = :en)
locale = count if count.is_a?(Symbol)
if count == 1
self
self.dup
else
ActiveSupport::Inflector.pluralize(self, locale)
end

View file

@ -58,6 +58,11 @@ class StringInflectionsTest < ActiveSupport::TestCase
assert_equal("blargles", "blargle".pluralize(2))
end
test 'pluralize with count = 1 still returns new string' do
name = "Kuldeep"
assert_not_same name.pluralize(1), name
end
def test_singularize
SingularToPlural.each do |singular, plural|
assert_equal(singular, plural.singularize)