Sha256: 5feacabe27ea41c5f35145490063583a7553ecb042c337d5c65a5a26fb080ee9

Contents?: true

Size: 923 Bytes

Versions: 4

Compression:

Stored size: 923 Bytes

Contents

require 'active_model'
require 'valvat/syntax'
require 'valvat/lookup'

module ActiveModel
  module Validations
    class ValvatValidator < ::ActiveModel::EachValidator
      
      def validate_each(record, attribute, value)
        vat = Valvat(value)
        
        is_valid = options[:lookup] ? vat.valid? && vat.exists? : vat.valid?
        is_valid.nil? && is_valid = (options[:lookup] != :fail_if_down)
        
        unless is_valid
          record.errors.add(attribute, :invalid_vat, 
            :message => options[:message], 
            :country_adjective => I18n.t(
              :"valvat.country_adjectives.#{(Valvat::Utils.split(value)[0] || "eu").downcase}", 
              :default => [:"valvat.country_adjectives.eu", "european"]
            )
          )
        end
      end
    end
  end
end

%w(en de).each do |locale|
  I18n.load_path << "#{File.dirname(__FILE__)}/../locale/#{locale}.yml"
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
valvat-0.2.3 lib/valvat/active_model.rb
valvat-0.2.2 lib/valvat/active_model.rb
valvat-0.2.1 lib/valvat/active_model.rb
valvat-0.2.0 lib/valvat/active_model.rb