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

Extended by:
AbstractFormat
Defined in:
platypus/tmp/set/gem-defaults/mod012-email/type/email_template.rb

Instance Method Summary collapse

Instance Method Details

#add_attachments(mail, list) ⇒ Object



79
80
81
82
83
84
85
86
# File 'platypus/tmp/set/gem-defaults/mod012-email/type/email_template.rb', line 79

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



96
97
98
# File 'platypus/tmp/set/gem-defaults/mod012-email/type/email_template.rb', line 96

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

#body_method_and_args(html, text) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'platypus/tmp/set/gem-defaults/mod012-email/type/email_template.rb', line 37

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
94
# File 'platypus/tmp/set/gem-defaults/mod012-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



69
70
71
72
# File 'platypus/tmp/set/gem-defaults/mod012-email/type/email_template.rb', line 69

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

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



20
21
22
23
24
25
26
27
# File 'platypus/tmp/set/gem-defaults/mod012-email/type/email_template.rb', line 20

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



29
30
31
32
33
34
35
# File 'platypus/tmp/set/gem-defaults/mod012-email/type/email_template.rb', line 29

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



57
58
59
60
61
62
63
64
65
66
67
# File 'platypus/tmp/set/gem-defaults/mod012-email/type/email_template.rb', line 57

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



47
48
49
50
51
52
53
54
55
# File 'platypus/tmp/set/gem-defaults/mod012-email/type/email_template.rb', line 47

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



74
75
76
77
# File 'platypus/tmp/set/gem-defaults/mod012-email/type/email_template.rb', line 74

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