Introduction

The use of environment variables in our Rails applications can become cumbersome, especially with larger applications. Thankfully theFigaro gem helps simplify this. In this article we will show you how to use Figaro to simplify the configuration for your application. Let's get started.

Rails Application Setup

The first thing we must do is add the Figaro gem to our gemfile. Open up your gemfile now and add in the code listed below.

Gemfile:

gem 'figaro'

Now run a bundle install to install the gem.

Terminal Commands:

bundle install

Now we must tell figaro to install itself. Figaro will create a new file in your config folder called application.yml. In addition, figaro will add an entry to the .gitignore file to tell git that we wish to exclude application.yml from git. Run the command below to install figaro now.

Terminal Commands:

bundle exec figaro install

Great, now if you open up config/application.yml you'll see a bunch of sample settings. You can define settings globally or on a per environment basis. Let's do this now. Let's move oursecret_key_base to our config/application.yml. Modify your application.yml file so that it looks like the code listed below. Note: while it's okay to use the values for an example application, do not use the values listed here for your production machine. Instead use the rake secret to generate unique values for each environment.

config/application.yml:

development:
  secret_key_base: fcfcb34e7d5bd30d905e9f11944bd4b1037851ad0f2fa8b91bdda366dc4341017a6124debf4c2cff1e3ffd4945c4a911a91c7c1fa75951fac52278c41219123f

test:
  secret_key_base: 413e9c3a2598f6afc8e1d18be6de8ae0228de9a669f62131d8a4cbb9ca42576676f13aa5f76194c1eab0e7f78c22f82711e008d1b27a0c379018612c3214e824

Now let's modify the secrets.yml to use figaro to load the configuration values. There are two ways of doing this. We can either use ENV['secret_key_base'] or we can use the figaro provided Figaro.env.secret_key_base. It is recommended you use Figaro.env over ENV as Figaro.env can be easily stubbed and unstubbed for testing purposes. Open up your config/secrets.yml file now and add in the code listed below.

config/secrets.yml:

development:
  secret_key_base: <%= Figaro.env.secret_key_base %>

test:
  secret_key_base: <%= Figaro.env.secret_key_base %>

production:
  secret_key_base: <%= Figaro.env.secret_key_base %>

The code above tells Rails to get the secret_key_base setting and make it accessible to the secrets.yml file. When this setting is accessed, Figaro will load the the setting from application.yml for the appropriate environment and use it. We can demonstrate this by loading up a rails console. If we start the rails console and run the command listed below, we will see that the appropriate setting gets loaded.

Rails Console:

irb(main):001:0> Rails.application.secrets.secret_key_base #=> "fcfcb34e7d5bd30d905e9f11944bd4b1037851ad0f2fa8b91bdda366dc4341017a6124debf4c2cff1e3ffd4945c4a911a91c7c1fa75951fac52278c41219123f"
irb(main):002:0> Figaro.env.secret_key_base #=> "fcfcb34e7d5bd30d905e9f11944bd4b1037851ad0f2fa8b91bdda366dc4341017a6124debf4c2cff1e3ffd4945c4a911a91c7c1fa75951fac52278c41219123f"
irb(main):003:0> ENV['secret_key_base'] # => "fcfcb34e7d5bd30d905e9f11944bd4b1037851ad0f2fa8b91bdda366dc4341017a6124debf4c2cff1e3ffd4945c4a911a91c7c1fa75951fac52278c41219123f"

You can also further see this by loading up the rails console in the test environment by running rails console testcommand:

Rails Console:

irb(main):002:0> Rails.application.secrets.secret_key_base #=> "413e9c3a2598f6afc8e1d18be6de8ae0228de9a669f62131d8a4cbb9ca42576676f13aa5f76194c1eab0e7f78c22f82711e008d1b27a0c379018612c3214e824"
irb(main):003:0> Figaro.env.secret_key_base #=> "413e9c3a2598f6afc8e1d18be6de8ae0228de9a669f62131d8a4cbb9ca42576676f13aa5f76194c1eab0e7f78c22f82711e008d1b27a0c379018612c3214e824"
irb(main):004:0> ENV['secret_key_base'] #=> "413e9c3a2598f6afc8e1d18be6de8ae0228de9a669f62131d8a4cbb9ca42576676f13aa5f76194c1eab0e7f78c22f82711e008d1b27a0c379018612c3214e824"

Required Keys

It may seem a bit redundant to use Figaro and application.yml when you have secrets.yml, but Figaro also has another feature, it allows you to make certain keys required. Calling Figaro.require_keys will instantly throw an exception if the keys specified are not present:

Rails Console:

Figaro.require_keys('this_is_a_fake_key') #=> Figaro::MissingKeys exception

We can also easily add the require keys at startup via an initializer. Create a new initializer called figaro.rb and add in the code listed below.

config/initializers/figaro.rb:

Figaro.require_keys [
  'secret_key_base'
]

Now if you try to start your application without the secret_key_base setting being set, figaro will throw an exception. That's it, thanks for reading!