Skip to content

Instantly share code, notes, and snippets.

@jrdi
Created February 26, 2012 16:48
Show Gist options
  • Save jrdi/1917623 to your computer and use it in GitHub Desktop.
Save jrdi/1917623 to your computer and use it in GitHub Desktop.
A simple module to enqueue emails with Resque
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