From 4e6c8045c3c5f7b914040c3bbb2cbbd901f45854 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Fri, 1 Mar 2019 14:07:59 -0800 Subject: Add test and change how format set in ActionMailer Previously this used self.formats= to set the format which render would use to find templates. This worked, but was untested, and looked a little confusing because it was doing the mutation within a loop. This commit replaces the assignment with passing formats: [format] into the render call, which makes it more obvious that that's the purpose of the format. It also adds a test to verify the formats being used. --- actionmailer/lib/action_mailer/base.rb | 3 +-- actionmailer/test/base_test.rb | 10 ++++++++++ .../fixtures/base_mailer/implicit_multipart_formats.html.erb | 1 + .../fixtures/base_mailer/implicit_multipart_formats.text.erb | 1 + actionmailer/test/mailers/base_mailer.rb | 4 ++++ 5 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 actionmailer/test/fixtures/base_mailer/implicit_multipart_formats.html.erb create mode 100644 actionmailer/test/fixtures/base_mailer/implicit_multipart_formats.text.erb (limited to 'actionmailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 8595b1e063..5b6e7121b3 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -974,9 +974,8 @@ module ActionMailer each_template(Array(templates_path), templates_name).map do |template| format = template.format || self.formats.first - self.formats = [format] { - body: render(template: template), + body: render(template: template, formats: [format]), content_type: Mime[format].to_s } end diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb index 531bf58ea9..c07fca5b5e 100644 --- a/actionmailer/test/base_test.rb +++ b/actionmailer/test/base_test.rb @@ -307,6 +307,16 @@ class BaseTest < ActiveSupport::TestCase assert_equal("HTML Implicit Multipart", email.parts[1].body.encoded) end + test "implicit multipart formats" do + email = BaseMailer.implicit_multipart_formats + assert_equal(2, email.parts.size) + assert_equal("multipart/alternative", email.mime_type) + assert_equal("text/plain", email.parts[0].mime_type) + assert_equal("Implicit Multipart [:text]", email.parts[0].body.encoded) + assert_equal("text/html", email.parts[1].mime_type) + assert_equal("Implicit Multipart [:html]", email.parts[1].body.encoded) + end + test "implicit multipart with sort order" do order = ["text/html", "text/plain"] with_default BaseMailer, parts_order: order do diff --git a/actionmailer/test/fixtures/base_mailer/implicit_multipart_formats.html.erb b/actionmailer/test/fixtures/base_mailer/implicit_multipart_formats.html.erb new file mode 100644 index 0000000000..0179b070b8 --- /dev/null +++ b/actionmailer/test/fixtures/base_mailer/implicit_multipart_formats.html.erb @@ -0,0 +1 @@ +Implicit Multipart <%= formats.inspect %> \ No newline at end of file diff --git a/actionmailer/test/fixtures/base_mailer/implicit_multipart_formats.text.erb b/actionmailer/test/fixtures/base_mailer/implicit_multipart_formats.text.erb new file mode 100644 index 0000000000..0179b070b8 --- /dev/null +++ b/actionmailer/test/fixtures/base_mailer/implicit_multipart_formats.text.erb @@ -0,0 +1 @@ +Implicit Multipart <%= formats.inspect %> \ No newline at end of file diff --git a/actionmailer/test/mailers/base_mailer.rb b/actionmailer/test/mailers/base_mailer.rb index dbe1c4f0e6..6bd58304e2 100644 --- a/actionmailer/test/mailers/base_mailer.rb +++ b/actionmailer/test/mailers/base_mailer.rb @@ -62,6 +62,10 @@ class BaseMailer < ActionMailer::Base mail(hash) end + def implicit_multipart_formats(hash = {}) + mail(hash) + end + def implicit_with_locale(hash = {}) mail(hash) end -- cgit v1.2.3