Skip to content

Commit 1f67343

Browse files
authored
Fix conflicts with Rails 7 active_support methods (#347)
1 parent a2fac25 commit 1f67343

4 files changed

Lines changed: 34 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Next
4+
5+
* Fix conflicts with Rails 7 active_support methods ([#347](https://github.com/rubyconfig/config/pull/339))
6+
37
## 5.0.0
48

59
### BREAKING CHANGES

lib/config/options.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,14 @@ def merge!(hash)
114114
# Some keywords that don't play nicely with OpenStruct
115115
SETTINGS_RESERVED_NAMES = %w[select collect test count zip min max exit! table].freeze
116116

117+
# Some keywords that don't play nicely with Rails 7.*
118+
RAILS_RESERVED_NAMES = %w[maximum minimum].freeze
119+
117120
# An alternative mechanism for property access.
118121
# This let's you do foo['bar'] along with foo.bar.
119122
def [](param)
120123
return super if SETTINGS_RESERVED_NAMES.include?(param)
124+
return super if RAILS_RESERVED_NAMES.include?(param)
121125
send("#{param}")
122126
end
123127

@@ -131,6 +135,12 @@ def []=(param, value)
131135
end
132136
end
133137

138+
RAILS_RESERVED_NAMES.each do |name|
139+
define_method name do
140+
self[name]
141+
end
142+
end
143+
134144
def key?(key)
135145
@table.key?(key)
136146
end

spec/fixtures/reserved_keywords.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# OpenStruct reserved keywords
12
select: apple
23
collect: banana
34
count: lemon
@@ -6,3 +7,7 @@ max: kumquat
67
min: fig
78
exit!: taro
89
table: strawberry
10+
11+
# Rails 7.* reserved keywords
12+
minimum: 10
13+
maximum: 20

spec/options_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@
4141
expect(config[:table]).to eq('strawberry')
4242
end
4343

44+
context 'when Settings file is using keywords reserved by Rails 7' do
45+
it 'should allow to access them via object member notation' do
46+
expect(config.maximum).to eq(20)
47+
expect(config.minimum).to eq(10)
48+
end
49+
50+
it 'should allow to access them using [] operator' do
51+
expect(config['maximum']).to eq(20)
52+
expect(config['minimum']).to eq(10)
53+
54+
expect(config[:maximum]).to eq(20)
55+
expect(config[:minimum]).to eq(10)
56+
end
57+
end
58+
4459
context 'when empty' do
4560
let(:config) do
4661
Config.load_files("#{fixture_path}/empty1.yml")

0 commit comments

Comments
 (0)