Inflector support for acronyms #1366

Closed
dlee opened this Issue May 27, 2011 · 15 comments

7 participants

@dlee

Rails' inflector bit me many times with inflecting acronyms. I want to add an acronym inflection rule alongside plural and singular that would provide better support for acronyms in camelize and titleize.

Right now, the camelize and titleize have no concept of acronyms:

Net::HTTP.name.underscore #=> "net/http"
"net/http".camelize #=> "Net::Http" :(

Nokogiri::HTML.name.underscore #=> "nokogiri/html"
"nokogiri/html".camelize #=> "Nokogiri::Html" :(

Notice how underscore and camelize are not completely reversible because of the acronyms. Adding the acronym inflection rule would allow you to do this:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.acronym "HTTP"
  inflect.acronym "HTML"
  inflect.acronym "API"
  inflect.acronym "RoR"
end

"net/http".camelize #=> "Net::HTTP"
"nokogiri/html".camelize #=> "Nokogiri::HTML"
"api_controller".camelize #=> "APIController"
"ror_rocks".titleize #=> "RoR Rocks"

This has bitten other developers out there, who probably just ended up renaming their APIController to ApiController, and such. I think adding the acronym inflection rule would go a long way in solving these headaches. If Rails core accepts this suggestion, I can cook up a pull request.

@sikachu
Ruby on Rails member

I think I like that idea. Do you mind cooking the pull request with documentation/guide? This will be very nice addition to Rails 3.1/3.2 (depends on how fast you can make it ;))

Thank you :)

@dmathieu

+1, this would be very nice to have.

@amatsuda
Ruby on Rails member

👍 apparently, we miss this functionality.

@josevalim
Ruby on Rails member
@fxn
Ruby on Rails member
fxn commented Jun 7, 2011

Sounds good to me.

Only concern is backwards compatibility, if this is added to AS in my view no acronym rules should come builtin in a minor version upgrade. New generated apps could perhaps have some in config/initializers if they do not create a clear conflict.

@sikachu
Ruby on Rails member

@fxn I think what we can have is a commented out definition in config/initializers/inflector.rb, just like others. Then we can have some default ones in there, which will only reflect new applications.

In the future (3.2), we then stuck the definitions in AS for useful terms :)

@fxn
Ruby on Rails member
fxn commented Jun 7, 2011

Doubtful about 3.x. Defining such rules within AS is unnecessarily brittle in my opinion for existing apps and plugins they depend on. A plugin should need to conditionally use a constant or other depending on a minor version of Rails.

Though I understand that the fact that camelize and underscore are not inverses of each other is not ideal (though it is documented), even if you change them via a new acronym category, you are changing the behavior of camelize. Not worthwhile in my opinion for a 3.x.

@sikachu
Ruby on Rails member

Ok, then let's forget about setting term in AS. How about just add support and add comment in the inflector.rb file? I, for one, willing to customize my own application for it for sure.

@sikachu
Ruby on Rails member

I.e. use at your own risk?

@fxn
Ruby on Rails member
fxn commented Jun 7, 2011

Yes, use at your own risk sounds good for me. Indeed I prefer the generator to output only commented out code. You are able to get the acronyms back, and if that breaks some other dependency of yours then that's your choice. Seems fine.

Think the canonical acronyms to configure here are ubiquitous.

@sikachu
Ruby on Rails member

Ok, @dlee do you want to make a patch for this?

@dlee

Yup, coming right up!

@dlee dlee added a commit to dlee/rails that referenced this issue Jun 11, 2011
@dlee dlee Add acronym support to Inflector; Issue #1366 d38ca78
@dlee

@sikachu: I just sent a pull request with tests, docs, and guide.

@sikachu
Ruby on Rails member

This was merged in. I'm going to submit another patch to add the template file in the new application, and add some guides to it ;)

@sikachu sikachu closed this in 75cfd03 Jul 11, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment