Skip to content

Commit

Permalink
Merge pull request #814 from PedroAugustoRamalhoDuarte/fix/fixes_fals…
Browse files Browse the repository at this point in the history
…e_negative_for_rails_redundant_presence_validation_on_belongs_to

[Fix #792] Fixes a false negative for Rails/RedundantPresenceValidationOnBelongsTo
  • Loading branch information
koic authored Oct 19, 2022
2 parents 558f55f + 2e22634 commit e7f9546
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#792](https://github.com/rubocop/rubocop-rails/issues/792): Fix a false negative for `Rails/RedundantPresenceValidationOnBelongsTo` when belongs_to at least one block and one hash like `belongs_to :company, -> { where(foo: true) }, inverse_of: :employee`. ([@PedroAugustoRamalhoDuarte][])
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class RedundantPresenceValidationOnBelongsTo < Base
def_node_matcher :belongs_to_without_fk?, <<~PATTERN
{
(send nil? :belongs_to (sym %1)) # belongs_to :user
(send nil? :belongs_to (sym %1) !hash) # belongs_to :user, -> { not_deleted }
(send nil? :belongs_to (sym %1) !hash ...) # belongs_to :user, -> { not_deleted }
(send nil? :belongs_to (sym %1) !(hash <(pair (sym :foreign_key) _) ...>))
}
PATTERN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ class Profile
RUBY
end

it 'registers an offense for multiple attributes and options in belongs_to' do
expect_offense(<<~RUBY)
belongs_to :user, -> { where(foo: true) }, inverse_of: :employee
validates :user_id, presence: true
^^^^^^^^^^^^^^ Remove explicit presence validation for `user_id`.
RUBY
end

pending 'registers an offense even when the presence option is factored out' do
expect_offense(<<~RUBY)
belongs_to :user
Expand Down

0 comments on commit e7f9546

Please sign in to comment.