Created
February 26, 2012 16:48
-
-
Save jrdi/1917623 to your computer and use it in GitHub Desktop.
A simple module to enqueue emails with Resque
This file contains hidden or 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 MailQueue | |
def self.included(base) | |
base.extend(ClassMethods) | |
end | |
module ClassMethods | |
def queue | |
:mailers | |
end | |
# Add mail to queue Notifier.async_reply(Notification.first) | |
def method_missing(m, *args, &block) | |
object = args.first | |
if m.to_s =~ /^async_([_a-zA-Z]*)$/ && self.respond_to?($1) | |
send_later($1, object, *args[1..-1]) | |
else | |
super | |
end | |
end | |
end | |
# Add mail to queue Notifier.send_later(:reply, Notification.first) | |
def send_later(method, object, *args) | |
Resque.enqueue(self.class, method, object.class.to_s, object.id, *args) | |
end | |
def perform(method, klass, id, *args) | |
object = klass.constantize.find(id) | |
self.class.send(method, object, *args).deliver | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment