Skip to content

Commit

Permalink
[Fix rubocop#528] Fix a false positive for `Rails/HasManyOrHasOneDepe…
Browse files Browse the repository at this point in the history
…ndent`

Fixes rubocop#528.

This PR fixes a false positive for `Rails/HasManyOrHasOneDependent`
when specifying `:dependent` strategy with double splat.
  • Loading branch information
koic committed Aug 31, 2021
1 parent 3e86c7b commit 68372fb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
* [#520](https://github.com/rubocop/rubocop-rails/pull/520): Support auto-correction for `Rails/ScopeArgs`. ([@koic][])
* [#524](https://github.com/rubocop/rubocop-rails/pull/524): Add new `Rails/RedundantTravelBack` cop. ([@koic][])

### Bug fixes

* [#528](https://github.com/rubocop/rubocop-rails/issues/528): Fix a false positive for `Rails/HasManyOrHasOneDependent` when specifying `:dependent` strategy with double splat. ([@koic][])

## Changes

* [#260](https://github.com/rubocop/rubocop-rails/issues/260): Change target of `Rails/ContentTag` from `content_tag` method to `tag` method. ([@tabuchi0919][])
Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ def contain_valid_options_in_with_options_block?(node)
end

def valid_options?(options)
options = options.first.children.first.pairs if options.first.kwsplat_type?

return true unless options
return true if options.any? do |o|
dependent_option?(o) || present_option?(o)
Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/rails/has_many_or_has_one_dependent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ class Person < ApplicationRecord
RUBY
end

it 'does not register an offense when specifying `:dependent` strategy with double splat' do
expect_no_offenses(<<~RUBY)
class Person < ApplicationRecord
has_one :foo, **{dependent: :destroy}
end
RUBY
end

it 'does not register an offense when specifying default `dependent: nil` strategy' do
expect_no_offenses(<<~RUBY)
class Person < ApplicationRecord
Expand Down

0 comments on commit 68372fb

Please sign in to comment.