Skip to content

Commit

Permalink
feat: add rails crendentails support
Browse files Browse the repository at this point in the history
  • Loading branch information
noxasch committed Mar 8, 2024
1 parent 89b7982 commit 61810ff
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ After installing `Config` in Rails, you will find automatically generated file t
* `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`
* `file_name` - name of the file to store general keys accessible in all environments. Default: `'settings'` - located at `config/settings.yml`
* `dir_name` - name of the directory to store environment-specific files. Default: `'settings'` - located at `config/settings/`
* `use_rails_credentials` - evaluate Rails credentials if loaded with `RAILS_MASTER_KEY` or `config/master.key`

### Merge customization

Expand Down
7 changes: 6 additions & 1 deletion lib/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ module Config
overwrite_arrays: true,
merge_hash_arrays: false,
validation_contract: nil,
evaluate_erb_in_yaml: true
evaluate_erb_in_yaml: true,
use_rails_credentials: false
)

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

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

if defined?(::Rails::Railtie) && Config.use_rails_credentials
config.add_source!(Rails.application.credentials.to_h.deep_stringify_keys)
end

config.load!
config
end
Expand Down
31 changes: 31 additions & 0 deletions spec/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -480,5 +480,36 @@
end

end

context 'rails crendentials' do
let(:config) do
Config.use_rails_credentials = true
end

if defined?(::Rails)
it 'shoud have secret_key_base loaded' do
expect(Settings.to_h.keys.include?('secret_key_base')).to eq(true)
end


context 'use_rails_credentials is false' do
let(:config) do
Config.use_rails_credentials = false
end

it 'shoud have secret_key_base loaded' do
expect(Settings.to_h.keys.include?('secret_key_base')).to eq(false)
end
end
end

unless defined?(::Rails)
context 'when not using rails' do
it 'shoud have secret_key_base loaded' do
expect(Settings.to_h.keys.include?('secret_key_base')).to eq(false)
end
end
end
end
end
end

0 comments on commit 61810ff

Please sign in to comment.