how to use inflections on a rails engine

2020-06-12 02:33发布

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?

3条回答
Rolldiameter
2楼-- · 2020-06-12 02:42

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!

查看更多
对你真心纯属浪费
3楼-- · 2020-06-12 02:44

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

  1. 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
    
  2. 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
    
  3. Use the generator as expected: bin/rails g model Regatta

查看更多
相关推荐>>
4楼-- · 2020-06-12 03:04

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.

查看更多
登录 后发表回答