Class: EqualityValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/active_validation/validators/equality_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/active_validation/validators/equality_validator.rb', line 3

def validate_each(record, attribute, value)
  to = options.fetch(:to, nil)
  if to.nil?
    raise ArgumentError,
      "ArgumentError: missing ':to' attribute for comparison."
  end

  operator = options.fetch(:operator, nil)
  operators = OPERATORS.keys
  unless operators.include?(operator)
    raise ArgumentError,
      "Unknown operator: #{operator.inspect}. Valid operators are: #{operators.map(&:inspect).join(", ")}"
  end
  operator = OPERATORS.fetch(operator)

  unless value.send(operator, record.send(to))
    record.errors[attribute] << options.fetch(:message, I18n.t("active_validation.errors.messages.equality", attr: to, operator: operator))
  end
end