Sha256: 529cef9fb23d90fadadf876ef3d27f272994a69b3ba39595c458d3a3d40e3a5c

Contents?: true

Size: 724 Bytes

Versions: 21

Compression:

Stored size: 724 Bytes

Contents

class ZipValidator < ActiveModel::EachValidator

  def initialize(options = {})
    super(options)
    @other = options[:other]
  end

  def validate_each(record, attribute, value)
    zip1 = value
    zip2 = get_other_value(record)

    return if (zip1.nil? or zip1.empty?) and (zip2.nil? or zip2.empty?)

    unless validate_zip(zip1, zip2)
      record.errors.add(attribute, I18n.t('errors.messages.zip'))
    end
  end

  private

  def validate_zip(zip1, zip2)
    return false unless zip1
    return false unless zip1.match(/^[0-9|0-9]{3}$/)

    return false unless zip2
    return false unless zip2.match(/^[0-9|0-9]{4}$/)

    true
  end

  def get_other_value(record)
    record.__send__(@other)
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
bizside-2.2.1 validations/zip_validator.rb