Sha256: 50647feaae696b5c362de9bc8c7e90c626b8f1fac96914de6ab91530d798bf52

Contents?: true

Size: 778 Bytes

Versions: 3

Compression:

Stored size: 778 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

3 entries across 3 versions & 1 rubygems

Version Path
flash_validators-1.1.0 lib/flash_validators/validators/equality_validator.rb
flash_validators-1.0.0 lib/flash_validators/validators/equality_validator.rb
flash_validators-0.0.1 lib/flash_validators/validators/equality_validator.rb