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

Support Thor::CoreExt::HashWithIndifferentAccess#except for Rails 6.0 #734

Merged
merged 1 commit into from
Feb 11, 2021
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
Support Thor::CoreExt::HashWithIndifferentAccess#except for Rails 6.0
This PR supports `Thor::CoreExt::HashWithIndifferentAccess#except`
and prevents breaking changes in Rails upgrades when using
`options.except(:key)` in Thor task.

When Thor is used with Rails (Active Support), the behavior changes as follows.

## With Rails 5.2 or lower

```ruby
h = Thor::CoreExt::HashWithIndifferentAccess.new(foo: 1, bar: 2)
h.except(:foo) #=> {"bar"=>2}
```

## With Rails 6.0

```ruby
h = Thor::CoreExt::HashWithIndifferentAccess.new(foo: 1, bar: 2)
h.except(:foo) #=> {"foo"=>1, "bar"=>2}
```

This difference behavior is due to the following changes in Rails 6.0.
rails/rails#35771

This PR makes the behavior between Rails 5.2 and Rails 6.0 compatible.
  • Loading branch information
koic committed Jul 30, 2020
commit 99a15e67ec2fb0e97cbbba684190f6514754b042
6 changes: 6 additions & 0 deletions lib/thor/core_ext/hash_with_indifferent_access.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ def delete(key)
super(convert_key(key))
end

def except(*keys)
dup.tap do |hash|
keys.each { |key| hash.delete(convert_key(key)) }
end
end

def fetch(key, *args)
super(convert_key(key), *args)
end
Expand Down
11 changes: 11 additions & 0 deletions spec/core_ext/hash_with_indifferent_access_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@
expect(@hash.delete(:foo)).to eq("bar")
end

it "supports except" do
unexcepted_hash = @hash.dup
@hash.except("foo")
expect(@hash).to eq(unexcepted_hash)

expect(@hash.except("foo")).to eq("baz" => "bee", "force" => true)
expect(@hash.except("foo", "baz")).to eq("force" => true)
expect(@hash.except(:foo)).to eq("baz" => "bee", "force" => true)
expect(@hash.except(:foo, :baz)).to eq("force" => true)
end

it "supports fetch" do
expect(@hash.fetch("foo")).to eq("bar")
expect(@hash.fetch("foo", nil)).to eq("bar")
Expand Down