Class: ActiveModel::Validations::AfterValidator

Inherits:
EachValidator
  • Object
show all
Defined in:
lib/can_has_validations/validators/ordering_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/can_has_validations/validators/ordering_validator.rb', line 27

def validate_each(record, attribute, value)
  compare_to = Array.wrap(options[:value_of] || options[:values_of] || options[:in] || options[:with])
  compare_to.each do |attr_name|
    lesser = attr_name.call(record) if attr_name.respond_to?(:call)
    lesser ||= Time.now if attr_name==:now && !record.respond_to?(:now)
    lesser ||= record.send attr_name
    next unless value && lesser
    unless value > lesser
      attr2 = attr_name.respond_to?(:call) ? 'it is' : record.class.human_attribute_name(attr_name)
      record.errors.add(attribute, :after, **options.except(:after).merge!(attribute2: attr2, value: value))
    end
  end
end