Improve Ident validation, extract Ident from Contact

#569
This commit is contained in:
Artur Beljajev 2017-08-18 13:24:56 +03:00
parent c5e1516d89
commit feb8d83a26
17 changed files with 809 additions and 195 deletions

View file

@ -0,0 +1,72 @@
class Contact::Ident
include ActiveModel::Model
attr_accessor :code
attr_accessor :type
attr_accessor :country_code
validates :code, presence: true, code: true
validates :code, iso8601: { date_only: true }, if: :birthday?
validates :type, presence: true, inclusion: { in: Proc.new { types } }
validates :country_code, presence: true, iso31661_alpha2: true
validate :mismatched
def self.epp_code_map
{
'2003' => [
[:code, :blank],
[:type, :blank],
[:country_code, :blank],
],
'2005' => [
[:base, :mismatch],
[:code, :invalid_national_id],
[:code, :invalid_reg_no],
[:code, :invalid_iso8601],
[:country_code, :invalid_iso31661_alpha2],
],
}
end
def self.types
%w[org priv birthday]
end
Mismatch = Struct.new(:type, :country)
def self.mismatches
[
Mismatch.new('birthday', Country.new('EE')),
]
end
def marked_for_destruction?
false
end
def birthday?
type == 'birthday'
end
def national_id?
type == 'priv'
end
def reg_no?
type == 'org'
end
def country
Country.new(country_code)
end
private
# https://github.com/rails/rails/issues/1513
def validation_context=(value); end
def mismatched
mismatched = self.class.mismatches.include?(Mismatch.new(type, country))
errors.add(:base, :mismatch, type: type, country: country) if mismatched
end
end