Module: CommunicationsHelper

Defined in:
app/helpers/communications_helper.rb

Instance Method Summary collapse

Instance Method Details

#attachment_name(upload) ⇒ Object



8
9
10
# File 'app/helpers/communications_helper.rb', line 8

def attachment_name(upload)
  upload.attachment_name
end

#clean_received_body(body) ⇒ Object

Strips quoted/forwarded content from a plain-text received email body for display.
Handles two common patterns:

  1. "On , wrote:" reply blocks (via EmailReplyParser)
  2. "From: / To: / Date: / Subject:" forwarded message header blocks (via regex)


124
125
126
127
128
# File 'app/helpers/communications_helper.rb', line 124

def clean_received_body(body)
  text = EmailReplyParser.parse_reply(body.to_s)
  # Strip forwarded-message header blocks: "From:\nTo: ..." and everything after
  text.sub(/\n+\s*From:[^\n]*\n[\t ]*(?:To|Sent|Date|Cc|Subject):.*\z/m, '').strip
end

#combo_category_select_options(category = nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/helpers/communications_helper.rb', line 80

def combo_category_select_options(category = nil)
  opts = []
  if category.nil? or category == 'email'
    opts += [
      ['Email TO', 'email_to'],
      ['Email CC', 'email_cc'],
      ['Email BCC', 'email_bcc']
    ]
  end
  opts += [%w[Fax fax]] if category.nil? or category == 'fax'
  opts
end

#communication_actions(c) ⇒ Object



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
# File 'app/helpers/communications_helper.rb', line 54

def communication_actions(c)
  [].tap do |cmds|
    cmds << c.human_state_name.titleize
    cmds << link_to(fa_icon('pen-to-square', text: 'Edit'), edit_communication_path(@communication)) if (c.draft? or c.exception?) and can?(:update, c)
    if can?(:destroy, c) and !c.sent?
      cmds << link_to(fa_icon('trash', text: 'Delete'), communication_path(@communication),
                                                                                            data: { turbo_confirm: 'Delete for sure?', turbo_method: :delete })
    end
    cmds << link_to(fa_icon('repeat', text: 'Resend this email'), resend_email_communication_path(@communication)) if can?(:create, Communication) and !@communication.draft? and @communication.direction_outbound?
    cmds << link_to(fa_icon('copy', text: 'Copy into new blank communication'), copy_communication_path(@communication)) if can?(:create, Communication) and !@communication.draft?
    if @communication.can_queue?
      cmds << link_to(fa_icon('envelope', text: 'Queue for delivery'), communication_path(@communication, communication: { state_event: :queue }),
                      data: { turbo_method: :put })
    end
    cmds << link_to(fa_icon('arrow-rotate-left', text: 'Back to Draft'), communication_path(@communication, communication: { state_event: :unqueue }), data: { turbo_method: :put }) if @communication.can_unqueue?
    cmds << link_to(fa_icon('envelope', text: 'Send Now'), send_now_communication_path(@communication), data: { turbo_method: :put }) if @communication.draft? or @communication.queued?
    if @communication.can_resend?
      cmds << link_to(fa_icon('envelope', text: 'Queue to Resend'), communication_path(@communication, communication: { state_event: :resend }),
                      data: { turbo_method: :put })
    end
    cmds << link_to(fa_icon('reply', text: 'Reply'), reply_communication_path(@communication))
    cmds << link_to(fa_icon('reply-all', text: 'Reply All'), reply_all_communication_path(@communication)) if @communication.communication_recipients.count > 1
    cmds << link_to(fa_icon('bell', text: 'Events'), events_communication_path(@communication))
  end
end

#communication_recipient_delete_button(f) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'app/helpers/communications_helper.rb', line 35

def communication_recipient_delete_button(f)
  if f.object.persisted?
    (:div, class: 'btn btn-outline-warning') do
      concat f.hidden_field '_destroy'
      concat f.label '_destroy', fa_icon('trash'), class: 'm-0 p-0'
      concat f.check_box '_destroy', class: 'destroy'
    end
  else
    link_to(fa_icon('trash'), '#', class: 'btn btn-outline-warning trash-remove', data: { 'class-to-remove': '.communication_recipient' })
  end
