diff options
-rw-r--r-- | actionmailer/CHANGELOG.md | 2 | ||||
-rw-r--r-- | actionmailer/lib/action_mailer/async.rb | 39 | ||||
-rw-r--r-- | actionmailer/lib/action_mailer/base.rb | 12 | ||||
-rw-r--r-- | actionmailer/test/base_test.rb | 23 | ||||
-rw-r--r-- | actionmailer/test/fixtures/async_mailer/welcome.erb | 1 | ||||
-rw-r--r-- | actionmailer/test/mailers/async_mailer.rb | 3 | ||||
-rw-r--r-- | railties/CHANGELOG.md | 3 | ||||
-rw-r--r-- | railties/lib/rails/generators/rails/app/templates/config/application.rb | 3 |
8 files changed, 86 insertions, 0 deletions
diff --git a/actionmailer/CHANGELOG.md b/actionmailer/CHANGELOG.md index a822412090..4d8f739403 100644 --- a/actionmailer/CHANGELOG.md +++ b/actionmailer/CHANGELOG.md @@ -2,6 +2,8 @@ * Raise an `ActionView::MissingTemplate` exception when no implicit template could be found. *Damien Mathieu* +* Asynchronously send messages via the Rails Queue *Brian Cardarella* + ## Rails 3.2.5 (Jun 1, 2012) ## * No changes. diff --git a/actionmailer/lib/action_mailer/async.rb b/actionmailer/lib/action_mailer/async.rb new file mode 100644 index 0000000000..86a5b0d035 --- /dev/null +++ b/actionmailer/lib/action_mailer/async.rb @@ -0,0 +1,39 @@ +require 'delegate' + +module ActionMailer::Async + 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 + + def queue + Rails.queue + end + + class QueuedMessage < ::Delegator + attr_reader :queue + + def initialize(queue, mailer_class, method_name, *args) + @queue = queue + @mailer_class = mailer_class + @method_name = method_name + @args = args + end + + def __getobj__ + @actual_message ||= @mailer_class.send(:new, @method_name, *@args).message + end + + def run + __getobj__.deliver + end + + # Will push the message onto the Queue to be processed + def deliver + @queue << self + end + end +end diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 739f9a52a9..89e0adc04d 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -456,6 +456,18 @@ module ActionMailer #:nodoc: super || action_methods.include?(method.to_s) end + # Will force ActionMailer to push new messages to the queue defined + # in the ActionMailer class when set to true + # + # class WelcomeMailer < ActionMailer::Base + # self.async = true + def async=(truth) + if truth + require 'action_mailer/async' + extend ActionMailer::Async + end + end + protected def set_payload_for_mail(payload, mail) #:nodoc: diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb index 1b2e39b3f7..144a6bfe39 100644 --- a/actionmailer/test/base_test.rb +++ b/actionmailer/test/base_test.rb @@ -7,6 +7,8 @@ require 'active_support/time' require 'mailers/base_mailer' require 'mailers/proc_mailer' require 'mailers/asset_mailer' +require 'mailers/async_mailer' +require 'rails/queueing' class BaseTest < ActiveSupport::TestCase def teardown @@ -419,6 +421,26 @@ 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 = Rails::Queueing::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) + end + test "calling deliver, ActionMailer should yield back to mail to let it call :do_delivery on itself" do mail = Mail::Message.new mail.expects(:do_delivery).once @@ -434,6 +456,7 @@ class BaseTest < ActiveSupport::TestCase end test "should raise if missing template in implicit render" do + BaseMailer.deliveries.clear assert_raises ActionView::MissingTemplate do BaseMailer.implicit_different_template('missing_template').deliver end diff --git a/actionmailer/test/fixtures/async_mailer/welcome.erb b/actionmailer/test/fixtures/async_mailer/welcome.erb new file mode 100644 index 0000000000..01f3f00c63 --- /dev/null +++ b/actionmailer/test/fixtures/async_mailer/welcome.erb @@ -0,0 +1 @@ +Welcome
\ No newline at end of file diff --git a/actionmailer/test/mailers/async_mailer.rb b/actionmailer/test/mailers/async_mailer.rb new file mode 100644 index 0000000000..ce601e7343 --- /dev/null +++ b/actionmailer/test/mailers/async_mailer.rb @@ -0,0 +1,3 @@ +class AsyncMailer < BaseMailer + self.async = true +end diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index e19aa68407..65f4b06699 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -39,6 +39,9 @@ * Rails::Plugin has gone. Instead of adding plugins to vendor/plugins use gems or bundler with path or git dependencies. *Santiago Pastorino* +* Set config.action_mailer.async = true to turn on asynchronous + message delivery *Brian Cardarella* + ## Rails 3.2.2 (March 1, 2012) ## diff --git a/railties/lib/rails/generators/rails/app/templates/config/application.rb b/railties/lib/rails/generators/rails/app/templates/config/application.rb index 2b8e5b5dcd..5915d20010 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/application.rb +++ b/railties/lib/rails/generators/rails/app/templates/config/application.rb @@ -67,5 +67,8 @@ module <%= app_const_base %> # Version of your assets, change this if you want to expire all your assets. config.assets.version = '1.0' <% end -%> + + # Enable app-wide asynchronous ActionMailer + # config.action_mailer.async = true end end |