Skip to content

Commit d739948

Browse files
committed
[Fix rubocop#535] Fix an error for Rails/HasManyOrHasOneDependent
Fixes rubocop#535. This PR fixes an error for `Rails/HasManyOrHasOneDependent` when using lambda argument and not specifying any options.
1 parent 1d22b8a commit d739948

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
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+
* [#535](https://github.com/rubocop/rubocop-rails/issues/535): Fix an error for `Rails/HasManyOrHasOneDependent` when using lambda argument and not specifying any options. ([@koic][])
8+
59
## 2.12.0 (2021-09-09)
610

711
### New features

lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ def valid_options_in_with_options_block?(node)
100100
end
101101

102102
def contain_valid_options_in_with_options_block?(node)
103-
if with_options_block(node)
104-
return true if valid_options?(with_options_block(node))
103+
if (options = with_options_block(node))
104+
return true if valid_options?(options)
105105

106106
return false unless node.parent
107107

@@ -114,6 +114,8 @@ def contain_valid_options_in_with_options_block?(node)
114114
end
115115

116116
def valid_options?(options)
117+
return false if options.nil?
118+
117119
options = options.first.children.first.pairs if options.first.kwsplat_type?
118120

119121
return true unless options

spec/rubocop/cop/rails/has_many_or_has_one_dependent_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ 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+
2332
it 'does not register an offense when specifying `:dependent` strategy' do
2433
expect_no_offenses(<<~RUBY)
2534
class Person < ApplicationRecord

0 commit comments

Comments
 (0)