Skip to content

Commit 18a18e4

Browse files
author
José Valim
committed
Provide a send_devise_notification hook
1 parent 85c9067 commit 18a18e4

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

lib/devise/models/authenticatable.rb

+37
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,43 @@ def #{method}(options=nil)
134134
RUBY
135135
end
136136

137+
protected
138+
139+
# This is an internal method called every time Devise needs
140+
# to send a notification/mail. This can be overriden if you
141+
# need to customize the e-mail delivery logic. For instance,
142+
# if you are using a queue to deliver e-mails (delayed job,
143+
# sidekiq, resque, etc), you must add the delivery to the queue
144+
# just after the transaction was committed. To achieve this,
145+
# you can override send_devise_notification to store the
146+
# deliveries until the after_commit callback is triggered:
147+
#
148+
# class User
149+
# devise :database_authenticatable, :confirmable
150+
#
151+
# after_commit :send_pending_notifications
152+
#
153+
# protected
154+
#
155+
# def send_devise_notification(notification)
156+
# pending_notifications << notification
157+
# end
158+
#
159+
# def send_pending_notifications
160+
# pending_notifications.each do |n|
161+
# devise_mailer.send(n, self).deliver
162+
# end
163+
# end
164+
#
165+
# def pending_notifications
166+
# @pending_notifications ||= []
167+
# end
168+
# end
169+
#
170+
def send_devise_notification(notification)
171+
devise_mailer.send(notification, self).deliver
172+
end
173+
137174
module ClassMethods
138175
Devise::Models.config(self, :authentication_keys, :request_keys, :strip_whitespace_keys,
139176
:case_insensitive_keys, :http_authenticatable, :params_authenticatable, :skip_session_storage)

lib/devise/models/confirmable.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def send_confirmation_instructions
7878
@reconfirmation_required = false
7979

8080
generate_confirmation_token! if self.confirmation_token.blank?
81-
self.devise_mailer.confirmation_instructions(self).deliver
81+
send_devise_notification(:confirmation_instructions)
8282
end
8383

8484
# Resend confirmation token. This method does not need to generate a new token.
@@ -125,7 +125,7 @@ def headers_for(action)
125125
# instructions on creation. This can be overriden
126126
# in models to map to a nice sign up e-mail.
127127
def send_on_create_confirmation_instructions
128-
self.devise_mailer.confirmation_instructions(self).deliver
128+
send_devise_notification(:confirmation_instructions)
129129
end
130130

131131
# Callback to overwrite if confirmation is required or not.

lib/devise/models/lockable.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def access_locked?
6060

6161
# Send unlock instructions by email
6262
def send_unlock_instructions
63-
self.devise_mailer.unlock_instructions(self).deliver
63+
send_devise_notification(:unlock_instructions)
6464
end
6565

6666
# Resend the unlock instructions if the user is locked.

lib/devise/models/recoverable.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def reset_password!(new_password, new_password_confirmation)
4545
# Resets reset password token and send reset password instructions by email
4646
def send_reset_password_instructions
4747
generate_reset_password_token! if should_generate_reset_token?
48-
self.devise_mailer.reset_password_instructions(self).deliver
48+
send_devise_notification(:reset_password_instructions)
4949
end
5050

5151
# Checks if the reset password token sent is within the limit time.

0 commit comments

Comments
 (0)