Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix conflicts with Rails 7 active_support methods #347

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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