WARNING: This is old and likely obsolete.
- published date: 2015-01-20 07:30
- keywords: [“configuration”, “rails”, “swaac”]
There have been a lot of blog posts and articles written on how to add application-specific configurations to a Rails app. In version 4.2, there is a method called config_for
which makes a lot of that obsolete, simplifying the results.
A seemingly very common idiom is to arrange appliation configuration values in YAML+ERB files under the config
directory in a Rails app. Then, in an initializer, read in the file, process it through ERB, then YAML extract the set appropriate to the running environment, and save it in a constant or global:
default: &default service_url: https://my_service.example.com development: <<: *default test: <<: *default production: service_url: <%= ENV['APP_SERVICE_URL'] %>
require 'yaml'
require 'erb'
APP_CONFIG = YAML.load(ERB.new(File.read(Rails.root.join('config','app_config.yml'))).result)[Rails.env] || {}
=config_for= is a method on Rails.application
. It can be called bar within all the normal configuration contexts, such as inside config/environments/development.rb
and such.
What it does is fairly straightforward, reading a YAML+ERB file from the config
directory, returning the appropriate section based on the current Rails environment.
It’s a simple enough implementation: https://github.com/rails/rails/blob/1d1239d32856b32b19c04edd17d0dd0d47611586/railties/lib/rails/application.rb#L226 (Note of course this is a volatile link). It’s implemented in Rails::Application#config_for.
You pass in a symbol that corresponds to a file name under config
, so config_for(:app_config)
loads up the appropriate environment variables from config/app_config.yml
.
Now, your initializer can become simply:
APP_CONFIG = Rails.application.config_for(:app_config)
I’ve an example github repo at https://github.com/tamouse/test_config_for. Feel free to fork it and play with it yourself.