Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 454
marlon Avatar asked Jun 22 '15 16:06

marlon


People also ask

What are inflections in Rails?

14.3. Inflections are Rails' way of knowing how to pluralize or singularize words. You can also define uncountable words, and Rails is smart enough already to know that you can't pluralize words like news and sheep. It also has some rules about irregulars, like where person becomes people when it's pluralized.

What is a Rails engine?

1 What are Engines? Engines can be considered miniature applications that provide functionality to their host applications. A Rails application is actually just a "supercharged" engine, with the Rails::Application class inheriting a lot of its behavior from Rails::Engine .


2 Answers

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.

like image 85
Alejandro Babio Avatar answered Nov 02 '22 04:11

Alejandro Babio


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!

like image 2
uday Avatar answered Nov 02 '22 03:11

uday