1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge remote branch 'mikel/master'

This commit is contained in:
José Valim 2010-01-26 16:00:57 +01:00
commit 1078677575
7 changed files with 252 additions and 162 deletions

View file

@ -23,7 +23,8 @@ module ActionMailer #:nodoc:
# Examples:
#
# class Notifier < ActionMailer::Base
# delivers_from 'system@example.com'
# defaults :from => 'no-reply@example.com',
# :return_path => 'system@example.com'
#
# def welcome(recipient)
# @account = recipient
@ -40,13 +41,17 @@ module ActionMailer #:nodoc:
# * <tt>headers[]=</tt> - Allows you to specify non standard headers in your email such
# as <tt>headers['X-No-Spam'] = 'True'</tt>
#
# * <tt>headers(hash)</tt> - Allows you to specify multiple headers in your email such
# as <tt>headers({'X-No-Spam' => 'True', 'In-Reply-To' => '1234@message.id'})</tt>
#
# * <tt>mail</tt> - Allows you to specify your email to send.
#
# The hash passed to the mail method allows you to specify the most used headers in an email
# message, such as <tt>Subject</tt>, <tt>To</tt>, <tt>From</tt>, <tt>Cc</tt>, <tt>Bcc</tt>,
# <tt>Reply-To</tt> and <tt>Date</tt>. See the <tt>ActionMailer#mail</tt> method for more details.
#
# If you need other headers not listed above, use the <tt>headers['name'] = value</tt> method.
# The hash passed to the mail method allows you to specify any header that a Mail::Message
# will accept (any valid Email header including optional fields). Obviously if you specify
# the same header in the headers method and then again in the mail method, the last one
# will over write the first, unless you are specifying a header field that can appear more
# than once per RFC, in which case, both will be inserted (X-value headers for example can
# appear multiple times.)
#
# The mail method, if not passed a block, will inspect your views and send all the views with
# the same name as the method, so the above action would send the +welcome.plain.erb+ view file
@ -186,9 +191,14 @@ module ActionMailer #:nodoc:
#
# These options are specified on the class level, like <tt>ActionMailer::Base.template_root = "/my/templates"</tt>
#
# * <tt>delivers_from</tt> - Pass this the address that then defaults as the +from+ address on all the
# emails sent. Can be overridden on a per mail basis by passing <tt>:from => 'another@address'</tt> in
# the +mail+ method.
# * <tt>defaults</tt> - This is a class wide hash of <tt>:key => value</tt> pairs containing
# default values for the specified header fields of the <tt>Mail::Message</tt>. You can
# specify a default for any valid header for <tt>Mail::Message</tt> and it will be used if
# you do not override it. The defaults set by Action Mailer are:
# * <tt>:mime_version => "1.0"</tt>
# * <tt>:charset => "utf-8",</tt>
# * <tt>:content_type => "text/plain",</tt>
# * <tt>:parts_order => [ "text/plain", "text/enriched", "text/html" ]</tt>
#
# * <tt>logger</tt> - the logger is used for generating information on the mailing run if available.
# Can be set to nil for no logging. Compatible with both Ruby's own Logger and Log4r loggers.
@ -222,20 +232,19 @@ module ActionMailer #:nodoc:
# * <tt>deliveries</tt> - Keeps an array of all the emails sent out through the Action Mailer with <tt>delivery_method :test</tt>. Most useful
# for unit and functional testing.
#
# * <tt>default_charset</tt> - The default charset used for the body and to encode the subject. Defaults to UTF-8. You can also
# pick a different charset from inside a method with +charset+.
# * <tt>default_charset</tt> - This is now deprecated, use the +defaults+ method above to
# set the default +:charset+.
#
# * <tt>default_content_type</tt> - The default content type used for the main part of the message. Defaults to "text/plain". You
# can also pick a different content type from inside a method with +content_type+.
# * <tt>default_content_type</tt> - This is now deprecated, use the +defaults+ method above
# to set the default +:content_type+.
#
# * <tt>default_mime_version</tt> - The default mime version used for the message. Defaults to <tt>1.0</tt>. You
# can also pick a different value from inside a method with +mime_version+.
# * <tt>default_mime_version</tt> - This is now deprecated, use the +defaults+ method above
# to set the default +:mime_version+.
#
# * <tt>default_implicit_parts_order</tt> - When a message is built implicitly (i.e. multiple parts are assembled from templates
# which specify the content type in their filenames) this variable controls how the parts are ordered. Defaults to
# <tt>["text/html", "text/enriched", "text/plain"]</tt>. Items that appear first in the array have higher priority in the mail client
# and appear last in the mime encoded message. You can also pick a different order from inside a method with
# +implicit_parts_order+.
# * <tt>default_implicit_parts_order</tt> - This is now deprecated, use the +defaults+ method above
# to set the default +:parts_order+. Parts Order is used when a message is built implicitly
# (i.e. multiple parts are assembled from templates which specify the content type in their
# filenames) this variable controls how the parts are ordered.
class Base < AbstractController::Base
include DeliveryMethods, Quoting
abstract!
@ -254,38 +263,25 @@ module ActionMailer #:nodoc:
private_class_method :new #:nodoc:
extlib_inheritable_accessor :default_from
self.default_from = nil
extlib_inheritable_accessor :default_charset
self.default_charset = "utf-8"
extlib_inheritable_accessor :default_content_type
self.default_content_type = "text/plain"
extlib_inheritable_accessor :default_mime_version
self.default_mime_version = "1.0"
# This specifies the order that the parts of a multipart email will be. Usually you put
# text/plain at the top so someone without a MIME capable email reader can read the plain
# text of your email first.
#
# Any content type that is not listed here will be inserted in the order you add them to
# the email after the content types you list here.
extlib_inheritable_accessor :default_implicit_parts_order
self.default_implicit_parts_order = [ "text/plain", "text/enriched", "text/html" ]
extlib_inheritable_accessor :default_params
self.default_params = {
:mime_version => "1.0",
:charset => "utf-8",
:content_type => "text/plain",
:parts_order => [ "text/plain", "text/enriched", "text/html" ]
}
class << self
def mailer_name
@mailer_name ||= name.underscore
end
attr_writer :mailer_name
alias :controller_path :mailer_name
# Sets who is the default sender for the e-mail
def delivers_from(value = nil)
self.default_from = value if value
self.default_from
def defaults(value=nil)
self.default_params.merge!(value) if value
self.default_params
end
# Receives a raw email, parses it into an email object, decodes it,
@ -361,13 +357,18 @@ module ActionMailer #:nodoc:
#
# headers['X-Special-Domain-Specific-Header'] = "SecretValue"
#
# You can also pass a hash into headers of header field names and values, which
# will then be set on the Mail::Message object:
#
# headers 'X-Special-Domain-Specific-Header' => "SecretValue",
# 'In-Reply-To' => incoming.message_id
#
# The resulting Mail::Message will have the following in it's header:
#
# X-Special-Domain-Specific-Header: SecretValue
def headers(args=nil)
if args
ActiveSupport::Deprecation.warn "headers(Hash) is deprecated, please do headers[key] = value instead", caller[0,2]
@headers = args
@_message.headers(args)
else
@_message
end
@ -419,8 +420,7 @@ module ActionMailer #:nodoc:
# humanized version of the <tt>action_name</tt>
# * <tt>:to</tt> - Who the message is destined for, can be a string of addresses, or an array
# of addresses.
# * <tt>:from</tt> - Who the message is from, if missing, will use the <tt>:delivers_from</tt>
# value in the class (if it exists)
# * <tt>:from</tt> - Who the message is from
# * <tt>:cc</tt> - Who you would like to Carbon-Copy on this email, can be a string of addresses,
# or an array of addresses.
# * <tt>:bcc</tt> - Who you would like to Blind-Carbon-Copy on this email, can be a string of
@ -428,6 +428,15 @@ module ActionMailer #:nodoc:
# * <tt>:reply_to</tt> - Who to set the Reply-To header of the email to.
# * <tt>:date</tt> - The date to say the email was sent on.
#
# You can set default values for any of the above headers (except :date) by using the <tt>defaults</tt>
# class method:
#
# class Notifier < ActionMailer::Base
# self.defaults :from => 'no-reply@test.lindsaar.net',
# :bcc => 'email_logger@test.lindsaar.net',
# :reply_to => 'bounces@test.lindsaar.net'
# end
#
# If you need other headers not listed above, use the <tt>headers['name'] = value</tt> method.
#
# When a <tt>:return_path</tt> is specified as header, that value will be used as the 'envelope from'
@ -472,40 +481,46 @@ module ActionMailer #:nodoc:
@mail_was_called = true
m = @_message
# 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
mime_version = headers[:mime_version] || m.mime_version || self.class.default_mime_version.dup
# At the beginning, do not consider class default for parts order neither content_type
content_type = headers[:content_type]
parts_order = headers[:parts_order]
# Set fields quotings
headers[:subject] ||= default_subject
headers[:from] ||= self.class.default_from.dup
# Merge defaults from class
headers = headers.reverse_merge(self.class.defaults)
charset = headers[:charset]
# Quote fields
headers[:subject] ||= default_i18n_subject
quote_fields!(headers, charset)
# Render the templates and blocks
responses, sort_order = collect_responses_and_sort_order(headers, &block)
responses, explicit_order = collect_responses_and_parts_order(headers, &block)
create_parts_from_responses(m, responses, charset)
# Tidy up content type, charset, mime version and sort order
m.content_type = set_content_type(m, content_type)
# Finally setup content type and parts order
m.content_type = set_content_type(m, content_type, headers[:content_type])
m.charset = charset
m.mime_version = mime_version
sort_order = headers[:parts_order] || sort_order || self.class.default_implicit_parts_order.dup
if m.multipart?
m.body.set_sort_order(sort_order)
parts_order ||= explicit_order || headers[:parts_order]
m.body.set_sort_order(parts_order)
m.body.sort_parts!
end
# Finaly set delivery behavior configured in class
# Set configure delivery behavior
wrap_delivery_behavior!(headers[:delivery_method])
# Remove headers already treated and assign all others
headers.except!(:subject, :to, :from, :cc, :bcc, :reply_to)
headers.except!(:body, :parts_order, :content_type, :charset, :delivery_method)
headers.each { |k, v| m[k] = v }
m
end
protected
def set_content_type(m, user_content_type)
def set_content_type(m, user_content_type, class_default)
params = m.content_type_parameters || {}
case
when user_content_type.present?
@ -515,11 +530,11 @@ module ActionMailer #:nodoc:
when m.multipart?
["multipart", "alternative", params]
else
self.class.default_content_type.dup
class_default
end
end
def default_subject #:nodoc:
def default_i18n_subject #:nodoc:
mailer_scope = self.class.mailer_name.gsub('/', '.')
I18n.t(:subject, :scope => [:actionmailer, mailer_scope, action_name], :default => action_name.humanize)
end
@ -533,21 +548,20 @@ module ActionMailer #:nodoc:
m.cc ||= quote_address_if_necessary(headers[:cc], charset) if headers[:cc]
m.bcc ||= quote_address_if_necessary(headers[:bcc], charset) if headers[:bcc]
m.reply_to ||= quote_address_if_necessary(headers[:reply_to], charset) if headers[:reply_to]
m.date ||= headers[:date] if headers[:date]
end
def collect_responses_and_sort_order(headers) #:nodoc:
responses, sort_order = [], nil
def collect_responses_and_parts_order(headers) #:nodoc:
responses, parts_order = [], nil
if block_given?
collector = ActionMailer::Collector.new(self) { render(action_name) }
yield(collector)
sort_order = collector.responses.map { |r| r[:content_type] }
parts_order = collector.responses.map { |r| r[:content_type] }
responses = collector.responses
elsif headers[:body]
responses << {
:body => headers[:body],
:content_type => self.class.default_content_type.dup
:content_type => self.class.defaults[:content_type] || "text/plain"
}
else
each_template do |template|
@ -558,7 +572,7 @@ module ActionMailer #:nodoc:
end
end
[responses, sort_order]
[responses, parts_order]
end
def each_template(&block) #:nodoc:
@ -575,9 +589,7 @@ module ActionMailer #:nodoc:
def create_parts_from_responses(m, responses, charset) #:nodoc:
if responses.size == 1 && !m.has_attachments?
headers = responses[0]
headers.each { |k,v| m[k] = v }
return responses[0][:content_type]
responses[0].each { |k,v| m[k] = v }
elsif responses.size > 1 && m.has_attachments?
container = Mail::Part.new
container.content_type = "multipart/alternative"

