Skip to content
This repository
Browse code

Register alternative template engines using ActionMailer::Base.regist…

…er_template_extension('haml'). Closes #7534.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6962 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 271404e8b929a5904dfe1a7007bee340139b6576 1 parent 869a172
Jeremy Kemper authored June 07, 2007
2  actionmailer/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* Register alternative template engines using ActionMailer::Base.register_template_extension('haml').  #7534 [cwd, Josh Peek]
  4
+
3 5
 * Only load ActionController::UrlWriter if ActionController is present [Rick Olson]
4 6
 
5 7
 * Make sure parsed emails recognized attachments nested inside multipart parts. #6714 [Jamis Buck]
18  actionmailer/lib/action_mailer/base.rb
@@ -230,6 +230,9 @@ class Base
230 230
     class_inheritable_accessor :template_root
231 231
     cattr_accessor :logger
232 232
 
  233
+    cattr_accessor :template_extensions
  234
+    @@template_extensions = ['erb', 'builder', 'rhtml', 'rxml']
  235
+
233 236
     @@smtp_settings = { 
234 237
       :address        => "localhost", 
235 238
       :port           => 25, 
@@ -363,6 +366,17 @@ def receive(raw_email)
363 366
       def deliver(mail)
364 367
         new.deliver!(mail)
365 368
       end
  369
+
  370
+      # Register a template extension so mailer templates written in a
  371
+      # templating language other than rhtml or rxml are supported.
  372
+      # To use this, include in your template-language plugin's init
  373
+      # code or on a per-application basis, this can be invoked from
  374
+      # config/environment.rb:
  375
+      #
  376
+      #   ActionMailer::Base.register_template_extension('haml')
  377
+      def register_template_extension(extension)
  378
+        template_extensions << extension
  379
+      end
366 380
     end
367 381
 
368 382
     # Instantiate a new mailer object. If +method_name+ is not +nil+, the mailer
@@ -388,9 +402,9 @@ def create!(method_name, *parameters) #:nodoc:
388 402
         if @parts.empty?
389 403
           templates = Dir.glob("#{template_path}/#{@template}.*")
390 404
           templates.each do |path|
391  
-            # TODO: don't hardcode erb|builder
392 405
             basename = File.basename(path)
393  
-            next unless md = /^([^\.]+)\.([^\.]+\.[^\.]+)\.(erb|builder|rhtml|rxml)$/.match(basename)
  406
+            template_regex = Regexp.new("^([^\\\.]+)\\\.([^\\\.]+\\\.[^\\\.]+)\\\.(" + template_extensions.join('|') + ")$")
  407
+            next unless md = template_regex.match(basename)
394 408
             template_name = basename
395 409
             content_type = md.captures[1].gsub('.', '/')
396 410
             @parts << Part.new(:content_type => content_type,
41  actionmailer/test/mail_service_test.rb
@@ -172,6 +172,15 @@ def custom_template(recipient)
172 172
     body["recipient"] = recipient
173 173
   end
174 174
 
  175
+  def custom_templating_extension(recipient)
  176
+    recipients recipient
  177
+    subject    "[Signed up] Welcome #{recipient}"
  178
+    from       "system@loudthinking.com"
  179
+    sent_on    Time.local(2004, 12, 12)
  180
+
  181
+    body["recipient"] = recipient
  182
+  end
  183
+
175 184
   def various_newlines(recipient)
176 185
     recipients   recipient
177 186
     subject      "various newlines"
@@ -329,7 +338,37 @@ def test_custom_template
329 338
     assert_not_nil created
330 339
     assert_equal expected.encoded, created.encoded
331 340
   end
332  
-  
  341
+
  342
+  def test_custom_templating_extension
  343
+    #
  344
+    # N.b., custom_templating_extension.text.plain.haml is expected to be in fixtures/test_mailer directory
  345
+    expected = new_mail
  346
+    expected.to      = @recipient
  347
+    expected.subject = "[Signed up] Welcome #{@recipient}"
  348
+    expected.body    = "Hello there, \n\nMr. #{@recipient}"
  349
+    expected.from    = "system@loudthinking.com"
  350
+    expected.date    = Time.local(2004, 12, 12)
  351
+    
  352
+    # Stub the render method so no alternative renderers need be present.
  353
+    ActionView::Base.any_instance.stubs(:render).returns("Hello there, \n\nMr. #{@recipient}")
  354
+    
  355
+    # If the template is not registered, there should be no parts.
  356
+    created = nil
  357
+    assert_nothing_raised { created = TestMailer.create_custom_templating_extension(@recipient) }
  358
+    assert_not_nil created
  359
+    assert_equal 0, created.parts.length
  360
+    
  361
+    ActionMailer::Base.register_template_extension('haml')
  362
+    
  363
+    # Now that the template is registered, there should be one part. The text/plain part.
  364
+    created = nil
  365
+    assert_nothing_raised { created = TestMailer.create_custom_templating_extension(@recipient) }
  366
+    assert_not_nil created
  367
+    assert_equal 2, created.parts.length
  368
+    assert_equal 'text/plain', created.parts[0].content_type
  369
+    assert_equal 'text/html', created.parts[1].content_type
  370
+  end
  371
+
333 372
   def test_cancelled_account
334 373
     expected = new_mail
335 374
     expected.to      = @recipient

0 notes on commit 271404e

Please sign in to comment.
Something went wrong with that request. Please try again.