diff --git a/lib/courrier/email/layouts.rb b/lib/courrier/email/layouts.rb index 51b5752..9c2cb42 100644 --- a/lib/courrier/email/layouts.rb +++ b/lib/courrier/email/layouts.rb @@ -20,13 +20,13 @@ def build def no_layouts? = @email.class.layouts.nil? def layouts - FORMATS.map(&:to_sym).to_h do |format| + FORMATS.map(&:to_sym).filter_map do |format| template = @email.class.layouts[format] next if template.nil? [format, render(template)] - end + end.to_h end def render(template) diff --git a/test/courrier/email/layouts_test.rb b/test/courrier/email/layouts_test.rb index 6074745..f3553ba 100644 --- a/test/courrier/email/layouts_test.rb +++ b/test/courrier/email/layouts_test.rb @@ -31,4 +31,24 @@ def test_mixed_layouts assert_equal expected, Courrier::Email::Layouts.new(email).build end + + def test_html_only_layout + email = TestEmailWithHtmlOnlyLayout.new( + from: "devs@railsdesigner.com", + to: "recipient@railsdesigner.com" + ) + + assert_equal [{ html: "%{content}" }], Courrier::Email::Layouts.new(email).build + assert_equal "

Body

", email.options.html + end + + def test_text_only_layout + email = TestEmailWithTextOnlyLayout.new( + from: "devs@railsdesigner.com", + to: "recipient@railsdesigner.com" + ) + + assert_equal [{ text: "%{content}\n\nThanks!" }], Courrier::Email::Layouts.new(email).build + assert_equal "Body\n\nThanks!", email.options.text + end end diff --git a/test/fixtures/test_email_with_single_format_layouts.rb b/test/fixtures/test_email_with_single_format_layouts.rb new file mode 100644 index 0000000..4438d83 --- /dev/null +++ b/test/fixtures/test_email_with_single_format_layouts.rb @@ -0,0 +1,13 @@ +require "courrier/email" + +class TestEmailWithHtmlOnlyLayout < Courrier::Email + layout html: "%{content}" + + def html = "

Body

" +end + +class TestEmailWithTextOnlyLayout < Courrier::Email + layout text: "%{content}\n\nThanks!" + + def text = "Body" +end