You can use the
config.x
Show archive.org snapshot
configuration in combination with config_for
to configure global settings for your Rails 4.2+ application.
Example
In your config/application.rb
assign the settings from e.g. config/settings.yml
as follows:
module FooApplication
class Application < Rails::Application
config.x.settings = config_for(:settings)
end
end
The config/settings.yml
might look as follows:
shared:
email: info@example.com
google_analytics:
container: UA-123456-12
production:
email: production@example.com
Keys under the shared
namespace are shared between all environments. Environment specific configs will override those shared values. Afterwards you can access these settings with e.g. Rails.application.config.x.settings.google_analytics.fetch(:container)
or Rails.configuration.x.settings.google_analytics.fetch(:container)
in your application.
Additional notes
- The
config.x
is a dedicated namespace and helps you to avoid collisions with config options related to Rails itself. - The
config.x
returns an OrderedOptions Show archive.org snapshot object for the first two levels, but not deeper. - This approach might be preferred over global constants like
config/constants/settings.rb
:- It can be used in the Rails Configuration itself and all initializers.
- It allows to write environment dependent settings.
- Undefined keys return
nil
if not present e.g.Rails.application.config.foobar => nil
Posted by Emanuel to makandra dev (2024-01-09 10:07)