Permalink
Browse files

Support for custom queues on the mailer

Credit goes to *Aaron Patterson* (tenderlove)
  • Loading branch information...
1 parent dee0b23 commit 812d1e88c5218d4075878559476eaad65df1b761 @bcardarella bcardarella committed Jun 24, 2012
Showing with 30 additions and 18 deletions.
  1. +13 −12 actionmailer/lib/action_mailer/async.rb
  2. +1 −1 actionmailer/lib/action_mailer/base.rb
  3. +16 −5 actionmailer/test/base_test.rb
View
25 actionmailer/lib/action_mailer/async.rb
@@ -1,22 +1,23 @@
require 'delegate'
module ActionMailer::Async
- def self.included(base)
- base.extend(ClassMethods)
+ def method_missing(method_name, *args)
+ if action_methods.include?(method_name.to_s)
+ QueuedMessage.new(queue, self, method_name, *args)
+ else
+ super
+ end
end
- module ClassMethods
- def method_missing(method_name, *args)
- if action_methods.include?(method_name.to_s)
- QueuedMessage.new(self, method_name, *args)
- else
- super
- end
- end
+ def queue
+ Rails.queue
end
class QueuedMessage < ::Delegator
- def initialize(mailer_class, method_name, *args)
+ attr_reader :queue
+
+ def initialize(queue, mailer_class, method_name, *args)
+ @queue = queue
@mailer_class = mailer_class
@method_name = method_name
@args = args
@@ -37,7 +38,7 @@ def deliver(force = false)
if force
run
else
- Rails.queue << self
+ @queue << self
end
end
end
View
2 actionmailer/lib/action_mailer/base.rb
@@ -460,7 +460,7 @@ def respond_to?(method, include_private = false) #:nodoc:
def async=(truth)
if truth
require 'action_mailer/async'
- include ActionMailer::Async
+ extend ActionMailer::Async
end
end
View
21 actionmailer/test/base_test.rb
@@ -422,22 +422,33 @@ def teardown
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
- Rails.stubs(:queue).returns(Rails::Queueing::TestQueue.new)
+ testing_queue = Rails::Queueing::TestQueue.new
AsyncMailer.delivery_method = :test
AsyncMailer.deliveries.clear
- AsyncMailer.welcome.deliver
+ stub_queue(AsyncMailer, testing_queue).welcome.deliver
assert_equal(0, AsyncMailer.deliveries.length)
- Rails.queue.drain
+ testing_queue.drain
assert_equal(1, AsyncMailer.deliveries.length)
end
test "forcing message delivery despite asynchronous" do
- Rails.stubs(:queue).returns(Rails::Queueing::TestQueue.new)
+ testing_queue = Rails::Queueing::TestQueue.new
AsyncMailer.delivery_method = :test
AsyncMailer.deliveries.clear
- AsyncMailer.welcome.deliver(true)
+ stub_queue(AsyncMailer, testing_queue).welcome.deliver(true)
assert_equal(1, AsyncMailer.deliveries.length)
+ assert_predicate testing_queue, :empty?
end
test "calling deliver, ActionMailer should yield back to mail to let it call :do_delivery on itself" do

0 comments on commit 812d1e8

Please sign in to comment.