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)
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
my_engine/config/initializers/inflections.rb
:engine.rb
: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âŚ
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 inlib/my_engine/engine.rb
:@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âŚ