diff --git a/README.md b/README.md index d979634c..4e5c2714 100644 --- a/README.md +++ b/README.md @@ -154,6 +154,18 @@ Example production environment config file: #{Rails.root}/config/environments/production.yml ``` +### Extra sources + +You can load extra sources from the config folder during initialization by setting the `extra_sources` configuration option. + +```ruby +Config.setup do |config| + config.extra_sources = ['extra_settings'] +end +``` + +This will also overwrite the same config entries from the main file. + ### Developer specific config files If you want to have local settings, specific to your machine or development environment, you can use the following files, which are automatically `.gitignore` : diff --git a/lib/config.rb b/lib/config.rb index e98d1e70..a8de23aa 100644 --- a/lib/config.rb +++ b/lib/config.rb @@ -29,7 +29,8 @@ module Config merge_hash_arrays: false, validation_contract: nil, evaluate_erb_in_yaml: true, - environment: nil + environment: nil, + extra_sources: [] ) def self.setup @@ -63,6 +64,7 @@ def self.load_and_set_settings(*sources) def self.setting_files(config_root, env) [ File.join(config_root, "#{Config.file_name}.yml").to_s, + *Config.extra_sources.map { |source| File.join(config_root, "#{source}.yml") }, File.join(config_root, Config.dir_name, "#{env}.yml").to_s, File.join(config_root, 'environments', "#{env}.yml").to_s, *local_setting_files(config_root, env) @@ -72,6 +74,7 @@ def self.setting_files(config_root, env) def self.local_setting_files(config_root, env) [ (File.join(config_root, "#{Config.file_name}.local.yml").to_s if env != 'test'), + *Config.extra_sources.map { |source| File.join(config_root, "#{source}.local.yml") }, File.join(config_root, Config.dir_name, "#{env}.local.yml").to_s, File.join(config_root, 'environments', "#{env}.local.yml").to_s ].compact diff --git a/lib/generators/config/templates/config.rb b/lib/generators/config/templates/config.rb index f6a222d0..9614f267 100644 --- a/lib/generators/config/templates/config.rb +++ b/lib/generators/config/templates/config.rb @@ -65,4 +65,8 @@ # # config.file_name = 'settings' # config.dir_name = 'settings' + + # Load extra sources from config folder + # + # config.extra_sources = ['extra_settings'] end diff --git a/spec/config_spec.rb b/spec/config_spec.rb index 505c6081..95517d7c 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -38,6 +38,24 @@ expect(config.root['google.com']).to eq(3) end + it "should load extra_sources files" do + Config.setup do |config| + config.extra_sources = ['settings2'] + end + + config = Config.setting_files("root/config", "staging") + expect(config).to eq([ + 'root/config/settings.yml', + 'root/config/settings2.yml', + 'root/config/settings/staging.yml', + 'root/config/environments/staging.yml', + 'root/config/settings.local.yml', + 'root/config/settings2.local.yml', + 'root/config/settings/staging.local.yml', + 'root/config/environments/staging.local.yml' + ]) + end + it "should load 2 basic config files" do config = Config.load_files("#{fixture_path}/settings.yml", "#{fixture_path}/settings2.yml") expect(config.size).to eq(1) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ca0739e7..554223b2 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -87,6 +87,7 @@ def self.reset self.fail_on_missing = false self.file_name = 'settings' self.dir_name = 'settings' + self.extra_sources = [] instance_variable_set(:@_ran_once, false) end end