Sha256: 2d0189aaf3ed22afe66e1b11d15f5eb48770e8164d71655d9f05813ace567b41

Contents?: true

Size: 732 Bytes

Versions: 11

Compression:

Stored size: 732 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('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

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
flash_validators-3.0.12 lib/flash_validators/validators/equality_validator.rb
flash_validators-3.0.11 lib/flash_validators/validators/equality_validator.rb
flash_validators-3.0.10 lib/flash_validators/validators/equality_validator.rb
flash_validators-3.0.9 lib/flash_validators/validators/equality_validator.rb
flash_validators-3.0.8 lib/flash_validators/validators/equality_validator.rb
flash_validators-3.0.7 lib/flash_validators/validators/equality_validator.rb
flash_validators-3.0.6 lib/flash_validators/validators/equality_validator.rb
flash_validators-3.0.5 lib/flash_validators/validators/equality_validator.rb
flash_validators-3.0.4 lib/flash_validators/validators/equality_validator.rb
flash_validators-3.0.3 lib/flash_validators/validators/equality_validator.rb
flash_validators-3.0.2 lib/flash_validators/validators/equality_validator.rb