Permalink
Browse files

Deprecate the old mailer API that was not deprecated yet.

  • Loading branch information...
1 parent f5a4313 commit 972efa11fd3339117e9f874beb4e85b146212c29 @josevalim josevalim committed Aug 29, 2010
@@ -1,26 +1,28 @@
module ActionMailer
module AdvAttrAccessor #:nodoc:
- def adv_attr_accessor(*names)
- names.each do |name|
- ivar = "@#{name}"
+ def adv_attr_accessor(name, deprecation=nil)
+ ivar = "@#{name}"
+ deprecation ||= "Please pass :#{name} as hash key to mail() instead"
- class_eval <<-ACCESSORS, __FILE__, __LINE__ + 1
- def #{name}=(value)
- #{ivar} = value
- end
+ class_eval <<-ACCESSORS, __FILE__, __LINE__ + 1
+ def #{name}=(value)
+ ActiveSupport::Deprecation.warn "#{name}= is deprecated. #{deprecation}"
+ #{ivar} = value
+ end
- def #{name}(*args)
- raise ArgumentError, "expected 0 or 1 parameters" unless args.length <= 1
- if args.empty?
- #{ivar} if instance_variable_names.include?(#{ivar.inspect})
- else
- #{ivar} = args.first
- end
+ def #{name}(*args)
+ raise ArgumentError, "expected 0 or 1 parameters" unless args.length <= 1
+ if args.empty?
+ ActiveSupport::Deprecation.warn "#{name}() is deprecated and will be removed in future versions."
+ #{ivar} if instance_variable_names.include?(#{ivar.inspect})
+ else
+ ActiveSupport::Deprecation.warn "#{name}(value) is deprecated. #{deprecation}"
+ #{ivar} = args.first
end
- ACCESSORS
+ end
+ ACCESSORS
- self.protected_instance_variables << ivar if self.respond_to?(:protected_instance_variables)
- end
+ self.protected_instance_variables << ivar if self.respond_to?(:protected_instance_variables)
end
end
end
@@ -341,8 +341,10 @@ class Base < AbstractController::Base
include AbstractController::Translation
include AbstractController::AssetPaths
- helper ActionMailer::MailHelper
+ cattr_reader :protected_instance_variables
+ @@protected_instance_variables = []
+ helper ActionMailer::MailHelper
include ActionMailer::OldApi
delegate :register_observer, :to => Mail
@@ -445,6 +447,10 @@ def process(*args) #:nodoc:
super
end
+ def mailer_name
+ self.class.mailer_name
+ end
+
# Allows you to pass random and unusual headers to the new +Mail::Message+ object
# which will add them to itself.
#
@@ -8,9 +8,7 @@ module OldApi #:nodoc:
included do
extend ActionMailer::AdvAttrAccessor
-
- @@protected_instance_variables = %w(@parts)
- cattr_reader :protected_instance_variables
+ self.protected_instance_variables.concat %w(@parts @mail_was_called)
# Specify the BCC addresses for the message
adv_attr_accessor :bcc
@@ -42,32 +40,24 @@ module OldApi #:nodoc:
# The recipient addresses for the message, either as a string (for a single
# address) or an array (for multiple addresses).
- adv_attr_accessor :recipients
+ adv_attr_accessor :recipients, "Please pass :to as hash key to mail() instead"
# The date on which the message was sent. If not set (the default), the
# header will be set by the delivery agent.
- adv_attr_accessor :sent_on
+ adv_attr_accessor :sent_on, "Please pass :date as hash key to mail() instead"
# Specify the subject of the message.
adv_attr_accessor :subject
# Specify the template name to use for current message. This is the "base"
# template name, without the extension or directory, and may be used to
# have multiple mailer methods share the same template.
- adv_attr_accessor :template
-
- # Override the mailer name, which defaults to an inflected version of the
- # mailer's class name. If you want to use a template in a non-standard
- # location, you can use this to specify that location.
- adv_attr_accessor :mailer_name
+ adv_attr_accessor :template, "Please pass :template_name or :template_path as hash key to mail() instead"
# Define the body of the message. This is either a Hash (in which case it
# specifies the variables to pass to the template when it is rendered),
# or a string, in which case it specifies the actual text of the message.
adv_attr_accessor :body
-
- # Alias controller_path to mailer_name so render :partial in views work.
- alias :controller_path :mailer_name
end
def process(method_name, *args)
@@ -84,6 +74,8 @@ def process(method_name, *args)
# part itself is yielded to the block so that other properties (charset,
# body, headers, etc.) can be set on it.
def part(params)
+ ActiveSupport::Deprecation.warn "part() is deprecated and will be removed in future versions. " <<
+ "Please pass a block to mail() instead."
params = {:content_type => params} if String === params
if custom_headers = params.delete(:headers)
@@ -99,6 +91,8 @@ def part(params)
# Add an attachment to a multipart message. This is simply a part with the
# content-disposition set to "attachment".
def attachment(params, &block)
+ ActiveSupport::Deprecation.warn "attachment() is deprecated and will be removed in future versions. " <<
+ "Please use the attachments[] API instead."
params = { :content_type => params } if String === params
params[:content] ||= params.delete(:data) || params.delete(:body)
@@ -148,11 +142,11 @@ def normalize_file_hash(params)
def create_mail
m = @_message
- set_fields!({:subject => subject, :to => recipients, :from => from,
- :bcc => bcc, :cc => cc, :reply_to => reply_to}, charset)
+ set_fields!({:subject => @subject, :to => @recipients, :from => @from,
+ :bcc => @bcc, :cc => @cc, :reply_to => @reply_to}, @charset)
- m.mime_version = mime_version unless mime_version.nil?
- m.date = sent_on.to_time rescue sent_on if sent_on
+ m.mime_version = @mime_version if @mime_version
+ m.date = @sent_on.to_time rescue @sent_on if @sent_on
@headers.each { |k, v| m[k] = v }
@@ -191,6 +185,8 @@ def initialize_defaults(method_name)
@implicit_parts_order ||= self.class.default[:parts_order].try(:dup)
@mime_version ||= self.class.default[:mime_version].try(:dup)
+ @cc, @bcc, @reply_to, @subject, @from, @recipients = nil, nil, nil, nil, nil, nil
+
@mailer_name ||= self.class.mailer_name.dup
@template ||= method_name
@mail_was_called = false
@@ -78,3 +78,5 @@ def set_delivery_method(method)
def restore_delivery_method
ActionMailer::Base.delivery_method = @old_delivery_method
end
+
+ActiveSupport::Deprecation.silenced = true
@@ -3,9 +3,9 @@
class AssetHostMailer < ActionMailer::Base
def email_with_asset
- recipients 'test@localhost'
- subject "testing email containing asset path while asset_host is set"
- from "tester@example.com"
+ mail :to => 'test@localhost',
+ :subject => 'testing email containing asset path while asset_host is set',
+ :from => 'tester@example.com'
end
end
@@ -7,9 +7,6 @@
require 'mailers/asset_mailer'
class BaseTest < ActiveSupport::TestCase
- # TODO Add some tests for implicity layout render and url helpers
- # so we can get rid of old base tests altogether with old base.
-
def teardown
ActionMailer::Base.asset_host = nil
ActionMailer::Base.assets_dir = nil
@@ -1,53 +1,45 @@
require 'abstract_unit'
class AutoLayoutMailer < ActionMailer::Base
+ default :to => 'test@localhost',
+ :subject => "You have a mail",
+ :from => "tester@example.com"
def hello
- recipients 'test@localhost'
- subject "You have a mail"
- from "tester@example.com"
+ mail()
end
def spam
- recipients 'test@localhost'
- subject "You have a mail"
- from "tester@example.com"
-
@world = "Earth"
- body render(:inline => "Hello, <%= @world %>", :layout => 'spam')
+ mail(:body => render(:inline => "Hello, <%= @world %>", :layout => 'spam'))
end
def nolayout
- recipients 'test@localhost'
- subject "You have a mail"
- from "tester@example.com"
-
@world = "Earth"
- body render(:inline => "Hello, <%= @world %>", :layout => false)
+ mail(:body => render(:inline => "Hello, <%= @world %>", :layout => false))
end
def multipart(type = nil)
- recipients 'test@localhost'
- subject "You have a mail"
- from "tester@example.com"
-
- content_type(type) if type
+ mail(:content_type => type) do |format|
+ format.text { render }
+ format.html { render }
+ end
end
end
class ExplicitLayoutMailer < ActionMailer::Base
layout 'spam', :except => [:logout]
+ default :to => 'test@localhost',
+ :subject => "You have a mail",
+ :from => "tester@example.com"
+
def signup
- recipients 'test@localhost'
- subject "You have a mail"
- from "tester@example.com"
+ mail()
end
def logout
- recipients 'test@localhost'
- subject "You have a mail"
- from "tester@example.com"
+ mail()
end
end
@@ -91,19 +83,6 @@ def test_should_pickup_multipartmixed_layout
assert_equal "Hello from layout text/html multipart", mail.parts.last.body.to_s
end
- def test_should_fix_multipart_layout
- mail = AutoLayoutMailer.multipart("text/plain")
- assert_equal "multipart/alternative", mail.mime_type
- assert_equal 2, mail.parts.size
-
- assert_equal 'text/plain', mail.parts.first.mime_type
- assert_equal "text/plain layout - text/plain multipart", mail.parts.first.body.to_s
-
- assert_equal 'text/html', mail.parts.last.mime_type
- assert_equal "Hello from layout text/html multipart", mail.parts.last.body.to_s
- end
-
-
def test_should_pickup_layout_given_to_render
mail = AutoLayoutMailer.spam
assert_equal "Spammer layout Hello, Earth", mail.body.to_s.strip
@@ -113,6 +113,6 @@ def different_layout(layout_name='')
end
def email_with_translations
- body render("email_with_translations.html")
+ mail :body => render("email_with_translations.html")
end
end
@@ -11,9 +11,14 @@ class Person
end
def setup
+ ActiveSupport::Deprecation.silenced = true
@person = Person.new
end
+ def teardown
+ ActiveSupport::Deprecation.silenced = false
+ end
+
def test_adv_attr
assert_nil @person.name
@person.name 'Bob'
@@ -19,18 +19,6 @@ def file_template
body render(:file => "templates/signed_up")
end
- def rxml_template
- recipients 'test@localhost'
- subject "rendering rxml template"
- from "tester@example.com"
- end
-
- def included_subtemplate
- recipients 'test@localhost'
- subject "Including another template in the one being rendered"
- from "tester@example.com"
- end
-
def no_instance_variable
recipients 'test@localhost'
subject "No Instance Variable"
@@ -41,11 +29,6 @@ def no_instance_variable
end
end
- def initialize_defaults(method_name)
- super
- mailer_name "test_mailer"
- end
-
def multipart_alternative
recipients 'test@localhost'
subject 'multipart/alternative'
@@ -97,11 +80,13 @@ def setup
set_delivery_method :test
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.deliveries.clear
+ ActiveSupport::Deprecation.silenced = true
@recipient = 'test@localhost'
end
def teardown
+ ActiveSupport::Deprecation.silenced = false
restore_delivery_method
end
@@ -115,16 +100,6 @@ def test_file_template
assert_equal "Hello there,\n\nMr. test@localhost", mail.body.to_s.strip
end
- def test_rxml_template
- mail = RenderMailer.rxml_template.deliver
- assert_equal %(<?xml version="1.0" encoding="UTF-8"?>\n<test/>), mail.body.to_s.strip
- end
-
- def test_included_subtemplate
- mail = RenderMailer.included_subtemplate.deliver
- assert_equal "Hey Ho, let's go!", mail.body.to_s.strip
- end
-
def test_no_instance_variable
mail = RenderMailer.no_instance_variable.deliver
assert_equal "Look, subject.nil? is true!", mail.body.to_s.strip
@@ -134,13 +109,15 @@ def test_no_instance_variable
class FirstSecondHelperTest < Test::Unit::TestCase
def setup
set_delivery_method :test
+ ActiveSupport::Deprecation.silenced = true
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.deliveries.clear
@recipient = 'test@localhost'
end
def teardown
+ ActiveSupport::Deprecation.silenced = false
restore_delivery_method
end
@@ -334,6 +334,7 @@ def setup
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.deliveries.clear
+ ActiveSupport::Deprecation.silenced = true
@original_logger = TestMailer.logger
@recipient = 'test@localhost'
@@ -343,6 +344,7 @@ def setup
def teardown
TestMailer.logger = @original_logger
+ ActiveSupport::Deprecation.silenced = false
restore_delivery_method
end
Oops, something went wrong.

0 comments on commit 972efa1

Please sign in to comment.