Permalink
Browse files

Delegated ActionMailer::Base.deliveries to Mail.deliveries, added cal…

…lback support in Mail to call ActionMailer on delivery, moved deliver to deprecated API in preparation for new API
  • Loading branch information...
José Valim and Mikel Lindsaar
José Valim and Mikel Lindsaar committed Jan 24, 2010
1 parent e7e4ed4 commit 258ca148004eaa63740ab2186338b3ac872b8187
@@ -11,7 +11,7 @@ Gem::Specification.new do |s|
s.homepage = "http://www.rubyonrails.org"
s.add_dependency('actionpack', '= 3.0.pre')
- s.add_dependency('mail', '~> 2.0.3')
+ s.add_dependency('mail', '~> 2.0.5')
s.files = Dir['CHANGELOG', 'README', 'MIT-LICENSE', 'lib/**/*']
s.has_rdoc = true
@@ -275,8 +275,16 @@ class Base < AbstractController::Base
@@perform_deliveries = true
cattr_accessor :perform_deliveries
- @@deliveries = []
- cattr_accessor :deliveries
+ # Provides a list of emails that have been delivered by Mail
+ def self.deliveries
+ Mail.deliveries
+ end
+
+ # Allows you to over write the default deliveries store from an array to some
+ # other object. If you just want to clear the store, call Mail.deliveries.clear.
+ def self.deliveries=(val)
+ Mail.deliveries = val
+ end
extlib_inheritable_accessor :default_charset
self.default_charset = "utf-8"
@@ -342,35 +350,6 @@ def receive(raw_mail)
end
end
- # Deliver the given mail object directly. This can be used to deliver
- # a preconstructed mail object, like:
- #
- # email = MyMailer.create_some_mail(parameters)
- # email.set_some_obscure_header "frobnicate"
- # MyMailer.deliver(email)
- def deliver(mail)
- raise "no mail object available for delivery!" unless mail
-
- ActiveSupport::Notifications.instrument("action_mailer.deliver", :mailer => self.name) do |payload|
- self.set_payload_for_mail(payload, mail)
-
- mail.delivery_method delivery_methods[delivery_method],
- delivery_settings[delivery_method]
-
- begin
- # TODO Move me to the instance
- if @@perform_deliveries
- mail.deliver!
- self.deliveries << mail
- end
- rescue Exception => e # Net::SMTP errors or sendmail pipe errors
- raise e if raise_delivery_errors
- end
- end
-
- mail
- end
-
def template_root
self.view_paths && self.view_paths.first
end
@@ -380,6 +359,12 @@ def template_root=(root)
self.view_paths = ActionView::Base.process_view_paths(root)
end
+ def delivered_email(mail)
+ ActiveSupport::Notifications.instrument("action_mailer.deliver", :mailer => self.name) do |payload|
+ self.set_payload_for_mail(payload, mail)
+ end
+ end
+
def set_payload_for_mail(payload, mail) #:nodoc:
payload[:message_id] = mail.message_id
payload[:subject] = mail.subject
@@ -402,13 +387,6 @@ def initialize(method_name=nil, *args)
process(method_name, *args) if method_name
end
- # Delivers a Mail object. By default, it delivers the cached mail
- # object (from the <tt>create!</tt> method). If no cached mail object exists, and
- # no alternate has been given as the parameter, this will fail.
- def deliver!(mail = @message)
- self.class.deliver(mail)
- end
-
# TODO Add new delivery method goodness
def mail(headers = {})
# Guard flag to prevent both the old and the new API from firing
@@ -417,6 +395,8 @@ def mail(headers = {})
m = @message
+ m.register_for_delivery_notification(self.class)
+
# Give preference to headers and fallback to the ones set in mail
content_type = headers[:content_type] || m.content_type
charset = headers[:charset] || m.charset || self.class.default_charset.dup
@@ -71,9 +71,34 @@ module DeprecatedApi #:nodoc:
# Alias controller_path to mailer_name so render :partial in views work.
alias :controller_path :mailer_name
+
end
module ClassMethods
+
+ # Deliver the given mail object directly. This can be used to deliver
+ # a preconstructed mail object, like:
+ #
+ # email = MyMailer.create_some_mail(parameters)
+ # email.set_some_obscure_header "frobnicate"
+ # MyMailer.deliver(email)
+ def deliver(mail)
+ raise "no mail object available for delivery!" unless mail
+
+ ActiveSupport::Notifications.instrument("action_mailer.deliver", :mailer => self.name) do |payload|
+ self.set_payload_for_mail(payload, mail)
+
+ mail.delivery_method delivery_methods[delivery_method],
+ delivery_settings[delivery_method]
+
+ mail.raise_delivery_errors = raise_delivery_errors
+ mail.perform_deliveries = perform_deliveries
+ mail.deliver
+ end
+
+ mail
+ end
+
def respond_to?(method_symbol, include_private = false) #:nodoc:
matches_dynamic_method?(method_symbol) || super
end
@@ -104,6 +129,13 @@ def initialize(*)
@mail_was_called = false
end
+ # Delivers a Mail object. By default, it delivers the cached mail
+ # object (from the <tt>create!</tt> method). If no cached mail object exists, and
+ # no alternate has been given as the parameter, this will fail.
+ def deliver!(mail = @message)
+ self.class.deliver(mail)
+ end
+
def render(*args)
options = args.last.is_a?(Hash) ? args.last : {}
if options[:body]
@@ -37,7 +37,7 @@ def determine_default_mailer(name)
def initialize_test_deliveries
ActionMailer::Base.delivery_method = :test
ActionMailer::Base.perform_deliveries = true
- ActionMailer::Base.deliveries = []
+ ActionMailer::Base.deliveries.clear
end
def set_expected_mail
@@ -12,7 +12,7 @@ class AssetHostTest < Test::Unit::TestCase
def setup
set_delivery_method :test
ActionMailer::Base.perform_deliveries = true
- ActionMailer::Base.deliveries = []
+ ActionMailer::Base.deliveries.clear
@recipient = 'test@localhost'
end
@@ -324,6 +324,15 @@ def explicit_multipart_with_any(hash = {})
assert_equal("Format with any!", email.parts[1].body.encoded)
end
+ test "ActionMailer should be told when Mail gets delivered" do
+ BaseMailer.expects(:delivered_email).once
+ email = BaseMailer.deliver_welcome
+ end
+
+ # test "ActionMailer should be told when Mail gets delivered using new API" do
+ # BaseMailer.expects(:delivered_email).once
+ # email = BaseMailer.welcome.deliver
+ # end
protected
@@ -63,7 +63,7 @@ def new_mail( charset="utf-8" )
def setup
set_delivery_method :test
ActionMailer::Base.perform_deliveries = true
- ActionMailer::Base.deliveries = []
+ ActionMailer::Base.deliveries.clear
@recipient = 'test@localhost'
end
@@ -55,7 +55,7 @@ class LayoutMailerTest < Test::Unit::TestCase
def setup
set_delivery_method :test
ActionMailer::Base.perform_deliveries = true
- ActionMailer::Base.deliveries = []
+ ActionMailer::Base.deliveries.clear
@recipient = 'test@localhost'
end
@@ -86,7 +86,7 @@ class RenderHelperTest < Test::Unit::TestCase
def setup
set_delivery_method :test
ActionMailer::Base.perform_deliveries = true
- ActionMailer::Base.deliveries = []
+ ActionMailer::Base.deliveries.clear
@recipient = 'test@localhost'
end
@@ -135,7 +135,7 @@ class FirstSecondHelperTest < Test::Unit::TestCase
def setup
set_delivery_method :test
ActionMailer::Base.perform_deliveries = true
- ActionMailer::Base.deliveries = []
+ ActionMailer::Base.deliveries.clear
@recipient = 'test@localhost'
end
@@ -337,7 +337,7 @@ def setup
set_delivery_method :test
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
- ActionMailer::Base.deliveries = []
+ ActionMailer::Base.deliveries.clear
@original_logger = TestMailer.logger
@recipient = 'test@localhost'
@@ -658,7 +658,7 @@ def test_perform_deliveries_flag
def test_doesnt_raise_errors_when_raise_delivery_errors_is_false
ActionMailer::Base.raise_delivery_errors = false
- Mail::Message.any_instance.expects(:deliver!).raises(Exception)
+ Mail::TestMailer.any_instance.expects(:deliver!).raises(Exception)
assert_nothing_raised { TestMailer.deliver_signed_up(@recipient) }
end
@@ -1113,7 +1113,7 @@ def send
def setup
set_delivery_method :test
ActionMailer::Base.perform_deliveries = true
- ActionMailer::Base.deliveries = []
+ ActionMailer::Base.deliveries.clear
end
def teardown
@@ -44,7 +44,7 @@ def new_mail( charset="utf-8" )
def setup
set_delivery_method :test
ActionMailer::Base.perform_deliveries = true
- ActionMailer::Base.deliveries = []
+ ActionMailer::Base.deliveries.clear
@recipient = 'test@localhost'
end

0 comments on commit 258ca14

Please sign in to comment.