class EqualityValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) to_value = options[:to] operator = OPERATORS.fetch(options[:operator]) if (options[:operator] == :less_than) || (options[:operator] == :greater_than) raise Exception if to_value.nil? || operator.nil? end unless value.send(operator, record.send(to_value.to_sym)) record.errors[attribute] << (options[:message] || I18n.t('flash_validators.errors.messages.equality', attr: to_value, 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: :!= } end