Skip to content

Commit 245bbbc

Browse files
committed
[Fix rubocop#541] Fix a false positive for Rails/HasManyOrHasOneDependent
Fixes rubocop#541. This PR fixes a false positive for `Rails/HasManyOrHasOneDependent` when using lambda argument and specifying `:dependent` strategy.
1 parent 7607908 commit 245bbbc

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## master (unreleased)
44

5+
### Bug fixes
6+
7+
* [#541](https://github.com/rubocop/rubocop-rails/issues/541): Fix an error for `Rails/HasManyOrHasOneDependent` when using lambda argument and specifying `:dependent` strategy. ([@koic][])
8+
59
## 2.12.1 (2021-09-10)
610

711
### Bug fixes

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
@@ -45,7 +45,7 @@ class HasManyOrHasOneDependent < Base
4545
PATTERN
4646

4747
def_node_matcher :association_with_options?, <<~PATTERN
48-
(send nil? {:has_many :has_one} _ (hash $...))
48+
(send nil? {:has_many :has_one} ... (hash $...))
4949
PATTERN
5050

5151
def_node_matcher :dependent_option?, <<~PATTERN

spec/rubocop/cop/rails/has_many_or_has_one_dependent_spec.rb

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,6 @@ class Person < ApplicationRecord
2020
RUBY
2121
end
2222

23-
it 'registers an offense when using lambda argument and not specifying any options' do
24-
expect_offense(<<~RUBY)
25-
class User < ApplicationRecord
26-
has_one :articles, -> { where(active: true) }
27-
^^^^^^^ Specify a `:dependent` option.
28-
end
29-
RUBY
30-
end
31-
3223
it 'does not register an offense when specifying `:dependent` strategy' do
3324
expect_no_offenses(<<~RUBY)
3425
class Person < ApplicationRecord
@@ -114,10 +105,27 @@ class Person < ApplicationRecord
114105
RUBY
115106
end
116107

108+
it 'registers an offense when using lambda argument and not specifying any options' do
109+
expect_offense(<<~RUBY)
110+
class User < ApplicationRecord
111+
has_many :articles, -> { where(active: true) }
112+
^^^^^^^^ Specify a `:dependent` option.
113+
end
114+
RUBY
115+
end
116+
117117
it 'does not register an offense when specifying `:dependent` strategy' do
118118
expect_no_offenses('has_many :foo, dependent: :bar')
119119
end
120120

121+
it 'does not register an offense when using lambda argument and specifying `:dependent` strategy' do
122+
expect_no_offenses(<<~RUBY)
123+
class User < ApplicationRecord
124+
has_many :articles, -> { where(active: true) }, dependent: :destroy
125+
end
126+
RUBY
127+
end
128+
121129
it 'does not register an offense when specifying default `dependent: nil` strategy' do
122130
expect_no_offenses(<<~RUBY)
123131
class Person < ApplicationRecord
@@ -131,6 +139,14 @@ class Person < ApplicationRecord
131139
expect_no_offenses('has_many :foo, through: :bars')
132140
end
133141

142+
it 'does not register an offense when using lambda argument and specifying non-nil `:through` option' do
143+
expect_no_offenses(<<~RUBY)
144+
class User < ApplicationRecord
145+
has_many :activities, -> { order(created_at: :desc) }, through: :notes, source: :activities
146+
end
147+
RUBY
148+
end
149+
134150
it 'registers an offense for nil value' do
135151
expect_offense(<<~RUBY)
136152
class Person < ApplicationRecord

0 commit comments

Comments
 (0)