Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Make pluralize follow singularize's logic of uncountability check #3536

Merged
merged 1 commit into from

3 participants

@pksunkara

I have added a test case which fails prior to the patch. All the tests pass including the new one now.

activesupport/lib/active_support/inflector/methods.rb
@@ -23,7 +23,7 @@ module ActiveSupport
def pluralize(word)
result = word.to_s.dup
- if word.empty? || inflections.uncountables.include?(result.downcase)
+ if word.empty? || inflections.uncountables.any? { |inflection| result =~ /\b#{inflection}\Z/i }
@josevalim Owner

Thanks for the pull request. Shouldn't we move this logic to a private method and have both pluralize and singularize calling it?

That would be DRYer. To me the two methods look almost exactly the same, except for the word.empty? part, so maybe we could extract most of the method into a private method and redefine pluralize to this:

def pluralize(word)
  somethingize(word, inflections.plurals)
  # Alternatively:
  somethingize(word, :plurals) # (and just do inflections.send(:plurals) in somethingize)
end
@josevalim Owner

Agreed with somethingize(word, inflections.plurals). The word.empty? check could exist in both methods, it is just a very simple optimization.

We would need a better name than somethingize though. Any suggestions?

@josevalim Owner

inflectionize? :P apply_inflections?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@henrikhodne henrikhodne referenced this pull request from a commit in henrikhodne/rails
@henrikhodne henrikhodne Refactored pluralize and singularize into a common method.
See diff discussion on rails/#3536.
52fa34f
@henrikhodne

I added the change in pluralize to my commit in #3537, but the test change should maybe still be added?

@josevalim
Owner

Yes, could you please rebase that pull request or send a new one with the test only? Thanks a lot!

@pksunkara

Damn! I wanted to have a commit in rails. It would inspire me to contribute more.

Test case coming through.

@josevalim
Owner

@pkumar you still can! I am waiting on the test :)

@pksunkara

Done. Please merge.

@josevalim josevalim merged commit 6950639 into rails:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 6, 2011
  1. @pksunkara
This page is out of date. Refresh to see the latest.
Showing with 1 addition and 0 deletions.
  1. +1 −0  activesupport/test/inflector_test_cases.rb
View
1  activesupport/test/inflector_test_cases.rb
@@ -14,6 +14,7 @@ module InflectorTestCases
"fish" => "fish",
"jeans" => "jeans",
"funky jeans" => "funky jeans",
+ "my money" => "my money",
"category" => "categories",
"query" => "queries",
Something went wrong with that request. Please try again.