Skip to content

Commit

Permalink
Fix conflicts with Rails 7 active_support methods (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
ikael21 authored Dec 18, 2023
1 parent a2fac25 commit 1f67343
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 10 additions & 0 deletions lib/config/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions spec/fixtures/reserved_keywords.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# OpenStruct reserved keywords
select: apple
collect: banana
count: lemon
Expand All @@ -6,3 +7,7 @@ max: kumquat
min: fig
exit!: taro
table: strawberry

# Rails 7.* reserved keywords
minimum: 10
maximum: 20
15 changes: 15 additions & 0 deletions spec/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 1f67343

Please sign in to comment.