Auto-reload `config_for` files in Rails

lxxxvi

Mario

Posted on December 31, 2021

Auto-reload `config_for` files in Rails

In Rails projects I often use YAML files to organize app-wide values or configurations.

# config/settings.yml
shared:
  important_value: 42

development:
  default_from_email: 'development@example.com'

test:
  default_from_email: 'test@example.com'

production:
  default_from_email: 'production@example.com'
Enter fullscreen mode Exit fullscreen mode

In the application configuration we create a new configuration:

# config/application.rb
module RailsApp
  class Application < Rails::Application
    # ...
    config.settings = config_for(:settings)
  end
end
Enter fullscreen mode Exit fullscreen mode

We can access these values using Rails.configuration.settings, for example:

# $ RAILS_ENV=test bin/rails c
# Loading test environment (Rails 7.0.0)
Rails.configuration.settings[:important_value]    # => 42
Rails.configuration.settings[:default_from_email] # => "test@example.com"
Enter fullscreen mode Exit fullscreen mode

Unfortunately the configuration is not reloaded automatically. If we change the important_value and refresh the page, we'd still see the old value. In order to see the value, we'd need to restart the server first. This can be confusing and tedious, particularly in development.

Here's a way to reload the configuration every time the file changes.

Create a new file config/initializers/config_for_reloader.rb:

# config/initializers/config_for_reloader.rb
if Rails.env.development?
  config_for_reloader = ActiveSupport::FileUpdateChecker.new(["config/settings.yml"]) do
    Rails.application.config.settings = Rails.application.config_for(:settings)
  end

  ActiveSupport::Reloader.to_prepare do
    config_for_reloader.execute_if_updated
  end
end
Enter fullscreen mode Exit fullscreen mode

In our case the block contains the same instruction as in config/application.rb for loading the YAML file.

At this point, I would have expected that Rails picks up the changes automatically after the file config/settings.yml is modififed, but for some reason it does not. It only reloads the file and sets the configuration after an "application file" (a model, template, controller, routes, etc.) is modified.

We can workaround this by adding config/settings.yml to the eager_load_paths:

# config/application.rb
module RailsApp
  class Application < Rails::Application
    # ...
    config.settings = config_for(:settings)
    config.eager_load_paths << Rails.root.join("config/settings.yml")
  end
end
Enter fullscreen mode Exit fullscreen mode

(If anybody knows why that is necessary, I'd be interested.)

Now, the configuration file is reloaded after changing it and a page refresh would show the latest value.

(Cover image by Christopher Alvarenga on Unsplash)

💖 💪 🙅 🚩
lxxxvi
Mario

Posted on December 31, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related