Class: EqualityValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/flash_validators/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
# File 'lib/flash_validators/validators/equality_validator.rb', line 3

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