Skip to content

Commit f4a9b40

Browse files
sampatbadhejonathanhefner
authored andcommitted
ActionMailer Email Preview - show date header when present or fallback to Time.current.rfc2822
Resolves rails#49788
1 parent 404ce7e commit f4a9b40

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

railties/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* In Action Mailer previews, show date from message `Date` header if present.
2+
3+
*Sampat Badhe*
4+
15
* Exit with non-zero status when the migration generator fails.
26

37
*Katsuhiko YOSHIDA*

railties/lib/rails/templates/rails/mailers/email.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
<% end %>
9494

9595
<dt>Date:</dt>
96-
<dd id="date"><%= Time.current.rfc2822 %></dd>
96+
<dd id="date"><%= @email.header['date'] || Time.current.rfc2822 %></dd>
9797

9898
<dt>Subject:</dt>
9999
<dd><strong id="subject"><%= @email.subject %></strong></dd>

railties/test/application/mailer_previews_test.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,66 @@ def foo
989989
assert_match "<dd id=\"bcc\">[email protected]</dd>", last_response.body
990990
end
991991

992+
test "mailer preview date tag renders date from message header" do
993+
mailer "notifier", <<-RUBY
994+
class Notifier < ActionMailer::Base
995+
default from: "[email protected]"
996+
997+
def foo
998+
mail to: "[email protected]", date: Time.utc(2023, 10, 20, 10, 20, 30)
999+
end
1000+
end
1001+
RUBY
1002+
1003+
text_template "notifier/foo", <<-RUBY
1004+
Hello, World!
1005+
RUBY
1006+
1007+
mailer_preview "notifier", <<-RUBY
1008+
class NotifierPreview < ActionMailer::Preview
1009+
def foo
1010+
Notifier.foo
1011+
end
1012+
end
1013+
RUBY
1014+
1015+
app("development")
1016+
1017+
get "/rails/mailers/notifier/foo"
1018+
assert_match "<dd id=\"date\">Fri, 20 Oct 2023 10:20:30 +0000</dd>", last_response.body
1019+
end
1020+
1021+
test "mailer preview date tag falls back to current time when date header is not present" do
1022+
mailer "notifier", <<-RUBY
1023+
class Notifier < ActionMailer::Base
1024+
default from: "[email protected]"
1025+
1026+
def foo
1027+
mail to: "[email protected]"
1028+
end
1029+
end
1030+
RUBY
1031+
1032+
text_template "notifier/foo", <<-RUBY
1033+
Hello, World!
1034+
RUBY
1035+
1036+
mailer_preview "notifier", <<-RUBY
1037+
class NotifierPreview < ActionMailer::Preview
1038+
def foo
1039+
Notifier.foo
1040+
end
1041+
end
1042+
RUBY
1043+
1044+
app("development")
1045+
1046+
travel_to(Time.utc(2023, 10, 20, 10, 20, 30)) do
1047+
get "/rails/mailers/notifier/foo"
1048+
end
1049+
assert_match "<dd id=\"date\">Fri, 20 Oct 2023 10:20:30 +0000</dd>", last_response.body
1050+
end
1051+
9921052
test "mailer preview has access to rendering context" do
9931053
mailer "notifier", <<-RUBY
9941054
class Notifier < ActionMailer::Base

0 commit comments

Comments
 (0)