diff options
Diffstat (limited to 'actionmailer')
-rw-r--r-- | actionmailer/lib/action_mailer/base.rb | 38 | ||||
-rw-r--r-- | actionmailer/lib/action_mailer/deprecated_api.rb | 2 | ||||
-rw-r--r-- | actionmailer/test/base_test.rb | 115 |
3 files changed, 107 insertions, 48 deletions
diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 2c1de0e3b4..d75bbe952f 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -277,14 +277,14 @@ module ActionMailer #:nodoc: @@deliveries = [] cattr_accessor :deliveries - @@default_charset = "utf-8" - cattr_accessor :default_charset + extlib_inheritable_accessor :default_charset + self.default_charset = "utf-8" - @@default_content_type = "text/plain" - cattr_accessor :default_content_type + extlib_inheritable_accessor :default_content_type + self.default_content_type = "text/plain" - @@default_mime_version = "1.0" - cattr_accessor :default_mime_version + 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 @@ -292,14 +292,24 @@ module ActionMailer #:nodoc: # # 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. - @@default_implicit_parts_order = [ "text/plain", "text/enriched", "text/html" ] - cattr_accessor :default_implicit_parts_order + extlib_inheritable_accessor :default_implicit_parts_order + self.default_implicit_parts_order = [ "text/plain", "text/enriched", "text/html" ] # Expose the internal Mail message attr_reader :message - # Pass calls to headers and attachment to the Mail#Message instance - delegate :headers, :attachments, :to => :@message + def headers(args=nil) + if args + ActiveSupport::Deprecation.warn "headers(Hash) is deprecated, please do headers[key] = value instead", caller + @headers = args + else + @message + end + end + + def attachments + @message.attachments + end class << self @@ -386,9 +396,9 @@ module ActionMailer #:nodoc: m = @message - m.content_type ||= headers[:content_type] || @@default_content_type - m.charset ||= headers[:charset] || @@default_charset - m.mime_version ||= headers[:mime_version] || @@default_mime_version + m.content_type ||= headers[:content_type] || self.class.default_content_type + m.charset ||= headers[:charset] || self.class.default_charset + m.mime_version ||= headers[:mime_version] || self.class.default_mime_version m.subject = quote_if_necessary(headers[:subject], m.charset) if headers[:subject] m.to = quote_address_if_necessary(headers[:to], m.charset) if headers[:to] @@ -398,7 +408,7 @@ module ActionMailer #:nodoc: m.reply_to = quote_address_if_necessary(headers[:reply_to], m.charset) if headers[:reply_to] m.date = headers[:date] if headers[:date] - m.body.set_sort_order(headers[:parts_order] || @@default_implicit_parts_order) + m.body.set_sort_order(headers[:parts_order] || self.class.default_implicit_parts_order) # # Set the subject if not set yet # @subject ||= I18n.t(:subject, :scope => [:actionmailer, mailer_name, method_name], diff --git a/actionmailer/lib/action_mailer/deprecated_api.rb b/actionmailer/lib/action_mailer/deprecated_api.rb index 430b0cef4a..90e2aebf53 100644 --- a/actionmailer/lib/action_mailer/deprecated_api.rb +++ b/actionmailer/lib/action_mailer/deprecated_api.rb @@ -227,7 +227,7 @@ module ActionMailer m.mime_version = mime_version unless mime_version.nil? m.date = sent_on.to_time rescue sent_on if sent_on - headers.each { |k, v| m[k] = v } + @headers.each { |k, v| m[k] = v } real_content_type, ctype_attrs = parse_content_type main_type, sub_type = split_content_type(real_content_type) diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb index f1f5f38482..b11631f444 100644 --- a/actionmailer/test/base_test.rb +++ b/actionmailer/test/base_test.rb @@ -37,37 +37,43 @@ require 'abstract_unit' # mail.deliver # # Notifier.welcome(user).deliver # => creates and sends the Mail in one step -class BaseTest < Test::Unit::TestCase - +class BaseTest < ActiveSupport::TestCase + DEFAULT_HEADERS = { + :to => 'mikel@test.lindsaar.net', + :from => 'jose@test.plataformatec.com', + :subject => 'The first email on new API!' + } + class TestMailer < ActionMailer::Base - def welcome(hash = {}) - hash = {:to => 'mikel@test.lindsaar.net', :from => 'jose@test.plataformatec.com', - :subject => 'The first email on new API!'}.merge!(hash) - mail(hash) + headers['X-SPAM'] = "Not SPAM" + mail(DEFAULT_HEADERS.merge(hash)) end - - def invoice(hash = {}) + + def attachment_with_content attachments['invoice.pdf'] = 'This is test File content' - hash = {:to => 'mikel@test.lindsaar.net', :from => 'jose@test.plataformatec.com', - :subject => 'Your invoice is attached'}.merge!(hash) - mail(hash) + mail(DEFAULT_HEADERS) + end + + def attachment_with_hash + attachments['invoice.jpg'] = { :content => "you smiling", :mime_type => "image/x-jpg", + :transfer_encoding => "base64" } + mail(DEFAULT_HEADERS) end - end - def test_the_method_call_to_mail_does_not_raise_error + test "method call to mail does not raise error" do assert_nothing_raised { TestMailer.deliver_welcome } end - def test_should_set_the_headers_of_the_mail_message + test "mail() should set the headers of the mail message" do email = TestMailer.deliver_welcome 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!') end - - def test_should_allow_all_headers_set + + test "mail() with bcc, cc, content_type, charset, mime_version, reply_to and date" do @time = Time.now email = TestMailer.deliver_welcome(:bcc => 'bcc@test.lindsaar.net', :cc => 'cc@test.lindsaar.net', @@ -85,32 +91,58 @@ class BaseTest < Test::Unit::TestCase assert_equal(email.date, @time) end -# def test_should_allow_custom_headers_to_be_set -# email = TestMailer.deliver_welcome -# assert_equal("Not SPAM", email['X-SPAM']) -# end - - def test_should_allow_you_to_send_an_attachment - assert_nothing_raised { TestMailer.deliver_invoice } + test "custom headers" do + email = TestMailer.deliver_welcome + assert_equal("Not SPAM", email['X-SPAM'].decoded) end - def test_should_allow_you_to_send_an_attachment - email = TestMailer.deliver_invoice + test "attachment with content" do + email = TestMailer.deliver_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 - def test_should_allow_you_to_send_an_attachment - email = TestMailer.deliver_invoice + test "attachment gets content type from filename" do + email = TestMailer.deliver_attachment_with_content assert_equal('invoice.pdf', email.attachments[0].filename) end - def test_should_allow_you_to_send_an_attachment - email = TestMailer.deliver_invoice - assert_equal('This is test File content', email.attachments['invoice.pdf'].decoded) + test "attachment with hash" do + email = TestMailer.deliver_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 "uses default charset from class" do + swap TestMailer, :default_charset => "US-ASCII" do + email = TestMailer.deliver_welcome + assert_equal("US-ASCII", email.charset) + + email = TestMailer.deliver_welcome(:charset => "iso-8559-1") + assert_equal("iso-8559-1", email.charset) + end end - def test_should_use_class_defaults - + test "uses default content type from class" do + swap TestMailer, :default_content_type => "text/html" do + email = TestMailer.deliver_welcome + assert_equal("text/html", email.mime_type) + + email = TestMailer.deliver_welcome(:content_type => "application/xml") + assert_equal("application/xml", email.mime_type) + end + end + + test "uses default mime version from class" do + swap TestMailer, :default_mime_version => "2.0" do + email = TestMailer.deliver_welcome + assert_equal("2.0", email.mime_version) + + email = TestMailer.deliver_welcome(:mime_version => "1.0") + assert_equal("1.0", email.mime_version) + end end # def test_that_class_defaults_are_set_on_instantiation @@ -120,5 +152,22 @@ class BaseTest < Test::Unit::TestCase # def test_should_set_the_subject_from_i18n # pending # end - + + protected + + # Execute the block setting the given values and restoring old values after + # the block is executed. + def swap(object, new_values) + old_values = {} + new_values.each do |key, value| + old_values[key] = object.send key + object.send :"#{key}=", value + end + yield + ensure + old_values.each do |key, value| + object.send :"#{key}=", value + end + end + end
\ No newline at end of file |