1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

ActionMailer: support overriding template name in multipart

Implicit rendering in multipart blocks now also uses the template
name from the options hash instead of always using the action name.

So you can now write

    mail(template_name: template_name) do |format|
      format.text
      format.html
    end
This commit is contained in:
Marcus Ilgner 2015-12-08 12:28:00 +01:00
parent a61e4ae58d
commit 5c54db290f
No known key found for this signature in database
GPG key ID: E31D682E2F71FB99
4 changed files with 26 additions and 3 deletions

View file

@ -1,3 +1,8 @@
* Mails with multipart `format` blocks with implicit render now also check for
a template name in options hash instead of only using the action name.
*Marcus Ilgner*
* `config.force_ssl = true` will set * `config.force_ssl = true` will set
`config.action_mailer.default_url_options = { protocol: 'https' }` `config.action_mailer.default_url_options = { protocol: 'https' }`

View file

@ -893,9 +893,7 @@ module ActionMailer
def collect_responses(headers) def collect_responses(headers)
if block_given? if block_given?
collector = ActionMailer::Collector.new(lookup_context) { render(action_name) } collect_responses_from_block(headers, &Proc.new)
yield(collector)
collector.responses
elsif headers[:body] elsif headers[:body]
[{ [{
body: headers.delete(:body), body: headers.delete(:body),
@ -906,6 +904,13 @@ module ActionMailer
end end
end end
def collect_responses_from_block(headers)
templates_name = headers[:template_name] || action_name
collector = ActionMailer::Collector.new(lookup_context) { render(templates_name) }
yield(collector)
collector.responses
end
def collect_responses_from_templates(headers) def collect_responses_from_templates(headers)
templates_path = headers[:template_path] || self.class.mailer_name templates_path = headers[:template_path] || self.class.mailer_name
templates_name = headers[:template_name] || action_name templates_name = headers[:template_name] || action_name

View file

@ -539,6 +539,12 @@ class BaseTest < ActiveSupport::TestCase
assert_equal("TEXT Implicit Multipart", mail.text_part.body.decoded) assert_equal("TEXT Implicit Multipart", mail.text_part.body.decoded)
end end
test "you can specify a different template for multipart render" do
mail = BaseMailer.implicit_different_template_with_block('explicit_multipart_templates').deliver
assert_equal("HTML Explicit Multipart Templates", mail.html_part.body.decoded)
assert_equal("TEXT Explicit Multipart Templates", mail.text_part.body.decoded)
end
test "should raise if missing template in implicit render" do test "should raise if missing template in implicit render" do
assert_raises ActionView::MissingTemplate do assert_raises ActionView::MissingTemplate do
BaseMailer.implicit_different_template('missing_template').deliver_now BaseMailer.implicit_different_template('missing_template').deliver_now

View file

@ -104,6 +104,13 @@ class BaseMailer < ActionMailer::Base
mail(template_name: template_name) mail(template_name: template_name)
end end
def implicit_different_template_with_block(template_name='')
mail(template_name: template_name) do |format|
format.text
format.html
end
end
def explicit_different_template(template_name='') def explicit_different_template(template_name='')
mail do |format| mail do |format|
format.text { render template: "#{mailer_name}/#{template_name}" } format.text { render template: "#{mailer_name}/#{template_name}" }