Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #415] Make Rails/HasManyOrHasOneDependent accept association extension #416

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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