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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/vat_validator.rb', line 42

def validate_each(record, attribute, value)
  format_valid = true
  
  country_code = options[:country_method] ? record.send(options[:country_method]).to_s : nil
  unless VatNumber.new(value, country_code).valid?
    record.errors.add(attribute, options[:message])
    format_valid = false
  end
  
  if format_valid && options[:vies]
    if options[:vies_host]
      valid = ViesChecker.check(value, options[:vies_host])
    else
      valid = ViesChecker.check(value)
    end

    unless valid
     record.errors.add(attribute, options[:message])
    end
  end
end