Skip to content

Commit

Permalink
Merge pull request #416 from ohbarye/allow-combinatin-of-association-…
Browse files Browse the repository at this point in the history
…extension-and-with_options

[Fix #415] Make `Rails/HasManyOrHasOneDependent` accept association extension
  • Loading branch information
koic authored Jan 12, 2021
2 parents 6979832 + b74094d commit f2613d6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Changes

* [#409](https://github.com/rubocop-hq/rubocop-rails/pull/409): Deconstruct "table.column" in `Rails/WhereNot`. ([@mobilutz][])
* [#416](https://github.com/rubocop-hq/rubocop-rails/pull/416): Make `Rails/HasManyOrHasOneDependent` accept combination of association extension and `with_options`. ([@ohbarye][])

## 2.9.1 (2020-12-16)

Expand Down Expand Up @@ -329,3 +330,4 @@
[@Tietew]: https://github.com/Tietew
[@cilim]: https://github.com/cilim
[@flanger001]: https://github.com/flanger001
[@ohbarye]: https://github.com/ohbarye
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 f2613d6

Please sign in to comment.