rails: Engines don't seem to load config/initializers

I seem to have found either a bug in Engines or a mistake in the Rails Guides about Engines. I figured this out after hours of trying to wrestle the engine into using minitest-rails as a generator, thinking it was minitest, when it turns out to be rails. 😢

PROBLEM: *.rb files in the initializers folder of the engine itself (not the dummy app) are not loaded.

The Rails Guide about engines references this at http://guides.rubyonrails.org/engines.html#general-engine-configuration

If you wish to use an initializer — code that should run before the engine is loaded — the place for it is the config/initializers folder.

It seems like either Rails or the Guide isn’t doing the right thing. Hopefully Rails in this case, so we can use the config/initializers folder as expected. 😄

REPRODUTION: If you make a config/initializers/generators.rb in the engine base directory, it is not loaded.

I tried it with several different entries

Rails.application.config.generators do |g|
  g.test_framework :mini_test, :spec => true, :fixture => false
end

and then

EngineName::Engine.config.generators do |g|
  g.test_framework :mini_test, :spec => true, :fixture => false
end

and then finally attempting to raise an error.

raise "this file isn't being loaded"

MORE INFO: I have tested this with both --full and --mountable with rails plugin new. From a brand new “new”, this is the result of 1) adding minitest-rails to the gemspec, 2) doing bundle, and then 3) attempting to generate a model.

      # rails generate model user title:string --spec  
      invoke  active_record
      create    db/migrate/20140324235224_create_users.rb
      create    app/models/user.rb
      invoke    test_unit
      create      test/models/user_test.rb
      create      test/fixtures/users.yml

And on a regular rails new application following the same process:

      # rails generate model user title:string --spec  
      invoke  active_record
      create    db/migrate/20140324235436_create_users.rb
      create    app/models/user.rb
      invoke    mini_test
      create      test/models/user_test.rb

WORKAROUND: Putting all configuration settings in lib/engine_name/engine.rb will work properly. Setting the config.generators in that location works perfectly, as seen here:

module EngineName
  class Engine < ::Rails::Engine
    isolate_namespace EngineName

    config.generators do |g|
      g.test_framework :mini_test, :spec => true, :fixture => false
    end
  end
end

Rails 4.0.4, ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin12.0]

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 17 (10 by maintainers)

Most upvoted comments

I haven’t checked if this issue is fixed in rails 5, yet. But I’m leaving a comment here for others still on rails 4.2 looking for a solution, since I found this thread when googling the problem.

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

I can confirm the OP’s comments. The reason it wasn’t reproduced above is that the problem shows up when running a generator, not when loading the application, which is what the commenter tried to do.

In my case I was trying generate a model that confused the default inflector…

$ bin/rails g model Regatta
[WARNING] The model name 'Regatta' was recognized as a plural, using the singular 'Regattum' instead. Override with --force-plural or setup custom inflection rules for this noun before running the generator.
      invoke  active_record
      create    db/migrate/20150902141927_create_regatta.rb
      create    app/models/regattum.rb
      invoke    test_unit
      create      test/models/regattum_test.rb
      create      test/fixtures/regatta.yml

If I try to do this with an application I get the same result, but I can fix it by putting the inflector in config/initializers/inflections.rb. However with an engine, I instead have to put it in lib/my_engine/engine.rb:

module MyEngine
  class Engine < ::Rails::Engine
    isolate_namespace MyEngine
    ::ActiveSupport::Inflector.inflections do |inflect|
      inflect.irregular 'regatta', 'regattas'
    end
  end
end

@johannesE thanks man, that explains it 😃

FYI: I had similar problems with a custom initializer, but it turned out that it was just spring messing with me.

hi, any updates on this? im trying to set my template engine to slim , or at least hopefully engine is using root app todo so, but i could not find any initialiser todo so too in generator…