Skip to content

Instantly share code, notes, and snippets.

@denniskuczynski
Created June 25, 2012 18:03
Show Gist options
  • Save denniskuczynski/2990249 to your computer and use it in GitHub Desktop.
Save denniskuczynski/2990249 to your computer and use it in GitHub Desktop.
Override Devise email logic for sending asynchronously with Sidekiq
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