Urgent.News

What's breaking now, across thousands of outlets.

Tech

Re-Organized configuration in Rails

A while back I wrote about organizing configuration in Rails . The idea was simple: drop YAML files into config/configurations/ and get namespaced constants like Config::Bot.api_key instead of the clunky Rails.application.config.bot.api_key . It worked well. But every YAML file needed manual wiring: <%= ENV.fetch("BOT_API_KEY", Rails.application.credentials.dig(:bot, :api_key)) %> . For every…

In a recent article, the author discussed a new way of organizing configuration in Rails. The original approach involved placing YAML files in the config/configurations/ directory and creating namespaced constants to access configuration values. While this worked well, it required manual wiring of each key in every YAML file, which became tedious and error-prone.

The author decided to improve upon the original system, maintaining the same clean Config::Namespace.key API but adding functionality to automatically chain through three sources: environment variables, Rails credentials, and YAML files. This resulted in a more streamlined and efficient configuration lookup process.

The updated configuration YAML file now features a single API to access configuration values from all three sources. The author provides examples of how user_agent, timeout, and other configuration values can be accessed using the Config::Bot namespace. This simplifies accessing configuration values and eliminates the need for manual wiring.

The author highlights two key features of their updated configuration module: lazy namespaces with const_missing and a three-source lookup chain. The const_missing method is used instead of scanning directories at boot to create namespace objects lazily. This approach ensures that namespaces are only created when they are needed, such as when Config::Bot is referenced for the first time.

A Mutex is used to prevent multiple threads from creating namespace objects simultaneously, which could lead to issues in threaded environments.

The three-source lookup chain involves checking environment variables first, then Rails credentials, and finally YAML files. This order allows for flexibility and ensures that sensitive values are prioritized in the environment and encrypted credentials, while public configuration values are stored in YAML files. The author also provides methods for checking for missing values (bang methods) and listing all available keys for a namespace, including both YAML and credential keys.

Additionally, there's a reload method that clears caches for situations where configuration values are changed during development.

The author also touches on Rails 8.2's new credentials feature, which introduces Rails.app.creds for checking secrets and options. The author notes that their Config module differs from Rails.app.creds in scope, as it includes YAML files as an additional tier for configuration values, such as URLs, timeouts, API versions, and feature flags.

Overall, this new approach to organizing configuration in Rails provides a more streamlined and flexible solution for accessing configuration values, reducing the likelihood of misconfiguration and improving the overall efficiency of Rails applications.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

The compiler was never what you wanted

You have an orders topic on a Kafka cluster, its values encoded with Avro against a schema in the Schema Registry . You want the orders worth more than fifty euros on a topic of their own, and you…

What are you doing this weekend?

Feel free to tell what you plan on doing this weekend and even ask for help or feedback. Please keep in mind it’s more than OK to do nothing at all too!

More from Friday 4 September →