Permalink
Browse files

inflection regexp are meant to be applied once

  • Loading branch information...
1 parent dc03a2f commit 5cff94fd851f23265b525fe9ce5489c0b28c771c @fxn fxn committed Feb 13, 2012
Showing with 2 additions and 2 deletions.
  1. +2 −2 activesupport/lib/active_support/inflector/methods.rb
View
4 activesupport/lib/active_support/inflector/methods.rb
@@ -92,7 +92,7 @@ def underscore(camel_cased_word)
# "author_id" # => "Author"
def humanize(lower_case_and_underscored_word)
result = lower_case_and_underscored_word.to_s.dup
- inflections.humans.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
+ inflections.humans.each { |(rule, replacement)| break if result.sub!(rule, replacement) }
result.gsub!(/_id$/, "")
result.gsub!(/_/, ' ')
result.gsub(/([a-z\d]*)/i) { |match|
@@ -311,7 +311,7 @@ def apply_inflections(word, rules)
if word.empty? || inflections.uncountables.include?(result.downcase[/\b\w+\Z/])
result
else
- rules.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
@apux
apux Jul 22, 2013

Any chances to revert this change?
In some languages, like Spanish, the pluralization/singularization must be applied more than once.
"transaccion_importante".pluralize # => "transacciones_importantes"

thanks

@steveklabnik
steveklabnik Jul 22, 2013

it's been released in 4.0.0, so we probably can't straight-up revert it. Please file an actual issue, and we can talk.

@fxn
fxn Jul 22, 2013

I don't think that pluralization should work, pluralize is not assumed to be callable on multi-word strings.

@apux
apux Jul 22, 2013

@fxn
It worked in Rails 3. Regex could be a little elaborated, but with gsub I got the spected string, which is not possible to do with sub.

@fxn
fxn Jul 22, 2013

I don't see it working in 3.2, but in the end that doesn't really matter. What the method says is "if you pass me a word, I'll return its pluralization". What is the method going to do if you pass an unsupported input value? It doesn't matter, is irrelevant, and you (generic you) should not even care.

What did that pluralize return if passed "red_book"? If it returned "reds_books", should it be called a bug because adjectives in English work different than in Spanish? Nope, whatever the method returns on unsupported input does not matter.

@fxn
fxn Jul 22, 2013

If needed, I think you could define a custom helper:

class String
  def multi_word_pluralize
    gsub(/[^\W_]+/) { $&.pluralize }
  end
end

or something in that line. It has to be your own extension.

@apux
apux Jul 23, 2013

In order to get it working in Rails 3, the regexp should be written as /([aeiou])([A-Z]|_|$)/ (just an example, there are other rules to add).
I don't think it is a bug, just gsub is more flexible because it allows to adapt the behaviour just changing the regexp.
The multi_word_pluralize looks good, but rails won't see at it when generating its resources. For example, rails generate scaffold libro_rojo will use the plural of that string in many places as libro_rojos instead of libros_rojos.

+ rules.each { |(rule, replacement)| break if result.sub!(rule, replacement) }
result
end
end

0 comments on commit 5cff94f

Please sign in to comment.