Commit 5ee708d7 authored by Dan Jensen's avatar Dan Jensen

Refactor unset of User secondary emails on email destroy

Prior to removing some User methods related to unsetting user input,
this removes 3 calls to those methods. This also introduces a list
of attributes that we consider "secondary emails". The intent of this
refactoring is to improve clarity of intent and reduce entanglement
between logic that is unrelated.
parent f07a46cf
...@@ -39,6 +39,12 @@ class User < ApplicationRecord ...@@ -39,6 +39,12 @@ class User < ApplicationRecord
MAX_USERNAME_LENGTH = 255 MAX_USERNAME_LENGTH = 255
MIN_USERNAME_LENGTH = 2 MIN_USERNAME_LENGTH = 2
SECONDARY_EMAIL_ATTRIBUTES = [
:commit_email,
:notification_email,
:public_email
].freeze
add_authentication_token_field :incoming_email_token, token_generator: -> { SecureRandom.hex.to_i(16).to_s(36) } add_authentication_token_field :incoming_email_token, token_generator: -> { SecureRandom.hex.to_i(16).to_s(36) }
add_authentication_token_field :feed_token add_authentication_token_field :feed_token
add_authentication_token_field :static_object_token add_authentication_token_field :static_object_token
...@@ -1310,11 +1316,15 @@ class User < ApplicationRecord ...@@ -1310,11 +1316,15 @@ class User < ApplicationRecord
end end
end end
def update_secondary_emails! def unset_secondary_emails_matching_deleted_email!(deleted_email)
set_notification_email secondary_email_attribute_changed = false
set_public_email SECONDARY_EMAIL_ATTRIBUTES.each do |attribute|
set_commit_email if read_attribute(attribute) == deleted_email
save if notification_email_changed? || public_email_changed? || commit_email_changed? self.write_attribute(attribute, nil)
secondary_email_attribute_changed = true
end
end
save if secondary_email_attribute_changed
end end
def admin_unsubscribe! def admin_unsubscribe!
......
...@@ -3,14 +3,14 @@ ...@@ -3,14 +3,14 @@
module Emails module Emails
class DestroyService < ::Emails::BaseService class DestroyService < ::Emails::BaseService
def execute(email) def execute(email)
email.destroy && update_secondary_emails! email.destroy && update_secondary_emails!(email.email)
end end
private private
def update_secondary_emails! def update_secondary_emails!(deleted_email)
result = ::Users::UpdateService.new(@current_user, user: @user).execute do |user| result = ::Users::UpdateService.new(@current_user, user: @user).execute do |user|
user.update_secondary_emails! user.unset_secondary_emails_matching_deleted_email!(deleted_email)
end end
result[:status] == :success result[:status] == :success
......
...@@ -6024,4 +6024,30 @@ RSpec.describe User do ...@@ -6024,4 +6024,30 @@ RSpec.describe User do
expect(described_class.by_provider_and_extern_uid(:github, 'my_github_id')).to match_array([expected_user]) expect(described_class.by_provider_and_extern_uid(:github, 'my_github_id')).to match_array([expected_user])
end end
end end
describe '#unset_secondary_emails_matching_deleted_email!' do
let(:deleted_email) { 'kermit@muppets.com' }
subject { build(:user, commit_email: commit_email) }
context 'when no secondary email matches the deleted email' do
let(:commit_email) { 'fozzie@muppets.com' }
it 'does nothing' do
expect(subject).not_to receive(:save)
subject.unset_secondary_emails_matching_deleted_email!(deleted_email)
expect(subject.read_attribute(:commit_email)).to eq commit_email
end
end
context 'when a secondary email matches the deleted_email' do
let(:commit_email) { deleted_email }
it 'un-sets the secondary email' do
expect(subject).to receive(:save)
subject.unset_secondary_emails_matching_deleted_email!(deleted_email)
expect(subject.read_attribute(:commit_email)).to be nil
end
end
end
end end
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