View file

@ -65,7 +65,10 @@ module ActionMailer
method ||= self.delivery_method
mail.delivery_handler = self
if method.is_a?(Symbol)
case method
when NilClass
raise "Delivery method cannot be nil"
when Symbol
if klass = delivery_methods[method.to_sym]
mail.delivery_method(klass, send(:"#{method}_settings"))
else

View file

@ -5,8 +5,25 @@ module ActionMailer
module DeprecatedApi #:nodoc:
extend ActiveSupport::Concern
module ClassMethods
included do
[:charset, :content_type, :mime_version, :implicit_parts_order].each do |method|
class_eval <<-FILE, __FILE__, __LINE__ + 1
def self.default_#{method}
@@default_#{method}
end
def self.default_#{method}=(value)
ActiveSupport::Deprecation.warn "ActionMailer::Base.default_#{method}=value is deprecated, " <<
"use defaults :#{method} => value instead"
@@default_#{method} = value
end
@@default_#{method} = nil
FILE
end
end
module ClassMethods
# Deliver the given mail object directly. This can be used to deliver
# a preconstructed mail object, like:
#
@ -99,7 +116,15 @@ module ActionMailer
end
private
def initialize_defaults(*)
@charset ||= self.class.default_charset.try(:dup)
@content_type ||= self.class.default_content_type.try(:dup)
@implicit_parts_order ||= self.class.default_implicit_parts_order.try(:dup)
@mime_version ||= self.class.default_mime_version.try(:dup)
super
end
def create_parts
if @body.is_a?(Hash) && !@body.empty?
ActiveSupport::Deprecation.warn "Giving a hash to body is deprecated, please use instance variables instead", caller[0,2]

