Skip to content

Commit

Permalink
[Fix rubocop#4909] Make Rails/HasManyOrHasOneDependent aware multip…
Browse files Browse the repository at this point in the history
…le associations

Fixes rubocop#4909. This commit tested and fixed the problem reproduction.
  • Loading branch information
koic committed Oct 31, 2017
1 parent af630aa commit 6a4ee87
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

* [#1583](https://github.com/bbatsov/rubocop/issues/1583): Add a quiet formatter. ([@drenmi][])
* [#4794](https://github.com/bbatsov/rubocop/issues/4794): Fix an error in `Layout/MultilineOperationIndentation` when an operation spans multiple lines and contains a ternary expression. ([@rrosenblum][])
* [#4909](https://github.com/bbatsov/rubocop/issues/4909): Make `Rails/HasManyOrHasOneDependent` aware of multiple associations in `with_options`. ([@koic][])

## 0.51.0 (2017-10-18)

Expand Down
8 changes: 6 additions & 2 deletions lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ class HasManyOrHasOneDependent < Cop
def on_send(node)
if !association_without_options?(node)
return if valid_options?(association_with_options?(node))
elsif with_options_block(node.parent)
return if valid_options?(with_options_block(node.parent))
else
n = node.parent.begin_type? ? node.parent.parent : node.parent

if with_options_block(n)
return if valid_options?(with_options_block(n))
end
end

add_offense(node, location: :selector)
Expand Down
14 changes: 14 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 @@ -113,6 +113,20 @@ class Person
end
RUBY
end

context 'Multiple associations' do
it "doesn't register an offense for " \
'`with_options dependent: :destroy`' do
expect_no_offenses(<<-RUBY.strip_indent)
class Person
with_options dependent: :destroy do
has_many :foo
has_many :bar
end
end
RUBY
end
end
end
end
end

0 comments on commit 6a4ee87

Please sign in to comment.