Strategy to access your Rails.application.credentials from ENV.
Used 32 times
D
Dale Zak
Usage
Read Rails Environment Variables Using Credentials for full details, once the template is installed you can access Rails.application.credentials using ENV.
ENV['HEROKU_APP_NAME'] # access Heroku defined variables
ENV['GITHUB_API_KEY'] # access variables from your credentials
ENV['git_api_key'] # also access variables in lowercase
ENV['rails_env_key'] # access environment specific variables
Run this command in your Rails app directory in the terminal:
rails app:template LOCATION="https://railsbytes.com/script/Vp7s90"
Template Source
Review the code before running this template on your machine.
inject_into_file 'config/application.rb', after: "< Rails::Application\n" do <<-EOF
credentials.config.each do |key, value|
if key.to_s == Rails.env.to_s
value.each do |env_key, env_value|
ENV[env_key.to_s.upcase] = env_value.to_s if ENV[env_key.to_s.upcase].blank?
ENV[env_key.to_s.downcase] = env_value.to_s if ENV[env_key.to_s.downcase].blank?
end
elsif ['development', 'staging', 'test', 'production'].include?(key.to_s) == false
ENV[key.to_s.upcase] = value.to_s if ENV[key.to_s.upcase].blank?
ENV[key.to_s.downcase] = value.to_s if ENV[key.to_s.downcase].blank?
end
end
EOF
end