lib/valvat/active_model.rb in valvat-0.3.0 vs lib/valvat/active_model.rb in valvat-0.3.1
- old
+ new
@@ -6,24 +6,31 @@
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?
+ iso_country_code = vat.iso_country_code
+ is_valid = true
- if is_valid.nil?
- is_valid = options[:lookup] != :fail_if_down
+ if options[:match_country]
+ iso_country_code = (record.send(options[:match_country]) || "").upcase
+ is_valid = iso_country_code == vat.iso_country_code
end
- if is_valid && options[:match_country]
- is_valid = (record.send(options[:match_country]) || "").upcase == vat.iso_country_code
+ if is_valid
+ is_valid = options[:lookup] ? vat.valid? && vat.exists? : vat.valid?
+
+ if is_valid.nil?
+ is_valid = options[:lookup] != :fail_if_down
+ end
end
unless is_valid
+ iso_country_code = "eu" if iso_country_code.blank?
record.errors.add(attribute, :invalid_vat,
:message => options[:message],
:country_adjective => I18n.t(
- :"valvat.country_adjectives.#{(Valvat::Utils.split(value)[0] || "eu").downcase}",
+ :"valvat.country_adjectives.#{iso_country_code.downcase}",
:default => [:"valvat.country_adjectives.eu", "european"]
)
)
end
end
\ No newline at end of file