diff --git a/app/models/contact/ident.rb b/app/models/contact/ident.rb index b95ad56de..3891b3ed8 100644 --- a/app/models/contact/ident.rb +++ b/app/models/contact/ident.rb @@ -12,7 +12,7 @@ class Contact::Ident validates :type, presence: true, inclusion: { in: proc { types } } validates :country_code, presence: true, iso31661_alpha2: true - validate :mismatched + validates_with MismatchValidator def self.epp_code_map { @@ -35,14 +35,6 @@ class Contact::Ident %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 @@ -66,10 +58,7 @@ class Contact::Ident 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 + def validation_context=(_value) + ; end end diff --git a/app/validators/contact/ident/mismatch_validator.rb b/app/validators/contact/ident/mismatch_validator.rb new file mode 100644 index 000000000..d20bb82cf --- /dev/null +++ b/app/validators/contact/ident/mismatch_validator.rb @@ -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 diff --git a/spec/models/contact/ident_spec.rb b/spec/models/contact/ident_spec.rb index 3b54b964c..5ef0875c9 100644 --- a/spec/models/contact/ident_spec.rb +++ b/spec/models/contact/ident_spec.rb @@ -129,8 +129,8 @@ RSpec.describe Contact::Ident, db: false do let(:ident) { described_class.new(type: 'test', country_code: 'DE') } before do - mismatches = [Contact::Ident::Mismatch.new('test', Country.new('DE'))] - allow(described_class).to receive(:mismatches).and_return(mismatches) + mismatches = [Contact::Ident::MismatchValidator::Mismatch.new('test', Country.new('DE'))] + allow(Contact::Ident::MismatchValidator).to receive(:mismatches).and_return(mismatches) end it 'rejects mismatched' do @@ -161,16 +161,6 @@ RSpec.describe Contact::Ident, db: false do end end - describe '::mismatches' do - it 'returns mismatches' do - mismatches = [ - Contact::Ident::Mismatch.new('birthday', Country.new('EE')) - ] - - expect(described_class.mismatches).to eq(mismatches) - end - end - describe '#birthday?' do context 'when type is birthday' do subject(:ident) { described_class.new(type: 'birthday') } diff --git a/spec/requests/epp/contact/create/ident_spec.rb b/spec/requests/epp/contact/create/ident_spec.rb index e02fc9c52..4fb9ffe65 100644 --- a/spec/requests/epp/contact/create/ident_spec.rb +++ b/spec/requests/epp/contact/create/ident_spec.rb @@ -271,9 +271,9 @@ RSpec.describe 'EPP contact:create' do before do mismatches = [ - Contact::Ident::Mismatch.new('priv', Country.new('DE')) + Contact::Ident::MismatchValidator::Mismatch.new('priv', Country.new('DE')) ] - allow(Contact::Ident).to receive(:mismatches).and_return(mismatches) + allow(Contact::Ident::MismatchValidator).to receive(:mismatches).and_return(mismatches) end it 'does not create a contact' do diff --git a/spec/validators/contact/ident/mismatch_validator_spec.rb b/spec/validators/contact/ident/mismatch_validator_spec.rb new file mode 100644 index 000000000..dfb30fe09 --- /dev/null +++ b/spec/validators/contact/ident/mismatch_validator_spec.rb @@ -0,0 +1,13 @@ +require 'rails_helper' + +RSpec.describe Contact::Ident::MismatchValidator do + describe '::mismatches' do + it 'returns mismatches' do + mismatches = [ + Contact::Ident::MismatchValidator::Mismatch.new('birthday', Country.new('EE')) + ] + + expect(described_class.mismatches).to eq(mismatches) + end + end +end