Skip to content

Commit e14def2

Browse files
authored
Merge pull request #1159 from samrjenkins/fix-has-many-or-has-one-dependent-false-positive
[Fix #1158] Rails/HasManyOrHasOneDependent reports false positive when has_many or has_one called on explicit receiver
2 parents e0fd6e9 + f625711 commit e14def2

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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][])

lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class HasManyOrHasOneDependent < Base
6060
(block
6161
(send nil? :with_options
6262
(hash $...))
63-
(args) ...)
63+
(args _?) ...)
6464
PATTERN
6565

6666
def_node_matcher :association_extension_block?, <<~PATTERN

spec/rubocop/cop/rails/has_many_or_has_one_dependent_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ class Person < ApplicationRecord
9292
end
9393
RUBY
9494
end
95+
96+
it 'does not register an offense when has_one called on explicit receiver' do
97+
expect_no_offenses(<<~RUBY)
98+
class Person < ApplicationRecord
99+
with_options dependent: :destroy do |model|
100+
model.has_one :foo
101+
end
102+
end
103+
RUBY
104+
end
95105
end
96106
end
97107

@@ -188,6 +198,16 @@ class Person < ApplicationRecord
188198
RUBY
189199
end
190200

201+
it 'does not register an offense when has_many called on explicit receiver' do
202+
expect_no_offenses(<<~RUBY)
203+
class Person < ApplicationRecord
204+
with_options dependent: :destroy do |model|
205+
model.has_many :foo
206+
end
207+
end
208+
RUBY
209+
end
210+
191211
it "doesn't register an offense for `with_options dependent: :destroy` and for using association extension" do
192212
expect_no_offenses(<<~RUBY)
193213
class Person < ApplicationRecord

0 commit comments

Comments
 (0)