From bb2c7b432c794af1e0e0ef16e29dcce604af416a Mon Sep 17 00:00:00 2001 From: Mikel Lindsaar Date: Fri, 29 Jan 2010 16:50:10 +1100 Subject: Updating Action Mailer documentation --- actionmailer/lib/action_mailer/base.rb | 98 ++++++++++++---------- actionmailer/lib/action_mailer/delivery_methods.rb | 2 +- actionmailer/lib/action_mailer/quoting.rb | 3 + 3 files changed, 58 insertions(+), 45 deletions(-) (limited to 'actionmailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index dc451417d7..aa9822c6ab 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -33,8 +33,12 @@ module ActionMailer #:nodoc: # * attachments[]= - Allows you to add attachments to your email in an intuitive # manner; attachments['filename.png'] = File.read('path/to/filename.png') # - # * headers[]= - Allows you to specify non standard headers in your email such - # as headers['X-No-Spam'] = 'True' + # * headers[]= - Allows you to specify any header field in your email such + # as headers['X-No-Spam'] = 'True'. Note, while most fields (like To: + # From: can only appear once in an email header, other fields like X-Anything + # can appear multiple times. If you want to change a field that can appear multiple times, + # you need to set it to nil first so that Mail knows you are replacing it, not adding + # another field of the same name.) # # * headers(hash) - Allows you to specify multiple headers in your email such # as headers({'X-No-Spam' => 'True', 'In-Reply-To' => '1234@message.id'}) @@ -42,15 +46,11 @@ module ActionMailer #:nodoc: # * mail - Allows you to specify your email to send. # # 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.) + # will accept (any valid Email header including optional fields). # # 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 - # as well as the +welcome.html.erb+ view file in a +multipart/alternative+ email. + # the same name as the method, so the above action would send the +welcome.text.plain.erb+ view + # file as well as the +welcome.text.html.erb+ view file in a +multipart/alternative+ email. # # If you want to explicitly render only certain templates, pass a block: # @@ -66,7 +66,7 @@ module ActionMailer #:nodoc: # format.html # end # - # Or even to renderize a special view: + # Or even to render a special view: # # mail(:to => user.emai) do |format| # format.text @@ -80,7 +80,7 @@ module ActionMailer #:nodoc: # # To define a template to be used with a mailing, create an .erb file with the same # name as the method in your mailer model. For example, in the mailer defined above, the template at - # app/views/notifier/signup_notification.text.erb would be used to generate the email. + # app/views/notifier/signup_notification.text.plain.erb would be used to generate the email. # # Variables defined in the model are accessible as instance variables in the view. # @@ -102,9 +102,9 @@ module ActionMailer #:nodoc: # # = Generating URLs # - # URLs can be generated in mailer views using url_for or named routes. - # Unlike controllers from Action Pack, the mailer instance doesn't have any context about the incoming request, - # so you'll need to provide all of the details needed to generate a URL. + # URLs can be generated in mailer views using url_for or named routes. Unlike controllers from + # Action Pack, the mailer instance doesn't have any context about the incoming request, so you'll need + # to provide all of the details needed to generate a URL. # # When using url_for you'll need to provide the :host, :controller, and :action: # @@ -114,11 +114,11 @@ module ActionMailer #:nodoc: # # <%= users_url(:host => "example.com") %> # - # You will want to avoid using the name_of_route_path form of named routes because it doesn't make sense to - # generate relative URLs in email messages. + # You will want to avoid using the name_of_route_path form of named routes because it doesn't + # make sense to generate relative URLs in email messages. # - # It is also possible to set a default host that will be used in all mailers by setting the :host option in - # the ActionMailer::Base.default_url_options hash as follows: + # It is also possible to set a default host that will be used in all mailers by setting the :host + # option in the ActionMailer::Base.default_url_options hash as follows: # # ActionMailer::Base.default_url_options[:host] = "example.com" # @@ -127,9 +127,9 @@ module ActionMailer #:nodoc: # config.action_mailer.default_url_options = { :host => "example.com" } # # If you do decide to set a default :host for your mailers you will want to use the - # :only_path => false option when using url_for. This will ensure that absolute URLs are generated because - # the url_for view helper will, by default, generate relative URLs when a :host option isn't - # explicitly provided. + # :only_path => false option when using url_for. This will ensure that absolute URLs are + # generated because the url_for view helper will, by default, generate relative URLs when a + # :host option isn't explicitly provided. # # = Sending mail # @@ -140,7 +140,7 @@ module ActionMailer #:nodoc: # mail = Notifier.welcome(david) # => a Mail::Message object # mail.deliver # sends the email # - # You never instantiate your mailer class. Rather, you just call the method on the class itself. + # You never instantiate your mailer class. Rather, you just call the method you defined on the class itself. # # = Multipart Emails # @@ -175,11 +175,11 @@ module ActionMailer #:nodoc: # end # end # - # Which will (if it had both a .text.erb and .html.erb tempalte in the view - # directory), send a complete multipart/mixed email with two parts, the first part being - # a multipart/alternative with the text and HTML email parts inside, and the second being - # a application/pdf with a Base64 encoded copy of the file.pdf book with the filename - # +free_book.pdf+. + # Which will (if it had both a welcome.text.plain.erb and welcome.text.html.erb + # tempalte in the view directory), send a complete multipart/mixed email with two parts, + # the first part being a multipart/alternative with the text and HTML email parts inside, + # and the second being a application/pdf with a Base64 encoded copy of the file.pdf book + # with the filename +free_book.pdf+. # # # = Configuration options @@ -189,7 +189,9 @@ module ActionMailer #:nodoc: # * default - 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: + # you do not override it. You pass in the header value as a symbol, all lower case with under + # scores instead of hyphens, so Content-Transfer-Encoding: + # becomes :content_transfer_encoding. The defaults set by Action Mailer are: # * :mime_version => "1.0" # * :charset => "utf-8", # * :content_type => "text/plain", @@ -199,33 +201,40 @@ module ActionMailer #:nodoc: # Can be set to nil for no logging. Compatible with both Ruby's own Logger and Log4r loggers. # # * smtp_settings - Allows detailed configuration for :smtp delivery method: - # * :address - Allows you to use a remote mail server. Just change it from its default "localhost" setting. + # * :address - Allows you to use a remote mail server. Just change it from its default + # "localhost" setting. # * :port - On the off chance that your mail server doesn't run on port 25, you can change it. # * :domain - If you need to specify a HELO domain, you can do it here. # * :user_name - If your mail server requires authentication, set the username in this setting. # * :password - If your mail server requires authentication, set the password in this setting. - # * :authentication - If your mail server requires authentication, you need to specify the authentication type here. + # * :authentication - If your mail server requires authentication, you need to specify the + # authentication type here. # This is a symbol and one of :plain, :login, :cram_md5. - # * :enable_starttls_auto - When set to true, detects if STARTTLS is enabled in your SMTP server and starts to use it. - # It works only on Ruby >= 1.8.7 and Ruby >= 1.9. Default is true. + # * :enable_starttls_auto - When set to true, detects if STARTTLS is enabled in your SMTP server + # and starts to use it. # # * sendmail_settings - Allows you to override options for the :sendmail delivery method. # * :location - The location of the sendmail executable. Defaults to /usr/sbin/sendmail. - # * :arguments - The command line arguments. Defaults to -i -t. + # * :arguments - The command line arguments. Defaults to -i -t with -f sender@addres + # added automatically before the message is sent. # # * file_settings - Allows you to override options for the :file delivery method. - # * :location - The directory into which emails will be written. Defaults to the application tmp/mails. + # * :location - The directory into which emails will be written. Defaults to the application + # tmp/mails. # # * raise_delivery_errors - Whether or not errors should be raised if the email fails to be delivered. # - # * delivery_method - Defines a delivery method. Possible values are :smtp (default), :sendmail, :test, - # and :file. Or you may provide a custom delivery method object eg. MyOwnDeliveryMethodClass.new + # * delivery_method - Defines a delivery method. Possible values are :smtp (default), + # :sendmail, :test, and :file. Or you may provide a custom delivery method + # object eg. MyOwnDeliveryMethodClass.new. See the Mail gem documentation on the interface you need to + # implement for a custom delivery agent. # - # * perform_deliveries - Determines whether deliver_* methods are actually carried out. By default they are, - # but this can be turned off to help functional testing. + # * perform_deliveries - Determines whether emails are actually sent from Action Mailer when you + # call .deliver on an mail message or on an Action Mailer method. This is on by default but can + # be turned off to aid in functional testing. # - # * 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. + # * 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 - This is now deprecated, use the +default+ method above to # set the default +:charset+. @@ -284,7 +293,7 @@ module ActionMailer #:nodoc: # 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 # process incoming messages, you'll need to implement a +receive+ - # method that accepts the email object as a parameter: + # method that accepts the raw email string as a parameter: # # class MyMailer < ActionMailer::Base # def receive(mail) @@ -299,9 +308,10 @@ module ActionMailer #:nodoc: end end - # Delivers a mail object. This is actually called by the Mail::Message object - # itself through a call back when you call :deliver on the Mail::Message, - # calling +deliver_mail+ directly and passing an Mail::Message will do nothing. + # Wraps an email delivery inside of Active Support Notifications instrumentation. This + # method is actually called by the Mail::Message object itself through a call back + # when you call :deliver on the Mail::Message, calling +deliver_mail+ directly + # and passing a Mail::Message will do nothing except tell the logger you sent the email. def deliver_mail(mail) #:nodoc: ActiveSupport::Notifications.instrument("action_mailer.deliver") do |payload| self.set_payload_for_mail(payload, mail) diff --git a/actionmailer/lib/action_mailer/delivery_methods.rb b/actionmailer/lib/action_mailer/delivery_methods.rb index f6321a240c..7e92aea8fd 100644 --- a/actionmailer/lib/action_mailer/delivery_methods.rb +++ b/actionmailer/lib/action_mailer/delivery_methods.rb @@ -2,7 +2,7 @@ require 'tmpdir' module ActionMailer # This modules handles everything related to the delivery, from registering new - # delivery methods to configuring the mail object to be send. + # delivery methods to configuring the mail object to be sent. module DeliveryMethods extend ActiveSupport::Concern diff --git a/actionmailer/lib/action_mailer/quoting.rb b/actionmailer/lib/action_mailer/quoting.rb index b30441f9de..70f636bf69 100644 --- a/actionmailer/lib/action_mailer/quoting.rb +++ b/actionmailer/lib/action_mailer/quoting.rb @@ -1,5 +1,8 @@ module ActionMailer module Quoting #:nodoc: + # TODO extract this into Mail itself. + # + # # Convert the given text into quoted printable format, with an instruction # that the text be eventually interpreted in the given charset. def quoted_printable(text, charset) -- cgit v1.2.3 From 9f01dff9c203821bf4ac6d7b885f1d6b018d5c79 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Sun, 31 Jan 2010 16:33:06 -0800 Subject: Get rails tests running on bundler 0.9 --- actionmailer/test/abstract_unit.rb | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) (limited to 'actionmailer') diff --git a/actionmailer/test/abstract_unit.rb b/actionmailer/test/abstract_unit.rb index ce09bb5d61..8fb941f9c9 100644 --- a/actionmailer/test/abstract_unit.rb +++ b/actionmailer/test/abstract_unit.rb @@ -1,12 +1,5 @@ -begin - require File.expand_path('../../../vendor/gems/environment', __FILE__) -rescue LoadError -end - -lib = File.expand_path('../../lib', __FILE__) -$:.unshift(lib) unless $:.include?('lib') || $:.include?(lib) +require File.expand_path('../../../load_paths', __FILE__) -require 'rubygems' require 'test/unit' require 'action_mailer' -- cgit v1.2.3 From e5ab4b0d07ade8d89d633ca744c0eafbc53ee921 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 31 Jan 2010 18:32:28 -0800 Subject: Convert to class_attribute --- actionmailer/lib/action_mailer/base.rb | 10 ++++----- actionmailer/lib/action_mailer/delivery_methods.rb | 14 +++++-------- actionmailer/lib/action_mailer/deprecated_api.rb | 2 +- actionmailer/test/base_test.rb | 24 +++++++++------------- actionmailer/test/delivery_methods_test.rb | 6 ++++-- 5 files changed, 25 insertions(+), 31 deletions(-) (limited to 'actionmailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index aa9822c6ab..ec85a20f70 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -268,13 +268,13 @@ module ActionMailer #:nodoc: private_class_method :new #:nodoc: - extlib_inheritable_accessor :default_params + class_attribute :default_params self.default_params = { :mime_version => "1.0", :charset => "utf-8", :content_type => "text/plain", :parts_order => [ "text/plain", "text/enriched", "text/html" ] - } + }.freeze class << self @@ -284,9 +284,9 @@ module ActionMailer #:nodoc: attr_writer :mailer_name alias :controller_path :mailer_name - def default(value=nil) - self.default_params.merge!(value) if value - self.default_params + def default(value = nil) + self.default_params = default_params.merge(value).freeze if value + default_params end # Receives a raw email, parses it into an email object, decodes it, diff --git a/actionmailer/lib/action_mailer/delivery_methods.rb b/actionmailer/lib/action_mailer/delivery_methods.rb index 7e92aea8fd..043794bb12 100644 --- a/actionmailer/lib/action_mailer/delivery_methods.rb +++ b/actionmailer/lib/action_mailer/delivery_methods.rb @@ -7,8 +7,7 @@ module ActionMailer extend ActiveSupport::Concern included do - extlib_inheritable_accessor :delivery_methods, :delivery_method, - :instance_writer => false + class_attribute :delivery_methods, :delivery_method # Do not make this inheritable, because we always want it to propagate cattr_accessor :raise_delivery_errors @@ -17,7 +16,7 @@ module ActionMailer cattr_accessor :perform_deliveries self.perform_deliveries = true - self.delivery_methods = {} + self.delivery_methods = {}.freeze self.delivery_method = :smtp add_delivery_method :smtp, Mail::SMTP, @@ -53,12 +52,9 @@ module ActionMailer # :arguments => '-i -t' # def add_delivery_method(symbol, klass, default_options={}) - unless respond_to?(:"#{symbol}_settings") - extlib_inheritable_accessor(:"#{symbol}_settings", :instance_writer => false) - end - + class_attribute(:"#{symbol}_settings") unless respond_to?(:"#{symbol}_settings") send(:"#{symbol}_settings=", default_options) - self.delivery_methods[symbol.to_sym] = klass + self.delivery_methods = delivery_methods.merge(symbol.to_sym => klass).freeze end def wrap_delivery_behavior(mail, method=nil) #:nodoc: @@ -87,4 +83,4 @@ module ActionMailer self.class.wrap_delivery_behavior(message, *args) end end -end \ No newline at end of file +end diff --git a/actionmailer/lib/action_mailer/deprecated_api.rb b/actionmailer/lib/action_mailer/deprecated_api.rb index 54ad18f796..c08ab4164e 100644 --- a/actionmailer/lib/action_mailer/deprecated_api.rb +++ b/actionmailer/lib/action_mailer/deprecated_api.rb @@ -47,7 +47,7 @@ module ActionMailer end def template_root=(root) - ActiveSupport::Deprecation.warn "template_root= is deprecated, use view_paths.unshift instead", caller[0,2] + ActiveSupport::Deprecation.warn "template_root= is deprecated, use prepend_view_path instead", caller[0,2] self.view_paths = ActionView::Base.process_view_paths(root) end diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb index 57bfe2375e..20ecfdcbdb 100644 --- a/actionmailer/test/base_test.rb +++ b/actionmailer/test/base_test.rb @@ -254,7 +254,7 @@ class BaseTest < ActiveSupport::TestCase end test "subject gets default from I18n" do - BaseMailer.default[:subject] = nil + BaseMailer.default :subject => nil email = BaseMailer.welcome(:subject => nil) assert_equal "Welcome", email.subject @@ -331,22 +331,24 @@ class BaseTest < ActiveSupport::TestCase end test "implicit multipart with several view paths uses the first one with template" do + old = BaseMailer.view_paths begin - BaseMailer.view_paths.unshift(File.join(FIXTURE_LOAD_PATH, "another.path")) + BaseMailer.view_paths = [File.join(FIXTURE_LOAD_PATH, "another.path")] + old.dup email = BaseMailer.welcome assert_equal("Welcome from another path", email.body.encoded) ensure - BaseMailer.view_paths.shift + BaseMailer.view_paths = old end end test "implicit multipart with inexistent templates uses the next view path" do + old = BaseMailer.view_paths begin - BaseMailer.view_paths.unshift(File.join(FIXTURE_LOAD_PATH, "unknown")) + BaseMailer.view_paths = [File.join(FIXTURE_LOAD_PATH, "unknown")] + old.dup email = BaseMailer.welcome assert_equal("Welcome", email.body.encoded) ensure - BaseMailer.view_paths.shift + BaseMailer.view_paths = old end end @@ -503,16 +505,10 @@ class BaseTest < ActiveSupport::TestCase end def with_default(klass, new_values) - hash = klass.default - old_values = {} - new_values.each do |key, value| - old_values[key] = hash[key] - hash[key] = value - end + old = klass.default_params + klass.default(new_values) yield ensure - old_values.each do |key, value| - hash[key] = value - end + klass.default_params = old end end diff --git a/actionmailer/test/delivery_methods_test.rb b/actionmailer/test/delivery_methods_test.rb index 4907ca0903..22a7d19bc2 100644 --- a/actionmailer/test/delivery_methods_test.rb +++ b/actionmailer/test/delivery_methods_test.rb @@ -45,7 +45,9 @@ class CustomDeliveryMethodsTest < ActiveSupport::TestCase def teardown ActionMailer::Base.delivery_method = @old_delivery_method - ActionMailer::Base.delivery_methods.delete(:custom) + new = ActionMailer::Base.delivery_methods.dup + new.delete(:custom) + ActionMailer::Base.delivery_methods = new end test "allow to add custom delivery method" do @@ -167,4 +169,4 @@ class MailDeliveryTest < ActiveSupport::TestCase assert_equal(0, DeliveryMailer.deliveries.length) end -end \ No newline at end of file +end -- cgit v1.2.3 From d74b5e440c04a1d89ae092cab7cb4d93d850f94a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 1 Feb 2010 23:34:23 +0100 Subject: Make AM test suite green. --- actionmailer/test/abstract_unit.rb | 1 + actionmailer/test/base_test.rb | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'actionmailer') diff --git a/actionmailer/test/abstract_unit.rb b/actionmailer/test/abstract_unit.rb index 8fb941f9c9..f6baa4a9e8 100644 --- a/actionmailer/test/abstract_unit.rb +++ b/actionmailer/test/abstract_unit.rb @@ -2,6 +2,7 @@ require File.expand_path('../../../load_paths', __FILE__) require 'test/unit' require 'action_mailer' +require 'action_mailer/test_case' # Show backtraces for deprecated behavior for quicker cleanup. ActiveSupport::Deprecation.debug = true diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb index 20ecfdcbdb..7e794e10e8 100644 --- a/actionmailer/test/base_test.rb +++ b/actionmailer/test/base_test.rb @@ -81,8 +81,8 @@ class BaseTest < ActiveSupport::TestCase def different_template(template_name='') mail do |format| - format.text { render :template => template_name } - format.html { render :template => template_name } + format.text { render :template => "#{mailer_name}/#{template_name}" } + format.html { render :template => "#{mailer_name}/#{template_name}" } end end -- cgit v1.2.3