aboutsummaryrefslogtreecommitdiffstats
path: root/actionmailer
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2007-06-07 23:42:47 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2007-06-07 23:42:47 +0000
commit271404e8b929a5904dfe1a7007bee340139b6576 (patch)
treeefa066c0032f5b5c3320c867ac5b9a101db164fd /actionmailer
parent869a172a8a6e2d2182f16e959f4a41fa10df133a (diff)
downloadrails-271404e8b929a5904dfe1a7007bee340139b6576.tar.gz
rails-271404e8b929a5904dfe1a7007bee340139b6576.tar.bz2
rails-271404e8b929a5904dfe1a7007bee340139b6576.zip
Register alternative template engines using ActionMailer::Base.register_template_extension('haml'). Closes #7534.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6962 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionmailer')
-rw-r--r--actionmailer/CHANGELOG2
-rw-r--r--actionmailer/lib/action_mailer/base.rb18
-rwxr-xr-xactionmailer/test/mail_service_test.rb41
3 files changed, 58 insertions, 3 deletions
diff --git a/actionmailer/CHANGELOG b/actionmailer/CHANGELOG
index f2a2e0bee1..f1ba5910ba 100644
--- a/actionmailer/CHANGELOG
+++ b/actionmailer/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Register alternative template engines using ActionMailer::Base.register_template_extension('haml'). #7534 [cwd, Josh Peek]
+
* Only load ActionController::UrlWriter if ActionController is present [Rick Olson]
* Make sure parsed emails recognized attachments nested inside multipart parts. #6714 [Jamis Buck]
diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb
index 378168bc04..43cbbcbcfe 100644
--- a/actionmailer/lib/action_mailer/base.rb
+++ b/actionmailer/lib/action_mailer/base.rb
@@ -230,6 +230,9 @@ module ActionMailer #:nodoc:
class_inheritable_accessor :template_root
cattr_accessor :logger
+ cattr_accessor :template_extensions
+ @@template_extensions = ['erb', 'builder', 'rhtml', 'rxml']
+
@@smtp_settings = {
:address => "localhost",
:port => 25,
@@ -363,6 +366,17 @@ module ActionMailer #:nodoc:
def deliver(mail)
new.deliver!(mail)
end
+
+ # Register a template extension so mailer templates written in a
+ # templating language other than rhtml or rxml are supported.
+ # To use this, include in your template-language plugin's init
+ # code or on a per-application basis, this can be invoked from
+ # config/environment.rb:
+ #
+ # ActionMailer::Base.register_template_extension('haml')
+ def register_template_extension(extension)
+ template_extensions << extension
+ end
end
# Instantiate a new mailer object. If +method_name+ is not +nil+, the mailer
@@ -388,9 +402,9 @@ module ActionMailer #:nodoc:
if @parts.empty?
templates = Dir.glob("#{template_path}/#{@template}.*")
templates.each do |path|
- # TODO: don't hardcode erb|builder
basename = File.basename(path)
- next unless md = /^([^\.]+)\.([^\.]+\.[^\.]+)\.(erb|builder|rhtml|rxml)$/.match(basename)
+ template_regex = Regexp.new("^([^\\\.]+)\\\.([^\\\.]+\\\.[^\\\.]+)\\\.(" + template_extensions.join('|') + ")$")
+ next unless md = template_regex.match(basename)
template_name = basename
content_type = md.captures[1].gsub('.', '/')
@parts << Part.new(:content_type => content_type,
diff --git a/actionmailer/test/mail_service_test.rb b/actionmailer/test/mail_service_test.rb
index 6e0d727abd..2532760b4a 100755
--- a/actionmailer/test/mail_service_test.rb
+++ b/actionmailer/test/mail_service_test.rb
@@ -172,6 +172,15 @@ class TestMailer < ActionMailer::Base
body["recipient"] = recipient
end
+ def custom_templating_extension(recipient)
+ recipients recipient
+ subject "[Signed up] Welcome #{recipient}"
+ from "system@loudthinking.com"
+ sent_on Time.local(2004, 12, 12)
+
+ body["recipient"] = recipient
+ end
+
def various_newlines(recipient)
recipients recipient
subject "various newlines"
@@ -329,7 +338,37 @@ class ActionMailerTest < Test::Unit::TestCase
assert_not_nil created
assert_equal expected.encoded, created.encoded
end
-
+
+ def test_custom_templating_extension
+ #
+ # N.b., custom_templating_extension.text.plain.haml is expected to be in fixtures/test_mailer directory
+ expected = new_mail
+ expected.to = @recipient
+ expected.subject = "[Signed up] Welcome #{@recipient}"
+ expected.body = "Hello there, \n\nMr. #{@recipient}"
+ expected.from = "system@loudthinking.com"
+ expected.date = Time.local(2004, 12, 12)
+
+ # Stub the render method so no alternative renderers need be present.
+ ActionView::Base.any_instance.stubs(:render).returns("Hello there, \n\nMr. #{@recipient}")
+
+ # If the template is not registered, there should be no parts.
+ created = nil
+ assert_nothing_raised { created = TestMailer.create_custom_templating_extension(@recipient) }
+ assert_not_nil created
+ assert_equal 0, created.parts.length
+
+ ActionMailer::Base.register_template_extension('haml')
+
+ # Now that the template is registered, there should be one part. The text/plain part.
+ created = nil
+ assert_nothing_raised { created = TestMailer.create_custom_templating_extension(@recipient) }
+ assert_not_nil created
+ assert_equal 2, created.parts.length
+ assert_equal 'text/plain', created.parts[0].content_type
+ assert_equal 'text/html', created.parts[1].content_type
+ end
+
def test_cancelled_account
expected = new_mail
expected.to = @recipient