2017-07-23 11:17:16 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 13:03:39 -04:00
|
|
|
require "abstract_unit"
|
2010-01-24 13:52:44 -05:00
|
|
|
|
|
|
|
class HelperMailer < ActionMailer::Base
|
|
|
|
def use_mail_helper
|
2017-01-12 03:39:16 -05:00
|
|
|
@text = "But soft! What light through yonder window breaks? It is the east, " \
|
|
|
|
"and Juliet is the sun. Arise, fair sun, and kill the envious moon, " \
|
|
|
|
"which is sick and pale with grief that thou, her maid, art far more " \
|
|
|
|
"fair than she. Be not her maid, for she is envious! Her vestal " \
|
|
|
|
"livery is but sick and green, and none but fools do wear it. Cast " \
|
2010-01-24 13:52:44 -05:00
|
|
|
"it off!"
|
|
|
|
|
|
|
|
mail_with_defaults do |format|
|
2012-10-07 13:54:14 -04:00
|
|
|
format.html { render(inline: "<%= block_format @text %>") }
|
2010-01-24 13:52:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-03-09 14:06:48 -05:00
|
|
|
def use_format_paragraph
|
|
|
|
@text = "But soft! What light through yonder window breaks?"
|
|
|
|
|
|
|
|
mail_with_defaults do |format|
|
2012-10-07 13:54:14 -04:00
|
|
|
format.html { render(inline: "<%= format_paragraph @text, 15, 1 %>") }
|
2011-03-09 14:06:48 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-15 01:04:27 -05:00
|
|
|
def use_format_paragraph_with_long_first_word
|
|
|
|
@text = "Antidisestablishmentarianism is very long."
|
|
|
|
|
|
|
|
mail_with_defaults do |format|
|
2012-10-07 13:54:14 -04:00
|
|
|
format.html { render(inline: "<%= format_paragraph @text, 10, 1 %>") }
|
2012-02-15 01:04:27 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-24 13:52:44 -05:00
|
|
|
def use_mailer
|
|
|
|
mail_with_defaults do |format|
|
2012-10-07 13:54:14 -04:00
|
|
|
format.html { render(inline: "<%= mailer.message.subject %>") }
|
2010-01-24 13:52:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def use_message
|
|
|
|
mail_with_defaults do |format|
|
2012-10-07 13:54:14 -04:00
|
|
|
format.html { render(inline: "<%= message.subject %>") }
|
2010-01-24 13:52:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-15 01:31:23 -05:00
|
|
|
def use_block_format
|
|
|
|
@text = <<-TEXT
|
|
|
|
This is the
|
|
|
|
first paragraph.
|
|
|
|
|
|
|
|
The second
|
|
|
|
paragraph.
|
|
|
|
|
|
|
|
* item1 * item2
|
|
|
|
* item3
|
|
|
|
TEXT
|
|
|
|
|
|
|
|
mail_with_defaults do |format|
|
2012-10-07 13:54:14 -04:00
|
|
|
format.html { render(inline: "<%= block_format @text %>") }
|
2012-02-15 01:31:23 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-28 11:46:17 -04:00
|
|
|
def use_cache
|
|
|
|
mail_with_defaults do |format|
|
|
|
|
format.html { render(inline: "<% cache(:foo) do %>Greetings from a cache helper block<% end %>") }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-23 09:41:41 -05:00
|
|
|
private
|
2010-01-24 13:52:44 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def mail_with_defaults(&block)
|
|
|
|
mail(to: "test@localhost", from: "tester@example.com",
|
|
|
|
subject: "using helpers", &block)
|
|
|
|
end
|
2010-01-24 13:52:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class MailerHelperTest < ActionMailer::TestCase
|
|
|
|
def test_use_mail_helper
|
|
|
|
mail = HelperMailer.use_mail_helper
|
|
|
|
assert_match %r{ But soft!}, mail.body.encoded
|
|
|
|
assert_match %r{east, and\r\n Juliet}, mail.body.encoded
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_use_mailer
|
|
|
|
mail = HelperMailer.use_mailer
|
|
|
|
assert_match "using helpers", mail.body.encoded
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_use_message
|
|
|
|
mail = HelperMailer.use_message
|
|
|
|
assert_match "using helpers", mail.body.encoded
|
|
|
|
end
|
2011-03-09 14:06:48 -05:00
|
|
|
|
|
|
|
def test_use_format_paragraph
|
|
|
|
mail = HelperMailer.use_format_paragraph
|
|
|
|
assert_match " But soft! What\r\n light through\r\n yonder window\r\n breaks?", mail.body.encoded
|
|
|
|
end
|
2012-02-15 01:31:23 -05:00
|
|
|
|
2012-02-15 01:04:27 -05:00
|
|
|
def test_use_format_paragraph_with_long_first_word
|
|
|
|
mail = HelperMailer.use_format_paragraph_with_long_first_word
|
|
|
|
assert_equal " Antidisestablishmentarianism\r\n is very\r\n long.", mail.body.encoded
|
|
|
|
end
|
|
|
|
|
2012-02-15 01:31:23 -05:00
|
|
|
def test_use_block_format
|
|
|
|
mail = HelperMailer.use_block_format
|
|
|
|
expected = <<-TEXT
|
|
|
|
This is the first paragraph.
|
|
|
|
|
|
|
|
The second paragraph.
|
|
|
|
|
|
|
|
* item1
|
|
|
|
* item2
|
|
|
|
* item3
|
|
|
|
TEXT
|
|
|
|
assert_equal expected.gsub("\n", "\r\n"), mail.body.encoded
|
|
|
|
end
|
2010-01-24 13:52:44 -05:00
|
|
|
|
2015-04-28 11:46:17 -04:00
|
|
|
def test_use_cache
|
|
|
|
assert_nothing_raised do
|
|
|
|
mail = HelperMailer.use_cache
|
|
|
|
assert_equal "Greetings from a cache helper block", mail.body.encoded
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|