Class: ActiveModel::Validations::ComparisonValidator

Inherits:
EachValidator show all
Includes:
Comparability
Defined in:
activemodel/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 Comparability

#error_options, #option_value

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


10
11
12
13
14
15
# File 'activemodel/lib/active_model/validations/comparison.rb', line 10

def check_validity!
  unless (options.keys & COMPARE_CHECKS.keys).any?
    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


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

def validate_each(record, attr_name, value)
  options.slice(*COMPARE_CHECKS.keys).each do |option, raw_option_value|
    option_value = option_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