Created
June 25, 2012 18:03
-
-
Save denniskuczynski/2990249 to your computer and use it in GitHub Desktop.
Override Devise email logic for sending asynchronously with Sidekiq
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
module DeviseAsync | |
####################################################################### | |
# Override Devise email logic for sending asynchronously with Sidekiq # | |
####################################################################### | |
def self.included(clazz) | |
clazz.class_eval do | |
public | |
after_commit :send_pending_notifications | |
protected | |
# If the resource has been changed, wait until the after_commit flag is fired before sending the notification | |
def send_devise_notification(notification) | |
if self.changed? | |
pending_notifications << notification | |
else | |
devise_mailer.delay.send(n, self) | |
end | |
end | |
def send_pending_notifications | |
pending_notifications.each do |n| | |
devise_mailer.delay.send(n, self) | |
end | |
end | |
def pending_notifications | |
@pending_notifications ||= [] | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment