Sha256: 3d1e26787b1a924dd684683cffeba3fa1ae2adfb2535d1ca96c32a514691340e

Contents?: true

Size: 715 Bytes

Versions: 2

Compression:

Stored size: 715 Bytes

Contents

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('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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
flash_validators-3.0.1 lib/flash_validators/validators/equality_validator.rb
flash_validators-3.0.0 lib/flash_validators/validators/equality_validator.rb