Skip to content

Commit

Permalink
[Fix rubocop#792] Fixes a false negative for Rails/RedundantPresenceV…
Browse files Browse the repository at this point in the history
…alidationOnBelongsTo
  • Loading branch information
PedroAugustoRamalhoDuarte committed Oct 16, 2022
1 parent 0ba3732 commit cc0d47d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* [#792](https://github.com/rubocop/rubocop-rails/issues/792): Fix a false negative
for `Rails/RedundantPresenceValidationOnBelongsTo` when belongs_to have 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 cc0d47d

Please sign in to comment.