aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--actionmailer/CHANGELOG2
-rw-r--r--actionmailer/lib/action_mailer/base.rb14
-rw-r--r--actionmailer/test/mail_render_test.rb48
3 files changed, 60 insertions, 4 deletions
diff --git a/actionmailer/CHANGELOG b/actionmailer/CHANGELOG
index d17955b7e9..4ae813e8aa 100644
--- a/actionmailer/CHANGELOG
+++ b/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
diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb
index fe6a9a4ead..96574fcfd9 100644
--- a/actionmailer/lib/action_mailer/base.rb
+++ b/actionmailer/lib/action_mailer/base.rb
@@ -156,7 +156,7 @@ module ActionMailer #:nodoc:
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 @@ module ActionMailer #:nodoc:
@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)
diff --git a/actionmailer/test/mail_render_test.rb b/actionmailer/test/mail_render_test.rb
new file mode 100644
index 0000000000..d581965284
--- /dev/null
+++ b/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
+