Module: EffectiveResourcesEmailHelper

Defined in:
app/helpers/effective_resources_email_helper.rb

Instance Method Summary collapse

Instance Method Details

#email_form_fields(form, action = nil, skip: true, to: nil, variables: nil, partial: nil) ⇒ Object

acts_as_email_form



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/helpers/effective_resources_email_helper.rb', line 14

def email_form_fields(form, action = nil, skip: true, to: nil, variables: nil, partial: nil)
  raise('expected a form') unless form.respond_to?(:object)

  resource = form.object

  # Intended for acts_as_email_form but sometimes we use a duck typed object to save these fields as well
  unless resource.class.respond_to?(:acts_as_email_form?) || resource.respond_to?("email_form_action=")
    raise('expected an acts_as_email_form resource or one that responds to email_form_action') 
  end

  # Load the template.
  email_template = if action.present? && defined?(EffectiveEmailTemplates)
    action.kind_of?(Effective::EmailTemplate) ? action : Effective::EmailTemplate.where(template_name: action).first!
  end

  # These defaults are only used when there is no email_template
  email_defaults = form.object.email_form_defaults(action) unless email_template.present?

  from = email_template&.from || email_defaults[:from] || EffectiveResources.mailer_froms.first
  subject = email_template&.subject || email_defaults[:subject] || ''
  body = email_template&.body || email_defaults[:body] || ''
  content_type = email_template&.content_type || email_defaults[:content_type] || ''

  locals = {
    form: form,
    email_to: to,
    email_from: from,
    email_subject: subject,
    email_body: body,
    email_content_type: content_type,
    email_skip: skip,
    email_action: (action || true),
    email_template: email_template,
    email_variables: variables
  }

  render(partial: (partial || 'effective/acts_as_email_form/fields'), locals: locals)
end

#email_message_body(message) ⇒ Object



94
95
96
97
98
99
100
# File 'app/helpers/effective_resources_email_helper.rb', line 94

def email_message_body(message)
  html_body = message.parts.find { |part| part.content_type.start_with?('text/html') }.try(:body).to_s
  plain_body = message.parts.find { |part| part.content_type.start_with?('text/plain') }.try(:body).to_s
  message_body = message.body.to_s

  html_body.presence || plain_body.presence || message_body.presence
end

#email_message_html?(message) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'app/helpers/effective_resources_email_helper.rb', line 86

def email_message_html?(message)
  message.parts.find { |part| part.content_type.start_with?('text/html') }.present?
end

#email_message_plain?(message) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'app/helpers/effective_resources_email_helper.rb', line 90

def email_message_plain?(message)
  message.parts.find { |part| part.content_type.start_with?('text/html') }.blank?
end

#email_notification_fields(form, action, partial: nil, variables: nil) ⇒ Object

acts_as_email_notification



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/helpers/effective_resources_email_helper.rb', line 54

def email_notification_fields(form, action, partial: nil, variables: nil)
  raise('expected a form') unless form.respond_to?(:object)

  resource = form.object

  # Intended for acts_as_email_notification
  raise('expected an acts_as_email_notification resource') unless resource.class.respond_to?(:acts_as_email_notification?)

  # Load the template.
  email_template = if action.present? && defined?(EffectiveEmailTemplates)
    action.kind_of?(Effective::EmailTemplate) ? action : Effective::EmailTemplate.where(template_name: action).first!
  end

  raise('expected an Effective::EmailTemplate') unless email_template.kind_of?(Effective::EmailTemplate)

  locals = {
    form: form,

    email_from: email_template.try(:from),
    email_subject: email_template.try(:subject),
    email_body: email_template.try(:body),
    email_cc: email_template.try(:cc),
    email_bcc: email_template.try(:bcc),
    email_content_type: email_template.try(:content_type),

    email_template: email_template,
    email_variables: variables
  }

  render(partial: (partial || 'effective/acts_as_email_notification/fields'), locals: locals)
end

#mailer_froms_collection(froms: nil) ⇒ Object



4
5
6
7
8
9
10
11
# File 'app/helpers/effective_resources_email_helper.rb', line 4

def mailer_froms_collection(froms: nil)
  froms ||= EffectiveResources.mailer_froms

  froms.map do |from|
    html = (:span, escape_once(from))
    [from, from, 'data-html': html]
  end
end