aboutsummaryrefslogtreecommitdiffstats
path: root/actionmailer
diff options
context:
space:
mode:
Diffstat (limited to 'actionmailer')
-rw-r--r--actionmailer/lib/action_mailer.rb1
-rw-r--r--actionmailer/lib/action_mailer/base.rb6
-rw-r--r--actionmailer/lib/action_mailer/deliver_later.rb9
-rw-r--r--actionmailer/lib/action_mailer/deliver_later/job.rb11
-rw-r--r--actionmailer/lib/action_mailer/deliver_later/mail_message_wrapper.rb46
-rw-r--r--actionmailer/test/deliver_later_test.rb82
-rw-r--r--actionmailer/test/mailers/delayed_mailer.rb6
7 files changed, 160 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
diff --git a/actionmailer/test/deliver_later_test.rb b/actionmailer/test/deliver_later_test.rb
new file mode 100644
index 0000000000..829c96872f
--- /dev/null
+++ b/actionmailer/test/deliver_later_test.rb
@@ -0,0 +1,82 @@
+# encoding: utf-8
+gem 'activejob'
+require 'action_mailer/deliver_later'
+require 'abstract_unit'
+require 'minitest/mock'
+require_relative 'mailers/delayed_mailer'
+
+class MailerTest < ActiveSupport::TestCase
+
+ setup do
+ @previous_logger = ActiveJob::Base.logger
+ @previous_delivery_method = ActionMailer::Base.delivery_method
+ ActionMailer::Base.delivery_method = :test
+ ActiveJob::Base.logger = Logger.new('/dev/null')
+ @mail = DelayedMailer.test_message(1, 2, 3)
+ ActionMailer::Base.deliveries.clear
+ end
+
+ teardown do
+ ActiveJob::Base.logger = @previous_logger
+ ActionMailer::Base.delivery_method = @previous_delivery_method
+ end
+
+ test 'should be a MailMessageWrapper' do
+ assert_equal @mail.class, ActionMailer::DeliverLater::MailMessageWrapper
+ end
+
+ test 'its object should be a Mail::Message' do
+ assert_equal @mail.__getobj__.class, Mail::Message
+ end
+
+ test 'should respond to .deliver' do
+ assert_respond_to @mail, :deliver
+ end
+
+ test 'should respond to .deliver!' do
+ assert_respond_to @mail, :deliver!
+ end
+
+ test 'should respond to .deliver_later' do
+ assert_respond_to @mail, :deliver_later
+ end
+
+ test 'should respond to .deliver_later!' do
+ assert_respond_to @mail, :deliver_later!
+ end
+
+ test 'should enqueue and run correctly in activejob' do
+ @mail.deliver_later!
+ assert_equal ActionMailer::Base.deliveries.size, 1
+ end
+
+ test 'should enqueue the email with :deliver delivery method' do
+ ret = ActionMailer::DeliverLater::Job.stub :enqueue, ->(*args){ args } do
+ @mail.deliver_later
+ end
+ assert_equal ret, ["DelayedMailer", "test_message", "deliver", 1, 2, 3]
+ end
+
+ test 'should enqueue the email with :deliver! delivery method' do
+ ret = ActionMailer::DeliverLater::Job.stub :enqueue, ->(*args){ args } do
+ @mail.deliver_later!
+ end
+ assert_equal ret, ["DelayedMailer", "test_message", "deliver!", 1, 2, 3]
+ end
+
+ test 'should enqueue a delivery with a delay' do
+ ret = ActionMailer::DeliverLater::Job.stub :enqueue_in, ->(*args){ args } do
+ @mail.deliver_later in: 600
+ end
+ assert_equal ret, [600, "DelayedMailer", "test_message", "deliver", 1, 2, 3]
+ end
+
+ test 'should enqueue a delivery at a specific time' do
+ later_time = Time.now.to_i + 3600
+ ret = ActionMailer::DeliverLater::Job.stub :enqueue_at, ->(*args){ args } do
+ @mail.deliver_later at: later_time
+ end
+ assert_equal ret, [later_time, "DelayedMailer", "test_message", "deliver", 1, 2, 3]
+ end
+
+end
diff --git a/actionmailer/test/mailers/delayed_mailer.rb b/actionmailer/test/mailers/delayed_mailer.rb
new file mode 100644
index 0000000000..62d4baa434
--- /dev/null
+++ b/actionmailer/test/mailers/delayed_mailer.rb
@@ -0,0 +1,6 @@
+class DelayedMailer < ActionMailer::Base
+
+ def test_message(*)
+ mail(from: 'test-sender@test.com', to: 'test-receiver@test.com', subject: 'Test Subject', body: 'Test Body')
+ end
+end