View file

@ -1,3 +1,5 @@
require 'active_support/core_ext/object/try'
module ActionMailer
module OldApi #:nodoc:
extend ActiveSupport::Concern
@ -185,10 +187,10 @@ module ActionMailer
# mailer. Subclasses may override this method to provide different
# defaults.
def initialize_defaults(method_name)
@charset ||= self.class.default_charset.dup
@content_type ||= self.class.default_content_type.dup
@implicit_parts_order ||= self.class.default_implicit_parts_order.dup
@mime_version ||= self.class.default_mime_version.dup if self.class.default_mime_version
@charset ||= self.class.defaults[:charset].try(:dup)
@content_type ||= self.class.defaults[:content_type].try(:dup)
@implicit_parts_order ||= self.class.defaults[:parts_order].try(:dup)
@mime_version ||= self.class.defaults[:mime_version].try(:dup)
@mailer_name ||= self.class.mailer_name.dup
@template ||= method_name

View file

@ -2,63 +2,70 @@
require 'abstract_unit'
class BaseTest < ActiveSupport::TestCase
DEFAULT_HEADERS = {
:to => 'mikel@test.lindsaar.net',
:subject => 'The first email on new API!'
}
class BaseMailer < ActionMailer::Base
delivers_from 'jose@test.plataformatec.com'
self.mailer_name = "base_mailer"
defaults :to => 'system@test.lindsaar.net',
:from => 'jose@test.plataformatec.com',
:reply_to => 'mikel@test.lindsaar.net'
def welcome(hash = {})
headers['X-SPAM'] = "Not SPAM"
mail(DEFAULT_HEADERS.merge(hash))
mail({:subject => "The first email on new API!"}.merge!(hash))
end
def simple(hash = {})
mail(hash)
end
def simple_with_headers(hash = {})
headers hash
mail
end
def attachment_with_content(hash = {})
attachments['invoice.pdf'] = 'This is test File content'
mail(DEFAULT_HEADERS.merge(hash))
mail(hash)
end
def attachment_with_hash
attachments['invoice.jpg'] = { :data => "you smiling", :mime_type => "image/x-jpg",
:transfer_encoding => "base64" }
mail(DEFAULT_HEADERS)
mail
end
def implicit_multipart(hash = {})
attachments['invoice.pdf'] = 'This is test File content' if hash.delete(:attachments)
mail(DEFAULT_HEADERS.merge(hash))
mail(hash)
end
def implicit_with_locale(hash = {})
mail(DEFAULT_HEADERS.merge(hash))
mail(hash)
end
def explicit_multipart(hash = {})
attachments['invoice.pdf'] = 'This is test File content' if hash.delete(:attachments)
mail(DEFAULT_HEADERS.merge(hash)) do |format|
mail(hash) do |format|
format.text { render :text => "TEXT Explicit Multipart" }
format.html { render :text => "HTML Explicit Multipart" }
end
end
def explicit_multipart_templates(hash = {})
mail(DEFAULT_HEADERS.merge(hash)) do |format|
mail(hash) do |format|
format.html
format.text
end
end
def explicit_multipart_with_any(hash = {})
mail(DEFAULT_HEADERS.merge(hash)) do |format|
mail(hash) do |format|
format.any(:text, :html){ render :text => "Format with any!" }
end
end
def custom_block(include_html=false)
mail(DEFAULT_HEADERS) do |format|
mail do |format|
format.text(:content_transfer_encoding => "base64"){ render "welcome" }
format.html{ render "welcome" } if include_html
end
@ -66,20 +73,22 @@ class BaseTest < ActiveSupport::TestCase
end
test "method call to mail does not raise error" do
assert_nothing_raised { BaseMailer.welcome.deliver }
assert_nothing_raised { BaseMailer.welcome }
end
# Basic mail usage without block
test "mail() should set the headers of the mail message" do
email = BaseMailer.welcome.deliver
assert_equal(email.to, ['mikel@test.lindsaar.net'])
assert_equal(email.from, ['jose@test.plataformatec.com'])
assert_equal(email.subject, 'The first email on new API!')
email = BaseMailer.welcome
assert_equal(['system@test.lindsaar.net'], email.to)
assert_equal(['jose@test.plataformatec.com'], email.from)
assert_equal('The first email on new API!', email.subject)
end
test "mail() with from overwrites the class level default" do
email = BaseMailer.welcome(:from => 'someone@else.com').deliver
assert_equal(email.from, ['someone@else.com'])
email = BaseMailer.welcome(:from => 'someone@example.com',
:to => 'another@example.org')
assert_equal(['someone@example.com'], email.from)
assert_equal(['another@example.org'], email.to)
end
test "mail() with bcc, cc, content_type, charset, mime_version, reply_to and date" do
@ -90,61 +99,77 @@ class BaseTest < ActiveSupport::TestCase
:charset => 'iso-8559-1',
:mime_version => '2.0',
:reply_to => 'reply-to@test.lindsaar.net',
:date => @time).deliver
assert_equal(email.bcc, ['bcc@test.lindsaar.net'])
assert_equal(email.cc, ['cc@test.lindsaar.net'])
assert_equal(email.content_type, 'multipart/mixed')
assert_equal(email.charset, 'iso-8559-1')
assert_equal(email.mime_version, '2.0')
assert_equal(email.reply_to, ['reply-to@test.lindsaar.net'])
assert_equal(email.date, @time)
:date => @time)
assert_equal(['bcc@test.lindsaar.net'], email.bcc)
assert_equal(['cc@test.lindsaar.net'], email.cc)
assert_equal('multipart/mixed', email.content_type)
assert_equal('iso-8559-1', email.charset)
assert_equal('2.0', email.mime_version)
assert_equal(['reply-to@test.lindsaar.net'], email.reply_to)
assert_equal(@time, email.date)
end
test "mail() renders the template using the method being processed" do
email = BaseMailer.welcome.deliver
email = BaseMailer.welcome
assert_equal("Welcome", email.body.encoded)
end
test "can pass in :body to the mail method hash" do
email = BaseMailer.welcome(:body => "Hello there").deliver
email = BaseMailer.welcome(:body => "Hello there")
assert_equal("text/plain", email.mime_type)
assert_equal("Hello there", email.body.encoded)
end
# Custom headers
test "custom headers" do
email = BaseMailer.welcome.deliver
email = BaseMailer.welcome
assert_equal("Not SPAM", email['X-SPAM'].decoded)
end
test "can pass random headers in as a hash to mail" do
hash = {'X-Special-Domain-Specific-Header' => "SecretValue",
'In-Reply-To' => '1234@mikel.me.com' }
mail = BaseMailer.simple(hash)
assert_equal('SecretValue', mail['X-Special-Domain-Specific-Header'].decoded)
assert_equal('1234@mikel.me.com', mail['In-Reply-To'].decoded)
end
test "can pass random headers in as a hash" do
hash = {'X-Special-Domain-Specific-Header' => "SecretValue",
'In-Reply-To' => '1234@mikel.me.com' }
mail = BaseMailer.simple_with_headers(hash)
assert_equal('SecretValue', mail['X-Special-Domain-Specific-Header'].decoded)
assert_equal('1234@mikel.me.com', mail['In-Reply-To'].decoded)
end
# Attachments
test "attachment with content" do
email = BaseMailer.attachment_with_content.deliver
email = BaseMailer.attachment_with_content
assert_equal(1, email.attachments.length)
assert_equal('invoice.pdf', email.attachments[0].filename)
assert_equal('This is test File content', email.attachments['invoice.pdf'].decoded)
end
test "attachment gets content type from filename" do
email = BaseMailer.attachment_with_content.deliver
email = BaseMailer.attachment_with_content
assert_equal('invoice.pdf', email.attachments[0].filename)
end
test "attachment with hash" do
email = BaseMailer.attachment_with_hash.deliver
email = BaseMailer.attachment_with_hash
assert_equal(1, email.attachments.length)
assert_equal('invoice.jpg', email.attachments[0].filename)
assert_equal("\312\213\254\232)b", email.attachments['invoice.jpg'].decoded)
end
test "sets mime type to multipart/mixed when attachment is included" do
email = BaseMailer.attachment_with_content.deliver
email = BaseMailer.attachment_with_content
assert_equal(1, email.attachments.length)
assert_equal("multipart/mixed", email.mime_type)
end
test "adds the rendered template as part" do
email = BaseMailer.attachment_with_content.deliver
email = BaseMailer.attachment_with_content
assert_equal(2, email.parts.length)
assert_equal("multipart/mixed", email.mime_type)
assert_equal("text/html", email.parts[0].mime_type)
@ -154,7 +179,7 @@ class BaseTest < ActiveSupport::TestCase
end
test "adds the given :body as part" do
email = BaseMailer.attachment_with_content(:body => "I'm the eggman").deliver
email = BaseMailer.attachment_with_content(:body => "I'm the eggman")
assert_equal(2, email.parts.length)
assert_equal("multipart/mixed", email.mime_type)
assert_equal("text/plain", email.parts[0].mime_type)
@ -165,47 +190,55 @@ class BaseTest < ActiveSupport::TestCase
# Defaults values
test "uses default charset from class" do
swap BaseMailer, :default_charset => "US-ASCII" do
email = BaseMailer.welcome.deliver
with_default BaseMailer, :charset => "US-ASCII" do
email = BaseMailer.welcome
assert_equal("US-ASCII", email.charset)
email = BaseMailer.welcome(:charset => "iso-8559-1").deliver
email = BaseMailer.welcome(:charset => "iso-8559-1")
assert_equal("iso-8559-1", email.charset)
end
end
test "uses default content type from class" do
swap BaseMailer, :default_content_type => "text/html" do
email = BaseMailer.welcome.deliver
with_default BaseMailer, :content_type => "text/html" do
email = BaseMailer.welcome
assert_equal("text/html", email.mime_type)
email = BaseMailer.welcome(:content_type => "text/plain").deliver
email = BaseMailer.welcome(:content_type => "text/plain")
assert_equal("text/plain", email.mime_type)
end
end
test "uses default mime version from class" do
swap BaseMailer, :default_mime_version => "2.0" do
email = BaseMailer.welcome.deliver
with_default BaseMailer, :mime_version => "2.0" do
email = BaseMailer.welcome
assert_equal("2.0", email.mime_version)
email = BaseMailer.welcome(:mime_version => "1.0").deliver
email = BaseMailer.welcome(:mime_version => "1.0")
assert_equal("1.0", email.mime_version)
end
end
test "uses random default headers from class" do
with_default BaseMailer, "X-SPAM" => "Not spam" do
email = BaseMailer.simple
assert_equal("Not spam", email["X-SPAM"].decoded)
end
end
test "subject gets default from I18n" do
email = BaseMailer.welcome(:subject => nil).deliver
BaseMailer.defaults[:subject] = nil
email = BaseMailer.welcome(:subject => nil)
assert_equal "Welcome", email.subject
I18n.backend.store_translations('en', :actionmailer => {:base_mailer => {:welcome => {:subject => "New Subject!"}}})
email = BaseMailer.welcome(:subject => nil).deliver
email = BaseMailer.welcome(:subject => nil)
assert_equal "New Subject!", email.subject
end
# Implicit multipart
test "implicit multipart" do
email = BaseMailer.implicit_multipart.deliver
email = BaseMailer.implicit_multipart
assert_equal(2, email.parts.size)
assert_equal("multipart/alternative", email.mime_type)
assert_equal("text/plain", email.parts[0].mime_type)
@ -216,19 +249,19 @@ class BaseTest < ActiveSupport::TestCase
test "implicit multipart with sort order" do
order = ["text/html", "text/plain"]
swap BaseMailer, :default_implicit_parts_order => order do
email = BaseMailer.implicit_multipart.deliver
with_default BaseMailer, :parts_order => order do
email = BaseMailer.implicit_multipart
assert_equal("text/html", email.parts[0].mime_type)
assert_equal("text/plain", email.parts[1].mime_type)
email = BaseMailer.implicit_multipart(:parts_order => order.reverse).deliver
email = BaseMailer.implicit_multipart(:parts_order => order.reverse)
assert_equal("text/plain", email.parts[0].mime_type)
assert_equal("text/html", email.parts[1].mime_type)
end
end
test "implicit multipart with attachments creates nested parts" do
email = BaseMailer.implicit_multipart(:attachments => true).deliver
email = BaseMailer.implicit_multipart(:attachments => true)
assert_equal("application/pdf", email.parts[0].mime_type)
assert_equal("multipart/alternative", email.parts[1].mime_type)
assert_equal("text/plain", email.parts[1].parts[0].mime_type)
@ -239,8 +272,8 @@ class BaseTest < ActiveSupport::TestCase
test "implicit multipart with attachments and sort order" do
order = ["text/html", "text/plain"]
swap BaseMailer, :default_implicit_parts_order => order do
email = BaseMailer.implicit_multipart(:attachments => true).deliver
with_default BaseMailer, :parts_order => order do
email = BaseMailer.implicit_multipart(:attachments => true)
assert_equal("application/pdf", email.parts[0].mime_type)
assert_equal("multipart/alternative", email.parts[1].mime_type)
assert_equal("text/plain", email.parts[1].parts[1].mime_type)
@ -249,7 +282,7 @@ class BaseTest < ActiveSupport::TestCase
end
test "implicit multipart with default locale" do
email = BaseMailer.implicit_with_locale.deliver
email = BaseMailer.implicit_with_locale
assert_equal(2, email.parts.size)
assert_equal("multipart/alternative", email.mime_type)
assert_equal("text/plain", email.parts[0].mime_type)
@ -260,7 +293,7 @@ class BaseTest < ActiveSupport::TestCase
test "implicit multipart with other locale" do
swap I18n, :locale => :pl do
email = BaseMailer.implicit_with_locale.deliver
email = BaseMailer.implicit_with_locale
assert_equal(2, email.parts.size)
assert_equal("multipart/alternative", email.mime_type)
assert_equal("text/plain", email.parts[0].mime_type)
@ -273,7 +306,7 @@ class BaseTest < ActiveSupport::TestCase
test "implicit multipart with several view paths uses the first one with template" do
begin
BaseMailer.view_paths.unshift(File.join(FIXTURE_LOAD_PATH, "another.path"))
email = BaseMailer.welcome.deliver
email = BaseMailer.welcome
assert_equal("Welcome from another path", email.body.encoded)
ensure
BaseMailer.view_paths.shift
@ -283,7 +316,7 @@ class BaseTest < ActiveSupport::TestCase
test "implicit multipart with inexistent templates uses the next view path" do
begin
BaseMailer.view_paths.unshift(File.join(FIXTURE_LOAD_PATH, "unknown"))
email = BaseMailer.welcome.deliver
email = BaseMailer.welcome
assert_equal("Welcome", email.body.encoded)
ensure
BaseMailer.view_paths.shift
@ -292,7 +325,7 @@ class BaseTest < ActiveSupport::TestCase
# Explicit multipart
test "explicit multipart" do
email = BaseMailer.explicit_multipart.deliver
email = BaseMailer.explicit_multipart
assert_equal(2, email.parts.size)
assert_equal("multipart/alternative", email.mime_type)
assert_equal("text/plain", email.parts[0].mime_type)
@ -303,19 +336,19 @@ class BaseTest < ActiveSupport::TestCase
test "explicit multipart does not sort order" do
order = ["text/html", "text/plain"]
swap BaseMailer, :default_implicit_parts_order => order do
email = BaseMailer.explicit_multipart.deliver
with_default BaseMailer, :parts_order => order do
email = BaseMailer.explicit_multipart
assert_equal("text/plain", email.parts[0].mime_type)
assert_equal("text/html", email.parts[1].mime_type)
email = BaseMailer.explicit_multipart(:parts_order => order.reverse).deliver
email = BaseMailer.explicit_multipart(:parts_order => order.reverse)
assert_equal("text/plain", email.parts[0].mime_type)
assert_equal("text/html", email.parts[1].mime_type)
end
end
test "explicit multipart with attachments creates nested parts" do
email = BaseMailer.explicit_multipart(:attachments => true).deliver
email = BaseMailer.explicit_multipart(:attachments => true)
assert_equal("application/pdf", email.parts[0].mime_type)
assert_equal("multipart/alternative", email.parts[1].mime_type)
assert_equal("text/plain", email.parts[1].parts[0].mime_type)
@ -325,7 +358,7 @@ class BaseTest < ActiveSupport::TestCase
end
test "explicit multipart with templates" do
email = BaseMailer.explicit_multipart_templates.deliver
email = BaseMailer.explicit_multipart_templates
assert_equal(2, email.parts.size)
assert_equal("multipart/alternative", email.mime_type)
assert_equal("text/html", email.parts[0].mime_type)
@ -335,7 +368,7 @@ class BaseTest < ActiveSupport::TestCase
end
test "explicit multipart with any" do
email = BaseMailer.explicit_multipart_with_any.deliver
email = BaseMailer.explicit_multipart_with_any
assert_equal(2, email.parts.size)
assert_equal("multipart/alternative", email.mime_type)
assert_equal("text/plain", email.parts[0].mime_type)
@ -345,7 +378,8 @@ class BaseTest < ActiveSupport::TestCase
end
test "explicit multipart with options" do
email = BaseMailer.custom_block(true).deliver
email = BaseMailer.custom_block(true)
email.ready_to_send!
assert_equal(2, email.parts.size)
assert_equal("multipart/alternative", email.mime_type)
assert_equal("text/plain", email.parts[0].mime_type)
@ -355,7 +389,7 @@ class BaseTest < ActiveSupport::TestCase
end
test "explicit multipart with one part is rendered as body" do
email = BaseMailer.custom_block.deliver
email = BaseMailer.custom_block
assert_equal(0, email.parts.size)
assert_equal("text/plain", email.mime_type)
assert_equal("base64", email.content_transfer_encoding)
@ -379,7 +413,8 @@ class BaseTest < ActiveSupport::TestCase
test "calling deliver on the action should deliver the mail object" do
BaseMailer.deliveries.clear
BaseMailer.expects(:deliver_mail).once
BaseMailer.welcome.deliver
mail = BaseMailer.welcome.deliver
assert_instance_of Mail::Message, mail
end
test "calling deliver on the action should increment the deliveries collection" do
@ -404,17 +439,30 @@ class BaseTest < ActiveSupport::TestCase
# Execute the block setting the given values and restoring old values after
# the block is executed.
def swap(object, new_values)
def swap(klass, new_values)
old_values = {}
new_values.each do |key, value|
old_values[key] = object.send key
object.send :"#{key}=", value
old_values[key] = klass.send key
klass.send :"#{key}=", value
end
yield
ensure
old_values.each do |key, value|
object.send :"#{key}=", value
klass.send :"#{key}=", value
end
end
def with_default(klass, new_values)
hash = klass.defaults
old_values = {}
new_values.each do |key, value|
old_values[key] = hash[key]
hash[key] = value
end
yield
ensure
old_values.each do |key, value|
hash[key] = value
end
end
end

