mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Refactor DeliverLater into MessageDelivery
This commit is contained in:
parent
b937ea893e
commit
18303f6e82
9 changed files with 73 additions and 83 deletions
|
@ -45,5 +45,6 @@ module ActionMailer
|
||||||
autoload :Previews, 'action_mailer/preview'
|
autoload :Previews, 'action_mailer/preview'
|
||||||
autoload :TestCase
|
autoload :TestCase
|
||||||
autoload :TestHelper
|
autoload :TestHelper
|
||||||
autoload :DeliverLater
|
autoload :MessageDelivery
|
||||||
|
autoload :DelayedDeliveryJob
|
||||||
end
|
end
|
||||||
|
|
|
@ -548,12 +548,8 @@ module ActionMailer
|
||||||
end
|
end
|
||||||
|
|
||||||
def method_missing(method_name, *args) # :nodoc:
|
def method_missing(method_name, *args) # :nodoc:
|
||||||
if respond_to?(method_name)
|
if action_methods.include?(method_name.to_s)
|
||||||
if defined?(::ActiveJob) && action_methods.include?(method_name.to_s)
|
MessageDelivery.new(self, method_name, *args)
|
||||||
DeliverLater::MailMessageWrapper.new(self, method_name, *args)
|
|
||||||
else
|
|
||||||
new(method_name, *args).message
|
|
||||||
end
|
|
||||||
else
|
else
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
11
actionmailer/lib/action_mailer/delayed_delivery_job.rb
Normal file
11
actionmailer/lib/action_mailer/delayed_delivery_job.rb
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
require 'active_job'
|
||||||
|
|
||||||
|
module ActionMailer
|
||||||
|
class DelayedDeliveryJob < 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
|
|
@ -1,9 +0,0 @@
|
||||||
require 'active_job'
|
|
||||||
|
|
||||||
module ActionMailer
|
|
||||||
module DeliverLater
|
|
||||||
extend ActiveSupport::Autoload
|
|
||||||
autoload :Job
|
|
||||||
autoload :MailMessageWrapper
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -1,11 +0,0 @@
|
||||||
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
|
|
|
@ -1,46 +0,0 @@
|
||||||
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
|
|
47
actionmailer/lib/action_mailer/message_delivery.rb
Normal file
47
actionmailer/lib/action_mailer/message_delivery.rb
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
module ActionMailer
|
||||||
|
class MessageDelivery < Delegator
|
||||||
|
def initialize(mailer, mail_method, *args)
|
||||||
|
@mailer = mailer
|
||||||
|
@mail_method = mail_method
|
||||||
|
@args = args
|
||||||
|
end
|
||||||
|
|
||||||
|
def __getobj__
|
||||||
|
@obj ||= @mailer.send(:new, @mail_method, *@args).message
|
||||||
|
end
|
||||||
|
|
||||||
|
def __setobj__(obj)
|
||||||
|
@obj = obj
|
||||||
|
end
|
||||||
|
|
||||||
|
def message #:nodoc:
|
||||||
|
__getobj__
|
||||||
|
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::DelayedDeliveryJob.send enqueue_method, *args
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -4,6 +4,7 @@ require 'set'
|
||||||
|
|
||||||
require 'action_dispatch'
|
require 'action_dispatch'
|
||||||
require 'active_support/time'
|
require 'active_support/time'
|
||||||
|
require 'active_support/core_ext/object/itself'
|
||||||
|
|
||||||
require 'mailers/base_mailer'
|
require 'mailers/base_mailer'
|
||||||
require 'mailers/proc_mailer'
|
require 'mailers/proc_mailer'
|
||||||
|
@ -243,7 +244,7 @@ class BaseTest < ActiveSupport::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
e = assert_raises(RuntimeError) { LateAttachmentMailer.welcome }
|
e = assert_raises(RuntimeError) { LateAttachmentMailer.welcome.message }
|
||||||
assert_match(/Can't add attachments after `mail` was called./, e.message)
|
assert_match(/Can't add attachments after `mail` was called./, e.message)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -255,7 +256,7 @@ class BaseTest < ActiveSupport::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
e = assert_raises(RuntimeError) { LateInlineAttachmentMailer.welcome }
|
e = assert_raises(RuntimeError) { LateInlineAttachmentMailer.welcome.message }
|
||||||
assert_match(/Can't add attachments after `mail` was called./, e.message)
|
assert_match(/Can't add attachments after `mail` was called./, e.message)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
gem 'activejob'
|
gem 'activejob'
|
||||||
require 'action_mailer/deliver_later'
|
require 'active_job'
|
||||||
require 'abstract_unit'
|
require 'abstract_unit'
|
||||||
require 'minitest/mock'
|
require 'minitest/mock'
|
||||||
require_relative 'mailers/delayed_mailer'
|
require_relative 'mailers/delayed_mailer'
|
||||||
|
|
||||||
class MailerTest < ActiveSupport::TestCase
|
class MessageDeliveryTest < ActiveSupport::TestCase
|
||||||
|
|
||||||
setup do
|
setup do
|
||||||
@previous_logger = ActiveJob::Base.logger
|
@previous_logger = ActiveJob::Base.logger
|
||||||
|
@ -21,8 +21,8 @@ class MailerTest < ActiveSupport::TestCase
|
||||||
ActionMailer::Base.delivery_method = @previous_delivery_method
|
ActionMailer::Base.delivery_method = @previous_delivery_method
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'should be a MailMessageWrapper' do
|
test 'should be a MessageDelivery' do
|
||||||
assert_equal @mail.class, ActionMailer::DeliverLater::MailMessageWrapper
|
assert_equal @mail.class, ActionMailer::MessageDelivery
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'its object should be a Mail::Message' do
|
test 'its object should be a Mail::Message' do
|
||||||
|
@ -51,21 +51,21 @@ class MailerTest < ActiveSupport::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'should enqueue the email with :deliver delivery method' do
|
test 'should enqueue the email with :deliver delivery method' do
|
||||||
ret = ActionMailer::DeliverLater::Job.stub :enqueue, ->(*args){ args } do
|
ret = ActionMailer::DelayedDeliveryJob.stub :enqueue, ->(*args){ args } do
|
||||||
@mail.deliver_later
|
@mail.deliver_later
|
||||||
end
|
end
|
||||||
assert_equal ret, ["DelayedMailer", "test_message", "deliver", 1, 2, 3]
|
assert_equal ret, ["DelayedMailer", "test_message", "deliver", 1, 2, 3]
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'should enqueue the email with :deliver! delivery method' do
|
test 'should enqueue the email with :deliver! delivery method' do
|
||||||
ret = ActionMailer::DeliverLater::Job.stub :enqueue, ->(*args){ args } do
|
ret = ActionMailer::DelayedDeliveryJob.stub :enqueue, ->(*args){ args } do
|
||||||
@mail.deliver_later!
|
@mail.deliver_later!
|
||||||
end
|
end
|
||||||
assert_equal ret, ["DelayedMailer", "test_message", "deliver!", 1, 2, 3]
|
assert_equal ret, ["DelayedMailer", "test_message", "deliver!", 1, 2, 3]
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'should enqueue a delivery with a delay' do
|
test 'should enqueue a delivery with a delay' do
|
||||||
ret = ActionMailer::DeliverLater::Job.stub :enqueue_in, ->(*args){ args } do
|
ret = ActionMailer::DelayedDeliveryJob.stub :enqueue_in, ->(*args){ args } do
|
||||||
@mail.deliver_later in: 600
|
@mail.deliver_later in: 600
|
||||||
end
|
end
|
||||||
assert_equal ret, [600, "DelayedMailer", "test_message", "deliver", 1, 2, 3]
|
assert_equal ret, [600, "DelayedMailer", "test_message", "deliver", 1, 2, 3]
|
||||||
|
@ -73,7 +73,7 @@ class MailerTest < ActiveSupport::TestCase
|
||||||
|
|
||||||
test 'should enqueue a delivery at a specific time' do
|
test 'should enqueue a delivery at a specific time' do
|
||||||
later_time = Time.now.to_i + 3600
|
later_time = Time.now.to_i + 3600
|
||||||
ret = ActionMailer::DeliverLater::Job.stub :enqueue_at, ->(*args){ args } do
|
ret = ActionMailer::DelayedDeliveryJob.stub :enqueue_at, ->(*args){ args } do
|
||||||
@mail.deliver_later at: later_time
|
@mail.deliver_later at: later_time
|
||||||
end
|
end
|
||||||
assert_equal ret, [later_time, "DelayedMailer", "test_message", "deliver", 1, 2, 3]
|
assert_equal ret, [later_time, "DelayedMailer", "test_message", "deliver", 1, 2, 3]
|
Loading…
Reference in a new issue