From 81a4e892621a8e984aa5d201735effc6cb5d82f7 Mon Sep 17 00:00:00 2001 From: vovanmozg Date: Wed, 6 Mar 2024 19:42:15 +0100 Subject: [PATCH 1/4] Block reload config files in Railtie if they already uploaded --- lib/config/integrations/rails/railtie.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/config/integrations/rails/railtie.rb b/lib/config/integrations/rails/railtie.rb index 6748c116..2637949d 100644 --- a/lib/config/integrations/rails/railtie.rb +++ b/lib/config/integrations/rails/railtie.rb @@ -7,6 +7,8 @@ def preload initializer = ::Rails.root.join('config', 'initializers', 'config.rb') require initializer if File.exist?(initializer) + return if Object.const_defined?(Config.const_name) + # Parse the settings before any of the initializers Config.load_and_set_settings( Config.setting_files(::Rails.root.join('config'), Config.environment.nil? ? ::Rails.env : Config.environment.to_sym) From 7b12549fe3656842be1dfff922f67e454729ac95 Mon Sep 17 00:00:00 2001 From: vovanmozg Date: Thu, 7 Mar 2024 09:16:26 +0100 Subject: [PATCH 2/4] Add tests --- spec/config_spec.rb | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/spec/config_spec.rb b/spec/config_spec.rb index 505c6081..e7d9cc73 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -151,6 +151,46 @@ expect(Settings.size).to eq(2) end + describe "load_and_set_settings in the Rails context" do + if defined?(Rails) + let(:config_name) { Rails.root.join("config", "initializers", "config.rb") } + let(:default_settings_file_name) { Rails.root.join("config", "settings.yml") } + let(:custom_settings_file_name) { Rails.root.join("config", "settings_custom.yml") } + let(:spring_file_name_for_rails_6_1) { Rails.root.join("bin", "spring") } + + before do + File.write(default_settings_file_name, "default: default_value") + File.write(custom_settings_file_name, "custom: custom_value") + # workaround for Rails 6.1 + File.write(spring_file_name_for_rails_6_1, "") if Rails.version == "6.1.7.6" + end + + after do + File.delete(config_name) if File.exist?(config_name) + File.delete(default_settings_file_name) if File.exist?(default_settings_file_name) + File.delete(custom_settings_file_name) if File.exist?(custom_settings_file_name) + File.delete(spring_file_name_for_rails_6_1) if File.exist?(spring_file_name_for_rails_6_1) + end + + it "should read value only from default settings without config.rb" do + values = `#{Rails.root.join("bin", "rails")} runner 'print "\#{Settings.default}|\#{Settings.custom}"'` + expect(values).to eq("default_value|") + end + + it "should read value only from default settings without use of load_and_set_settings" do + File.write(config_name, "Config.setup { |config| }") + values = `#{Rails.root.join("bin", "rails")} runner 'print "\#{Settings.default}|\#{Settings.custom}"'` + expect(values).to eq("default_value|") + end + + it "should read value only from custom settings with use of load_and_set_settings" do + File.write(config_name, "Config.setup { |config| config.load_and_set_settings(['#{custom_settings_file_name}']) }") + values = `#{Rails.root.join("bin", "rails")} runner 'print "\#{Settings.default}|\#{Settings.custom}"'` + expect(values).to eq("|custom_value") + end + end + end + context "Nested Settings" do From 96d7be9b990345e91660f705ec5c3f58e5a693e8 Mon Sep 17 00:00:00 2001 From: vovanmozg Date: Sun, 31 Mar 2024 00:37:44 +0100 Subject: [PATCH 3/4] Add ENV recovery after clearing during test run It affects tests which execute `rails runner` --- spec/config_env_spec.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spec/config_env_spec.rb b/spec/config_env_spec.rb index a5eee612..9ddb151b 100644 --- a/spec/config_env_spec.rb +++ b/spec/config_env_spec.rb @@ -10,8 +10,14 @@ Config.load_files "#{fixture_path}/settings.yml", "#{fixture_path}/multilevel.yml" end + before :all do + ENV_BACKUP = ENV.to_hash + end + after :all do Config.use_env = false + + ENV_BACKUP.each { |k, v| ENV[k] = v } end before :each do From 5db7fc4d99f45fab837a2e3ea1bee89480442d4a Mon Sep 17 00:00:00 2001 From: vovanmozg Date: Sun, 31 Mar 2024 00:53:22 +0100 Subject: [PATCH 4/4] Add documentation --- README.md | 8 ++++++++ spec/config_spec.rb | 1 - 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 119580fe..0fa2cf9d 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,14 @@ You can now edit them to adjust to your needs. > Note: By default, the config environment will match the Rails environment (`Rails.env`). This can be changed by setting `config.environment`. +You can also define your own settings files and load them in the initializer: + +```ruby +Config.setup do |config| + config.load_and_set_settings("/path/to/yaml1", "/path/to/yaml2", ...) +end +``` + ### Installing on Padrino Add the gem to your `Gemfile` and run `bundle install` to install it. Then edit `app.rb` and register `Config` diff --git a/spec/config_spec.rb b/spec/config_spec.rb index e7d9cc73..d912453b 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -192,7 +192,6 @@ end - context "Nested Settings" do let(:config) do Config.load_files("#{fixture_path}/development.yml")