-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide a send_devise_notification hook
- Loading branch information
José Valim
committed
Jun 16, 2012
1 parent
85c9067
commit 18a18e4
Showing
4 changed files
with
41 additions
and
4 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -134,6 +134,43 @@ def #{method}(options=nil) | |
RUBY | ||
end | ||
|
||
protected | ||
|
||
# This is an internal method called every time Devise needs | ||
# to send a notification/mail. This can be overriden if you | ||
# need to customize the e-mail delivery logic. For instance, | ||
# if you are using a queue to deliver e-mails (delayed job, | ||
# sidekiq, resque, etc), you must add the delivery to the queue | ||
# just after the transaction was committed. To achieve this, | ||
# you can override send_devise_notification to store the | ||
# deliveries until the after_commit callback is triggered: | ||
# | ||
# class User | ||
# devise :database_authenticatable, :confirmable | ||
# | ||
# after_commit :send_pending_notifications | ||
# | ||
# protected | ||
# | ||
# def send_devise_notification(notification) | ||
# pending_notifications << notification | ||
# end | ||
# | ||
# def send_pending_notifications | ||
# pending_notifications.each do |n| | ||
# devise_mailer.send(n, self).deliver | ||
# end | ||
# end | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
lucasmazza
Contributor
|
||
# | ||
# def pending_notifications | ||
# @pending_notifications ||= [] | ||
# end | ||
# end | ||
# | ||
def send_devise_notification(notification) | ||
devise_mailer.send(notification, self).deliver | ||
end | ||
|
||
module ClassMethods | ||
Devise::Models.config(self, :authentication_keys, :request_keys, :strip_whitespace_keys, | ||
:case_insensitive_keys, :http_authenticatable, :params_authenticatable, :skip_session_storage) | ||
|
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
As a PSA to anyone else, you'll want to clear the pending notifications array after sending them because
after_commit
can be called multiple times. If you don't you'll end up spamming your new user with multiple copies of the email message. Alsoafter_commit
won't be called if your record isn't a new record or hasn't been changed - in this case yoursend_devise_notification
method will be something like this:You'll want to check for
new_record?
andchanged?
becauseUser.new.changed? => false
.