Skip to content

Commit eae29d8

Browse files
committed
[Fix #1319] Fix false positive for RedundantPresenceValidationOnBelongsTo
If presence is the only validation option and other non-validation options are present, removing it will cause rails to error.
1 parent 65d20a9 commit eae29d8

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1319](https://github.com/rubocop/rubocop-rails/issues/1319): Fix a false positive for `Rails/RedundantPresenceValidationOnBelongsTo` when removing `presence` would leave other non-validation options like `allow_blank` without validations. ([@earlopain][])

lib/rubocop/cop/rails/redundant_presence_validation_on_belongs_to.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ class RedundantPresenceValidationOnBelongsTo < Base
3939
MSG = 'Remove explicit presence validation for %<association>s.'
4040
RESTRICT_ON_SEND = %i[validates].freeze
4141

42+
# From https://github.com/rails/rails/blob/7a0bf93b9dd291c7f61121a41b3a813ac8857e6a/activemodel/lib/active_model/validations/validates.rb#L157-L159
43+
NON_VALIDATION_OPTIONS = %i[if unless on allow_blank allow_nil strict].freeze
44+
4245
minimum_target_rails_version 5.0
4346

4447
# @!method presence_validation?(node)
@@ -170,6 +173,12 @@ class RedundantPresenceValidationOnBelongsTo < Base
170173

171174
def on_send(node)
172175
presence_validation?(node) do |all_keys, options, presence|
176+
# If presence is the only validation option and other non-validation options
177+
# are present, removing it will cause rails to error.
178+
used_option_keys = options.keys.select(&:sym_type?).map(&:value)
179+
remaining_validations = used_option_keys - NON_VALIDATION_OPTIONS - [:presence]
180+
return if remaining_validations.none? && options.keys.length > 1
181+
173182
keys = non_optional_belongs_to(node.parent, all_keys)
174183
return if keys.none?
175184

spec/rubocop/cop/rails/redundant_presence_validation_on_belongs_to_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,13 @@ class Profile
278278
RUBY
279279
end
280280

281+
it 'does not register an offense with `if` and other validation option' do
282+
expect_no_offenses(<<~RUBY)
283+
belongs_to :user
284+
validates :user, presence: true, if: -> { condition }, numericality: true
285+
RUBY
286+
end
287+
281288
it 'does not register an offense with `unless` option' do
282289
expect_no_offenses(<<~RUBY)
283290
belongs_to :user
@@ -298,6 +305,13 @@ class Profile
298305
validates :user, presence: true, strict: MissingUserError
299306
RUBY
300307
end
308+
309+
it 'does not register an offense when other options are present but none are validations' do
310+
expect_no_offenses(<<~RUBY)
311+
belongs_to :user
312+
validates :user, presence: true, allow_blank: false
313+
RUBY
314+
end
301315
end
302316

303317
context 'Rails < 5.0', :rails42 do

0 commit comments

Comments
 (0)