Class: Users::UpdateCanonicalEmailService

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Concern
Defined in:
app/services/users/update_canonical_email_service.rb

Constant Summary collapse

INCLUDED_DOMAINS_PATTERN =
[/gmail.com/].freeze

Instance Method Summary collapse

Constructor Details

#initialize(user:) ⇒ UpdateCanonicalEmailService

Returns a new instance of UpdateCanonicalEmailService.

Raises:

  • (ArgumentError)


9
10
11
12
13
# File 'app/services/users/update_canonical_email_service.rb', line 9

def initialize(user:)
  raise ArgumentError, "Please provide a user" unless user.is_a?(User)

  @user = user
end

Instance Method Details

#executeObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/services/users/update_canonical_email_service.rb', line 15

def execute
  return unless user.email
  return unless user.email.match? Devise.email_regexp

  canonical_email = canonicalize_email

  unless canonical_email
    # the canonical email doesn't exist, probably because the domain doesn't match
    # destroy any UserCanonicalEmail record associated with this user
    user.user_canonical_email&.delete
    # nothing else to do here
    return
  end

  if user.user_canonical_email
    # update to the new value
    user.user_canonical_email.canonical_email = canonical_email
  else
    user.build_user_canonical_email(canonical_email: canonical_email)
  end
end