Extract mismatch validator to separate class

#569
This commit is contained in:
Artur Beljajev 2017-09-12 14:55:41 +03:00
parent 4d5830efdf
commit d5197962ee
5 changed files with 40 additions and 28 deletions

View file

@ -12,7 +12,7 @@ class Contact::Ident
validates :type, presence: true, inclusion: { in: proc { types } } validates :type, presence: true, inclusion: { in: proc { types } }
validates :country_code, presence: true, iso31661_alpha2: true validates :country_code, presence: true, iso31661_alpha2: true
validate :mismatched validates_with MismatchValidator
def self.epp_code_map def self.epp_code_map
{ {
@ -35,14 +35,6 @@ class Contact::Ident
%w[org priv birthday] %w[org priv birthday]
end end
Mismatch = Struct.new(:type, :country)
def self.mismatches
[
Mismatch.new('birthday', Country.new('EE'))
]
end
def marked_for_destruction? def marked_for_destruction?
false false
end end
@ -66,10 +58,7 @@ class Contact::Ident
private private
# https://github.com/rails/rails/issues/1513 # https://github.com/rails/rails/issues/1513
def validation_context=(_value); end def validation_context=(_value)
;
def mismatched
mismatched = self.class.mismatches.include?(Mismatch.new(type, country))
errors.add(:base, :mismatch, type: type, country: country) if mismatched
end end
end end

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

@ -129,8 +129,8 @@ RSpec.describe Contact::Ident, db: false do
let(:ident) { described_class.new(type: 'test', country_code: 'DE') } let(:ident) { described_class.new(type: 'test', country_code: 'DE') }
before do before do
mismatches = [Contact::Ident::Mismatch.new('test', Country.new('DE'))] mismatches = [Contact::Ident::MismatchValidator::Mismatch.new('test', Country.new('DE'))]
allow(described_class).to receive(:mismatches).and_return(mismatches) allow(Contact::Ident::MismatchValidator).to receive(:mismatches).and_return(mismatches)
end end
it 'rejects mismatched' do it 'rejects mismatched' do
@ -161,16 +161,6 @@ RSpec.describe Contact::Ident, db: false do
end end
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 describe '#birthday?' do
context 'when type is birthday' do context 'when type is birthday' do
subject(:ident) { described_class.new(type: 'birthday') } subject(:ident) { described_class.new(type: 'birthday') }

View file

@ -271,9 +271,9 @@ RSpec.describe 'EPP contact:create' do
before do before do
mismatches = [ 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 end
it 'does not create a contact' do it 'does not create a contact' do

View file

@ -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