Class: ActiveModel::Validations::ComparisonValidator

Inherits:
EachValidator show all
Includes:
Comparability, ResolveValue
Defined in:
lib/active_model/validations/comparison.rb

Overview

:nodoc:

Constant Summary

Constants included from Comparability

ActiveModel::Validations::Comparability::COMPARE_CHECKS

Instance Attribute Summary

Attributes inherited from EachValidator

#attributes

Attributes inherited from ActiveModel::Validator

#options

Instance Method Summary collapse

Methods included from ResolveValue

#resolve_value

Methods included from Comparability

#error_options

Methods inherited from EachValidator

#initialize, #validate

Methods inherited from ActiveModel::Validator

#initialize, kind, #kind, #validate

Constructor Details

This class inherits a constructor from ActiveModel::EachValidator

Instance Method Details

#check_validity!Object



12
13
14
15
16
17
# File 'lib/active_model/validations/comparison.rb', line 12

def check_validity!
  unless options.keys.intersect?(COMPARE_CHECKS.keys)
    raise ArgumentError, "Expected one of :greater_than, :greater_than_or_equal_to, "\
    ":equal_to, :less_than, :less_than_or_equal_to, or :other_than option to be supplied."
  end
end

#validate_each(record, attr_name, value) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/active_model/validations/comparison.rb', line 19

def validate_each(record, attr_name, value)
  options.slice(*COMPARE_CHECKS.keys).each do |option, raw_option_value|
    option_value = resolve_value(record, raw_option_value)

    if value.nil? || value.blank?
      return record.errors.add(attr_name, :blank, **error_options(value, option_value))
    end

    unless value.public_send(COMPARE_CHECKS[option], option_value)
      record.errors.add(attr_name, option, **error_options(value, option_value))
    end
  rescue ArgumentError => e
    record.errors.add(attr_name, e.message)
  end
end