提交 ee70d1b6 编写于 作者: J José Valim

adv_attr_accessors in ActionMailer are not sent to the views, use the mailer...

adv_attr_accessors in ActionMailer are not sent to the views, use the mailer object if you need to access the subject, recipients, from, etc.
上级 135d32c8
module ActionMailer module ActionMailer
module AdvAttrAccessor #:nodoc: module AdvAttrAccessor #:nodoc:
def self.included(base) def adv_attr_accessor(*names)
base.extend(ClassMethods) names.each do |name|
end ivar = "@#{name}"
module ClassMethods #:nodoc:
def adv_attr_accessor(*names)
names.each do |name|
ivar = "@#{name}"
define_method("#{name}=") do |value| class_eval <<-ACCESSORS, __FILE__, __LINE__ + 1
instance_variable_set(ivar, value) def #{name}=(value)
#{ivar} = value
end end
define_method(name) do |*parameters| def #{name}(*args)
raise ArgumentError, "expected 0 or 1 parameters" unless parameters.length <= 1 raise ArgumentError, "expected 0 or 1 parameters" unless args.length <= 1
if parameters.empty? if args.empty?
if instance_variable_names.include?(ivar) #{ivar} if instance_variable_names.include?(#{ivar.inspect})
instance_variable_get(ivar)
end
else else
instance_variable_set(ivar, parameters.first) #{ivar} = args.first
end end
end end
end ACCESSORS
self.protected_instance_variables << ivar if self.respond_to?(:protected_instance_variables)
end end
end end
end end
......
...@@ -25,7 +25,8 @@ module ActionMailer #:nodoc: ...@@ -25,7 +25,8 @@ module ActionMailer #:nodoc:
# bcc ["bcc@example.com", "Order Watcher <watcher@example.com>"] # bcc ["bcc@example.com", "Order Watcher <watcher@example.com>"]
# from "system@example.com" # from "system@example.com"
# subject "New account information" # subject "New account information"
# body :account => recipient #
# @account = recipient
# end # end
# end # end
# #
...@@ -45,13 +46,6 @@ module ActionMailer #:nodoc: ...@@ -45,13 +46,6 @@ module ActionMailer #:nodoc:
# address. Setting this is useful when you want delivery notifications sent to a different address than # address. Setting this is useful when you want delivery notifications sent to a different address than
# the one in <tt>from</tt>. # the one in <tt>from</tt>.
# #
# The <tt>body</tt> method has special behavior. It takes a hash which generates an instance variable
# named after each key in the hash containing the value that that key points to.
#
# So, for example, <tt>body :account => recipient</tt> would result
# in an instance variable <tt>@account</tt> with the value of <tt>recipient</tt> being accessible in the
# view.
#
# #
# = Mailer views # = Mailer views
# #
...@@ -71,7 +65,12 @@ module ActionMailer #:nodoc: ...@@ -71,7 +65,12 @@ module ActionMailer #:nodoc:
# You can even use Action Pack helpers in these views. For example: # You can even use Action Pack helpers in these views. For example:
# #
# You got a new note! # You got a new note!
# <%= truncate(note.body, 25) %> # <%= truncate(@note.body, 25) %>
#
# If you need to access the subject, from or the recipients in the view, you can do that through mailer object:
#
# You got a new note from <%= mailer.from %>!
# <%= truncate(@note.body, 25) %>
# #
# #
# = Generating URLs # = Generating URLs
...@@ -254,14 +253,15 @@ module ActionMailer #:nodoc: ...@@ -254,14 +253,15 @@ module ActionMailer #:nodoc:
# and appear last in the mime encoded message. You can also pick a different order from inside a method with # and appear last in the mime encoded message. You can also pick a different order from inside a method with
# +implicit_parts_order+. # +implicit_parts_order+.
class Base < AbstractController::Base class Base < AbstractController::Base
include AdvAttrAccessor, PartContainer, Quoting, Utils include PartContainer, Quoting, Utils
extend AdvAttrAccessor
include AbstractController::Rendering include AbstractController::Rendering
include AbstractController::LocalizedCache include AbstractController::LocalizedCache
include AbstractController::Layouts include AbstractController::Layouts
include AbstractController::Helpers include AbstractController::Helpers
helper ActionMailer::MailHelper helper ActionMailer::MailHelper
include ActionController::UrlWriter include ActionController::UrlWriter
include ActionMailer::DeprecatedBody include ActionMailer::DeprecatedBody
...@@ -289,7 +289,7 @@ class Base < AbstractController::Base ...@@ -289,7 +289,7 @@ class Base < AbstractController::Base
@@default_implicit_parts_order = [ "text/html", "text/enriched", "text/plain" ] @@default_implicit_parts_order = [ "text/html", "text/enriched", "text/plain" ]
cattr_accessor :default_implicit_parts_order cattr_accessor :default_implicit_parts_order
@@protected_instance_variables = [] @@protected_instance_variables = %w(@parts @mail)
cattr_reader :protected_instance_variables cattr_reader :protected_instance_variables
# Specify the BCC addresses for the message # Specify the BCC addresses for the message
......
...@@ -15,5 +15,10 @@ def block_format(text) ...@@ -15,5 +15,10 @@ def block_format(text)
formatted formatted
end end
# Access the mailer instance.
def mailer #:nodoc:
@controller
end
end end
end end
\ No newline at end of file
...@@ -4,7 +4,8 @@ module ActionMailer ...@@ -4,7 +4,8 @@ module ActionMailer
# and add them to the +parts+ list of the mailer, it is easier # and add them to the +parts+ list of the mailer, it is easier
# to use the helper methods in ActionMailer::PartContainer. # to use the helper methods in ActionMailer::PartContainer.
class Part class Part
include AdvAttrAccessor, PartContainer, Utils include PartContainer, Utils
extend AdvAttrAccessor
# Represents the body of the part, as a string. This should not be a # Represents the body of the part, as a string. This should not be a
# Hash (like ActionMailer::Base), but if you want a template to be rendered # Hash (like ActionMailer::Base), but if you want a template to be rendered
......
require 'abstract_unit' require 'abstract_unit'
require 'action_mailer/adv_attr_accessor' require 'action_mailer/adv_attr_accessor'
class AdvAttrTest < Test::Unit::TestCase class AdvAttrTest < ActiveSupport::TestCase
class Person class Person
include ActionMailer::AdvAttrAccessor cattr_reader :protected_instance_variables
@@protected_instance_variables = []
extend ActionMailer::AdvAttrAccessor
adv_attr_accessor :name adv_attr_accessor :name
end end
def setup
@person = Person.new
end
def test_adv_attr def test_adv_attr
bob = Person.new assert_nil @person.name
assert_nil bob.name @person.name 'Bob'
bob.name 'Bob' assert_equal 'Bob', @person.name
assert_equal 'Bob', bob.name end
def test_adv_attr_writer
assert_nil @person.name
@person.name = 'Bob'
assert_equal 'Bob', @person.name
end
def test_raise_an_error_with_multiple_args
assert_raise(ArgumentError) { @person.name('x', 'y') }
end
assert_raise(ArgumentError) {bob.name 'x', 'y'} def test_ivar_is_added_to_protected_instnace_variables
assert Person.protected_instance_variables.include?('@name')
end end
end end
...@@ -40,13 +40,20 @@ def included_subtemplate(recipient) ...@@ -40,13 +40,20 @@ def included_subtemplate(recipient)
from "tester@example.com" from "tester@example.com"
end end
def included_old_subtemplate(recipient) def mailer_accessor(recipient)
recipients recipient recipients recipient
subject "Including another template in the one being rendered" subject "Mailer Accessor"
from "tester@example.com" from "tester@example.com"
@world = "Earth" render :inline => "Look, <%= mailer.subject %>!"
render :inline => "Hello, <%= render \"subtemplate\" %>" end
def no_instance_variable(recipient)
recipients recipient
subject "No Instance Variable"
from "tester@example.com"
render :inline => "Look, subject.nil? is <%= @subject.nil? %>!"
end end
def initialize_defaults(method_name) def initialize_defaults(method_name)
...@@ -108,6 +115,16 @@ def test_included_subtemplate ...@@ -108,6 +115,16 @@ def test_included_subtemplate
mail = RenderMailer.deliver_included_subtemplate(@recipient) mail = RenderMailer.deliver_included_subtemplate(@recipient)
assert_equal "Hey Ho, let's go!", mail.body.strip assert_equal "Hey Ho, let's go!", mail.body.strip
end end
def test_mailer_accessor
mail = RenderMailer.deliver_mailer_accessor(@recipient)
assert_equal "Look, Mailer Accessor!", mail.body.strip
end
def test_no_instance_variable
mail = RenderMailer.deliver_no_instance_variable(@recipient)
assert_equal "Look, subject.nil? is true!", mail.body.strip
end
end end
class FirstSecondHelperTest < Test::Unit::TestCase class FirstSecondHelperTest < Test::Unit::TestCase
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册