Permalink
Browse files

Add a unified render method to ActionMailer (delegates to ActionView:…

…:Base#render)

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2037 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 516d920 commit ae1e85200e1480a47db7c3ccfcc7d256f37e88ba @jamis jamis committed Aug 22, 2005
Showing with 60 additions and 4 deletions.
  1. +2 −0 actionmailer/CHANGELOG
  2. +10 −4 actionmailer/lib/action_mailer/base.rb
  3. +48 −0 actionmailer/test/mail_render_test.rb
View
2 actionmailer/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Add a unified render method to ActionMailer (delegates to ActionView::Base#render)
+
* Move mailer initialization to a separate (overridable) method, so that subclasses may alter the various defaults #1727
* Look at content-location header (if available) to determine filename of attachments #1670
View
14 actionmailer/lib/action_mailer/base.rb
@@ -156,7 +156,7 @@ class Base
adv_attr_accessor :recipients, :subject, :body, :from, :sent_on, :headers,
:bcc, :cc, :charset, :content_type, :implicit_parts_order,
- :template
+ :template, :mailer_name
attr_reader :mail
@@ -269,17 +269,23 @@ def initialize_defaults(method_name)
@content_type = @@default_content_type.dup
@implicit_parts_order = @@default_implicit_parts_order.dup
@template = method_name
+ @mailer_name = Inflector.underscore(self.class.name)
@parts = []
@headers = {}
@body = {}
end
def render_message(method_name, body)
- initialize_template_class(body).render_file(method_name)
+ render :file => method_name, :body => body
end
-
+
+ def render(opts)
+ body = opts.delete(:body)
+ initialize_template_class(body).render(opts)
+ end
+
def template_path
- template_root + "/" + Inflector.underscore(self.class.name)
+ "#{template_root}/#{mailer_name}"
end
def initialize_template_class(assigns)
View
48 actionmailer/test/mail_render_test.rb
@@ -0,0 +1,48 @@
+$:.unshift(File.dirname(__FILE__) + "/../lib/")
+
+require 'test/unit'
+require 'action_mailer'
+
+class RenderMailer < ActionMailer::Base
+ def inline_template(recipient)
+ recipients recipient
+ subject "using helpers"
+ from "tester@example.com"
+ body render(:inline => "Hello, <%= @world %>", :body => { :world => "Earth" })
+ end
+
+ def file_template(recipient)
+ recipients recipient
+ subject "using helpers"
+ from "tester@example.com"
+ body render(:file => "signed_up", :body => { :recipient => recipient })
+ end
+
+ def initialize_defaults(method_name)
+ super
+ mailer_name "test_mailer"
+ end
+end
+
+RenderMailer.template_root = File.dirname(__FILE__) + "/fixtures"
+
+class RenderHelperTest < Test::Unit::TestCase
+ def setup
+ ActionMailer::Base.delivery_method = :test
+ ActionMailer::Base.perform_deliveries = true
+ ActionMailer::Base.deliveries = []
+
+ @recipient = 'test@localhost'
+ end
+
+ def test_inline_template
+ mail = RenderMailer.create_inline_template(@recipient)
+ assert_equal "Hello, Earth", mail.body.strip
+ end
+
+ def test_file_template
+ mail = RenderMailer.create_file_template(@recipient)
+ assert_equal "Hello there, \n\nMr. test@localhost", mail.body.strip
+ end
+end
+

0 comments on commit ae1e852

Please sign in to comment.