Skip to content

Commit

Permalink
[Fix #415] Make Rails/HasManyOrHasOneDependent accept association e…
Browse files Browse the repository at this point in the history
…xtension
  • Loading branch information
ohbarye committed Jan 6, 2021
1 parent 2d19e93 commit 0d10d98
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
8 changes: 7 additions & 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 @@ -51,6 +51,12 @@ class HasManyOrHasOneDependent < Base
(args) ...)
PATTERN

def_node_matcher :association_extension_block?, <<~PATTERN
(block
(send nil? :has_many _)
(args) ...)
PATTERN

def on_send(node)
return if active_resource?(node.parent)
return if !association_without_options?(node) && valid_options?(association_with_options?(node))
Expand All @@ -64,7 +70,7 @@ def on_send(node)
def valid_options_in_with_options_block?(node)
return true unless node.parent

n = node.parent.begin_type? ? node.parent.parent : node.parent
n = node.parent.begin_type? || association_extension_block?(node.parent) ? node.parent.parent : node.parent

contain_valid_options_in_with_options_block?(n)
end
Expand Down
13 changes: 13 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 @@ -132,6 +132,19 @@ class Person < ApplicationRecord
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
with_options dependent: :destroy do
has_many :foo do
def bar
end
end
end
end
RUBY
end

context 'Multiple associations' do
it "doesn't register an offense for " \
'`with_options dependent: :destroy`' do
Expand Down

0 comments on commit 0d10d98

Please sign in to comment.