aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorJames Le Cuirot <james.le-cuirot@yakara.com>2014-10-03 22:45:40 +0100
committerJames Le Cuirot <chewi@aura-online.co.uk>2014-10-03 22:45:40 +0100
commit6a8464fa4f50a0fce3ce245f717b813712ea27a8 (patch)
tree2a68543951d7785aba163a6a728b5321e4c2c178 /activesupport
parent1609ec9879d0011f31f0ae90cf3b2bc8b1833390 (diff)
downloadrails-6a8464fa4f50a0fce3ce245f717b813712ea27a8.tar.gz
rails-6a8464fa4f50a0fce3ce245f717b813712ea27a8.tar.bz2
rails-6a8464fa4f50a0fce3ce245f717b813712ea27a8.zip
Fix underscore inflector handling of adjacent acronyms
I suspect that positive lookbehind would have been used in the original implementation had it been available in supported Ruby versions at the time. Now that Rails requires Ruby 1.9.2 or above, this is no longer an issue. This fixes #14146 for acronyms such as APIRESTful. This technique also addresses namespaced acronyms that are not entirely uppercased. This was broken when the commit was originally written but has since been fixed in ccbb481. The latter does not deal with adjacent acronyms so this commit wins.
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/inflector/methods.rb2
-rw-r--r--activesupport/test/inflector_test.rb3
2 files changed, 4 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/inflector/methods.rb b/activesupport/lib/active_support/inflector/methods.rb
index f35e71ce81..72da0dd189 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])|\b)(#{inflections.acronym_regex})(?=\b|[^a-z])/) { "#{$1}#{$1 && '_'}#{$2.downcase}" }
+ word.gsub!(/(?<=[A-Za-z\d\/]|^)(#{inflections.acronym_regex})(?=\b|[^a-z])/) { "#{'_' unless $`.empty? or $`[-1] == '/'}#{$1.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 b37f31bc5f..5446c5ec3c 100644
--- a/activesupport/test/inflector_test.rb
+++ b/activesupport/test/inflector_test.rb
@@ -125,6 +125,9 @@ class InflectorTest < ActiveSupport::TestCase
["PhDRequired", "phd_required", "PhD required", "PhD Required"],
["IRoRU", "i_ror_u", "I RoR u", "I RoR U"],
["RESTfulHTTPAPI", "restful_http_api", "RESTful HTTP API", "RESTful HTTP API"],
+ ["HTTP::RESTful", "http/restful", "HTTP/RESTful", "HTTP/RESTful"],
+ ["HTTP::RESTfulAPI", "http/restful_api", "HTTP/RESTful API", "HTTP/RESTful API"],
+ ["APIRESTful", "api_restful", "API RESTful", "API RESTful"],
# misdirection
["Capistrano", "capistrano", "Capistrano", "Capistrano"],