Class: VatValidator::VatValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/vat_validator.rb

Overview

Classes ——————————————————————–

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/vat_validator.rb', line 40

def validate_each(record, attribute, value)
  if options[:country_method]
    country_code = record.send(options[:country_method]).to_s
    unless VAT_PATTERNS.has_key?(country_code) && value.to_s =~ VAT_PATTERNS[country_code]
      record.errors.add(attribute, options[:message])
    end
  else
    unless value =~ VAT_PATTERNS.values.detect { |p| value.to_s =~ p }
      record.errors.add(attribute, options[:message])
    end
  end
end