Skip to content

Commit a24c238

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

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-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

+32
Original file line numberDiff line numberDiff line change
@@ -480,5 +480,37 @@
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+
puts Settings
492+
expect(Settings.keys.include?('secret_key_base')).to eq(true)
493+
end
494+
495+
496+
context 'use_rails_credentials is false' do
497+
let(:config) do
498+
Config.use_rails_credentials = false
499+
end
500+
501+
it 'shoud have secret_key_base loaded' do
502+
expect(Settings.keys.include?('secret_key_base')).to eq(false)
503+
end
504+
end
505+
end
506+
507+
unless defined?(::Rails)
508+
context 'when not using rails' do
509+
it 'shoud have secret_key_base loaded' do
510+
expect(Settings.keys.include?('secret_key_base')).to eq(false)
511+
end
512+
end
513+
end
514+
end
483515
end
484516
end

0 commit comments

Comments
 (0)