aboutsummaryrefslogtreecommitdiffstats
path: root/actionmailer
diff options
context:
space:
mode:
authorMikel Lindsaar <raasdnil@gmail.com>2010-01-26 17:08:55 +1100
committerMikel Lindsaar <raasdnil@gmail.com>2010-01-26 17:08:55 +1100
commit0b05acd42439b197f71e168354020393cbf42b4f (patch)
treec4125aa8561a5cba352e811c77b1ea1ecdf523ac /actionmailer
parent64f8c87b1dab9bc9b6324fd6a3d77945f4b62821 (diff)
downloadrails-0b05acd42439b197f71e168354020393cbf42b4f.tar.gz
rails-0b05acd42439b197f71e168354020393cbf42b4f.tar.bz2
rails-0b05acd42439b197f71e168354020393cbf42b4f.zip
Implementing class level :defaults hash, instead of delivers_from et al
Diffstat (limited to 'actionmailer')
-rw-r--r--actionmailer/lib/action_mailer/base.rb39
-rw-r--r--actionmailer/test/base_test.rb26
2 files changed, 49 insertions, 16 deletions
diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb
index 44df30b1ba..5adf4973b5 100644
--- a/actionmailer/lib/action_mailer/base.rb
+++ b/actionmailer/lib/action_mailer/base.rb
@@ -254,8 +254,8 @@ module ActionMailer #:nodoc:
private_class_method :new #:nodoc:
- extlib_inheritable_accessor :default_from
- self.default_from = nil
+ extlib_inheritable_accessor :defaults
+ self.defaults = {}
extlib_inheritable_accessor :default_charset
self.default_charset = "utf-8"
@@ -276,18 +276,13 @@ module ActionMailer #:nodoc:
self.default_implicit_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
- 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
@@ -419,8 +414,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 +422,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
+ # 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'
@@ -478,8 +481,8 @@ module ActionMailer #:nodoc:
mime_version = headers[:mime_version] || m.mime_version || self.class.default_mime_version.dup
# Set fields quotings
- headers[:subject] ||= default_subject
- headers[:from] ||= self.class.default_from.dup
+ headers = set_defaults(headers)
+
quote_fields!(headers, charset)
# Render the templates and blocks
@@ -519,9 +522,19 @@ module ActionMailer #:nodoc:
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:
mailer_scope = self.class.mailer_name.gsub('/', '.')
- I18n.t(:subject, :scope => [:actionmailer, mailer_scope, action_name], :default => action_name.humanize)
+ self.class.defaults[:subject] || I18n.t(:subject, :scope => [:actionmailer, mailer_scope, action_name], :default => action_name.humanize)
end
# TODO: Move this into Mail
diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb
index 4b8427fb92..8a54455633 100644
--- a/actionmailer/test/base_test.rb
+++ b/actionmailer/test/base_test.rb
@@ -8,9 +8,18 @@ class BaseTest < ActiveSupport::TestCase
}
class BaseMailer < ActionMailer::Base
- delivers_from 'jose@test.plataformatec.com'
+
+ 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
+
def welcome(hash = {})
headers['X-SPAM'] = "Not SPAM"
mail(DEFAULT_HEADERS.merge(hash))
@@ -77,9 +86,19 @@ class BaseTest < ActiveSupport::TestCase
assert_equal(email.subject, 'The first email on new API!')
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@else.com').deliver
- assert_equal(email.from, ['someone@else.com'])
+ email = BaseMailer.welcome(:from => 'someone@example.com',
+ :to => 'another@example.org').deliver
+ 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
@@ -195,6 +214,7 @@ class BaseTest < ActiveSupport::TestCase
end
test "subject gets default from I18n" do
+ BaseMailer.defaults[:subject] = nil
email = BaseMailer.welcome(:subject => nil).deliver
assert_equal "Welcome", email.subject