Skip to content

Commit

Permalink
Merge pull request #1159 from samrjenkins/fix-has-many-or-has-one-dep…
Browse files Browse the repository at this point in the history
…endent-false-positive

[Fix #1158] Rails/HasManyOrHasOneDependent reports false positive when has_many or has_one called on explicit receiver
  • Loading branch information
koic committed Oct 18, 2023
2 parents e0fd6e9 + f625711 commit e14def2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1158](https://github.com/rubocop/rubocop-rails/issues/1158): `Rails/HasManyOrHasOneDependent` does not add offence when has_many or has_one is called on an explicit receiver. ([@samrjenkins][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class HasManyOrHasOneDependent < Base
(block
(send nil? :with_options
(hash $...))
(args) ...)
(args _?) ...)
PATTERN

def_node_matcher :association_extension_block?, <<~PATTERN
Expand Down
20 changes: 20 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 @@ -92,6 +92,16 @@ class Person < ApplicationRecord
end
RUBY
end

it 'does not register an offense when has_one called on explicit receiver' do
expect_no_offenses(<<~RUBY)
class Person < ApplicationRecord
with_options dependent: :destroy do |model|
model.has_one :foo
end
end
RUBY
end
end
end

Expand Down Expand Up @@ -188,6 +198,16 @@ class Person < ApplicationRecord
RUBY
end

it 'does not register an offense when has_many called on explicit receiver' do
expect_no_offenses(<<~RUBY)
class Person < ApplicationRecord
with_options dependent: :destroy do |model|
model.has_many :foo
end
end
RUBY
end

it "doesn't register an offense for `with_options dependent: :destroy` and for using association extension" do
expect_no_offenses(<<~RUBY)
class Person < ApplicationRecord
Expand Down

0 comments on commit e14def2

Please sign in to comment.