Skip to content
This repository
Browse code

Allow template to be explicitly specified #1448 [tuxie@dekadance.se]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1575 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 813a8b9d2a17e1f0d7f7dfdcf04a79c94722e283 1 parent 2455f96
Jamis Buck authored June 30, 2005
2  actionmailer/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* Allow template to be explicitly specified #1448 [tuxie@dekadance.se]
  4
+
3 5
 * Allow specific "multipart/xxx" content-type to be set on multipart messages #1412 [Flurin Egger]
4 6
 
5 7
 * Unquoted @ characters in headers are now accepted in spite of RFC 822 #1206
9  actionmailer/lib/action_mailer/base.rb
@@ -143,7 +143,7 @@ class Base
143 143
     cattr_accessor :default_content_type
144 144
 
145 145
     adv_attr_accessor :recipients, :subject, :body, :from, :sent_on, :headers,
146  
-                      :bcc, :cc, :charset, :content_type
  146
+                      :bcc, :cc, :charset, :content_type, :template
147 147
 
148 148
     attr_reader       :mail
149 149
 
@@ -161,6 +161,7 @@ def create!(method_name, *parameters)
161 161
       @bcc = @cc = @from = @recipients = @sent_on = @subject = nil
162 162
       @charset = @@default_charset.dup
163 163
       @content_type = @@default_content_type.dup
  164
+      @template = method_name
164 165
       @parts = []
165 166
       @headers = {}
166 167
       @body = {}
@@ -173,7 +174,7 @@ def create!(method_name, *parameters)
173 174
         # which include the content-type in their file name (i.e.,
174 175
         # "the_template_file.text.html.rhtml", etc.).
175 176
         if @parts.empty?
176  
-          templates = Dir.glob("#{template_path}/#{method_name}.*")
  177
+          templates = Dir.glob("#{template_path}/#{@template}.*")
177 178
           templates.each do |path|
178 179
             type = (File.basename(path).split(".")[1..-2] || []).join("/")
179 180
             next if type.empty?
@@ -188,8 +189,8 @@ def create!(method_name, *parameters)
188 189
         # normal template exists (or if there were no implicit parts) we render
189 190
         # it.
190 191
         template_exists = @parts.empty?
191  
-        template_exists ||= Dir.glob("#{template_path}/#{method_name}.*").any? { |i| i.split(".").length == 2 }
192  
-        @body = render_message(method_name, @body) if template_exists
  192
+        template_exists ||= Dir.glob("#{template_path}/#{@template}.*").any? { |i| i.split(".").length == 2 }
  193
+        @body = render_message(@template, @body) if template_exists
193 194
 
194 195
         # Finally, if there are other message parts and a textual body exists,
195 196
         # we shift it onto the front of the parts and set the body to nil (so
24  actionmailer/test/mail_service_test.rb
@@ -127,6 +127,16 @@ def html_mail(recipient)
127 127
     content_type "text/html"
128 128
   end
129 129
 
  130
+  def custom_template(recipient)
  131
+    recipients recipient
  132
+    subject    "[Signed up] Welcome #{recipient}"
  133
+    from       "system@loudthinking.com"
  134
+    sent_on    Time.local(2004, 12, 12)
  135
+    template   "signed_up"
  136
+
  137
+    body["recipient"] = recipient
  138
+  end
  139
+
130 140
   class <<self
131 141
     attr_accessor :received_body
132 142
   end
@@ -179,6 +189,20 @@ def test_signed_up
179 189
     assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
180 190
   end
181 191
   
  192
+  def test_custom_template
  193
+    expected = new_mail
  194
+    expected.to      = @recipient
  195
+    expected.subject = "[Signed up] Welcome #{@recipient}"
  196
+    expected.body    = "Hello there, \n\nMr. #{@recipient}"
  197
+    expected.from    = "system@loudthinking.com"
  198
+    expected.date    = Time.local(2004, 12, 12)
  199
+
  200
+    created = nil
  201
+    assert_nothing_raised { created = TestMailer.create_custom_template(@recipient) }
  202
+    assert_not_nil created
  203
+    assert_equal expected.encoded, created.encoded
  204
+  end
  205
+  
182 206
   def test_cancelled_account
183 207
     expected = new_mail
184 208
     expected.to      = @recipient

0 notes on commit 813a8b9

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