Module: Card::Set::Type::EmailTemplate::EmailConfig

Extended by:
Card::Set
Defined in:
tmpsets/set/mod016-email/type/email_template/email_config.rb

Overview

Set: All "EmailTemplate+EmailConfig" cards (EmailConfig)

Constant Summary collapse

EMAIL_FIELDS =
%i[to from cc bcc attach subject text_message html_message].freeze
EMAIL_FIELD_METHODS =
{ subject: :contextual_content,
text_message: :contextual_content,
attach: :extended_item_contents }.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from I18nScope

#mod_name, #scope

Methods included from Loader

#clean_empty_module_from_hash, #clean_empty_modules, #extended, #process_base_modules, #register_set

Methods included from Helpers

#abstract_set?, #all_set?, #num_set_parts, #shortname, #underscore

Methods included from AdvancedApi

#attachment, #ensure_set, #stage_method

Methods included from Format

#before, #format, layout_method_name, #view, view_method_name, view_setting_method_name, wrapper_method_name

Methods included from Inheritance

#include_set, #include_set_formats

Methods included from Basket

#abstract_basket, #add_to_basket, #basket, #unshift_basket

Methods included from Card::Set::Trait

#card_accessor, #card_reader, #card_writer, #require_field

Methods included from Event::Api

#event

Class Method Details

.source_locationObject



7
# File 'tmpsets/set/mod016-email/type/email_template/email_config.rb', line 7

def self.source_location; "/Users/ethan/dev/decko/gem/card/mod/email/set/type/email_template/email_config.rb"; end

Instance Method Details

#configured_from_name_and_email(raw_string) ⇒ Object



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

def configured_from_name_and_email raw_string
  if raw_string =~ /(.*)\<(.*)>/
    [Regexp.last_match(1).strip, Regexp.last_match(2)]
  else
    [nil, raw_string]
  end
end

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

Parameters:

  • context (Card)

    the card in whose context all email fields will be interpreted

  • fields (Hash) (defaults to: {})

    override any templated field configurations with hash values

  • opts (Hash) (defaults to: {})

    options for rendering. unknown options become format options

Options Hash (opts):

  • :auth (Card, String, Integer)

    user identifier. render as this user



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

def email_config context, fields={}, opts={}
  @active_email_context = context || self
  auth = opts.delete :auth
  config = EMAIL_FIELDS.each_with_object({}) do |field, conf|
    conf[field] = fields[field] || email_field_from_card(field, auth, opts)
  end
  safe_from_and_reply_to! config
  config.select { |_k, v| v.present? }
end

#email_field_from_card(field, auth, format_opts) ⇒ Object



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

def email_field_from_card field, auth, format_opts
  return unless (field_card = fetch(trait: field))
  auth ||= field_card.updater
  special_email_field_method(field, field_card, auth, format_opts) ||
    standard_email_field(field, field_card, auth, format_opts)
end

#email_from_field_value(conf_name, conf_email, actual_email) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'tmpsets/set/mod016-email/type/email_template/email_config.rb', line 71

def email_from_field_value conf_name, conf_email, actual_email
  conf_text = conf_name || conf_email
  if conf_text != actual_email
    %("#{conf_text}" <#{actual_email}>)
  elsif actual_email.present?
    actual_email
  else
    Card[Card::WagnBotID]..email
  end
end

#email_html_message_field(message_card, auth, format_opts) ⇒ Object

html messages return procs because image attachments can't be properly rendered without a mail object. (which isn't available at initial config time)



53
54
55
56
57
58
59
60
# File 'tmpsets/set/mod016-email/type/email_template/email_config.rb', line 53

def email_html_message_field message_card, auth, format_opts
  proc do |mail|
    Auth.as auth do
      format_opts = format_opts.merge format: :email_html, active_mail: mail
      message_card.format(format_opts).email_content @active_email_context
    end
  end
end

#safe_from_and_reply_to!(config) ⇒ Object

whenever a default "from" field is configured in Card::Mailer, emails are always actually "from" that address



64
65
66
67
68
69
# File 'tmpsets/set/mod016-email/type/email_template/email_config.rb', line 64

def safe_from_and_reply_to! config
  conf_name, conf_email = configured_from_name_and_email config[:from]
  actual_email = Card::Mailer.default[:from] || conf_email
  config[:from] = email_from_field_value conf_name, conf_email, actual_email
  config[:reply_to] ||= actual_email
end

#special_email_field_method(field, field_card, auth, format_opts) ⇒ Object



37
38
39
40
41
# File 'tmpsets/set/mod016-email/type/email_template/email_config.rb', line 37

def special_email_field_method field, field_card, auth, format_opts
  method = "email_#{field}_field"
  return unless respond_to? method
  send method, field_card, auth, format_opts
end

#standard_email_field(field, field_card, auth, format_opts) ⇒ Object



43
44
45
46
47
48
49
# File 'tmpsets/set/mod016-email/type/email_template/email_config.rb', line 43

def standard_email_field field, field_card, auth, format_opts
  method = EMAIL_FIELD_METHODS[field] || :email_addresses
  format_opts = format_opts.merge format: :email_text
  Auth.as auth do
    field_card.format(format_opts).send method, @active_email_context
  end
end