diff --git a/CHANGELOG.md b/CHANGELOG.md index 67857f17..6ba05593 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Next + +* Fix conflicts with Rails 7 active_support methods ([#347](https://github.com/rubyconfig/config/pull/339)) + ## 5.0.0 ### BREAKING CHANGES diff --git a/lib/config/options.rb b/lib/config/options.rb index 8a510a81..08f3d409 100644 --- a/lib/config/options.rb +++ b/lib/config/options.rb @@ -114,10 +114,14 @@ def merge!(hash) # Some keywords that don't play nicely with OpenStruct SETTINGS_RESERVED_NAMES = %w[select collect test count zip min max exit! table].freeze + # Some keywords that don't play nicely with Rails 7.* + RAILS_RESERVED_NAMES = %w[maximum minimum].freeze + # An alternative mechanism for property access. # This let's you do foo['bar'] along with foo.bar. def [](param) return super if SETTINGS_RESERVED_NAMES.include?(param) + return super if RAILS_RESERVED_NAMES.include?(param) send("#{param}") end @@ -131,6 +135,12 @@ def []=(param, value) end end + RAILS_RESERVED_NAMES.each do |name| + define_method name do + self[name] + end + end + def key?(key) @table.key?(key) end diff --git a/spec/fixtures/reserved_keywords.yml b/spec/fixtures/reserved_keywords.yml index 74e851bf..134941b1 100644 --- a/spec/fixtures/reserved_keywords.yml +++ b/spec/fixtures/reserved_keywords.yml @@ -1,3 +1,4 @@ +# OpenStruct reserved keywords select: apple collect: banana count: lemon @@ -6,3 +7,7 @@ max: kumquat min: fig exit!: taro table: strawberry + +# Rails 7.* reserved keywords +minimum: 10 +maximum: 20 diff --git a/spec/options_spec.rb b/spec/options_spec.rb index 82eeef4a..ff65cc61 100644 --- a/spec/options_spec.rb +++ b/spec/options_spec.rb @@ -41,6 +41,21 @@ expect(config[:table]).to eq('strawberry') end + context 'when Settings file is using keywords reserved by Rails 7' do + it 'should allow to access them via object member notation' do + expect(config.maximum).to eq(20) + expect(config.minimum).to eq(10) + end + + it 'should allow to access them using [] operator' do + expect(config['maximum']).to eq(20) + expect(config['minimum']).to eq(10) + + expect(config[:maximum]).to eq(20) + expect(config[:minimum]).to eq(10) + end + end + context 'when empty' do let(:config) do Config.load_files("#{fixture_path}/empty1.yml")