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 #147] Fix a false positive for Rails/HasManyOrHasOneDependent #467

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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* [#450](https://github.com/rubocop/rubocop-rails/issues/450): Fix a crash for `Rails/ContentTag` with nested content tags. ([@tejasbubane][])
* [#103](https://github.com/rubocop/rubocop-rails/issues/103): Fix a false positive for `Rails/FindEach` when not inheriting `ActiveRecord::Base` and using `all.each`. ([@koic][])
* [#466](https://github.com/rubocop/rubocop-rails/pull/466): Fix a false positive for `Rails/DynamicFindBy` when not inheriting `ApplicationRecord` and without no receiver. ([@koic][])
* [#147](https://github.com/rubocop/rubocop-rails/issues/147): Fix a false positive for `Rails/HasManyOrHasOneDependent` when specifying default `dependent: nil` strategy. ([@koic][])

### Changes

Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/cops_rails.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1586,6 +1586,7 @@ end
class User < ActiveRecord::Base
has_many :comments, dependent: :restrict_with_exception
has_one :avatar, dependent: :destroy
has_many :articles, dependent: nil
has_many :patients, through: :appointments
end
----
Expand Down
3 changes: 2 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 @@ -18,6 +18,7 @@ module Rails
# class User < ActiveRecord::Base
# has_many :comments, dependent: :restrict_with_exception
# has_one :avatar, dependent: :destroy
# has_many :articles, dependent: nil
# has_many :patients, through: :appointments
# end
class HasManyOrHasOneDependent < Base
Expand All @@ -37,7 +38,7 @@ class HasManyOrHasOneDependent < Base
PATTERN

def_node_matcher :dependent_option?, <<~PATTERN
(pair (sym :dependent) !nil)
(pair (sym :dependent) {!nil (nil)})
PATTERN

def_node_matcher :present_option?, <<~PATTERN
Expand Down
16 changes: 16 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 @@ -28,6 +28,14 @@ class Person < ApplicationRecord
RUBY
end

it 'does not register an offense when specifying default `dependent: nil` strategy' do
expect_no_offenses(<<~RUBY)
class Person < ApplicationRecord
has_one :foo, dependent: nil
end
RUBY
end

context 'with :through option' do
it 'does not register an offense for non-nil value' do
expect_no_offenses(<<~RUBY)
Expand Down Expand Up @@ -93,6 +101,14 @@ class Person < ApplicationRecord
expect_no_offenses('has_many :foo, dependent: :bar')
end

it 'does not register an offense when specifying default `dependent: nil` strategy' do
expect_no_offenses(<<~RUBY)
class Person < ApplicationRecord
has_many :foo, dependent: nil
end
RUBY
end

context 'with :through option' do
it 'does not register an offense for non-nil value' do
expect_no_offenses('has_many :foo, through: :bars')
Expand Down