Module: Card::Set::Type::EmailTemplate::Format

Extended by:
AbstractFormat
Defined in:
tmpsets/set/mod016-email/type/email_template.rb

Instance Method Summary collapse

Instance Method Details

#add_attachments(mail, list) ⇒ Object



80
81
82
83
84
85
86
# File 'tmpsets/set/mod016-email/type/email_template.rb', line 80

def add_attachments mail, list
  return unless list.present?
  each_valid_attachment list do |file, index|
    mail.add_file filename: attachment_name(file, index),
                  content: File.read(file.path)
  end
end

#attachment_name(file, number) ⇒ Object



95
96
97
# File 'tmpsets/set/mod016-email/type/email_template.rb', line 95

def attachment_name file, number
  "attachment-#{number + 1}.#{file.extension}"
end

#body_method_and_args(html, text) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'tmpsets/set/mod016-email/type/email_template.rb', line 38

def body_method_and_args html, text
  if html && text
    [:text_and_html_message, %i[text_message html_message attach]]
  elsif html
    %i[html_body html_message]
  else
    %i[text_body text_message]
  end
end

#each_valid_attachment(list) ⇒ Object



88
89
90
91
92
93
# File 'tmpsets/set/mod016-email/type/email_template.rb', line 88

def each_valid_attachment list
  list.each_with_index do |cardname, index|
    next unless (file = Card[cardname]&.try(:attachment))
    yield file, index
  end
end

#html_body(mail, message) ⇒ Object



70
71
72
73
# File 'tmpsets/set/mod016-email/type/email_template.rb', line 70

def html_body mail, message
  mail.content_type "text/html; charset=UTF-8"
  mail.body message
end

#mail(context = nil, fields = {}, opts = {}) ⇒ Object



21
22
23
24
25
26
27
28
# File 'tmpsets/set/mod016-email/type/email_template.rb', line 21

def mail context=nil, fields={}, opts={}
  config = card.email_config context, fields, opts
  fmt = self # self is <Mail::Message> within the new_mail block
  Card::Mailer.new_mail config do
    fmt.message_body self, config
    fmt.add_attachments self, config.delete(:attach)
  end
end

#message_body(mail, config) ⇒ Object



30
31
32
33
34
35
36
# File 'tmpsets/set/mod016-email/type/email_template.rb', line 30

def message_body mail, config
  config[:html_message] &&= config[:html_message].call mail
  method, args = body_method_and_args config[:html_message].present?,
                                      config[:text_message].present?
  args = Array.wrap(args).map { |arg| config[arg] }
  send method, mail, *args
end

#multipart_mixed(mail, text_message, html_message) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'tmpsets/set/mod016-email/type/email_template.rb', line 58

def multipart_mixed mail, text_message, html_message
  mail.content_type "multipart/mixed"
  mail.part content_type: "multipart/alternative" do |copy|
    copy.part content_type: "text/plain" do |plain|
      plain.body = text_message
    end
    copy.part content_type: "text/html" do |html|
      html.body = html_message
    end
  end
end

#text_and_html_message(mail, text_message, html_message, attachment_list = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'tmpsets/set/mod016-email/type/email_template.rb', line 48

def text_and_html_message mail, text_message, html_message, attachment_list=nil
  fmt = self
  if attachment_list&.any?
    mail.multipart_mixed text_message, html_message
  else
    mail.text_part { body text_message }
    mail.html_part { fmt.html_body self, html_message }
  end
end

#text_body(mail, message) ⇒ Object



75
76
77
78
# File 'tmpsets/set/mod016-email/type/email_template.rb', line 75

def text_body mail, message
  mail.content_type "text/plain; charset=UTF-8"
  mail.text_part { body message }
end