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|
"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.
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 :)
+1, this would be very nice to have.
👍 apparently, we miss this functionality.
/cc @jeremy @fxn
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.
@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 :)
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.
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.
I.e. use at your own risk?
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.
Ok, @dlee do you want to make a patch for this?
Yup, coming right up!
Add acronym support to Inflector; Issue #1366
@sikachu: I just sent a pull request with tests, docs, and guide.
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 ;)
Mention about acronym inflection in config/initializers/inflections.rb