Revert "Revert "Registry 569""

This reverts commit 4786dbb
This commit is contained in:
Artur Beljajev 2017-10-22 23:57:11 +03:00
parent 0f352cab24
commit d520b5b157
50 changed files with 2462 additions and 323 deletions

View file

@ -0,0 +1,20 @@
class Contact::Ident::MismatchValidator < ActiveModel::Validator
Mismatch = Struct.new(:type, :country)
def self.mismatches
[
Mismatch.new('birthday', Country.new('EE')),
]
end
def validate(record)
record.errors.add(:base, :mismatch, type: record.type, country: record.country) if mismatched?(record)
end
private
def mismatched?(record)
mismatch = Mismatch.new(record.type, record.country)
self.class.mismatches.include?(mismatch)
end
end

View file

@ -0,0 +1,22 @@
class Contact::Ident::NationalIdValidator < ActiveModel::EachValidator
def self.country_specific_validations
{
Country.new('EE') => proc { |code| Isikukood.new(code).valid? },
}
end
def validate_each(record, attribute, value)
validation = validation_for(record.country)
return unless validation
valid = validation.call(value)
record.errors.add(attribute, :invalid_national_id, country: record.country) unless valid
end
private
def validation_for(country)
self.class.country_specific_validations[country]
end
end

View file

@ -0,0 +1,21 @@
class Contact::Ident::RegNoValidator < ActiveModel::EachValidator
def self.country_specific_formats
{
Country.new('EE') => /\A[0-9]{8}\z/,
}
end
def validate_each(record, attribute, value)
format = format_for(record.country)
return unless format
record.errors.add(attribute, :invalid_reg_no, country: record.country) unless value =~ format
end
private
def format_for(country)
self.class.country_specific_formats[country]
end
end