Skip to content

Commit

Permalink
Merge pull request #542 from koic/fix_a_false_positive_for_rails_has_…
Browse files Browse the repository at this point in the history
…many_or_has_one_dependent

[Fix #541] Fix a false positive for `Rails/HasManyOrHasOneDependent`
  • Loading branch information
koic authored Sep 11, 2021
2 parents 7607908 + 245bbbc commit ad0c558
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Bug fixes

* [#541](https://github.com/rubocop/rubocop-rails/issues/541): Fix an error for `Rails/HasManyOrHasOneDependent` when using lambda argument and specifying `:dependent` strategy. ([@koic][])

## 2.12.1 (2021-09-10)

### Bug fixes
Expand Down
2 changes: 1 addition & 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 @@ -45,7 +45,7 @@ class HasManyOrHasOneDependent < Base
PATTERN

def_node_matcher :association_with_options?, <<~PATTERN
(send nil? {:has_many :has_one} _ (hash $...))
(send nil? {:has_many :has_one} ... (hash $...))
PATTERN

def_node_matcher :dependent_option?, <<~PATTERN
Expand Down
34 changes: 25 additions & 9 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 @@ -20,15 +20,6 @@ class Person < ApplicationRecord
RUBY
end

it 'registers an offense when using lambda argument and not specifying any options' do
expect_offense(<<~RUBY)
class User < ApplicationRecord
has_one :articles, -> { where(active: true) }
^^^^^^^ Specify a `:dependent` option.
end
RUBY
end

it 'does not register an offense when specifying `:dependent` strategy' do
expect_no_offenses(<<~RUBY)
class Person < ApplicationRecord
Expand Down Expand Up @@ -114,10 +105,27 @@ class Person < ApplicationRecord
RUBY
end

it 'registers an offense when using lambda argument and not specifying any options' do
expect_offense(<<~RUBY)
class User < ApplicationRecord
has_many :articles, -> { where(active: true) }
^^^^^^^^ Specify a `:dependent` option.
end
RUBY
end

it 'does not register an offense when specifying `:dependent` strategy' do
expect_no_offenses('has_many :foo, dependent: :bar')
end

it 'does not register an offense when using lambda argument and specifying `:dependent` strategy' do
expect_no_offenses(<<~RUBY)
class User < ApplicationRecord
has_many :articles, -> { where(active: true) }, dependent: :destroy
end
RUBY
end

it 'does not register an offense when specifying default `dependent: nil` strategy' do
expect_no_offenses(<<~RUBY)
class Person < ApplicationRecord
Expand All @@ -131,6 +139,14 @@ class Person < ApplicationRecord
expect_no_offenses('has_many :foo, through: :bars')
end

it 'does not register an offense when using lambda argument and specifying non-nil `:through` option' do
expect_no_offenses(<<~RUBY)
class User < ApplicationRecord
has_many :activities, -> { order(created_at: :desc) }, through: :notes, source: :activities
end
RUBY
end

it 'registers an offense for nil value' do
expect_offense(<<~RUBY)
class Person < ApplicationRecord
Expand Down

0 comments on commit ad0c558

Please sign in to comment.