mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
refactor, don't duplicate presence validator logic.
This is a small refactoring that simplifies the Active Record specific lenght validator.
This commit is contained in:
parent
792de3f1ed
commit
006a4d787e
1 changed files with 4 additions and 10 deletions
|
@ -1,18 +1,12 @@
|
|||
module ActiveRecord
|
||||
module Validations
|
||||
class PresenceValidator < ActiveModel::Validations::PresenceValidator # :nodoc:
|
||||
def validate(record)
|
||||
def validate_each(record, attribute, association_or_value)
|
||||
return unless should_validate?(record)
|
||||
if record.class._reflect_on_association(attribute)
|
||||
association_or_value = Array.wrap(association_or_value).reject(&:marked_for_destruction?)
|
||||
end
|
||||
super
|
||||
attributes.each do |attribute|
|
||||
next unless record.class._reflect_on_association(attribute)
|
||||
associated_records = Array.wrap(record.send(attribute))
|
||||
|
||||
# Superclass validates presence. Ensure present records aren't about to be destroyed.
|
||||
if associated_records.present? && associated_records.all?(&:marked_for_destruction?)
|
||||
record.errors.add(attribute, :blank, options)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue