Class: ActiveModel::Validations::LessOrGreaterThanValidator

Inherits:
EachValidator
  • Object
show all
Defined in:
lib/activemodel-validators/less_or_greater_than_validator.rb

Instance Method Summary collapse

Instance Method Details

#check_validity!Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/activemodel-validators/less_or_greater_than_validator.rb', line 4

def check_validity!
  unless options[:attr] or options[:value]
    # TODO: If options[:attr], check that it is an actual column or virtual attribute
    raise ArgumentError, "must supply :attr or :value option"
  end
  unless allowed_operators.include?(operator)
    # TODO: If options[:attr], check that it is an actual column or virtual attribute
    raise ArgumentError, ":operator must be one of #{allowed_operators.join(', ')} but was #{operator}"
  end
end

#validate_each(record, attribute, value) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/activemodel-validators/less_or_greater_than_validator.rb', line 71

def validate_each(record, attribute, value)
  @record = record
  return if value.blank? or other_value.blank?
  unless value.send(operator, other_value)
    record.errors.add(attribute, message_key,
                      **options.merge(
                        value:         value,
                        operator:      operator,
                        operator_text: operator_text,
                        attr_name:     human_attr_name,
                        other_value:   other_value,
                      )
    )
  end
end