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?
You can actually create a file named
inflections.rb
in project _root/config/initializers/ and write your rules in that file for e.g.,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
:Require the inflections initializer in the
engine.rb
:Use the generator as expected:
bin/rails g model Regatta
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 foldermyapp
.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:
Yes, you must create the folder
initializers
and the fileinflections.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.