From 3f9d1459f27f74708448639c849cdafea3211f36 Mon Sep 17 00:00:00 2001 From: David Genord II Date: Fri, 11 Jun 2010 11:02:38 -0400 Subject: [PATCH 1/3] Better handle pluralizing uncountable words --- .../lib/active_support/inflector/inflections.rb | 10 ++++++---- 1 files changed, 6 insertions(+), 4 deletions(-) diff --git a/activesupport/lib/active_support/inflector/inflections.rb b/activesupport/lib/active_support/inflector/inflections.rb index 3caf78b..f9e2609 100644 --- a/activesupport/lib/active_support/inflector/inflections.rb +++ b/activesupport/lib/active_support/inflector/inflections.rb @@ -129,12 +129,14 @@ module ActiveSupport def pluralize(word) result = word.to_s.dup - if word.empty? || inflections.uncountables.include?(result.downcase) - result - else + if m = (/(.*_)([a-z]+)$/i.match(result) || /^([A-Za-z]+)([A-Z][a-z]+)$/.match(result)) + result = m[2] + end + + unless word.empty? || inflections.uncountables.include?(result.downcase) inflections.plurals.each { |(rule, replacement)| break if result.gsub!(rule, replacement) } - result end + m ? (m[1] + result) : result end # The reverse of +pluralize+, returns the singular form of a word in a string. -- 1.6.6.1 From e3b0cb5b199a9a7f1e67c9ce2ac7223df7c26917 Mon Sep 17 00:00:00 2001 From: David Genord II Date: Fri, 11 Jun 2010 15:41:27 -0400 Subject: [PATCH 2/3] Tests for uncountable behavior --- activesupport/test/inflector_test.rb | 7 +++++++ activesupport/test/inflector_test_cases.rb | 12 ++++++++++++ 2 files changed, 19 insertions(+), 0 deletions(-) diff --git a/activesupport/test/inflector_test.rb b/activesupport/test/inflector_test.rb index 2990177..09dc50f 100644 --- a/activesupport/test/inflector_test.rb +++ b/activesupport/test/inflector_test.rb @@ -36,6 +36,13 @@ class InflectorTest < Test::Unit::TestCase end end + Uncountable.each do |word| + define_method "test_uncountable_#{word}" do + assert_equal(word, ActiveSupport::Inflector.singularize(word)) + assert_equal(word, ActiveSupport::Inflector.pluralize(word)) + end + end + def test_overwrite_previous_inflectors assert_equal("series", ActiveSupport::Inflector.singularize("series")) ActiveSupport::Inflector.inflections.singular "series", "serie" diff --git a/activesupport/test/inflector_test_cases.rb b/activesupport/test/inflector_test_cases.rb index 59515da..7b19e15 100644 --- a/activesupport/test/inflector_test_cases.rb +++ b/activesupport/test/inflector_test_cases.rb @@ -105,6 +105,18 @@ module InflectorTestCases "database" => "databases" } + Uncountable = %w( + equipment StandardEquipment standard_equipment + information TechnicalInformation technical_information + rice WhiteRice white_rice + money EasyMoney easy_money + species OriginOfSpecies origin_of_species + series LinearSeries linear_series + fish BigFish big_fish + sheep BlackSheep black_sheep + jeans BlueJeans blue_jeans + ) + CamelToUnderscore = { "Product" => "product", "SpecialGuest" => "special_guest", -- 1.6.6.1 From 83a35f00be89b219ccc67a3d5c91e267b9c3523b Mon Sep 17 00:00:00 2001 From: Paul Barry Date: Fri, 11 Jun 2010 20:23:34 -0400 Subject: [PATCH 3/3] [#4836] Refactored implementation of pluralize to make intent clear and reuse existing methods for finding last word in a string --- .../lib/active_support/inflector/inflections.rb | 14 ++++++-------- 1 files changed, 6 insertions(+), 8 deletions(-) diff --git a/activesupport/lib/active_support/inflector/inflections.rb b/activesupport/lib/active_support/inflector/inflections.rb index f9e2609..e2f42dd 100644 --- a/activesupport/lib/active_support/inflector/inflections.rb +++ b/activesupport/lib/active_support/inflector/inflections.rb @@ -128,15 +128,13 @@ module ActiveSupport # "CamelOctopus".pluralize # => "CamelOctopi" def pluralize(word) result = word.to_s.dup - - if m = (/(.*_)([a-z]+)$/i.match(result) || /^([A-Za-z]+)([A-Z][a-z]+)$/.match(result)) - result = m[2] - end - - unless word.empty? || inflections.uncountables.include?(result.downcase) - inflections.plurals.each { |(rule, replacement)| break if result.gsub!(rule, replacement) } + unless word.empty? + words = result.underscore.split('_') + unless inflections.uncountables.include?(words.last.downcase) + inflections.plurals.each { |(rule, replacement)| break if result.gsub!(rule, replacement) } + end end - m ? (m[1] + result) : result + result end # The reverse of +pluralize+, returns the singular form of a word in a string. -- 1.6.6.1