From 0b05acd42439b197f71e168354020393cbf42b4f Mon Sep 17 00:00:00 2001 From: Mikel Lindsaar Date: Tue, 26 Jan 2010 17:08:55 +1100 Subject: Implementing class level :defaults hash, instead of delivers_from et al --- actionmailer/lib/action_mailer/base.rb | 39 ++++++++++++++++++++++------------ actionmailer/test/base_test.rb | 26 ++++++++++++++++++++--- 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 action_name # * :to - Who the message is destined for, can be a string of addresses, or an array # of addresses. - # * :from - Who the message is from, if missing, will use the :delivers_from - # value in the class (if it exists) + # * :from - Who the message is from # * :cc - Who you would like to Carbon-Copy on this email, can be a string of addresses, # or an array of addresses. # * :bcc - Who you would like to Blind-Carbon-Copy on this email, can be a string of @@ -428,6 +422,15 @@ module ActionMailer #:nodoc: # * :reply_to - Who to set the Reply-To header of the email to. # * :date - 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 defaults + # 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 headers['name'] = value method. # # When a :return_path 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 -- cgit v1.2.3 From e297eed4f21a57234ea28fe3dc9265ed8793dba1 Mon Sep 17 00:00:00 2001 From: Mikel Lindsaar Date: Tue, 26 Jan 2010 17:11:36 +1100 Subject: Fixing up expectations in base_test.rb --- actionmailer/test/base_test.rb | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb index 8a54455633..20a35ddc73 100644 --- a/actionmailer/test/base_test.rb +++ b/actionmailer/test/base_test.rb @@ -81,9 +81,9 @@ 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(email.to, ['mikel@test.lindsaar.net']) - assert_equal(email.from, ['jose@test.plataformatec.com']) - assert_equal(email.subject, 'The first email on new API!') + assert_equal(['mikel@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 @@ -110,13 +110,13 @@ class BaseTest < ActiveSupport::TestCase :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) + 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 -- cgit v1.2.3 From b8c82edc1f5b95bceea3d40de778ec0cd5a37d15 Mon Sep 17 00:00:00 2001 From: Mikel Lindsaar Date: Tue, 26 Jan 2010 18:59:52 +1100 Subject: Updating generators for mailer to reflect changes in API --- railties/lib/generators/rails/mailer/templates/mailer.rb | 4 ++-- railties/test/generators/mailer_generator_test.rb | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/railties/lib/generators/rails/mailer/templates/mailer.rb b/railties/lib/generators/rails/mailer/templates/mailer.rb index 1685b73633..df9d0a0923 100644 --- a/railties/lib/generators/rails/mailer/templates/mailer.rb +++ b/railties/lib/generators/rails/mailer/templates/mailer.rb @@ -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 \ No newline at end of file diff --git a/railties/test/generators/mailer_generator_test.rb b/railties/test/generators/mailer_generator_test.rb index 99ce53323e..e585298a93 100644 --- a/railties/test/generators/mailer_generator_test.rb +++ b/railties/test/generators/mailer_generator_test.rb @@ -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 -- cgit v1.2.3 From 9dd65c368b40093cfc686956a48c18b78abacf7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Tue, 26 Jan 2010 11:21:20 +0100 Subject: Make defaults accept a hash. --- actionmailer/lib/action_mailer/base.rb | 78 ++++++++++++++++--------------- actionmailer/test/base_test.rb | 85 +++++++++++++++++----------------- 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 defaults # 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 headers['name'] = value 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 -- cgit v1.2.3 From 39a1b06f13221e00ab38dff960155cb5fe9eaabb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Tue, 26 Jan 2010 11:46:42 +0100 Subject: Deprecate old defaults API. --- actionmailer/lib/action_mailer/base.rb | 31 +++++----------------- actionmailer/lib/action_mailer/delivery_methods.rb | 5 +++- actionmailer/lib/action_mailer/deprecated_api.rb | 29 ++++++++++++++++++-- actionmailer/lib/action_mailer/old_api.rb | 10 ++++--- 4 files changed, 43 insertions(+), 32 deletions(-) diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index dbaf1424ed..95c45ec54b 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -262,24 +262,6 @@ module ActionMailer #:nodoc: :parts_order => [ "text/plain", "text/enriched", "text/html" ] } - 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" ] - class << self def mailer_name @@ -498,7 +480,7 @@ module ActionMailer #:nodoc: quote_fields!(headers, charset) # Render the templates and blocks - responses, explicit_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) # Finally setup content type and parts order @@ -554,18 +536,18 @@ module ActionMailer #:nodoc: m.reply_to ||= quote_address_if_necessary(headers[:reply_to], charset) if headers[:reply_to] 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| @@ -576,7 +558,7 @@ module ActionMailer #:nodoc: end end - [responses, sort_order] + [responses, parts_order] end def each_template(&block) #:nodoc: @@ -594,7 +576,6 @@ module ActionMailer #:nodoc: def create_parts_from_responses(m, responses, charset) #:nodoc: if responses.size == 1 && !m.has_attachments? responses[0].each { |k,v| m[k] = v } - return responses[0][:content_type] elsif responses.size > 1 && m.has_attachments? container = Mail::Part.new container.content_type = "multipart/alternative" diff --git a/actionmailer/lib/action_mailer/delivery_methods.rb b/actionmailer/lib/action_mailer/delivery_methods.rb index 34bfe6000a..f6321a240c 100644 --- a/actionmailer/lib/action_mailer/delivery_methods.rb +++ b/actionmailer/lib/action_mailer/delivery_methods.rb @@ -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 diff --git a/actionmailer/lib/action_mailer/deprecated_api.rb b/actionmailer/lib/action_mailer/deprecated_api.rb index 0eb8d85676..61101c26a1 100644 --- a/actionmailer/lib/action_mailer/deprecated_api.rb +++ b/actionmailer/lib/action_mailer/deprecated_api.rb @@ -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] diff --git a/actionmailer/lib/action_mailer/old_api.rb b/actionmailer/lib/action_mailer/old_api.rb index f5b077ab98..22c3c518b1 100644 --- a/actionmailer/lib/action_mailer/old_api.rb +++ b/actionmailer/lib/action_mailer/old_api.rb @@ -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 -- cgit v1.2.3 From 8fabcb2eca03150b1c0c3dbc88dd13123f76894f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Tue, 26 Jan 2010 11:48:25 +0100 Subject: Update generators to use new defaults. --- railties/lib/generators/rails/mailer/templates/mailer.rb | 2 +- railties/test/generators/mailer_generator_test.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/railties/lib/generators/rails/mailer/templates/mailer.rb b/railties/lib/generators/rails/mailer/templates/mailer.rb index df9d0a0923..cdc6e41266 100644 --- a/railties/lib/generators/rails/mailer/templates/mailer.rb +++ b/railties/lib/generators/rails/mailer/templates/mailer.rb @@ -1,5 +1,5 @@ class <%= class_name %> < ActionMailer::Base - self.defaults = { :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 diff --git a/railties/test/generators/mailer_generator_test.rb b/railties/test/generators/mailer_generator_test.rb index e585298a93..0b7f5c6817 100644 --- a/railties/test/generators/mailer_generator_test.rb +++ b/railties/test/generators/mailer_generator_test.rb @@ -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 /self\.defaults\ =\ \{\ :from\ =>\ "from@example\.com"\ \}/, mailer + assert_match /self\.defaults :from => "from@example.com"/, mailer end end -- cgit v1.2.3 From 9520166f70b84dd56640b7dbe8e3737c91e04bd9 Mon Sep 17 00:00:00 2001 From: Mikel Lindsaar Date: Wed, 27 Jan 2010 00:06:19 +1100 Subject: Fixed up being able to pass random headers in with headers, or mail. Also, undeprecated headers(hash) as this works now too --- actionmailer/lib/action_mailer/base.rb | 31 +++++++++++++++++++----------- actionmailer/test/base_test.rb | 35 +++++++++++++++++++++++++++++----- 2 files changed, 50 insertions(+), 16 deletions(-) diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index dbaf1424ed..c01ca876dc 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -40,13 +40,17 @@ module ActionMailer #:nodoc: # * headers[]= - Allows you to specify non standard headers in your email such # as headers['X-No-Spam'] = 'True' # + # * headers(hash) - Allows you to specify multiple headers in your email such + # as headers({'X-No-Spam' => 'True', 'In-Reply-To' => '1234@message.id'}) + # # * mail - 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 Subject, To, From, Cc, Bcc, - # Reply-To and Date. See the ActionMailer#mail method for more details. - # - # If you need other headers not listed above, use the headers['name'] = value 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 @@ -263,13 +267,13 @@ module ActionMailer #:nodoc: } extlib_inheritable_accessor :default_charset - self.default_charset = "utf-8" + self.default_charset = self.default_params[:charset] extlib_inheritable_accessor :default_content_type - self.default_content_type = "text/plain" + self.default_content_type = self.default_params[:content_type] extlib_inheritable_accessor :default_mime_version - self.default_mime_version = "1.0" + self.default_mime_version = self.default_params[:mime_version] # 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 @@ -278,7 +282,7 @@ 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. extlib_inheritable_accessor :default_implicit_parts_order - self.default_implicit_parts_order = [ "text/plain", "text/enriched", "text/html" ] + self.default_implicit_parts_order = self.default_params[:parts_order] class << self @@ -366,13 +370,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 diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb index 7abdf7f4f8..9a39204998 100644 --- a/actionmailer/test/base_test.rb +++ b/actionmailer/test/base_test.rb @@ -5,15 +5,24 @@ class BaseTest < ActiveSupport::TestCase class BaseMailer < ActionMailer::Base self.mailer_name = "base_mailer" - self.defaults :to => 'system@test.lindsaar.net', - :from => 'jose@test.plataformatec.com', - :reply_to => 'mikel@test.lindsaar.net' + 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({: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(hash) @@ -194,9 +203,9 @@ class BaseTest < ActiveSupport::TestCase end end - test "uses default headers from class" do + test "uses random default headers from class" do with_default BaseMailer, "X-SPAM" => "Not spam" do - email = BaseMailer.welcome.deliver + email = BaseMailer.simple assert_equal("Not spam", email["X-SPAM"].decoded) end end @@ -407,6 +416,22 @@ class BaseTest < ActiveSupport::TestCase mail = BaseMailer.explicit_multipart assert_not_nil(mail.content_type_parameters[:boundary]) 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 + + 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 protected -- cgit v1.2.3 From 21dcc20ed29053c8ffd4d3a5a68a40f6e225512b Mon Sep 17 00:00:00 2001 From: Mikel Lindsaar Date: Wed, 27 Jan 2010 00:18:40 +1100 Subject: Fixed up documentation to reflect code change and cleaned up tests of spurious #deliver calls --- actionmailer/lib/action_mailer/base.rb | 40 +++++++++--------- actionmailer/test/base_test.rb | 77 +++++++++++++++++----------------- 2 files changed, 58 insertions(+), 59 deletions(-) diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index c01ca876dc..868d785129 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -23,7 +23,7 @@ module ActionMailer #:nodoc: # Examples: # # class Notifier < ActionMailer::Base - # delivers_from 'system@example.com' + # defaults({:from => 'system@example.com'}) # # def welcome(recipient) # @account = recipient @@ -190,9 +190,14 @@ module ActionMailer #:nodoc: # # These options are specified on the class level, like ActionMailer::Base.template_root = "/my/templates" # - # * delivers_from - 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 :from => 'another@address' in - # the +mail+ method. + # * defaults - This is a class wide hash of :key => value pairs containing + # default values for the specified header fields of the Mail::Message. You can + # specify a default for any valid header for Mail::Message and it will be used if + # you do not override it. The defaults set by Action Mailer are: + # * :mime_version => "1.0" + # * :charset => "utf-8", + # * :content_type => "text/plain", + # * :parts_order => [ "text/plain", "text/enriched", "text/html" ] # # * logger - 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. @@ -226,20 +231,19 @@ module ActionMailer #:nodoc: # * deliveries - Keeps an array of all the emails sent out through the Action Mailer with delivery_method :test. Most useful # for unit and functional testing. # - # * default_charset - 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+. + # * default_charset - This is now deprecated, use the +defaults+ method above to + # set the default +:charset+. # - # * default_content_type - 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+. + # * default_content_type - This is now deprecated, use the +defaults+ method above + # to set the default +:content_type+. # - # * default_mime_version - The default mime version used for the message. Defaults to 1.0. You - # can also pick a different value from inside a method with +mime_version+. + # * default_mime_version - This is now deprecated, use the +defaults+ method above + # to set the default +:mime_version+. # - # * default_implicit_parts_order - 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 - # ["text/html", "text/enriched", "text/plain"]. 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+. + # * default_implicit_parts_order - 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! @@ -275,12 +279,6 @@ module ActionMailer #:nodoc: extlib_inheritable_accessor :default_mime_version self.default_mime_version = self.default_params[:mime_version] - # 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 = self.default_params[:parts_order] diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb index 9a39204998..6900e77178 100644 --- a/actionmailer/test/base_test.rb +++ b/actionmailer/test/base_test.rb @@ -73,12 +73,12 @@ 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 + 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) @@ -86,7 +86,7 @@ class BaseTest < ActiveSupport::TestCase test "mail() with from overwrites the class level default" do email = BaseMailer.welcome(:from => 'someone@example.com', - :to => 'another@example.org').deliver + :to => 'another@example.org') assert_equal(['someone@example.com'], email.from) assert_equal(['another@example.org'], email.to) end @@ -99,7 +99,7 @@ class BaseTest < ActiveSupport::TestCase :charset => 'iso-8559-1', :mime_version => '2.0', :reply_to => 'reply-to@test.lindsaar.net', - :date => @time).deliver + :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) @@ -110,50 +110,50 @@ class BaseTest < ActiveSupport::TestCase 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 # 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) @@ -163,7 +163,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) @@ -175,30 +175,30 @@ class BaseTest < ActiveSupport::TestCase # Defaults values test "uses default charset from class" do with_default BaseMailer, :charset => "US-ASCII" do - email = BaseMailer.welcome.deliver + 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 with_default BaseMailer, :content_type => "text/html" do - email = BaseMailer.welcome.deliver + 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 with_default BaseMailer, :mime_version => "2.0" do - email = BaseMailer.welcome.deliver + 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 @@ -212,17 +212,17 @@ class BaseTest < ActiveSupport::TestCase test "subject gets default from I18n" do BaseMailer.defaults[:subject] = nil - email = BaseMailer.welcome(:subject => nil).deliver + 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) @@ -234,18 +234,18 @@ class BaseTest < ActiveSupport::TestCase test "implicit multipart with sort order" do order = ["text/html", "text/plain"] with_default BaseMailer, :parts_order => order do - email = BaseMailer.implicit_multipart.deliver + 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) @@ -257,7 +257,7 @@ class BaseTest < ActiveSupport::TestCase test "implicit multipart with attachments and sort order" do order = ["text/html", "text/plain"] with_default BaseMailer, :parts_order => order 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[1].mime_type) @@ -266,7 +266,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) @@ -277,7 +277,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) @@ -290,7 +290,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 @@ -300,7 +300,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 @@ -309,7 +309,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) @@ -321,18 +321,18 @@ class BaseTest < ActiveSupport::TestCase test "explicit multipart does not sort order" do order = ["text/html", "text/plain"] with_default BaseMailer, :parts_order => order do - email = BaseMailer.explicit_multipart.deliver + 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) @@ -342,7 +342,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) @@ -352,7 +352,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) @@ -362,7 +362,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) @@ -372,7 +373,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) -- cgit v1.2.3 From ccea6ab07d38c5bc2de341c3f08d199ef55bccc6 Mon Sep 17 00:00:00 2001 From: Mikel Lindsaar Date: Wed, 27 Jan 2010 00:37:22 +1100 Subject: Fixing up tests and docs to use defaults :from => 'name' instead of defaults({:from => 'name'}) --- actionmailer/lib/action_mailer/base.rb | 3 ++- actionmailer/test/base_test.rb | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index f677ab629e..669e49c91c 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -23,7 +23,8 @@ module ActionMailer #:nodoc: # Examples: # # class Notifier < ActionMailer::Base - # defaults({:from => 'system@example.com'}) + # defaults :from => 'no-reply@example.com', + # :return_path => 'system@example.com' # # def welcome(recipient) # @account = recipient diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb index 6900e77178..0709c92326 100644 --- a/actionmailer/test/base_test.rb +++ b/actionmailer/test/base_test.rb @@ -5,9 +5,9 @@ class BaseTest < ActiveSupport::TestCase class BaseMailer < ActionMailer::Base self.mailer_name = "base_mailer" - defaults({:to => 'system@test.lindsaar.net', - :from => 'jose@test.plataformatec.com', - :reply_to => 'mikel@test.lindsaar.net'}) + 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" -- cgit v1.2.3 From 05c4ad9d3f6ff8d0017f4bca9b52e552629c4813 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Tue, 26 Jan 2010 16:00:24 +0100 Subject: Tidy up tests and docs. --- actionmailer/lib/action_mailer/base.rb | 4 ++-- actionmailer/test/base_test.rb | 37 +++++++++++++++++----------------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 669e49c91c..b230e34631 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -360,8 +360,8 @@ module ActionMailer #:nodoc: # 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 } + # 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: # diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb index 0709c92326..4a65363e3e 100644 --- a/actionmailer/test/base_test.rb +++ b/actionmailer/test/base_test.rb @@ -79,7 +79,7 @@ class BaseTest < ActiveSupport::TestCase # Basic mail usage without block test "mail() should set the headers of the mail message" do email = BaseMailer.welcome - assert_equal(['system@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 @@ -126,6 +126,22 @@ class BaseTest < ActiveSupport::TestCase 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 @@ -397,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 @@ -417,22 +434,6 @@ class BaseTest < ActiveSupport::TestCase mail = BaseMailer.explicit_multipart assert_not_nil(mail.content_type_parameters[:boundary]) 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 - - 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 protected -- cgit v1.2.3