diff options
author | Yehuda Katz <wycats@Yehuda-Katz.local> | 2009-12-25 13:06:11 -0800 |
---|---|---|
committer | Yehuda Katz <wycats@Yehuda-Katz.local> | 2009-12-25 13:06:11 -0800 |
commit | 8f6da9483b28ec82e4577bf4cbe5209e2c750c84 (patch) | |
tree | 4c52a09b3268ff90dae12f8bc525f0b2f3acbace /actionmailer | |
parent | 2e79ec71a542c2d2e7bedbe12eda0b5e177fb0e0 (diff) | |
parent | 9f1c359a201bfacff7b89cd1e8579bc1bdf541e2 (diff) | |
download | rails-8f6da9483b28ec82e4577bf4cbe5209e2c750c84.tar.gz rails-8f6da9483b28ec82e4577bf4cbe5209e2c750c84.tar.bz2 rails-8f6da9483b28ec82e4577bf4cbe5209e2c750c84.zip |
Merge remote branch 'jose/am'
Diffstat (limited to 'actionmailer')
-rw-r--r-- | actionmailer/lib/action_mailer/adv_attr_accessor.rb | 32 | ||||
-rw-r--r-- | actionmailer/lib/action_mailer/base.rb | 36 | ||||
-rw-r--r-- | actionmailer/lib/action_mailer/mail_helper.rb | 5 | ||||
-rw-r--r-- | actionmailer/lib/action_mailer/part.rb | 15 | ||||
-rw-r--r-- | actionmailer/lib/action_mailer/part_container.rb | 20 | ||||
-rw-r--r-- | actionmailer/lib/action_mailer/utils.rb | 7 | ||||
-rw-r--r-- | actionmailer/test/adv_attr_test.rb | 32 | ||||
-rw-r--r-- | actionmailer/test/mail_render_test.rb | 25 |
8 files changed, 101 insertions, 71 deletions
diff --git a/actionmailer/lib/action_mailer/adv_attr_accessor.rb b/actionmailer/lib/action_mailer/adv_attr_accessor.rb index e77029afdd..be6b1feca9 100644 --- a/actionmailer/lib/action_mailer/adv_attr_accessor.rb +++ b/actionmailer/lib/action_mailer/adv_attr_accessor.rb @@ -1,29 +1,25 @@ module ActionMailer module AdvAttrAccessor #:nodoc: - def self.included(base) - base.extend(ClassMethods) - end - - module ClassMethods #:nodoc: - def adv_attr_accessor(*names) - names.each do |name| - ivar = "@#{name}" + def adv_attr_accessor(*names) + names.each do |name| + ivar = "@#{name}" - define_method("#{name}=") do |value| - instance_variable_set(ivar, value) + class_eval <<-ACCESSORS, __FILE__, __LINE__ + 1 + def #{name}=(value) + #{ivar} = value end - define_method(name) do |*parameters| - raise ArgumentError, "expected 0 or 1 parameters" unless parameters.length <= 1 - if parameters.empty? - if instance_variable_names.include?(ivar) - instance_variable_get(ivar) - 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 - instance_variable_set(ivar, parameters.first) + #{ivar} = args.first end end - end + ACCESSORS + + self.protected_instance_variables << ivar if self.respond_to?(:protected_instance_variables) end end end diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index de78e87fb4..84f5bd23a9 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -25,7 +25,8 @@ module ActionMailer #:nodoc: # bcc ["bcc@example.com", "Order Watcher <watcher@example.com>"] # from "system@example.com" # subject "New account information" - # body :account => recipient + # + # @account = recipient # end # end # @@ -45,13 +46,6 @@ module ActionMailer #:nodoc: # address. Setting this is useful when you want delivery notifications sent to a different address than # 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 # @@ -71,7 +65,12 @@ module ActionMailer #:nodoc: # You can even use Action Pack helpers in these views. For example: # # 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 @@ -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 # +implicit_parts_order+. class Base < AbstractController::Base - include AdvAttrAccessor, PartContainer, Quoting, Utils + include PartContainer, Quoting + extend AdvAttrAccessor include AbstractController::Rendering include AbstractController::LocalizedCache include AbstractController::Layouts include AbstractController::Helpers - helper ActionMailer::MailHelper + helper ActionMailer::MailHelper include ActionController::UrlWriter include ActionMailer::DeprecatedBody @@ -289,7 +289,7 @@ module ActionMailer #:nodoc: @@default_implicit_parts_order = [ "text/html", "text/enriched", "text/plain" ] cattr_accessor :default_implicit_parts_order - @@protected_instance_variables = [] + @@protected_instance_variables = %w(@parts @mail) cattr_reader :protected_instance_variables # Specify the BCC addresses for the message @@ -454,7 +454,7 @@ module ActionMailer #:nodoc: :default => method_name.humanize) # Build the mail object itself - @mail = create_mail + create_mail end # Delivers a TMail::Mail object. By default, it delivers the cached mail @@ -582,15 +582,7 @@ module ActionMailer #:nodoc: m.set_content_type(real_content_type, nil, ctype_attrs) m.body = normalize_new_lines(@parts.first.body) else - @parts.each do |p| - part = (TMail::Mail === p ? p : p.to_mail(self)) - m.parts << part - end - - if real_content_type =~ /multipart/ - ctype_attrs.delete "charset" - m.set_content_type(real_content_type, nil, ctype_attrs) - end + setup_multiple_parts(m, real_content_type, ctype_attrs) end @mail = m diff --git a/actionmailer/lib/action_mailer/mail_helper.rb b/actionmailer/lib/action_mailer/mail_helper.rb index 9aa178cdef..a1dec68c5d 100644 --- a/actionmailer/lib/action_mailer/mail_helper.rb +++ b/actionmailer/lib/action_mailer/mail_helper.rb @@ -15,5 +15,10 @@ module ActionMailer formatted end + + # Access the mailer instance. + def mailer #:nodoc: + @controller + end end end
\ No newline at end of file diff --git a/actionmailer/lib/action_mailer/part.rb b/actionmailer/lib/action_mailer/part.rb index 2bbb59cdb6..716eaae86e 100644 --- a/actionmailer/lib/action_mailer/part.rb +++ b/actionmailer/lib/action_mailer/part.rb @@ -4,7 +4,8 @@ module ActionMailer # and add them to the +parts+ list of the mailer, it is easier # to use the helper methods in ActionMailer::PartContainer. class Part - include AdvAttrAccessor, PartContainer, Utils + include PartContainer + extend AdvAttrAccessor # 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 @@ -82,16 +83,8 @@ module ActionMailer @parts.unshift Part.new(:charset => charset, :body => @body, :content_type => 'text/plain') @body = nil end - - @parts.each do |p| - prt = (TMail::Mail === p ? p : p.to_mail(defaults)) - part.parts << prt - end - - if real_content_type =~ /multipart/ - ctype_attrs.delete 'charset' - part.set_content_type(real_content_type, nil, ctype_attrs) - end + + setup_multiple_parts(part, real_content_type, ctype_attrs) end headers.each { |k,v| part[k] = v } diff --git a/actionmailer/lib/action_mailer/part_container.rb b/actionmailer/lib/action_mailer/part_container.rb index abfd8f8426..3fe502b1fb 100644 --- a/actionmailer/lib/action_mailer/part_container.rb +++ b/actionmailer/lib/action_mailer/part_container.rb @@ -39,8 +39,24 @@ module ActionMailer end private - - def parse_content_type(defaults=nil) + + def normalize_new_lines(text) #:nodoc: + text.to_s.gsub(/\r\n?/, "\n") + end + + def setup_multiple_parts(mailer, real_content_type, ctype_attrs) #:nodoc: + @parts.each do |p| + part = (TMail::Mail === p ? p : p.to_mail(self)) + mailer.parts << part + end + + if real_content_type =~ /multipart/ + ctype_attrs.delete "charset" + mailer.set_content_type(real_content_type, nil, ctype_attrs) + end + end + + def parse_content_type(defaults=nil) #:nodoc: if content_type.blank? return defaults ? [ defaults.content_type, { 'charset' => defaults.charset } ] : diff --git a/actionmailer/lib/action_mailer/utils.rb b/actionmailer/lib/action_mailer/utils.rb deleted file mode 100644 index 26d2e60aaf..0000000000 --- a/actionmailer/lib/action_mailer/utils.rb +++ /dev/null @@ -1,7 +0,0 @@ -module ActionMailer - module Utils #:nodoc: - def normalize_new_lines(text) - text.to_s.gsub(/\r\n?/, "\n") - end - end -end diff --git a/actionmailer/test/adv_attr_test.rb b/actionmailer/test/adv_attr_test.rb index fd909a5627..f22d733bc5 100644 --- a/actionmailer/test/adv_attr_test.rb +++ b/actionmailer/test/adv_attr_test.rb @@ -1,18 +1,36 @@ require 'abstract_unit' require 'action_mailer/adv_attr_accessor' -class AdvAttrTest < Test::Unit::TestCase +class AdvAttrTest < ActiveSupport::TestCase class Person - include ActionMailer::AdvAttrAccessor + cattr_reader :protected_instance_variables + @@protected_instance_variables = [] + + extend ActionMailer::AdvAttrAccessor adv_attr_accessor :name end + def setup + @person = Person.new + end + def test_adv_attr - bob = Person.new - assert_nil bob.name - bob.name 'Bob' - assert_equal 'Bob', bob.name + assert_nil @person.name + @person.name 'Bob' + assert_equal 'Bob', @person.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 diff --git a/actionmailer/test/mail_render_test.rb b/actionmailer/test/mail_render_test.rb index 514f7ed798..0e38a0caba 100644 --- a/actionmailer/test/mail_render_test.rb +++ b/actionmailer/test/mail_render_test.rb @@ -40,13 +40,20 @@ class RenderMailer < ActionMailer::Base from "tester@example.com" end - def included_old_subtemplate(recipient) + def mailer_accessor(recipient) recipients recipient - subject "Including another template in the one being rendered" + subject "Mailer Accessor" from "tester@example.com" - @world = "Earth" - render :inline => "Hello, <%= render \"subtemplate\" %>" + render :inline => "Look, <%= mailer.subject %>!" + 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 def initialize_defaults(method_name) @@ -108,6 +115,16 @@ class RenderHelperTest < Test::Unit::TestCase mail = RenderMailer.deliver_included_subtemplate(@recipient) assert_equal "Hey Ho, let's go!", mail.body.strip 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 class FirstSecondHelperTest < Test::Unit::TestCase |