Permalink
Browse files

Implementing class level :defaults hash, instead of delivers_from et al

  • Loading branch information...
1 parent 64f8c87 commit 0b05acd42439b197f71e168354020393cbf42b4f @mikel mikel committed Jan 26, 2010
Showing with 49 additions and 16 deletions.
  1. +26 −13 actionmailer/lib/action_mailer/base.rb
  2. +23 −3 actionmailer/test/base_test.rb
@@ -254,8 +254,8 @@ class Base < AbstractController::Base
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 @@ class Base < AbstractController::Base
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,15 +414,23 @@ def attachments
# 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
# addresses, or an array of addresses.
# * <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 @@ def mail(headers={}, &block)
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 @@ def set_content_type(m, user_content_type)
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
@@ -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 @@ def custom_block(include_html=false)
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 @@ def custom_block(include_html=false)
end
test "subject gets default from I18n" do
+ BaseMailer.defaults[:subject] = nil
email = BaseMailer.welcome(:subject => nil).deliver
assert_equal "Welcome", email.subject

0 comments on commit 0b05acd

Please sign in to comment.