From 9dcad4ab58590903bfee63b9844629947490a75b Mon Sep 17 00:00:00 2001 From: Feruz Oripov Date: Wed, 16 Aug 2023 17:59:27 +0500 Subject: [PATCH 1/3] use custom filename --- lib/config.rb | 10 ++++++---- lib/config/integrations/heroku.rb | 4 ++-- lib/config/version.rb | 2 +- lib/generators/config/install_generator.rb | 12 ++++++------ lib/generators/config/templates/config.rb | 5 +++++ spec/config_spec.rb | 17 +++++++++++++++++ 6 files changed, 37 insertions(+), 13 deletions(-) diff --git a/lib/config.rb b/lib/config.rb index 4713f8ba..23dfcf52 100644 --- a/lib/config.rb +++ b/lib/config.rb @@ -19,6 +19,8 @@ module Config env_converter: :downcase, env_parse_values: true, fail_on_missing: false, + file_name: 'settings', + dir_name: 'settings', # deep_merge options knockout_prefix: nil, merge_nil_values: true, @@ -58,8 +60,8 @@ def self.load_and_set_settings(*sources) def self.setting_files(config_root, env) [ - File.join(config_root, 'settings.yml').to_s, - File.join(config_root, 'settings', "#{env}.yml").to_s, + File.join(config_root, "#{Config.file_name}.yml").to_s, + 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) ].freeze @@ -67,8 +69,8 @@ def self.setting_files(config_root, env) def self.local_setting_files(config_root, env) [ - (File.join(config_root, 'settings.local.yml').to_s if env != 'test'), - File.join(config_root, 'settings', "#{env}.local.yml").to_s, + (File.join(config_root, "#{Config.file_name}.local.yml").to_s if env != 'test'), + File.join(config_root, Config.dir_name, "#{env}.local.yml").to_s, File.join(config_root, 'environments', "#{env}.local.yml").to_s ].compact end diff --git a/lib/config/integrations/heroku.rb b/lib/config/integrations/heroku.rb index 8e41bc73..914893a6 100644 --- a/lib/config/integrations/heroku.rb +++ b/lib/config/integrations/heroku.rb @@ -14,8 +14,8 @@ def invoke def vars # Load only local options to Heroku Config.load_and_set_settings( - Rails.root.join("config", "settings.local.yml").to_s, - Rails.root.join("config", "settings", "#{environment}.local.yml").to_s, + Rails.root.join("config", "#{Config.file_name}.local.yml").to_s, + Rails.root.join("config", Config.dir_name, "#{environment}.local.yml").to_s, Rails.root.join("config", "environments", "#{environment}.local.yml").to_s ) diff --git a/lib/config/version.rb b/lib/config/version.rb index a2db3069..ce898cdf 100644 --- a/lib/config/version.rb +++ b/lib/config/version.rb @@ -1,3 +1,3 @@ module Config - VERSION = '4.2.1'.freeze + VERSION = '4.3.1'.freeze end diff --git a/lib/generators/config/install_generator.rb b/lib/generators/config/install_generator.rb index 827e8ac1..3b183731 100644 --- a/lib/generators/config/install_generator.rb +++ b/lib/generators/config/install_generator.rb @@ -12,18 +12,18 @@ def copy_initializer end def copy_settings - template "settings.yml", "config/settings.yml" - template "settings.local.yml", "config/settings.local.yml" - directory "settings", "config/settings" + template "settings.yml", "config/#{Config.file_name}.yml" + template "settings.local.yml", "config/#{Config.file_name}.local.yml" + directory "settings", "config/#{Config.dir_name}" end def modify_gitignore create_file '.gitignore' unless File.exist? '.gitignore' append_to_file '.gitignore' do - "\n" + - "config/settings.local.yml\n" + - "config/settings/*.local.yml\n" + + "\n" + + "config/#{Config.file_name}.local.yml\n" + + "config/#{Config.dir_name}/*.local.yml\n" + "config/environments/*.local.yml\n" end end diff --git a/lib/generators/config/templates/config.rb b/lib/generators/config/templates/config.rb index 46ccb7b9..7bf6acd4 100644 --- a/lib/generators/config/templates/config.rb +++ b/lib/generators/config/templates/config.rb @@ -55,4 +55,9 @@ # Evaluate ERB in YAML config files at load time. # # config.evaluate_erb_in_yaml = true + + # Name of directory and file to store config keys + # + # config.file_name = 'settings.yml' + # config.dir_name = 'settings' end diff --git a/spec/config_spec.rb b/spec/config_spec.rb index 9ba5f21a..505c6081 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -206,6 +206,23 @@ expect(Config.const_name).to eq("Settings2") end + + context "filename and directory" do + it "should allow to use custom names" do + Config.setup do |config| + config.file_name = "custom_file" + config.dir_name = "custom_dir" + end + + expect(Config.file_name).to eq("custom_file") + expect(Config.dir_name).to eq("custom_dir") + end + + it "should have default values" do + expect(Config.file_name).to eq("settings") + expect(Config.dir_name).to eq("settings") + end + end end context "Settings with a type value of 'hash'" do From 38c70f5f09b98f5254e61792dd74502db6b7c8db Mon Sep 17 00:00:00 2001 From: Feruz Oripov Date: Thu, 17 Aug 2023 11:22:32 +0500 Subject: [PATCH 2/3] update README && revert version --- README.md | 2 ++ lib/config/version.rb | 2 +- spec/spec_helper.rb | 2 ++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 32e05ff4..ba23315e 100644 --- a/README.md +++ b/README.md @@ -270,6 +270,8 @@ After installing `Config` in Rails, you will find automatically generated file t * `const_name` - name of the object holing you settings. Default: `'Settings'` * `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'` +* `dir_name` - name of the directory to store environment-specific files. Default: `'settings'` ### Merge customization diff --git a/lib/config/version.rb b/lib/config/version.rb index ce898cdf..a2db3069 100644 --- a/lib/config/version.rb +++ b/lib/config/version.rb @@ -1,3 +1,3 @@ module Config - VERSION = '4.3.1'.freeze + VERSION = '4.2.1'.freeze end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2f1c7075..55149dc9 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -78,6 +78,8 @@ def self.reset self.schema = nil self.validation_contract = nil self.fail_on_missing = false + self.file_name = 'settings' + self.dir_name = 'settings' instance_variable_set(:@_ran_once, false) end end From ce6ce5f4ad8d6ca72544f0e6b80ff613d188b07f Mon Sep 17 00:00:00 2001 From: Feruz Oripov Date: Thu, 17 Aug 2023 11:29:02 +0500 Subject: [PATCH 3/3] update README --- README.md | 4 ++-- lib/generators/config/templates/config.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ba23315e..fcecf3e4 100644 --- a/README.md +++ b/README.md @@ -270,8 +270,8 @@ After installing `Config` in Rails, you will find automatically generated file t * `const_name` - name of the object holing you settings. Default: `'Settings'` * `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'` -* `dir_name` - name of the directory to store environment-specific files. Default: `'settings'` +* `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/` ### Merge customization diff --git a/lib/generators/config/templates/config.rb b/lib/generators/config/templates/config.rb index 7bf6acd4..6ea74ecc 100644 --- a/lib/generators/config/templates/config.rb +++ b/lib/generators/config/templates/config.rb @@ -58,6 +58,6 @@ # Name of directory and file to store config keys # - # config.file_name = 'settings.yml' + # config.file_name = 'settings' # config.dir_name = 'settings' end