Rails model naming partial acronym word - ruby-on-rails

So. For some reason I'm struggling with Rails naming today. I feel like the best naming for a model I'm creating is DNSRecord for the camel case class model name and dns_record for snake case referencing -- Rails wants to name it DnsRecord.
I've seen a solution if the entire word is capitalized for example: API
ActiveSupport::Inflector.inflections do |inflect|
inflect.acronym 'API'
end
but doesn't seem to work well as:
ActiveSupport::Inflector.inflections do |inflect|
inflect.acronym 'DNSRecord'
end
it still wants to refer to DnsRecord. I know this is a small thing but DNSRecord feels better to me.

I believe you will want inflect.acronym 'DNS' rather than 'DNSRecord'. You will likely also have to restart your server after updating the inflections file.

Related

Inconsistent Routing name generation in Rails default routes

I have a model Leave and a controller name 'LeavesController'. When I set resources :leave[singular] to the routes.rb then the routes name are ok. But when I set the resources :leaves[plural], then I get some funny routing name. See screenshot for details. [leafe, new_leafe, edit_leafe]
How can I get rid of these funny routing name?
In config/initializers/inflections.rb uncomment/add the following
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.irregular 'leave', 'leaves'
end
Explanation:
Rails uses Inflectors to singularize/pluralize words automatically. This is how things like singular model names are transformed into their plural table names. Rails is typically able to guess the correct pluralization/singularization but can sometimes get confused. The inflections.rb initializer allows you to explicitly define how you want to handle singularization/pluralization when Rails doesn't give you what you are looking for.

How to get accurate pluralization with Rails pluralization

I need very accurate pluralization for my application
I am currently using the Rails built in pluralization method.
I found a problem with the word 'foot'.
When I do:
"foot".pluralize(2)
=> "foots"
The correct pluralization should be feet.
How can I get more accurate results?
I have looked for other gems and they all seem to make the same error.
you need to define below code inside your config/initializers/inflections.rb
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.irregular 'foot', 'feet'
end
there are some of plural are not define yet, so you need to define it inside inflections.

How do you disable puralization of generated models in rails?

There's a config file config/initializers/inflections.rb that, as per this question lets you modify the puralization of certain items:
ruby on rails pluralization help?
However, I'm not interested in that. I want to turn the automatic modification of generated model names off.
Here's an example:
rails generate scaffold UserData data1:string data2:string
Data is changed to 'Datum':
%] cat app/models/user_datum.rb
class UserDatum < ActiveRecord::Base
attr_accessible :data1, :data2
end
This is undesirable behaviour.
How do I turn it off?
Specifically if you can please; I've seen a few threads with people saying things like 'you'll have to modify the recipe for that', but no actual guide to doing this.
(I appreciate people are going to want to start answering this with 'you should just stick to the rails way of doing things, there's a good reason for this and it'll work out in the long run'; please dont)
First of all, UserDatum is singular.
In any case:
Change your config/initializers/inflections.rb:
ActiveSupport::Inflector.inflections do |inflect|
inflect.uncountable %w(UserData)
end
(Use whatever naming convention you use, e.g., if you use underscores, user_data instead, or both.)
If you want to remove all pluralizations (sketchy: this will affect everything in the world):
ActiveSupport::Inflector.inflections do |inflect|
inflect.clear
inflect.singular(/$/i, '')
end
If you want to control only model/model file naming, patch ModelGenerator:
module Rails
module Generators
class ModelGenerator
def plural_name; singular_name; end
def plural_file_name; file_name; end
end
end
end
The best way of doing this is:
rails generate scaffold HouseData --force-plural
(it's still an item by item fix, but it doesn't mess around with the pluralization stuff, which is global and affects other parts of the app too)

Using titleize for acronym in Rails

I am defining some active records with acronyms. RvPark (Recreational Vehicle Park). When I titleize the class name, I get 'Rv Park'. It really should be 'RV Park'. Is there a good way to do this? Since this model shares code with other models, I need to create a generic solution, but I haven't been able to come up with one.
I did see a discussion on this, but there wasn't a solution that worked for me. any insight is appreciated.
https://rails.lighthouseapp.com/projects/8994/tickets/2944-titleize-doesnt-take-all-uppercase-words-into-account
You can do this by configuring ActiveSupport::Inflector, which provides the titleize method. Simply define your own inflections in an initializer.
# config/initializers/inflections.rb
ActiveSupport::Inflector.inflections do |inflect|
inflect.acronym 'RV'
end
Restart your app to pick up the change. Now titleize knows how to handle "RV". Fire up a Rails console to check it out:
> "RvPark".titleize
=> "RV Park"
> "rv".titleize
=> "RV"
See the linked docs for more cool stuff you can do with inflections.
UPDATE: Acronym support was added to the Rails Inflector after I posted this.
See #Anson's answer for Rails 3.2 and up.
This looks like an edge-case that titleize wasn't designed for. The problem is the capitalize call inside will always turn RV into Rv.
I would create a generic name function for the models that call self.class.titleize internally, and then overload it in the RVPark model.

Ruby on Rails: How do you explicitly define plural names and singular names in Rails?

For example, I'm using "Bonus" as my model, so I'd expect "bonuses" to be the plural form and "bonus" to be the singular form.
However, in Ruby, this results in:
"bonus".pluralize # bonus
"bonuses".singularize # bonuse
So, when I do a "has_many :bonuses", for example, it doesn't use the Bonus.rb model (since Ruby expects a Bonuse.rb model instead). Is there a way to correct that in Ruby on Rails somehow such that "bonuses" acts as the plural form for the model bonus.rb?
In config/initializers, you will find a file called inflections.rb. There are some instructions in here, but you will want something along the lines of:
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'bonus', 'bonuses'
end
Just to back up bcarlso, more on Inflector can be found here:
http://4loc.wordpress.com/2009/04/09/inflector-rails-pluralization/
Note that the position of the Inflector.inflections block is important and, as noted in the link reference, must be after the Initializer.run block.
I believe you use the Inflector in your environment.rb (memory's a bit sketchy though)
If I remember correctly you put it in a block
Inflector.inflections { | i | i.irregular 'bonus', 'bonuses' }

Resources