# Terminal
rails credentials:help
rails credentials:edit
EDITOR='code --wait' rails credentials:edit
rails c
RAILS_ENV=production rails c
# Rails Console
Rails.application.credentials.aws
Rails.application.credentials.env.aws
Rails.application.credentials.env.aws[:access_key_id]
Rails.application.credentials.env.secret_key_base
# config/application.rb
require_relative 'boot'
require 'rails/all'
require_relative 'rails_env'
Bundler.require(*Rails.groups)
module Template
class Application < Rails::Application
config.load_defaults 5.2
config.after_initialize do
Rails.application.credentials.env = RailsEnv.new
end
end
end
# config/rails_env.rb
class RailsEnv
def initialize
load_environment_variables unless Rails.env.production?
allow_encrypted_credentials
end
private
def load_environment_variables
return unless File.exist?(file_name)
HashWithIndifferentAccess.new(YAML.safe_load(File.open(file_name))).each do |key, value|
self.class.send :define_method, key.downcase do
value
end
end
end
def allow_encrypted_credentials
self.class.send :define_method, :method_missing do |m, *_args, &_block|
Rails.application.credentials.send(m)
end
end
def file_name
File.join(Rails.root, 'config', "#{Rails.env}.yml")
end
end