-
Notifications
You must be signed in to change notification settings - Fork 122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use a service class to send confirmation emails #2948
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6e3cab0
Use a service class to send confirmation emails
jmhooper b620159
cleanup old code
jmhooper b7f1ae1
implement pending specs
jmhooper 7843ad0
move confirmation instructions out of the devise mailer into the user…
jmhooper bf2d34b
some cleanup
jmhooper 015b054
supress some reek warnings
jmhooper 294c88d
Merge branch 'master' into jmhooper-move-email-sending-off-of-user
jmhooper 79c1056
cleanup
jmhooper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
class SendSignUpEmailConfirmation | ||
attr_reader :user | ||
|
||
def initialize(user) | ||
@user = user | ||
end | ||
|
||
def call(request_id: nil, instructions: nil) | ||
update_user_record | ||
update_email_address_record | ||
send_confirmation_email(request_id, instructions) | ||
end | ||
|
||
private | ||
|
||
def confirmation_token | ||
return email_address.confirmation_token if valid_confirmation_token_exists? | ||
@token ||= Devise.friendly_token | ||
end | ||
|
||
def confirmation_sent_at | ||
return email_address.confirmation_sent_at if valid_confirmation_token_exists? | ||
@confirmation_sent_at ||= Time.zone.now | ||
end | ||
|
||
def confirmation_period_expired? | ||
@confirmation_period_expired ||= user.confirmation_period_expired? | ||
end | ||
|
||
# :reek:DuplicateMethodCall | ||
def email_address | ||
@email_address ||= begin | ||
raise handle_multiple_email_address_error if user.email_addresses.count > 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
user.email_addresses.take | ||
end | ||
end | ||
|
||
def update_user_record | ||
user.update!( | ||
confirmation_token: confirmation_token, | ||
confirmation_sent_at: confirmation_sent_at, | ||
) | ||
end | ||
|
||
def valid_confirmation_token_exists? | ||
email_address.confirmation_token.present? && !confirmation_period_expired? | ||
end | ||
|
||
def update_email_address_record | ||
email_address.update!( | ||
confirmation_token: confirmation_token, | ||
confirmation_sent_at: confirmation_sent_at, | ||
) | ||
end | ||
|
||
def send_confirmation_email(request_id, instructions) | ||
UserMailer.email_confirmation_instructions( | ||
user, | ||
email_address.email, | ||
confirmation_token, | ||
request_id: request_id, | ||
instructions: instructions, | ||
).deliver_later | ||
end | ||
|
||
def handle_multiple_email_address_error | ||
raise 'sign up user has multiple email address records' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,7 +59,7 @@ | |
|
||
expect(response).to redirect_to account_url | ||
expect(flash[:notice]).to eq t('devise.registrations.email_update_needs_confirmation') | ||
expect(response).to render_template('devise/mailer/confirmation_instructions') | ||
expect(response).to render_template('user_mailer/email_confirmation_instructions') | ||
expect(user.reload.email).to eq '[email protected]' | ||
expect(@analytics).to have_received(:track_event). | ||
with(Analytics::EMAIL_CHANGE_REQUEST, analytics_hash) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume you want to leave in the 3 lines above. I am pointing this out in case you intended to remove them later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I meant to disable these. Normally I'd add a method for consuming the parameters, but this class is already pretty fat and I'd be worried about that getting lost in the concerns it has for the other mails it sends.