diff options
Diffstat (limited to 'actionmailer')
-rw-r--r-- | actionmailer/lib/action_mailer/base.rb | 2 | ||||
-rw-r--r-- | actionmailer/lib/action_mailer/queued_message.rb | 30 | ||||
-rw-r--r-- | actionmailer/test/abstract_unit.rb | 1 | ||||
-rw-r--r-- | actionmailer/test/base_test.rb | 23 | ||||
-rw-r--r-- | actionmailer/test/mailers/async_mailer.rb | 1 |
5 files changed, 30 insertions, 27 deletions
diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 26787c9b5e..1676c23856 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -4,6 +4,7 @@ require 'action_mailer/collector' require 'active_support/core_ext/string/inflections' require 'active_support/core_ext/hash/except' require 'active_support/core_ext/module/anonymous' +require 'active_support/queueing' require 'action_mailer/log_subscriber' module ActionMailer #:nodoc: @@ -393,6 +394,7 @@ module ActionMailer #:nodoc: }.freeze class_attribute :queue + self.queue = ActiveSupport::SynchronousQueue.new class << self # Register one or more Observers which will be notified when mail is delivered. diff --git a/actionmailer/lib/action_mailer/queued_message.rb b/actionmailer/lib/action_mailer/queued_message.rb index e5868ab43b..8d200617c4 100644 --- a/actionmailer/lib/action_mailer/queued_message.rb +++ b/actionmailer/lib/action_mailer/queued_message.rb @@ -5,23 +5,33 @@ module ActionMailer attr_reader :queue def initialize(queue, mailer_class, method_name, *args) - @queue = queue - @mailer_class = mailer_class - @method_name = method_name - @args = args + @queue = queue + @job = DeliveryJob.new(mailer_class, method_name, args) end def __getobj__ - @actual_message ||= @mailer_class.send(:new, @method_name, *@args).message + @job.message end - def run - __getobj__.deliver + # Queues the message for delivery. + def deliver + tap { @queue.push @job } end - # Will push the message onto the Queue to be processed - def deliver - @queue << self + class DeliveryJob + def initialize(mailer_class, method_name, args) + @mailer_class = mailer_class + @method_name = method_name + @args = args + end + + def message + @message ||= @mailer_class.send(:new, @method_name, *@args).message + end + + def run + message.deliver + end end end end diff --git a/actionmailer/test/abstract_unit.rb b/actionmailer/test/abstract_unit.rb index 0b418c4ea1..4b38d4bd31 100644 --- a/actionmailer/test/abstract_unit.rb +++ b/actionmailer/test/abstract_unit.rb @@ -27,7 +27,6 @@ ActionView::Template.register_template_handler :bak, lambda { |template| "Lame b FIXTURE_LOAD_PATH = File.expand_path('fixtures', File.dirname(__FILE__)) ActionMailer::Base.view_paths = FIXTURE_LOAD_PATH -ActionMailer::Base.queue = ActiveSupport::SynchronousQueue.new class MockSMTP def self.deliveries diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb index 6a06cec041..4f2af50fdd 100644 --- a/actionmailer/test/base_test.rb +++ b/actionmailer/test/base_test.rb @@ -412,7 +412,7 @@ class BaseTest < ActiveSupport::TestCase BaseMailer.deliveries.clear BaseMailer.expects(:deliver_mail).once mail = BaseMailer.welcome.deliver - assert_instance_of Mail::Message, mail + assert_equal 'The first email on new API!', mail.subject end test "calling deliver on the action should increment the deliveries collection if using the test mailer" do @@ -422,24 +422,15 @@ class BaseTest < ActiveSupport::TestCase assert_equal(1, BaseMailer.deliveries.length) end - def stub_queue(klass, queue) - Class.new(klass) { - extend Module.new { - define_method :queue do - queue - end - } - } - end - test "delivering message asynchronously" do - testing_queue = ActiveSupport::TestQueue.new AsyncMailer.delivery_method = :test AsyncMailer.deliveries.clear - stub_queue(AsyncMailer, testing_queue).welcome.deliver - assert_equal(0, AsyncMailer.deliveries.length) - testing_queue.drain - assert_equal(1, AsyncMailer.deliveries.length) + + AsyncMailer.welcome.deliver + assert_equal 0, AsyncMailer.deliveries.length + + AsyncMailer.queue.drain + assert_equal 1, AsyncMailer.deliveries.length end test "calling deliver, ActionMailer should yield back to mail to let it call :do_delivery on itself" do diff --git a/actionmailer/test/mailers/async_mailer.rb b/actionmailer/test/mailers/async_mailer.rb index 8a87e2e1cf..c21a464f38 100644 --- a/actionmailer/test/mailers/async_mailer.rb +++ b/actionmailer/test/mailers/async_mailer.rb @@ -1,2 +1,3 @@ class AsyncMailer < BaseMailer + self.queue = ActiveSupport::TestQueue.new end |