aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-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
-rw-r--r--activejob/lib/active_job.rb2
-rw-r--r--activejob/lib/active_job/logging.rb4
-rw-r--r--activejob/lib/activejob.rb1
-rw-r--r--activejob/test/helper.rb5
11 files changed, 163 insertions, 10 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
diff --git a/activejob/lib/active_job.rb b/activejob/lib/active_job.rb
index ddfdda4fb4..f0a34ffb44 100644
--- a/activejob/lib/active_job.rb
+++ b/activejob/lib/active_job.rb
@@ -23,8 +23,6 @@
require 'active_support'
require 'active_support/rails'
-
-require 'active_job/railtie' if defined?(Rails)
require 'active_job/version'
module ActiveJob
diff --git a/activejob/lib/active_job/logging.rb b/activejob/lib/active_job/logging.rb
index d913aee03d..d20bc3efce 100644
--- a/activejob/lib/active_job/logging.rb
+++ b/activejob/lib/active_job/logging.rb
@@ -7,7 +7,7 @@ module ActiveJob
included do
cattr_accessor(:logger) { ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT)) }
- around_enqueue do |job, block, _|
+ around_enqueue do |_, block, _|
tag_logger do
block.call
end
@@ -17,7 +17,7 @@ module ActiveJob
tag_logger(job.class.name, job.job_id) do
payload = {adapter: job.class.queue_adapter, job: job.class, args: job.arguments}
ActiveSupport::Notifications.instrument("perform_start.active_job", payload.dup)
- ActiveSupport::Notifications.instrument("perform.active_job", payload) do |payload|
+ ActiveSupport::Notifications.instrument("perform.active_job", payload) do
block.call
end
end
diff --git a/activejob/lib/activejob.rb b/activejob/lib/activejob.rb
deleted file mode 100644
index fea38731af..0000000000
--- a/activejob/lib/activejob.rb
+++ /dev/null
@@ -1 +0,0 @@
-require 'active_job'
diff --git a/activejob/test/helper.rb b/activejob/test/helper.rb
index ad263c05cd..8e255c1696 100644
--- a/activejob/test/helper.rb
+++ b/activejob/test/helper.rb
@@ -1,7 +1,4 @@
-require 'bundler'
-Bundler.setup
-
-$LOAD_PATH << File.dirname(__FILE__) + "/../lib"
+require File.expand_path('../../../load_paths', __FILE__)
require 'active_job'