fix: Check phone_number and email changes are exist before validation (#4551)
This commit is contained in:
@@ -145,13 +145,13 @@ class Contact < ApplicationRecord
|
|||||||
def phone_number_format
|
def phone_number_format
|
||||||
return if phone_number.blank?
|
return if phone_number.blank?
|
||||||
|
|
||||||
self.phone_number = changes['phone_number'].first unless phone_number.match?(/\+[1-9]\d{1,14}\z/)
|
self.phone_number = phone_number_was unless phone_number.match?(/\+[1-9]\d{1,14}\z/)
|
||||||
end
|
end
|
||||||
|
|
||||||
def email_format
|
def email_format
|
||||||
return if email.blank?
|
return if email.blank?
|
||||||
|
|
||||||
self.email = changes['email'].first unless email.match(Devise.email_regexp)
|
self.email = email_was unless email.match(Devise.email_regexp)
|
||||||
end
|
end
|
||||||
|
|
||||||
def prepare_contact_attributes
|
def prepare_contact_attributes
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ RSpec.describe Contact do
|
|||||||
it { is_expected.to have_many(:conversations).dependent(:destroy_async) }
|
it { is_expected.to have_many(:conversations).dependent(:destroy_async) }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'pepare contact attributes before validation' do
|
context 'prepare contact attributes before validation' do
|
||||||
it 'sets email to lowercase' do
|
it 'sets email to lowercase' do
|
||||||
contact = create(:contact, email: 'Test@test.com')
|
contact = create(:contact, email: 'Test@test.com')
|
||||||
expect(contact.email).to eq('test@test.com')
|
expect(contact.email).to eq('test@test.com')
|
||||||
@@ -43,4 +43,42 @@ RSpec.describe Contact do
|
|||||||
expect(contact.additional_attributes).to eq({})
|
expect(contact.additional_attributes).to eq({})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# rubocop:disable Rails/SkipsModelValidations
|
||||||
|
context 'when phone number format' do
|
||||||
|
it 'will not throw error for existing invalid phone number' do
|
||||||
|
contact = create(:contact)
|
||||||
|
contact.update_column(:phone_number, '344234')
|
||||||
|
contact.reload
|
||||||
|
expect(contact.update!(name: 'test')).to eq true
|
||||||
|
expect(contact.phone_number).to eq '344234'
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'updates phone number when adding valid phone number' do
|
||||||
|
contact = create(:contact)
|
||||||
|
contact.update_column(:phone_number, '344234')
|
||||||
|
contact.reload
|
||||||
|
expect(contact.update!(phone_number: '+12312312321')).to eq true
|
||||||
|
expect(contact.phone_number).to eq '+12312312321'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when email format' do
|
||||||
|
it 'will not throw error for existing invalid email' do
|
||||||
|
contact = create(:contact)
|
||||||
|
contact.update_column(:email, 'ssfdasdf <test@test')
|
||||||
|
contact.reload
|
||||||
|
expect(contact.update!(name: 'test')).to eq true
|
||||||
|
expect(contact.email).to eq 'ssfdasdf <test@test'
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'updates email when adding valid email' do
|
||||||
|
contact = create(:contact)
|
||||||
|
contact.update_column(:email, 'ssfdasdf <test@test')
|
||||||
|
contact.reload
|
||||||
|
expect(contact.update!(email: 'test@test.com')).to eq true
|
||||||
|
expect(contact.email).to eq 'test@test.com'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
# rubocop:enable Rails/SkipsModelValidations
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user