Skip to content
This repository
Browse code

raise an error if no implicit mailer template could be found

  • Loading branch information...
commit 45b1045d16126e69ad5af395fa14d1291914a4d4 1 parent b046ea4
Damien Mathieu authored June 20, 2012
4  actionmailer/CHANGELOG.md
Source Rendered
... ...
@@ -1,3 +1,7 @@
  1
+## Rails 4.0.0 (unreleased) ##
  2
+
  3
+* Raise an `ActionView::MissingTemplate` exception when no implicit template could be found. *Damien Mathieu*
  4
+
1 5
 ## Rails 3.2.5 (Jun 1, 2012) ##
2 6
 
3 7
 *   No changes.
22  actionmailer/lib/action_mailer/base.rb
@@ -184,6 +184,16 @@ module ActionMailer #:nodoc:
184 184
   # and the second being a <tt>application/pdf</tt> with a Base64 encoded copy of the file.pdf book
185 185
   # with the filename +free_book.pdf+.
186 186
   #
  187
+  # If you need to send attachments with no content, you need to create an empty view for it,
  188
+  # or add an empty body parameter like this:
  189
+  #
  190
+  #     class ApplicationMailer < ActionMailer::Base
  191
+  #       def welcome(recipient)
  192
+  #         attachments['free_book.pdf'] = File.read('path/to/file.pdf')
  193
+  #         mail(:to => recipient, :subject => "New account information", :body => "")
  194
+  #       end
  195
+  #     end
  196
+  #
187 197
   # = Inline Attachments
188 198
   #
189 199
   # You can also specify that a file should be displayed inline with other HTML. This is useful
@@ -598,8 +608,10 @@ def attachments
598 608
     #     end
599 609
     #   end
600 610
     #
601  
-    # Will look for all templates at "app/views/notifier" with name "welcome". However, those
602  
-    # can be customized:
  611
+    # Will look for all templates at "app/views/notifier" with name "welcome".
  612
+    # If no welcome template exists, it will raise an ActionView::MissingTemplate error.
  613
+    #
  614
+    # However, those can be customized:
603 615
     #
604 616
     #   mail(:template_path => 'notifications', :template_name => 'another')
605 617
     #
@@ -733,7 +745,11 @@ def collect_responses_and_parts_order(headers) #:nodoc:
733 745
 
734 746
     def each_template(paths, name, &block) #:nodoc:
735 747
       templates = lookup_context.find_all(name, Array(paths))
736  
-      templates.uniq { |t| t.formats }.each(&block)
  748
+      if templates.empty?
  749
+        raise ActionView::MissingTemplate.new([paths], name, [paths], false, 'mailer')
  750
+      else
  751
+        templates.uniq { |t| t.formats }.each(&block)
  752
+      end
737 753
     end
738 754
 
739 755
     def create_parts_from_responses(m, responses) #:nodoc:
7  actionmailer/test/base_test.rb
@@ -433,6 +433,13 @@ def teardown
433 433
     assert_equal("TEXT Implicit Multipart", mail.text_part.body.decoded)
434 434
   end
435 435
 
  436
+  test "should raise if missing template in implicit render" do
  437
+    assert_raises ActionView::MissingTemplate do
  438
+      BaseMailer.implicit_different_template('missing_template').deliver
  439
+    end
  440
+    assert_equal(0, BaseMailer.deliveries.length)
  441
+  end
  442
+
436 443
   test "you can specify a different template for explicit render" do
437 444
     mail = BaseMailer.explicit_different_template('explicit_multipart_templates').deliver
438 445
     assert_equal("HTML Explicit Multipart Templates", mail.html_part.body.decoded)
0  actionmailer/test/fixtures/base_mailer/attachment_with_hash.html.erb
No changes.
0  actionmailer/test/fixtures/base_mailer/attachment_with_hash_default_encoding.html.erb
No changes.
0  actionmailer/test/fixtures/base_mailer/welcome_with_headers.html.erb
No changes.
0  actionmailer/test/fixtures/base_test/after_filter_mailer/welcome.html.erb
No changes.
0  actionmailer/test/fixtures/base_test/before_filter_mailer/welcome.html.erb
No changes.
0  actionmailer/test/fixtures/base_test/default_inline_attachment_mailer/welcome.html.erb
No changes.
0  actionmailer/test/fixtures/mail_delivery_test/delivery_mailer/welcome.html.erb
No changes.
0  actionmailer/test/fixtures/proc_mailer/welcome.html.erb
No changes.

0 notes on commit 45b1045

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