Sha256: 486829efd70d65eb640639297c599e77c54654c90036cd4a4ce7f1508cda744e

Contents?: true

Size: 935 Bytes

Versions: 3

Compression:

Stored size: 935 Bytes

Contents

module ActiveRecord
  module Validations
    class AbsenceValidator < ActiveModel::Validations::AbsenceValidator # :nodoc:
      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
      end
    end

    module ClassMethods
      # Validates that the specified attributes are not present (as defined by
      # Object#present?). If the attribute is an association, the associated object
      # is considered absent if it was marked for destruction.
      #
      # See ActiveModel::Validations::HelperMethods.validates_absence_of for more information.
      def validates_absence_of(*attr_names)
        validates_with AbsenceValidator, _merge_attributes(attr_names)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
activerecord-5.0.0.beta2 lib/active_record/validations/absence.rb
activerecord-5.0.0.beta1.1 lib/active_record/validations/absence.rb
activerecord-5.0.0.beta1 lib/active_record/validations/absence.rb