Sha256: 611f5ef9feffc44d20329cb4e6bb2891dfbc78fa5ae5985608375910e0b2b2e1
Contents?: true
Size: 989 Bytes
Versions: 4
Compression:
Stored size: 989 Bytes
Contents
class EqualityValidator < ActiveModel::EachValidator 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(', '.freeze)}" end operator = OPERATORS.fetch(operator) unless value.send(operator.to_sym, record.send(to.to_sym)) record.errors[attribute] << (options.fetch(:message, false) || I18n.t('active_validation.errors.messages.equality'.freeze, attr: to, operator: operator)) end end private OPERATORS = { less_than: :<, less_than_or_equal_to: :<=, greater_than: :>, greater_than_or_equal_to: :>=, equal_to: :==, not_equal_to: :!= }.freeze end
Version data entries
4 entries across 4 versions & 1 rubygems