There is a common configuration pattern of Rails developers to extract Rails.application.config
values to individual config/initializer/*.rb
files. The $ rails new
template includes such initializer files; for example Rails.application.config.assets
is configured within config/initializer/assets.rb
.
Unfortunately, not all Rails.application.config
values can be set in an initializer and function properly. I have been bitten by this many times in my career. This is raised occasionally in Issues with bugs:
In general, configuration specific to the application object needs to go either in config/application.rb or in config/environments/#{Rails.env}.rb , you can’t just assign to an arbitrary config point in config/initializers and expect it to work.
… This is by design. In general Rails does not know about its components. Rails has configuration settings that components pick in their railties initialization, so they are expected to have their value by the time the initialization of railties run. If you check a standard list of initializers you’ll see the ones in config/initializers get executed very late in the boot process (
load_config_initializers
).Unfortunately this is not very well documented right now, but at least you know now you need to move that setting.
I think the reason the initializer files are used by developers is because it is very nice to organize configuration by context, or, if by gem/engine. As well as when running rails app:update
the initializer files do not get overwritten like config/application.rb
and config/environments/{Rails.env}.rb
do.
I’d like to get feedback on having a separate config directory (config/configurizers
) specifically for setting Rails.application.config
variables. It would be loaded immediately after config/application.rb
, but before config/environments/{Rails.env}.rb
, and most importantly, before Railtie initialization.
I am interested in this proposal because, as a Gem/Engine maintainer, it is challenging to hint/document which configuration is safe to be put in an initializer, and which must be configured prior to initialization, and it would be nice to have a single safe place to tell developers to put all of the configuration.
What do you think? (the directory name obviously needs work)