Rails 5 custom inflections not working - ruby-on-rails

I'm new in Rails and now I'm trying to make a custom pluralization so I put this in my initializers/inflections.rb:
ActiveSupport::Inflector.inflections do |inflect|
inflect.clear
# Irregulares
inflect.irregular "país", "países"
end
But when I try on rails console I get:
Running via Spring preloader in process 3137
Loading development environment (Rails 5.0.2)
2.3.3 :001 > "país".pluralize
=> "país"
2.3.3 :002 >
I also tried to put this in my inflections.rb but with the same result:
ActiveSupport::Inflector.inflections ("pt-BR") do |inflect|
inflect.clear
# Irregulares
inflect.irregular "país", "países"
end
As I'm using rails-i18n my application.rb has this extra line:
config.i18n.default_locale = 'pt-BR'
I searched a lot and didn't find a solution.
Any suggestions?

Try running the console without Spring with DISABLE_SPRING=1

Related

how to use inflections on a rails engine

i have created an engine with
rails plugin new myengine --mountable
when searching for 'inflections' in the project folder, i find the
/test/dummy/config/initializers/inflections.rb file
in this file i put
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.irregular 'singular_model', 'plural_model'
end
when i try to use the model generator (from project root)
bin/rails generate model singular_model
then i get the a migration with singular_models as the table name and migration name
when i run the same command from myengine/test/dummy
bin/rails generate model singular_model
i get the spected result: plural_model as table name and migration name
so, how can i load the inflector into the rails generator?
At your engine, you must think on your test/dummy/ folder as the root of the app that is using this engine. This folder only exists for testing purpose, is not a setting for the engine.
Let say your engine is at folder myengine and your app is at folder myapp.
1) If you want a custom inflection, which is defined at engine level and it be used at the engine and at the app level. Then it must be defined in:
# myengine/config/initializers/inflections.rb
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.irregular 'singular_model', 'plural_model'
end
Yes, you must create the folder initializers and the file inflections.rb in it. And this inflection will be valid for each app that use the engine.
2) If you want your custom inflections works only for your app, but not for others apps that use the same engine. You must use the same code at yours myapp/config/initializers/inflections.rb, this file does exists by default in a Rails app.
In this last case the scope of the custom inflections is only the current app, just like at the test/dummy folder.
Depending on what behavior you need, is where you must put the code of your custom inflections.
You can actually create a file named inflections.rb in project _root/config/initializers/ and write your rules in that file for e.g.,
# Be sure to restart your server when you modify this file.
# Add new inflection rules using the following format. Inflections
# are locale specific, and you may define rules for as many different
# locales as you wish. All of these examples are active by default:
ActiveSupport::Inflector.inflections(:en) do |inflect|
# inflect.plural /^(ox)$/i, '\1en'
# inflect.singular /^(ox)en/i, '\1'
# inflect.irregular 'person', 'people'
# inflect.uncountable %w( fish sheep )
inflect.irregular 'cloth', 'clothes'
end
# These inflection rules are supported but not enabled by default:
# ActiveSupport::Inflector.inflections(:en) do |inflect|
# inflect.acronym 'RESTful'
# end
And it gets applicable to the engine as well.
Hope it helps!
In Rails 4.2, rails engines do not load the initializers when running generators, which is discussed in this issue: https://github.com/rails/rails/issues/14472
Workaround
Define inflections in my_engine/config/initializers/inflections.rb:
# my_engine/config/initializers/inflections.rb
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'regatta', 'regattas'
end
Require the inflections initializer in the engine.rb:
# my_engine/lib/my_engine/engine.rb
require_relative '../../config/initializers/inflections'
module MyEngine
class Engine < ::Rails::Engine
# ...
end
end
Use the generator as expected: bin/rails g model Regatta

ruby on rails 4 command line localization issue

i'm developing a rails 4.1.1 application that uses custom inflections:
inflections.rb:
ActiveSupport::Inflector.inflections(:es) do |inflect|
inflect.plural /([taeiou])([A-Z]|_|\$)/, '\1s\2'
inflect.plural /([rlnd])([A-Z]|_|$)/, '\1es\2'
inflect.singular /([taeiou])s([A-Z]|_|$)/, '\1\2'
inflect.singular /([rlnd])es([A-Z]|_|$)/, '\1\2'
end
and in the application.rb:
...
config.i18n.default_locale = :es
config.i18n.locale = :es
...
but when I try to generate a model it doesn't use the inflectors, I tryed opening a rails console and test using this command:
"direccion".pluralize
The answer was "direcions" wich is incorrect, the right answer is is "direcciones". What should I change so it uses my custom inflector at command line or in rails console so it can generate correctly my models?

Rails generate wrong pluralize form

I use Ruby 1.9.3 and Rails 3.2.9, when I do the following in a rails console:
1.9.3p125 :003 > "foot".pluralize
=> "foots" # shouldn't it be "feet"?
1.9.3p125 :004 > "tooth".pluralize
=> "tooths" # shouldn't it be "teeth"?
1.9.3p125 :009 > "goose".pluralize
=> "gooses" # shouldn't it be "geese"?
is that a bug in rails pluralize or I did something wrong?
You can configure the rails inflector. There should be an initializer file in your application to do so: config/initializers/inflections.rb
You can then add a call to "teach" rails the new rule:
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'tooth', 'teeth'
end
After you restart the server/console the new pluralization should be in place.

Using Rails Inflections with `rails generate`

I'm trying to generate a model called ClassAttendance, but Rails keeps naming the migrations class_attendances. I've tried correcting this problem by placing the following code the following code in \config\initializers\inflections.rb:
ActiveSupport::Inflector.inflections do |inflect|
inflect.uncountable "attendance"
end
This seems to work fine in the rails console:
$ rails console
Loading development environment (Rails 3.2.6)
irb(main):001:0> "attendance".pluralize
=> "attendance"
Unfortunately, the rails model generator seems to be unaffected:
$ rails generate model ClassAttendance
invoke active_record
create db/migrate/20120806201910_create_class_attendances.rb
create app/models/class_attendance.rb
invoke rspec
create spec/models/class_attendance_spec.rb
Does it have something to do with this?
irb(main):002:0> "class_attendance".pluralize
=> "class_attendances"
Or is there some other problem I'm not seeing?
That is the workaround, you need to place it in the inflections.rb file in the config/initializers/. So your config/initializers/inflections.rb would be
ActiveSupport::Inflector.inflections do |inflect|
inflect.uncountable %w( attendance class_attendance ClassAttendance)
end

What is the best way to add inflections in a Rails plugin?

I'm writing a plugin that needs some inflections of it's own. Using rails 2.3 which has the engines imbedded, where I should place my inflections?
I'd recommend adding a separate file (inflections.rb) in your plugins lib directory plugin. You should be able to load the inflections.rb file from the plugin by adding the following at the beginning of the plugin Ruby file.
require 'inflections"
Your inflections.rb file should follow the format provided as an example in new Rails projects:
# Sample Inflections
# ActiveSupport::Inflector.inflections do |inflect|
# inflect.plural /^(ox)$/i, '\1en'
# inflect.singular /^(ox)en/i, '\1'
# inflect.irregular 'person', 'people'
# inflect.uncountable %w( fish sheep )
# end
UPDATE: for anyone who is having the same issue but uses Rails 5, here is the right answer.
Don't place inflections.rb file in plugin/lib folder, but in plugin/config/initializers folder.
Then simply write your inflections (eg cliche/cliches):
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'cliche', 'cliches'
end

Resources