Skip to content

Commit 61810ff

Browse files
committed
feat: add rails crendentails support
1 parent 89b7982 commit 61810ff

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ After installing `Config` in Rails, you will find automatically generated file t
272272
* `evaluate_erb_in_yaml` - evaluate ERB in YAML config files. Set to false if the config file contains ERB that should not be evaluated at load time. Default: `true`
273273
* `file_name` - name of the file to store general keys accessible in all environments. Default: `'settings'` - located at `config/settings.yml`
274274
* `dir_name` - name of the directory to store environment-specific files. Default: `'settings'` - located at `config/settings/`
275+
* `use_rails_credentials` - evaluate Rails credentials if loaded with `RAILS_MASTER_KEY` or `config/master.key`
275276

276277
### Merge customization
277278

lib/config.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ module Config
2727
overwrite_arrays: true,
2828
merge_hash_arrays: false,
2929
validation_contract: nil,
30-
evaluate_erb_in_yaml: true
30+
evaluate_erb_in_yaml: true,
31+
use_rails_credentials: false
3132
)
3233

3334
def self.setup
@@ -47,6 +48,10 @@ def self.load_files(*sources)
4748

4849
config.add_source!(Sources::EnvSource.new(ENV)) if Config.use_env
4950

51+
if defined?(::Rails::Railtie) && Config.use_rails_credentials
52+
config.add_source!(Rails.application.credentials.to_h.deep_stringify_keys)
53+
end
54+
5055
config.load!
5156
config
5257
end

spec/config_spec.rb

+31
Original file line numberDiff line numberDiff line change
@@ -480,5 +480,36 @@
480480
end
481481

482482
end
483+
484+
context 'rails crendentials' do
485+
let(:config) do
486+
Config.use_rails_credentials = true
487+
end
488+
489+
if defined?(::Rails)
490+
it 'shoud have secret_key_base loaded' do
491+
expect(Settings.to_h.keys.include?('secret_key_base')).to eq(true)
492+
end
493+
494+
495+
context 'use_rails_credentials is false' do
496+
let(:config) do
497+
Config.use_rails_credentials = false
498+
end
499+
500+
it 'shoud have secret_key_base loaded' do
501+
expect(Settings.to_h.keys.include?('secret_key_base')).to eq(false)
502+
end
503+
end
504+
end
505+
506+
unless defined?(::Rails)
507+
context 'when not using rails' do
508+
it 'shoud have secret_key_base loaded' do
509+
expect(Settings.to_h.keys.include?('secret_key_base')).to eq(false)
510+
end
511+
end
512+
end
513+
end
483514
end
484515
end

0 commit comments

Comments
 (0)