Skip to content

Commit 2e22634

Browse files
[Fix rubocop#792] Fixes a false negative for Rails/RedundantPresenceValidationOnBelongsTo
1 parent 0ba3732 commit 2e22634

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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][])

lib/rubocop/cop/rails/redundant_presence_validation_on_belongs_to.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class RedundantPresenceValidationOnBelongsTo < Base
143143
def_node_matcher :belongs_to_without_fk?, <<~PATTERN
144144
{
145145
(send nil? :belongs_to (sym %1)) # belongs_to :user
146-
(send nil? :belongs_to (sym %1) !hash) # belongs_to :user, -> { not_deleted }
146+
(send nil? :belongs_to (sym %1) !hash ...) # belongs_to :user, -> { not_deleted }
147147
(send nil? :belongs_to (sym %1) !(hash <(pair (sym :foreign_key) _) ...>))
148148
}
149149
PATTERN

spec/rubocop/cop/rails/redundant_presence_validation_on_belongs_to_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,14 @@ class Profile
180180
RUBY
181181
end
182182

183+
it 'registers an offense for multiple attributes and options in belongs_to' do
184+
expect_offense(<<~RUBY)
185+
belongs_to :user, -> { where(foo: true) }, inverse_of: :employee
186+
validates :user_id, presence: true
187+
^^^^^^^^^^^^^^ Remove explicit presence validation for `user_id`.
188+
RUBY
189+
end
190+
183191
pending 'registers an offense even when the presence option is factored out' do
184192
expect_offense(<<~RUBY)
185193
belongs_to :user

0 commit comments

Comments
 (0)