From ccbb48196efe06a0c1c360951caff74ee74a8d14 Mon Sep 17 00:00:00 2001 From: Matthew Draper Date: Sat, 6 Sep 2014 04:56:25 +0930 Subject: Fix for inflector's incorrect camelCase replacement for acronyms Fixes #8015, #9756. [Fred Wu & Matthew Draper] --- activesupport/CHANGELOG.md | 7 +++++++ activesupport/lib/active_support/inflector/methods.rb | 2 +- activesupport/test/inflector_test.rb | 1 + activesupport/test/inflector_test_cases.rb | 1 + 4 files changed, 10 insertions(+), 1 deletion(-) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index cae5ac6e17..f995082a15 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,10 @@ +* Fixed a bug in Inflector#underscore where acroynms in nested constant names + are incorrectly parsed as camelCase. + + Fixes #8015. + + *Fred Wu*, *Matthew Draper* + * Make Time#change throw an exception if the :usec option is out of range and the time has an offset other than UTC or local. diff --git a/activesupport/lib/active_support/inflector/methods.rb b/activesupport/lib/active_support/inflector/methods.rb index 53022de549..f35e71ce81 100644 --- a/activesupport/lib/active_support/inflector/methods.rb +++ b/activesupport/lib/active_support/inflector/methods.rb @@ -91,7 +91,7 @@ module ActiveSupport def underscore(camel_cased_word) return camel_cased_word unless camel_cased_word =~ /[A-Z-]|::/ word = camel_cased_word.to_s.gsub('::', '/') - word.gsub!(/(?:([A-Za-z\d])|^)(#{inflections.acronym_regex})(?=\b|[^a-z])/) { "#{$1}#{$1 && '_'}#{$2.downcase}" } + word.gsub!(/(?:([A-Za-z\d])|\b)(#{inflections.acronym_regex})(?=\b|[^a-z])/) { "#{$1}#{$1 && '_'}#{$2.downcase}" } word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2') word.gsub!(/([a-z\d])([A-Z])/,'\1_\2') word.tr!("-", "_") diff --git a/activesupport/test/inflector_test.rb b/activesupport/test/inflector_test.rb index 58fdea0972..b37f31bc5f 100644 --- a/activesupport/test/inflector_test.rb +++ b/activesupport/test/inflector_test.rb @@ -120,6 +120,7 @@ class InflectorTest < ActiveSupport::TestCase ["SSLError", "ssl_error", "SSL error", "SSL Error"], ["RESTful", "restful", "RESTful", "RESTful"], ["RESTfulController", "restful_controller", "RESTful controller", "RESTful Controller"], + ["Nested::RESTful", "nested/restful", "Nested/RESTful", "Nested/RESTful"], ["IHeartW3C", "i_heart_w3c", "I heart W3C", "I Heart W3C"], ["PhDRequired", "phd_required", "PhD required", "PhD Required"], ["IRoRU", "i_ror_u", "I RoR u", "I RoR U"], diff --git a/activesupport/test/inflector_test_cases.rb b/activesupport/test/inflector_test_cases.rb index b556da0046..3770f00fe3 100644 --- a/activesupport/test/inflector_test_cases.rb +++ b/activesupport/test/inflector_test_cases.rb @@ -141,6 +141,7 @@ module InflectorTestCases "HTMLTidyGenerator" => "html_tidy_generator", "FreeBSD" => "free_bsd", "HTML" => "html", + "ForceXMLController" => "force_xml_controller", } CamelWithModuleToUnderscoreWithSlash = { -- cgit v1.2.3