aboutsummaryrefslogtreecommitdiffstats
path: root/actionmailer
diff options
context:
space:
mode:
authorJosé Valim and Mikel Lindsaar <pair@programming.com>2010-01-26 11:21:20 +0100
committerJosé Valim and Mikel Lindsaar <pair@programming.com>2010-01-26 11:21:20 +0100
commit9dd65c368b40093cfc686956a48c18b78abacf7f (patch)
tree6b014805fa1ef780ff267536c751566d09981fb0 /actionmailer
parentb8c82edc1f5b95bceea3d40de778ec0cd5a37d15 (diff)
downloadrails-9dd65c368b40093cfc686956a48c18b78abacf7f.tar.gz
rails-9dd65c368b40093cfc686956a48c18b78abacf7f.tar.bz2
rails-9dd65c368b40093cfc686956a48c18b78abacf7f.zip
Make defaults accept a hash.
Diffstat (limited to 'actionmailer')
-rw-r--r--actionmailer/lib/action_mailer/base.rb78
-rw-r--r--actionmailer/test/base_test.rb85
2 files changed, 84 insertions, 79 deletions
diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb
index 5adf4973b5..dbaf1424ed 100644
--- a/actionmailer/lib/action_mailer/base.rb
+++ b/actionmailer/lib/action_mailer/base.rb
@@ -254,8 +254,13 @@ module ActionMailer #:nodoc:
private_class_method :new #:nodoc:
- extlib_inheritable_accessor :defaults
- self.defaults = {}
+ 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" ]
+ }
extlib_inheritable_accessor :default_charset
self.default_charset = "utf-8"
@@ -283,6 +288,11 @@ module ActionMailer #:nodoc:
attr_writer :mailer_name
alias :controller_path :mailer_name
+ 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,
# instantiates a new mailer, and passes the email object to the mailer
# object's +receive+ method. If you want your mailer to be able to
@@ -425,10 +435,10 @@ module ActionMailer #:nodoc:
# You can set default values for any of the above headers (except :date) by using the <tt>defaults</tt>
# class method:
#
- # class Notifier
- # self.defaults = {:from => 'no-reply@test.lindsaar.net',
- # :bcc => 'email_logger@test.lindsaar.net',
- # :reply_to => 'bounces@test.lindsaar.net' }
+ # 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.
@@ -475,40 +485,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 = set_defaults(headers)
+ # 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_sort_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?
@@ -518,23 +534,13 @@ module ActionMailer #:nodoc:
when m.multipart?
["multipart", "alternative", params]
else
- self.class.default_content_type.dup
+ class_default
end
end
- def set_defaults(headers)
- headers[:subject] ||= default_subject
- headers[:to] ||= self.class.defaults[:to].to_s.dup
- headers[:from] ||= self.class.defaults[:from].to_s.dup
- headers[:cc] ||= self.class.defaults[:cc].to_s.dup
- headers[:bcc] ||= self.class.defaults[:bcc].to_s.dup
- headers[:reply_to] ||= self.class.defaults[:reply_to].to_s.dup
- headers
- end
-
- def default_subject #:nodoc:
+ def default_i18n_subject #:nodoc:
mailer_scope = self.class.mailer_name.gsub('/', '.')
- self.class.defaults[:subject] || I18n.t(:subject, :scope => [:actionmailer, mailer_scope, action_name], :default => action_name.humanize)
+ I18n.t(:subject, :scope => [:actionmailer, mailer_scope, action_name], :default => action_name.humanize)
end
# TODO: Move this into Mail
@@ -546,7 +552,6 @@ 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:
@@ -588,8 +593,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 }
+ responses[0].each { |k,v| m[k] = v }
return responses[0][:content_type]
elsif responses.size > 1 && m.has_attachments?
container = Mail::Part.new
diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb
index 20a35ddc73..7abdf7f4f8 100644
--- a/actionmailer/test/base_test.rb
+++ b/actionmailer/test/base_test.rb
@@ -2,72 +2,61 @@
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
-
- self.defaults = {:to => 'system@test.lindsaar.net',
- :from => 'jose@test.plataformatec.com',
- :reply_to => 'mikel@test.lindsaar.net',
- :subject => 'Default Subject!'}
-
self.mailer_name = "base_mailer"
- def empty(hash = {})
- mail(hash)
- end
+ self.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 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
@@ -81,19 +70,11 @@ class BaseTest < ActiveSupport::TestCase
# Basic mail usage without block
test "mail() should set the headers of the mail message" do
email = BaseMailer.welcome.deliver
- assert_equal(['mikel@test.lindsaar.net'], email.to)
+ 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() should pull the defaults from the class if nothing is specified" do
- email = BaseMailer.empty.deliver
- assert_equal(['system@test.lindsaar.net'], email.to)
- assert_equal(['jose@test.plataformatec.com'], email.from)
- assert_equal(['mikel@test.lindsaar.net'], email.reply_to)
- assert_equal('Default Subject!', email.subject)
- end
-
test "mail() with from overwrites the class level default" do
email = BaseMailer.welcome(:from => 'someone@example.com',
:to => 'another@example.org').deliver
@@ -184,7 +165,7 @@ class BaseTest < ActiveSupport::TestCase
# Defaults values
test "uses default charset from class" do
- swap BaseMailer, :default_charset => "US-ASCII" do
+ with_default BaseMailer, :charset => "US-ASCII" do
email = BaseMailer.welcome.deliver
assert_equal("US-ASCII", email.charset)
@@ -194,7 +175,7 @@ class BaseTest < ActiveSupport::TestCase
end
test "uses default content type from class" do
- swap BaseMailer, :default_content_type => "text/html" do
+ with_default BaseMailer, :content_type => "text/html" do
email = BaseMailer.welcome.deliver
assert_equal("text/html", email.mime_type)
@@ -204,7 +185,7 @@ class BaseTest < ActiveSupport::TestCase
end
test "uses default mime version from class" do
- swap BaseMailer, :default_mime_version => "2.0" do
+ with_default BaseMailer, :mime_version => "2.0" do
email = BaseMailer.welcome.deliver
assert_equal("2.0", email.mime_version)
@@ -213,6 +194,13 @@ class BaseTest < ActiveSupport::TestCase
end
end
+ test "uses default headers from class" do
+ with_default BaseMailer, "X-SPAM" => "Not spam" do
+ email = BaseMailer.welcome.deliver
+ assert_equal("Not spam", email["X-SPAM"].decoded)
+ end
+ end
+
test "subject gets default from I18n" do
BaseMailer.defaults[:subject] = nil
email = BaseMailer.welcome(:subject => nil).deliver
@@ -236,7 +224,7 @@ class BaseTest < ActiveSupport::TestCase
test "implicit multipart with sort order" do
order = ["text/html", "text/plain"]
- swap BaseMailer, :default_implicit_parts_order => order do
+ with_default BaseMailer, :parts_order => order do
email = BaseMailer.implicit_multipart.deliver
assert_equal("text/html", email.parts[0].mime_type)
assert_equal("text/plain", email.parts[1].mime_type)
@@ -259,7 +247,7 @@ 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
+ with_default BaseMailer, :parts_order => order do
email = BaseMailer.implicit_multipart(:attachments => true).deliver
assert_equal("application/pdf", email.parts[0].mime_type)
assert_equal("multipart/alternative", email.parts[1].mime_type)
@@ -323,7 +311,7 @@ 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
+ with_default BaseMailer, :parts_order => order do
email = BaseMailer.explicit_multipart.deliver
assert_equal("text/plain", email.parts[0].mime_type)
assert_equal("text/html", email.parts[1].mime_type)
@@ -424,17 +412,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 \ No newline at end of file