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.to_sym, record.send(to.to_sym))
record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.equality', attr: to, operator: operator))
end
end
|