From 81a4e892621a8e984aa5d201735effc6cb5d82f7 Mon Sep 17 00:00:00 2001 From: vovanmozg Date: Wed, 6 Mar 2024 19:42:15 +0100 Subject: [PATCH 1/8] 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/8] 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/8] 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/8] 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") From 1856702e5ec62b8e534b6e3e0c0bbdee52faf5e1 Mon Sep 17 00:00:00 2001 From: Nuzair46 Date: Thu, 21 Aug 2025 19:07:22 +0900 Subject: [PATCH 5/8] Fix test failures --- spec/app/rails_6.1/config/boot.rb | 1 + spec/app/rails_7.0/config/boot.rb | 1 + spec/config_spec.rb | 2 +- 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/spec/app/rails_6.1/config/boot.rb b/spec/app/rails_6.1/config/boot.rb index 3cda23b4..e724cfb2 100644 --- a/spec/app/rails_6.1/config/boot.rb +++ b/spec/app/rails_6.1/config/boot.rb @@ -2,3 +2,4 @@ require "bundler/setup" # Set up gems listed in the Gemfile. require "bootsnap/setup" # Speed up boot time by caching expensive operations. +require "logger" # Fix for Ruby 3.3 compatibility with ActiveSupport 6.1.7.10 diff --git a/spec/app/rails_7.0/config/boot.rb b/spec/app/rails_7.0/config/boot.rb index 988a5ddc..504244c5 100644 --- a/spec/app/rails_7.0/config/boot.rb +++ b/spec/app/rails_7.0/config/boot.rb @@ -2,3 +2,4 @@ require "bundler/setup" # Set up gems listed in the Gemfile. require "bootsnap/setup" # Speed up boot time by caching expensive operations. +require "logger" # Fix for Ruby 3.3 compatibility with ActiveSupport 6.1.7.10 diff --git a/spec/config_spec.rb b/spec/config_spec.rb index 175d9d1d..c2702df4 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -186,7 +186,7 @@ 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" + File.write(spring_file_name_for_rails_6_1, "") if Rails.version.start_with?("6.1") end after do From 98ecbfedd8170c0200fe3afd3ec2ab991a677437 Mon Sep 17 00:00:00 2001 From: Nuzair46 Date: Thu, 21 Aug 2025 19:19:12 +0900 Subject: [PATCH 6/8] fix config_availabe? in rails 5.x --- spec/app/rails_5.2/config/application.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/app/rails_5.2/config/application.rb b/spec/app/rails_5.2/config/application.rb index 673a427f..66d03134 100644 --- a/spec/app/rails_5.2/config/application.rb +++ b/spec/app/rails_5.2/config/application.rb @@ -19,6 +19,6 @@ class Application < Rails::Application ## # Config # - config_available? + # config_available? # This is only available in test context end end From 70c2509a5f14e398a3619fa0d9a9541ba482bc53 Mon Sep 17 00:00:00 2001 From: Nuzair46 Date: Thu, 21 Aug 2025 19:23:57 +0900 Subject: [PATCH 7/8] Added more logger --- spec/app/rails_5.2/config/boot.rb | 1 + spec/app/rails_6.0/config/boot.rb | 1 + spec/app/rails_6.1/config/boot.rb | 2 +- spec/app/rails_7.0/config/boot.rb | 2 +- 4 files changed, 4 insertions(+), 2 deletions(-) diff --git a/spec/app/rails_5.2/config/boot.rb b/spec/app/rails_5.2/config/boot.rb index b9e460ce..2a8971f5 100644 --- a/spec/app/rails_5.2/config/boot.rb +++ b/spec/app/rails_5.2/config/boot.rb @@ -2,3 +2,4 @@ require 'bundler/setup' # Set up gems listed in the Gemfile. require 'bootsnap/setup' # Speed up boot time by caching expensive operations. +require "logger" diff --git a/spec/app/rails_6.0/config/boot.rb b/spec/app/rails_6.0/config/boot.rb index b9e460ce..2a8971f5 100644 --- a/spec/app/rails_6.0/config/boot.rb +++ b/spec/app/rails_6.0/config/boot.rb @@ -2,3 +2,4 @@ require 'bundler/setup' # Set up gems listed in the Gemfile. require 'bootsnap/setup' # Speed up boot time by caching expensive operations. +require "logger" diff --git a/spec/app/rails_6.1/config/boot.rb b/spec/app/rails_6.1/config/boot.rb index e724cfb2..676662ab 100644 --- a/spec/app/rails_6.1/config/boot.rb +++ b/spec/app/rails_6.1/config/boot.rb @@ -2,4 +2,4 @@ require "bundler/setup" # Set up gems listed in the Gemfile. require "bootsnap/setup" # Speed up boot time by caching expensive operations. -require "logger" # Fix for Ruby 3.3 compatibility with ActiveSupport 6.1.7.10 +require "logger" diff --git a/spec/app/rails_7.0/config/boot.rb b/spec/app/rails_7.0/config/boot.rb index 504244c5..b7d101e1 100644 --- a/spec/app/rails_7.0/config/boot.rb +++ b/spec/app/rails_7.0/config/boot.rb @@ -2,4 +2,4 @@ require "bundler/setup" # Set up gems listed in the Gemfile. require "bootsnap/setup" # Speed up boot time by caching expensive operations. -require "logger" # Fix for Ruby 3.3 compatibility with ActiveSupport 6.1.7.10 +require "logger" From 427c95099d3aa6127ef75f42a7d54e445644af2b Mon Sep 17 00:00:00 2001 From: Nuzair46 Date: Fri, 29 Aug 2025 14:32:41 +0900 Subject: [PATCH 8/8] review changes --- spec/config_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/config_spec.rb b/spec/config_spec.rb index c2702df4..b7963381 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -186,7 +186,7 @@ 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.start_with?("6.1") + File.write(spring_file_name_for_rails_6_1, "") if Gem::Version.new(Rails.version) >= Gem::Version.new('6.1.0') end after do