-1

I am currently working with the Rails application which depends on a huge amount of ENV variables.

The problem is when somebody adds a new ENV variable and forgets to notify the team about it, we spend a lot of time debugging errors like undefined method '[]' for nil:NilClass (NoMethodError) in very unexpected contexts.

After some googling, I have found the Anyway Config: Keep your Ruby configuration sane article.

It contains the following principle:

The application must fail on boot if a system secret is missing or invalid

I really believe we can leverage from it since instead of too generic errors we can have something like ENV variable #{some_name} is NOT set before the application is even started.

So, I set up anyway_config as in the Installation guide.

gem "anyway_config", "~> 2.0"

Run a generator:

rails g anyway:install

and created the following config file in the config/configs directory.

# frozen_string_literal: true

class CurrentApplicationConfig < ApplicationConfig
  env_prefix 'CURRENT_APPLICATION'

  attr_config :name

  required :name
end

After that, when I execute rails server without specifying CURRENT_APPLICATION_NAME env variable, I expect to receive an error, but it is not raised.

Could somebody explain what I am missing? Thanks in advance.

Marian13
  • 7,740
  • 2
  • 47
  • 51

1 Answers1

1
# config/application.rb
config.current_app_config = CurrentApplicationConfig.new

> rails s # error
 
> CURRENT_APPLICATION_NAME=demo rails s # ok
Lam Phan
  • 3,405
  • 2
  • 9
  • 20