Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

ActiveSupport::Inflector.underscore messes up acronyms under namespaces #12510

Closed
wants to merge 1 commit into from

6 participants

@agios

For example:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.acronym 'ISP'
  inflect.acronym 'ISPs'
end

"ISP".pluralize.underscore 
  => "isps" (ok)
"SomeISP".pluralize.underscore 
  => "some_isps" (ok)
"Provider::ISP".pluralize.underscore
  => "provider/is_ps" (should be provider/isps)
@agios

Has anyone had a chance to look at this yet? It is a simple and straight-forward fix.

Currently this issue breaks url_for in our application if we use namespaced model names with an acronym, among other problems. Ie, if the model was named Provider::ISP, the helper method would be incorrectly named provider_is_ps_url and url_for(Provider::ISP) fails with undefined method `provider_isps_url'

@pftg

Please add tests and changelog entry

@agios

The patch already includes the test. I'll add a changelog entry.

@pftg

Also please squash commits

@agios

@pftg does this look ok now?

@rafaelfranca
Owner

Cc @fxn

@frodsan frodsan was assigned
@fxn fxn was assigned
@agios

Is anyone still looking at this? Given it's a bug, albeit small, it would be nice if it could be fixed (and possibly backported too, allowing us to remove a monkey-patch :wink:)

@jaggederest

This is failing CI, can you update it and get it to pass?

@agios

The CI fail was on the master branch, if it is fixed I can rebase it.

@agios

I believe the regex in #14146 is slightly more elegant, closing in favor of that

@agios agios closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 26, 2013
  1. @agios
This page is out of date. Refresh to see the latest.
View
5 activesupport/CHANGELOG.md
@@ -1,3 +1,8 @@
+* Fix the underscore inflection of acronyms directly after module names, as in
+ `"API::RESTful".underscore => "api/restful"`
+
+ *Alexandros Giouzenis*
+
* Fix `slice!` deleting the default value of the hash.
*Antonio Santos*
View
2  activesupport/lib/active_support/inflector/methods.rb
@@ -90,7 +90,7 @@ def camelize(term, uppercase_first_letter = true)
# 'SSLError'.underscore.camelize # => "SslError"
def underscore(camel_cased_word)
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])|(\/)|^)(#{inflections.acronym_regex})(?=\b|[^a-z])/) { "#{$1}#{$2}#{$1 && '_'}#{$3.downcase}" }
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
word.tr!("-", "_")
View
1  activesupport/test/inflector_test.rb
@@ -118,6 +118,7 @@ def test_acronyms
["HTTP::Get", "http/get", "HTTP/get", "HTTP/Get"],
["SSLError", "ssl_error", "SSL error", "SSL Error"],
["RESTful", "restful", "RESTful", "RESTful"],
+ ["API::RESTful", "api/restful", "API/RESTful", "API/RESTful"],
["RESTfulController", "restful_controller", "RESTful controller", "RESTful Controller"],
["IHeartW3C", "i_heart_w3c", "I heart W3C", "I Heart W3C"],
["PhDRequired", "phd_required", "PhD required", "PhD Required"],
Something went wrong with that request. Please try again.