Skip to content

Commit

Permalink
Add new class delivery method API.
Browse files Browse the repository at this point in the history
  • Loading branch information
José Valim and Mikel Lindsaar committed Jan 24, 2010
1 parent 7409b73 commit f30d73b
Show file tree
Hide file tree
Showing 12 changed files with 259 additions and 271 deletions.
18 changes: 17 additions & 1 deletion actionmailer/lib/action_mailer/base.rb
Expand Up @@ -253,6 +253,8 @@ module ActionMailer #:nodoc:
# and appear last in the mime encoded message. You can also pick a different order from inside a method with
# +implicit_parts_order+.
class Base < AbstractController::Base
abstract!

include Quoting

include AbstractController::Logger
Expand All @@ -264,8 +266,8 @@ class Base < AbstractController::Base

helper ActionMailer::MailHelper

include ActionMailer::DeprecatedApi
extend ActionMailer::DeliveryMethods
include ActionMailer::DeprecatedApi

add_delivery_method :smtp, Mail::SMTP,
:address => "localhost",
Expand Down Expand Up @@ -370,6 +372,20 @@ def set_payload_for_mail(payload, mail) #:nodoc:
payload[:date] = mail.date
payload[:mail] = mail.encoded
end

def respond_to?(method, *args)
super || action_methods.include?(method.to_s)
end

protected

def method_missing(method, *args)
if action_methods.include?(method.to_s)
new(method, *args).message
else
super
end
end
end

attr_internal :message
Expand Down
25 changes: 15 additions & 10 deletions actionmailer/lib/action_mailer/deprecated_api.rb
@@ -1,8 +1,8 @@
module ActionMailer
# TODO Remove this module all together in Rails 3.1. Ensure that super
# hooks in ActionMailer::Base are removed as well.
#
# Moved here to allow us to add the new Mail API
# Part of this API is deprecated and is going to be removed in Rails 3.1 (just check
# the methods which give you a warning).
# All the rest will be deprecated after 3.1 release instead, this allows a smoother
# migration path.
module DeprecatedApi #:nodoc:
extend ActiveSupport::Concern

Expand Down Expand Up @@ -83,8 +83,8 @@ module ClassMethods
# MyMailer.deliver(email)
def deliver(mail, show_warning=true)
if show_warning
ActiveSupport::Deprecation.warn "ActionMailer::Base.deliver is deprecated, just call " <<
"deliver in the instance instead", caller
ActiveSupport::Deprecation.warn "#{self}.deliver is deprecated, call " <<
"deliver in the mailer instance instead", caller[0,2]
end

raise "no mail object available for delivery!" unless mail
Expand All @@ -100,9 +100,14 @@ def respond_to?(method_symbol, include_private = false) #:nodoc:
def method_missing(method_symbol, *parameters) #:nodoc:
if match = matches_dynamic_method?(method_symbol)
case match[1]
when 'create' then new(match[2], *parameters).message
when 'deliver' then new(match[2], *parameters).deliver!
when 'new' then nil
when 'create'
ActiveSupport::Deprecation.warn "#{self}.create_#{match[2]} is deprecated, " <<
"use #{self}.#{match[2]} instead", caller[0,2]
new(match[2], *parameters).message
when 'deliver'
ActiveSupport::Deprecation.warn "#{self}.deliver_#{match[2]} is deprecated, " <<
"use #{self}.#{match[2]}.deliver instead", caller[0,2]
new(match[2], *parameters).deliver
else super
end
else
Expand Down Expand Up @@ -167,7 +172,6 @@ def part(params)
# Add an attachment to a multipart message. This is simply a part with the
# content-disposition set to "attachment".
def attachment(params, &block)
ActiveSupport::Deprecation.warn "attachment is deprecated, please use the attachments API instead", caller[0,2]
params = { :content_type => params } if String === params

params[:content] ||= params.delete(:data) || params.delete(:body)
Expand Down Expand Up @@ -259,6 +263,7 @@ def create_mail #:nodoc:
end
end

wrap_delivery_behavior!
m.content_transfer_encoding = '8bit' unless m.body.only_us_ascii?

@_message
Expand Down
12 changes: 5 additions & 7 deletions actionmailer/test/asset_host_test.rb
@@ -1,8 +1,8 @@
require 'abstract_unit'

class AssetHostMailer < ActionMailer::Base
def email_with_asset(recipient)
recipients recipient
def email_with_asset
recipients 'test@localhost'
subject "testing email containing asset path while asset_host is set"
from "tester@example.com"
end
Expand All @@ -13,8 +13,6 @@ def setup
set_delivery_method :test
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.deliveries.clear

@recipient = 'test@localhost'
end

def teardown
Expand All @@ -23,7 +21,7 @@ def teardown

def test_asset_host_as_string
ActionController::Base.asset_host = "http://www.example.com"
mail = AssetHostMailer.deliver_email_with_asset(@recipient)
mail = AssetHostMailer.email_with_asset
assert_equal "<img alt=\"Somelogo\" src=\"http://www.example.com/images/somelogo.png\" />", mail.body.to_s.strip
end

Expand All @@ -35,7 +33,7 @@ def test_asset_host_as_one_arguement_proc
"http://assets.example.com"
end
}
mail = AssetHostMailer.deliver_email_with_asset(@recipient)
mail = AssetHostMailer.email_with_asset
assert_equal "<img alt=\"Somelogo\" src=\"http://images.example.com/images/somelogo.png\" />", mail.body.to_s.strip
end

Expand All @@ -48,7 +46,7 @@ def test_asset_host_as_two_arguement_proc
end
}
mail = nil
assert_nothing_raised { mail = AssetHostMailer.deliver_email_with_asset(@recipient) }
assert_nothing_raised { mail = AssetHostMailer.email_with_asset }
assert_equal "<img alt=\"Somelogo\" src=\"http://www.example.com/images/somelogo.png\" />", mail.body.to_s.strip
end
end

0 comments on commit f30d73b

Please sign in to comment.