Skip to content

Commit

Permalink
headers["X-Foo-Count"] = 2 is deprecated properly now
Browse files Browse the repository at this point in the history
  • Loading branch information
wycats committed Aug 15, 2010
1 parent 19fb031 commit 6883681
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
31 changes: 29 additions & 2 deletions actionmailer/lib/action_mailer/base.rb
Expand Up @@ -446,6 +446,33 @@ def process(*args) #:nodoc:
super
end

class DeprecatedHeaderProxy < ActiveSupport::BasicObject
def initialize(message)
@message = message
end

def []=(key, value)
unless value.is_a?(String)
ActiveSupport::Deprecation.warn("Using a non-String object for a header's value is deprecated. " \
"You specified #{value.inspect} (a #{value.class}) for #{key}", caller)

value = value.to_s
end

@message[key] = value
end

def headers(hash = {})
hash.each_pair do |k,v|
self[k] = v
end
end

def method_missing(meth, *args, &block)
@message.send(meth, *args, &block)
end
end

# Allows you to pass random and unusual headers to the new +Mail::Message+ object
# which will add them to itself.
#
Expand All @@ -462,9 +489,9 @@ def process(*args) #:nodoc:
# X-Special-Domain-Specific-Header: SecretValue
def headers(args=nil)
if args
@_message.headers(args)
DeprecatedHeaderProxy.new(@_message).headers(args)
else
@_message
DeprecatedHeaderProxy.new(@_message)
end
end

Expand Down
5 changes: 5 additions & 0 deletions actionmailer/test/base_test.rb
Expand Up @@ -76,6 +76,11 @@ def teardown
assert_equal("Not SPAM", email['X-SPAM'].decoded)
end

test "deprecated non-String custom headers" do
email = assert_deprecated { BaseMailer.welcome_with_fixnum_header }
assert_equal("2", email['X-SPAM-COUNT'].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' }
Expand Down
5 changes: 5 additions & 0 deletions actionmailer/test/mailers/base_mailer.rb
Expand Up @@ -10,6 +10,11 @@ def welcome(hash = {})
mail({:subject => "The first email on new API!"}.merge!(hash))
end

def welcome_with_fixnum_header(hash = {})
headers['X-SPAM-COUNT'] = 2
mail({:template_name => "welcome", :subject => "The first email on new API!"}.merge!(hash))
end

def welcome_with_headers(hash = {})
headers hash
mail
Expand Down

0 comments on commit 6883681

Please sign in to comment.