Commit d7d335c0 authored by Brett Walker's avatar Brett Walker

add primary email as a secondary email whenever the primary is changed

parent 945667ab
...@@ -535,15 +535,13 @@ class User < ActiveRecord::Base ...@@ -535,15 +535,13 @@ class User < ActiveRecord::Base
# By using an `after_commit` instead of `after_update`, we avoid the recursive callback # By using an `after_commit` instead of `after_update`, we avoid the recursive callback
# scenario, though it then requires us to use the `previous_changes` hash # scenario, though it then requires us to use the `previous_changes` hash
def update_emails_with_primary_email def update_emails_with_primary_email
previous_email = previous_changes[:email][0] # grab this before the DestroyService is called
primary_email_record = emails.find_by(email: email) primary_email_record = emails.find_by(email: email)
if primary_email_record Emails::DestroyService.new(self).execute(primary_email_record) if primary_email_record
previous_email = previous_changes[:email][0]
Emails::DestroyService.new(self).execute(primary_email_record)
# the original primary email was confirmed, and we want that to carry over. We don't # the original primary email was confirmed, and we want that to carry over. We don't
# have access to the original confirmation values at this point, so just set confirmed_at # have access to the original confirmation values at this point, so just set confirmed_at
Emails::CreateService.new(self, email: previous_email).execute(confirmed_at: confirmed_at) Emails::CreateService.new(self, email: previous_email).execute(confirmed_at: confirmed_at)
end
end end
def update_invalid_gpg_signatures def update_invalid_gpg_signatures
......
...@@ -32,8 +32,8 @@ describe Email do ...@@ -32,8 +32,8 @@ describe Email do
it 'scopes confirmed emails' do it 'scopes confirmed emails' do
create(:email, :confirmed, user: user) create(:email, :confirmed, user: user)
expect(user.emails.count).to eq 1 create(:email, user: user)
expect(user.emails.unconfirmed.count).to eq 0 expect(user.emails.count).to eq 2
expect(user.emails.confirmed.count).to eq 1 expect(user.emails.confirmed.count).to eq 1
end end
end end
......
...@@ -374,7 +374,7 @@ describe User do ...@@ -374,7 +374,7 @@ describe User do
end end
end end
describe 'after update hook' do describe 'after commit hook' do
describe '.update_invalid_gpg_signatures' do describe '.update_invalid_gpg_signatures' do
let(:user) do let(:user) do
create(:user, email: 'tula.torphy@abshire.ca').tap do |user| create(:user, email: 'tula.torphy@abshire.ca').tap do |user|
...@@ -388,7 +388,7 @@ describe User do ...@@ -388,7 +388,7 @@ describe User do
end end
it 'synchronizes the gpg keys when the email is updated' do it 'synchronizes the gpg keys when the email is updated' do
expect(user).to receive(:update_invalid_gpg_signatures) expect(user).to receive(:update_invalid_gpg_signatures).at_most(:twice)
user.update_attributes!(email: 'shawnee.ritchie@denesik.com') user.update_attributes!(email: 'shawnee.ritchie@denesik.com')
end end
end end
...@@ -407,11 +407,11 @@ describe User do ...@@ -407,11 +407,11 @@ describe User do
@user.update_attributes!(email: 'new_primary@example.com') @user.update_attributes!(email: 'new_primary@example.com')
end end
it 'does not add old primary to secondary emails' do it 'adds old primary to secondary emails when secondary is a new email ' do
@user.update_attributes!(email: 'new_primary@example.com') @user.update_attributes!(email: 'new_primary@example.com')
@user.reload @user.reload
expect(@user.emails.count).to eq 1 expect(@user.emails.count).to eq 2
expect(@user.emails.first.email).to eq @secondary.email expect(@user.emails.pluck(:email)).to match_array([@secondary.email, 'primary@example.com'])
end end
it 'adds old primary to secondary emails if secondary is becoming a primary' do it 'adds old primary to secondary emails if secondary is becoming a primary' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment