diff options
Diffstat (limited to 'actionmailer/lib')
-rw-r--r-- | actionmailer/lib/action_mailer.rb | 1 | ||||
-rw-r--r-- | actionmailer/lib/action_mailer/base.rb | 6 | ||||
-rw-r--r-- | actionmailer/lib/action_mailer/deliver_later.rb | 9 | ||||
-rw-r--r-- | actionmailer/lib/action_mailer/deliver_later/job.rb | 11 | ||||
-rw-r--r-- | actionmailer/lib/action_mailer/deliver_later/mail_message_wrapper.rb | 46 |
5 files changed, 72 insertions, 1 deletions
diff --git a/actionmailer/lib/action_mailer.rb b/actionmailer/lib/action_mailer.rb index 83969d4074..49a380419f 100644 --- a/actionmailer/lib/action_mailer.rb +++ b/actionmailer/lib/action_mailer.rb @@ -45,4 +45,5 @@ module ActionMailer autoload :Previews, 'action_mailer/preview' autoload :TestCase autoload :TestHelper + autoload :DeliverLater end diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index bc540aece0..df0c945434 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -549,7 +549,11 @@ module ActionMailer def method_missing(method_name, *args) # :nodoc: if respond_to?(method_name) - new(method_name, *args).message + if defined?(::ActiveJob) && action_methods.include?(method_name.to_s) + DeliverLater::MailMessageWrapper.new(self, method_name, *args) + else + new(method_name, *args).message + end else super end diff --git a/actionmailer/lib/action_mailer/deliver_later.rb b/actionmailer/lib/action_mailer/deliver_later.rb new file mode 100644 index 0000000000..5609e35d01 --- /dev/null +++ b/actionmailer/lib/action_mailer/deliver_later.rb @@ -0,0 +1,9 @@ +require 'active_job' + +module ActionMailer + module DeliverLater + extend ActiveSupport::Autoload + autoload :Job + autoload :MailMessageWrapper + end +end
\ No newline at end of file diff --git a/actionmailer/lib/action_mailer/deliver_later/job.rb b/actionmailer/lib/action_mailer/deliver_later/job.rb new file mode 100644 index 0000000000..fda3b626b2 --- /dev/null +++ b/actionmailer/lib/action_mailer/deliver_later/job.rb @@ -0,0 +1,11 @@ +module ActionMailer + module DeliverLater + class Job < ActiveJob::Base + queue_as :mailers + + def perform(mailer, mail_method, delivery_method, *args) + mailer.constantize.send(mail_method, *args).send(delivery_method) + end + end + end +end diff --git a/actionmailer/lib/action_mailer/deliver_later/mail_message_wrapper.rb b/actionmailer/lib/action_mailer/deliver_later/mail_message_wrapper.rb new file mode 100644 index 0000000000..5c0a5304d4 --- /dev/null +++ b/actionmailer/lib/action_mailer/deliver_later/mail_message_wrapper.rb @@ -0,0 +1,46 @@ +module ActionMailer + module DeliverLater + class MailMessageWrapper < Delegator + def initialize(mailer, mail_method, *args) + @mailer = mailer + @mail_method = mail_method + @args = args + __getobj__ + end + + def __getobj__ + @obj ||= @mailer.send(:new, @mail_method, *@args).message + end + + def __setobj__(obj) + @obj = obj + end + + def deliver_later!(options={}) + enqueue_delivery :deliver!, options + end + + def deliver_later(options={}) + enqueue_delivery :deliver, options + end + + def method_missing(m, *args, &block) + __getobj__.__send__(m, *args, &block) + end + + private + def enqueue_delivery(delivery_method, options={}) + args = @mailer.name, @mail_method.to_s, delivery_method.to_s, *@args + enqueue_method = :enqueue + if options[:at] + enqueue_method = :enqueue_at + args.unshift options[:at] + elsif options[:in] + enqueue_method = :enqueue_in + args.unshift options[:in] + end + ActionMailer::DeliverLater::Job.send enqueue_method, *args + end + end + end +end |