View file

@ -1,5 +1,5 @@
class <%= class_name %> < ActionMailer::Base
delivers_from "from@example.com"
self.defaults :from => "from@example.com"
<% for action in actions -%>
# Subject can be set in your I18n file at config/locales/en.yml
@ -9,7 +9,7 @@ class <%= class_name %> < ActionMailer::Base
#
def <%= action %>
@greeting = "Hi"
mail(:to => "to@example.com")
mail(:to => "to@example.org")
end
<% end -%>
end

View file

@ -9,7 +9,7 @@ class MailerGeneratorTest < Rails::Generators::TestCase
run_generator
assert_file "app/mailers/notifier.rb" do |mailer|
assert_match /class Notifier < ActionMailer::Base/, mailer
assert_match /delivers_from "from@example.com"/, mailer
assert_match /self\.defaults :from => "from@example.com"/, mailer
end
end
@ -61,12 +61,12 @@ class MailerGeneratorTest < Rails::Generators::TestCase
assert_file "app/mailers/notifier.rb" do |mailer|
assert_instance_method :foo, mailer do |foo|
assert_match /mail\(:to => "to@example.com"\)/, foo
assert_match /mail\(:to => "to@example.org"\)/, foo
assert_match /@greeting = "Hi"/, foo
end
assert_instance_method :bar, mailer do |bar|
assert_match /mail\(:to => "to@example.com"\)/, bar
assert_match /mail\(:to => "to@example.org"\)/, bar
assert_match /@greeting = "Hi"/, bar
end
end