diff --git a/lib/devise/models/authenticatable.rb b/lib/devise/models/authenticatable.rb index 9b6f69682d..5562dbebd9 100644 --- a/lib/devise/models/authenticatable.rb +++ b/lib/devise/models/authenticatable.rb @@ -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 + # + # 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) diff --git a/lib/devise/models/confirmable.rb b/lib/devise/models/confirmable.rb index 07d6a3576b..afcda349bb 100644 --- a/lib/devise/models/confirmable.rb +++ b/lib/devise/models/confirmable.rb @@ -78,7 +78,7 @@ def send_confirmation_instructions @reconfirmation_required = false generate_confirmation_token! if self.confirmation_token.blank? - self.devise_mailer.confirmation_instructions(self).deliver + send_devise_notification(:confirmation_instructions) end # Resend confirmation token. This method does not need to generate a new token. @@ -125,7 +125,7 @@ def headers_for(action) # instructions on creation. This can be overriden # in models to map to a nice sign up e-mail. def send_on_create_confirmation_instructions - self.devise_mailer.confirmation_instructions(self).deliver + send_devise_notification(:confirmation_instructions) end # Callback to overwrite if confirmation is required or not. diff --git a/lib/devise/models/lockable.rb b/lib/devise/models/lockable.rb index 0e7c8f394d..b7ac84f47a 100644 --- a/lib/devise/models/lockable.rb +++ b/lib/devise/models/lockable.rb @@ -60,7 +60,7 @@ def access_locked? # Send unlock instructions by email def send_unlock_instructions - self.devise_mailer.unlock_instructions(self).deliver + send_devise_notification(:unlock_instructions) end # Resend the unlock instructions if the user is locked. diff --git a/lib/devise/models/recoverable.rb b/lib/devise/models/recoverable.rb index 2a6106e3c5..0bed681ea9 100644 --- a/lib/devise/models/recoverable.rb +++ b/lib/devise/models/recoverable.rb @@ -45,7 +45,7 @@ def reset_password!(new_password, new_password_confirmation) # Resets reset password token and send reset password instructions by email def send_reset_password_instructions generate_reset_password_token! if should_generate_reset_token? - self.devise_mailer.reset_password_instructions(self).deliver + send_devise_notification(:reset_password_instructions) end # Checks if the reset password token sent is within the limit time.