Created
January 21, 2010 04:01
-
-
Save armstrjare/282564 to your computer and use it in GitHub Desktop.
Simply make Mailer.deliver_xxx_email(args) dispatch to delayed job to send, but still generate the mail content locally.
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
class DelayedJobMailer | |
def self.apply! | |
ActionMailer::Base.class_eval do | |
class << self | |
def method_missing_with_delayed_job_mailer(name, *args) | |
# If we are calling the Mailer.deliver_(mail_name) method, we want to catch that and | |
# convert it to a create_(mail) to create the email content. Then we call the deliver component | |
# via DelayedJob: Mailer.deliver(email) | |
if name.to_s =~ /^deliver_/ | |
email = self.send(name.to_s.sub("deliver", "create"), *args) | |
self.send_later(:deliver, email) | |
else | |
method_missing_without_delayed_job_mailer(name, *args) | |
end | |
end | |
alias_method_chain :method_missing, :delayed_job_mailer | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment