2017-07-23 11:17:16 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2009-12-16 20:23:08 -05:00
|
|
|
module ActionMailer
|
2013-05-15 00:36:07 -04:00
|
|
|
# Provides helper methods for ActionMailer::Base that can be used for easily
|
|
|
|
# formatting messages, accessing mailer or message instances, and the
|
|
|
|
# attachments list.
|
2009-12-16 20:23:08 -05:00
|
|
|
module MailHelper
|
2012-09-17 20:18:56 -04:00
|
|
|
# Take the text and format it, indented two spaces for each line, and
|
2015-02-28 15:24:59 -05:00
|
|
|
# wrapped at 72 columns:
|
|
|
|
#
|
|
|
|
# text = <<-TEXT
|
|
|
|
# This is
|
|
|
|
# the paragraph.
|
|
|
|
#
|
|
|
|
# * item1 * item2
|
|
|
|
# TEXT
|
|
|
|
#
|
|
|
|
# block_format text
|
|
|
|
# # => " This is the paragraph.\n\n * item1\n * item2\n"
|
2009-12-16 20:23:08 -05:00
|
|
|
def block_format(text)
|
2012-02-15 01:31:23 -05:00
|
|
|
formatted = text.split(/\n\r?\n/).collect { |paragraph|
|
2011-03-08 15:40:03 -05:00
|
|
|
format_paragraph(paragraph)
|
2012-02-15 01:31:23 -05:00
|
|
|
}.join("\n\n")
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2009-12-16 20:23:08 -05:00
|
|
|
# Make list points stand on their own line
|
2014-06-19 10:40:37 -04:00
|
|
|
formatted.gsub!(/[ ]*([*]+) ([^*]*)/) { " #{$1} #{$2.strip}\n" }
|
|
|
|
formatted.gsub!(/[ ]*([#]+) ([^#]*)/) { " #{$1} #{$2.strip}\n" }
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2009-12-16 20:23:08 -05:00
|
|
|
formatted
|
|
|
|
end
|
2009-12-25 15:35:40 -05:00
|
|
|
|
|
|
|
# Access the mailer instance.
|
2010-01-24 13:52:44 -05:00
|
|
|
def mailer
|
2010-01-23 05:37:34 -05:00
|
|
|
@_controller
|
2009-12-25 15:35:40 -05:00
|
|
|
end
|
2010-01-22 05:01:21 -05:00
|
|
|
|
|
|
|
# Access the message instance.
|
2010-01-24 13:52:44 -05:00
|
|
|
def message
|
2010-01-24 13:36:42 -05:00
|
|
|
@_message
|
2010-01-22 05:01:21 -05:00
|
|
|
end
|
2010-06-07 21:54:53 -04:00
|
|
|
|
|
|
|
# Access the message attachments list.
|
|
|
|
def attachments
|
2014-09-19 04:32:08 -04:00
|
|
|
mailer.attachments
|
2010-06-07 21:54:53 -04:00
|
|
|
end
|
2011-02-09 13:11:29 -05:00
|
|
|
|
2011-03-09 14:06:48 -05:00
|
|
|
# Returns +text+ wrapped at +len+ columns and indented +indent+ spaces.
|
2015-02-15 12:24:51 -05:00
|
|
|
# By default column length +len+ equals 72 characters and indent
|
|
|
|
# +indent+ equal two spaces.
|
2011-03-09 14:06:48 -05:00
|
|
|
#
|
2012-09-17 20:18:56 -04:00
|
|
|
# my_text = 'Here is a sample text with more than 40 characters'
|
2011-03-09 14:06:48 -05:00
|
|
|
#
|
2011-03-09 17:17:30 -05:00
|
|
|
# format_paragraph(my_text, 25, 4)
|
2011-03-09 14:06:48 -05:00
|
|
|
# # => " Here is a sample text with\n more than 40 characters"
|
2011-03-08 15:40:03 -05:00
|
|
|
def format_paragraph(text, len = 72, indent = 2)
|
2011-02-09 13:11:29 -05:00
|
|
|
sentences = [[]]
|
|
|
|
|
|
|
|
text.split.each do |word|
|
2016-08-06 13:01:31 -04:00
|
|
|
if sentences.first.present? && (sentences.last + [word]).join(" ").length > len
|
2011-02-09 13:11:29 -05:00
|
|
|
sentences << [word]
|
|
|
|
else
|
|
|
|
sentences.last << word
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-12 23:50:19 -04:00
|
|
|
indentation = " " * indent
|
2013-08-02 08:34:34 -04:00
|
|
|
sentences.map! { |sentence|
|
2013-05-12 23:50:19 -04:00
|
|
|
"#{indentation}#{sentence.join(' ')}"
|
2011-02-09 13:11:29 -05:00
|
|
|
}.join "\n"
|
|
|
|
end
|
2009-12-16 20:23:08 -05:00
|
|
|
end
|
|
|
|
end
|