ruby-on-rails

How to access to values set in an initializer in `Rails.application.configure`?


I have a global constant (APP_CONFIG) for all my custom configs that reads settings from app_config.yml file.

I initialize this constant in config/initializer/00_load_app_config.rb file.

I want to access to these configs values on my Rails.application.configure like:

config.action_mailer.asset_host = APP_CONFIG[:host]

But when Rails.application.configure executes, my initializer still has not been executed.

I tried:

config.action_mailer.asset_host = -> { APP_CONFIG[:host] }

In case I could make use of some lazy magic, but it didn't work.


Solution

  • I used config.after_initialize to solve it:

    # config/environments/development.rb
    Rails.application.configure do
      [...normal configs here]
    end
    
    # at the end
    Rails.application.config.after_initialize do
      Rails.configuration.action_mailer.asset_host = APP_CONFIG[:host]
    end
    

    It is not the most elegant way because it forces me to separate part of the configuration to the end of the file, and it may be the case I forgot this is there when I look for it in the future (or another developer is looking for it).