end

#communication_recipient_state_label(communication_recipient) ⇒ Object



107
108
109
110
111
112
# File 'app/helpers/communications_helper.rb', line 107

def communication_recipient_state_label(communication_recipient)
  label_class = { processed: 'secondary', dropped: 'danger', delivered: 'success',
                  deferred: 'warning', bounced: 'danger', opened: 'success', ok: 'success',
                  clicked: 'success', spammed: 'danger', unsubscribed: 'warning' }[communication_recipient.state.to_sym] || 'default'
   :span, communication_recipient.human_state_name.upcase, class: "badge bg-#{label_class}"
end

#communication_state_details(cr) ⇒ Object



47
48
49
50
51
52
# File 'app/helpers/communications_helper.rb', line 47

def communication_state_details(cr)
  msg = []
  msg << "State triggered at #{cr.state_updated_at.to_fs(:crm_default)}" if cr.state_updated_at
  msg << "Response: #{cr.state_response}" if cr.state_response.present?
  raw(msg.join('. '))
end

#communication_state_label(communication) ⇒ Object



100
101
102
103
104
105
# File 'app/helpers/communications_helper.rb', line 100

def communication_state_label(communication)
  label_class = { exception: 'danger', suppressed: 'danger', queued: 'warning', sent: 'success', received: 'info', draft: 'secondary' }[communication.state.to_sym] || 'default'
  comm_label = communication.human_state_name.upcase
  comm_label << ' (Scheduled)' if communication.transmit_at && communication.queued?
   :span, comm_label, class: "badge bg-#{label_class}"
end

#email_template_option(template) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'app/helpers/communications_helper.rb', line 25

def email_template_option(template)
  # All templates are selectable, but mark v3 templates in the label
  label = if template.redactor_4_ready?
            "#{template.description} [#{template.category}]"
          else
            "#{template.description} [#{template.category}] (v3)"
          end
  [label, template.id]
end

#email_templates_for_user(include_email_template_id) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/helpers/communications_helper.rb', line 12

def email_templates_for_user(include_email_template_id)
  options = {}
  # Show all templates - Redactor 3 templates are shown but disabled (only v4 can be selected)
  base_query = EmailTemplate.non_campaign.active.order(:description)
  person_filter_sql = "resource_type = 'Employee' and resource_id = :employee_id"
  person_filter_sql = "(#{person_filter_sql}) OR email_templates.id = :email_template_id" if include_email_template_id
  tpl_pers = base_query.where(person_filter_sql, employee_id: @context_user.id, email_template_id: include_email_template_id)
  tpl_global = base_query.where(resource_id: nil)
  options['Private Templates'] = tpl_pers.map { |t| email_template_option(t) } if tpl_pers.present?
  options['Global Templates'] = tpl_global.map { |t| email_template_option(t) } if tpl_global.present?
  options
end

#preview_communication_body(communication) ⇒ Object



114
115
116
117
118
# File 'app/helpers/communications_helper.rb', line 114

def preview_communication_body(communication)
  return unless communication

  render partial: '/communications/preview_body', locals: { communication: communication }
end

#setup_communication(communication) ⇒ Object



2
3
4
5
6
# File 'app/helpers/communications_helper.rb', line 2

def setup_communication(communication)
  communication.tap do |c|
    c.communication_recipients.build(category: ContactPoint::EMAIL, email_method: 'to') unless c.communication_recipients.detect { |cr| cr.category == ContactPoint::EMAIL }
  end
end

#translate_cp_category_to_combo(category) ⇒ Object



93
94
95
96
97
98
# File 'app/helpers/communications_helper.rb', line 93

def translate_cp_category_to_combo(category)
  {
    'email' => 'email_to',
    'fax' => 'fax'
  }[category]
end