From 18303f6e82258c1d6175f1faf1becd260397fb57 Mon Sep 17 00:00:00 2001 From: Abdelkader Boudih Date: Wed, 13 Aug 2014 21:50:38 +0000 Subject: [PATCH] Refactor DeliverLater into MessageDelivery --- actionmailer/lib/action_mailer.rb | 3 +- actionmailer/lib/action_mailer/base.rb | 8 +--- .../lib/action_mailer/delayed_delivery_job.rb | 11 +++++ .../lib/action_mailer/deliver_later.rb | 9 ---- .../lib/action_mailer/deliver_later/job.rb | 11 ----- .../deliver_later/mail_message_wrapper.rb | 46 ------------------ .../lib/action_mailer/message_delivery.rb | 47 +++++++++++++++++++ actionmailer/test/base_test.rb | 5 +- ...later_test.rb => message_delivery_test.rb} | 16 +++---- 9 files changed, 73 insertions(+), 83 deletions(-) create mode 100644 actionmailer/lib/action_mailer/delayed_delivery_job.rb delete mode 100644 actionmailer/lib/action_mailer/deliver_later.rb delete mode 100644 actionmailer/lib/action_mailer/deliver_later/job.rb delete mode 100644 actionmailer/lib/action_mailer/deliver_later/mail_message_wrapper.rb create mode 100644 actionmailer/lib/action_mailer/message_delivery.rb rename actionmailer/test/{deliver_later_test.rb => message_delivery_test.rb} (79%) diff --git a/actionmailer/lib/action_mailer.rb b/actionmailer/lib/action_mailer.rb index 49a380419f..1add881054 100644 --- a/actionmailer/lib/action_mailer.rb +++ b/actionmailer/lib/action_mailer.rb @@ -45,5 +45,6 @@ module ActionMailer autoload :Previews, 'action_mailer/preview' autoload :TestCase autoload :TestHelper - autoload :DeliverLater + autoload :MessageDelivery + autoload :DelayedDeliveryJob end diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index df0c945434..3c144c8e5c 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -548,12 +548,8 @@ module ActionMailer end def method_missing(method_name, *args) # :nodoc: - if respond_to?(method_name) - if defined?(::ActiveJob) && action_methods.include?(method_name.to_s) - DeliverLater::MailMessageWrapper.new(self, method_name, *args) - else - new(method_name, *args).message - end + if action_methods.include?(method_name.to_s) + MessageDelivery.new(self, method_name, *args) else super end diff --git a/actionmailer/lib/action_mailer/delayed_delivery_job.rb b/actionmailer/lib/action_mailer/delayed_delivery_job.rb new file mode 100644 index 0000000000..159198829f --- /dev/null +++ b/actionmailer/lib/action_mailer/delayed_delivery_job.rb @@ -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 diff --git a/actionmailer/lib/action_mailer/deliver_later.rb b/actionmailer/lib/action_mailer/deliver_later.rb deleted file mode 100644 index 5609e35d01..0000000000 --- a/actionmailer/lib/action_mailer/deliver_later.rb +++ /dev/null @@ -1,9 +0,0 @@ -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 deleted file mode 100644 index fda3b626b2..0000000000 --- a/actionmailer/lib/action_mailer/deliver_later/job.rb +++ /dev/null @@ -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 diff --git a/actionmailer/lib/action_mailer/deliver_later/mail_message_wrapper.rb b/actionmailer/lib/action_mailer/deliver_later/mail_message_wrapper.rb deleted file mode 100644 index 5c0a5304d4..0000000000 --- a/actionmailer/lib/action_mailer/deliver_later/mail_message_wrapper.rb +++ /dev/null @@ -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 diff --git a/actionmailer/lib/action_mailer/message_delivery.rb b/actionmailer/lib/action_mailer/message_delivery.rb new file mode 100644 index 0000000000..fa0aa43a2d --- /dev/null +++ b/actionmailer/lib/action_mailer/message_delivery.rb @@ -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 diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb index fd5f4e2831..c17e59f746 100644 --- a/actionmailer/test/base_test.rb +++ b/actionmailer/test/base_test.rb @@ -4,6 +4,7 @@ require 'set' require 'action_dispatch' require 'active_support/time' +require 'active_support/core_ext/object/itself' require 'mailers/base_mailer' require 'mailers/proc_mailer' @@ -243,7 +244,7 @@ class BaseTest < ActiveSupport::TestCase 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) end @@ -255,7 +256,7 @@ class BaseTest < ActiveSupport::TestCase 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) end diff --git a/actionmailer/test/deliver_later_test.rb b/actionmailer/test/message_delivery_test.rb similarity index 79% rename from actionmailer/test/deliver_later_test.rb rename to actionmailer/test/message_delivery_test.rb index 829c96872f..6ad9b099a3 100644 --- a/actionmailer/test/deliver_later_test.rb +++ b/actionmailer/test/message_delivery_test.rb @@ -1,11 +1,11 @@ # encoding: utf-8 gem 'activejob' -require 'action_mailer/deliver_later' +require 'active_job' require 'abstract_unit' require 'minitest/mock' require_relative 'mailers/delayed_mailer' -class MailerTest < ActiveSupport::TestCase +class MessageDeliveryTest < ActiveSupport::TestCase setup do @previous_logger = ActiveJob::Base.logger @@ -21,8 +21,8 @@ class MailerTest < ActiveSupport::TestCase ActionMailer::Base.delivery_method = @previous_delivery_method end - test 'should be a MailMessageWrapper' do - assert_equal @mail.class, ActionMailer::DeliverLater::MailMessageWrapper + test 'should be a MessageDelivery' do + assert_equal @mail.class, ActionMailer::MessageDelivery end test 'its object should be a Mail::Message' do @@ -51,21 +51,21 @@ class MailerTest < ActiveSupport::TestCase end 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 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 + ret = ActionMailer::DelayedDeliveryJob.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 + ret = ActionMailer::DelayedDeliveryJob.stub :enqueue_in, ->(*args){ args } do @mail.deliver_later in: 600 end 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 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 end assert_equal ret, [later_time, "DelayedMailer", "test_message", "deliver", 1, 2, 3]