跳至内容 跳至搜索

Action Mailer MailHelper

提供用于 ActionMailer::Base 的辅助方法,可用于轻松格式化消息、访问邮件程序或消息实例以及附件列表。

方法
A
B
F
M

实例公共方法

attachments()

访问消息附件列表。

# File actionmailer/lib/action_mailer/mail_helper.rb, line 53
def attachments
  mailer.attachments
end

block_format(text)

获取文本并对其进行格式化,每行缩进两个空格,并在 72 列处换行

text = <<-TEXT
  This is
  the      paragraph.

  * item1 * item2
TEXT

block_format text
# => "  This is the paragraph.\n\n  * item1\n  * item2\n"
# File actionmailer/lib/action_mailer/mail_helper.rb, line 22
def block_format(text)
  formatted = text.split(/\n\r?\n/).collect { |paragraph|
    format_paragraph(paragraph)
  }.join("\n\n")

  # Make list points stand on their own line
  output = +""
  splits = formatted.split(/(\*+|\#+)/)
  while line = splits.shift
    if line.start_with?("*", "#") && splits.first&.start_with?(" ")
      output.chomp!(" ") while output.end_with?(" ")
      output << "  #{line} #{splits.shift.strip}\n"
    else
      output << line
    end
  end

  output
end

format_paragraph(text, len = 72, indent = 2)

返回在 len 列处换行并在 indent 个空格处缩进的 text。默认情况下,列长度 len 等于 72 个字符,缩进 indent 等于两个空格。

my_text = 'Here is a sample text with more than 40 characters'

format_paragraph(my_text, 25, 4)
# => "    Here is a sample text with\n    more than 40 characters"
# File actionmailer/lib/action_mailer/mail_helper.rb, line 65
def format_paragraph(text, len = 72, indent = 2)
  sentences = [[]]

  text.split.each do |word|
    if sentences.first.present? && (sentences.last + [word]).join(" ").length > len
      sentences << [word]
    else
      sentences.last << word
    end
  end

  indentation = " " * indent
  sentences.map! { |sentence|
    "#{indentation}#{sentence.join(' ')}"
  }.join "\n"
end

mailer()

访问邮件程序实例。

# File actionmailer/lib/action_mailer/mail_helper.rb, line 43
def mailer
  @_controller
end

message()

访问消息实例。

# File actionmailer/lib/action_mailer/mail_helper.rb, line 48
def message
  @_message
end