Module: Crm::SmsMessagesHelper

Defined in:
app/helpers/crm/sms_messages_helper.rb

Overview

View helpers for the CRM SMS message screens: action links, status badges,
sender/recipient links, and the participant filter collection.

Instance Method Summary collapse

Instance Method Details

#sms_message_command_options(sms_message) ⇒ Array<String>

Builds the action links available for an SMS message (resend, edit,
block/unblock sender, forward, delete), filtered by direction and ability.

Parameters:

  • sms_message (SmsMessage)

    message to build links for

Returns:

  • (Array<String>)

    rendered link HTML fragments



13
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
# File 'app/helpers/crm/sms_messages_helper.rb', line 13

def sms_message_command_options(sms_message)
  links = []
  if can?(:update, sms_message)
    if sms_message.outbound?
      links << link_to(fa_icon('share', text: 'Resend'), resend_sms_message_path(sms_message, return_path: @return_path))

      links << link_to(fa_icon('pen-to-square', text: 'Edit'), edit_sms_message_path(sms_message, return_path: @return_path)) if sms_message.draft?
    end
  end
  if sms_message.inbound?
    # Reply and mark read/unread live inline in the thread now — not here.
    if sms_message.sender_blocked?
      links << link_to(fa_icon('check', text: 'Unblock Sender'), unblock_sender_sms_message_path(sms_message, return_path: @return_path), data: { turbo_method: :post })
    else
      links << link_to(fa_icon('ban', text: 'Block Sender'), block_sender_sms_message_path(sms_message, return_path: @return_path), data: { turbo_method: :post })
      if can?(:destroy, sms_message)
        links << link_to(fa_icon('ban', text: 'Delete and Block Sender'),
                        destroy_and_block_sender_sms_message_path(sms_message, return_path: @return_path),
                        data: { turbo_confirm: 'Are you sure? this will delete this message and block sender', turbo_method: :delete })
      end
    end
  end
  if sms_message.inbound? && can?(:update, sms_message)
    links << link_to(fa_icon('share-from-square', text: 'Forward'),
                     forward_sms_message_path(sms_message, return_path: @return_path))
  end
  if can?(:destroy, sms_message)
    links << link_to(fa_icon('trash', text: 'Delete'), sms_message_path(sms_message, return_path: @return_path),
                data: { turbo_confirm: "Are you sure?", turbo_method: :delete })
  end
  links
end

Renders the recipient's phone number as a link to the filtered message
list, flagged when the recipient is do-not-text, plus a link to the
associated party when present.

Parameters:

  • sms_message (SmsMessage)

    message whose recipient to render

Returns:

  • (String)

    captured HTML fragment



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/helpers/crm/sms_messages_helper.rb', line 99

def sms_message_recipient_link(sms_message)
  capture do
    if sms_message.recipient_do_not_text
      concat link_to(fa_icon('ban', class: 'text-danger'), do_not_call_path(sms_message.recipient_do_not_text))
      concat tag.del(link_to(number_to_phone(sms_message.recipient), sms_messages_path(q: { between_participants: [sms_message.recipient] })))
    else
      concat link_to(number_to_phone(sms_message.recipient), sms_messages_path(q: { between_participants: [sms_message.recipient] }))
    end
    if sms_message.recipient_party
      concat ''
      concat link_to(party_icon(sms_message.recipient_party), polymorphic_path(sms_message.recipient_party, tab: "sms"))
    end
  end
end

Renders the sender's phone number as a link to the filtered message list,
flagged when the sender is do-not-text or blocked, plus a link to the
associated party when present.

Parameters:

  • sms_message (SmsMessage)

    message whose sender to render

Returns:

  • (String)

    captured HTML fragment



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/helpers/crm/sms_messages_helper.rb', line 75

def sms_message_sender_link(sms_message)
  capture do
    if sms_message.sender_do_not_text
      concat link_to(fa_icon('ban', class: 'text-danger'), do_not_call_path(sms_message.sender_do_not_text))
      concat tag.del(link_to(number_to_phone(sms_message.sender), sms_messages_path(q: { between_participants: [sms_message.sender] })))
    elsif sms_message.sender_blocked?
      concat fa_icon('ban', text: 'Sender Blocked', class: 'text-danger')
      concat tag.del(link_to(number_to_phone(sms_message.sender), sms_messages_path(q: { between_participants: [sms_message.sender] })))
    else
      concat link_to(number_to_phone(sms_message.sender), sms_messages_path(q: { between_participants: [sms_message.sender] }))
    end
    if sms_message.sender_party
      concat ''
      concat link_to(party_icon(sms_message.sender_party), polymorphic_path(sms_message.sender_party, tab: "sms"))
    end
  end
end

#sms_message_status_label(sms_message) ⇒ String

Renders a colored Bootstrap badge for an SMS message state, with a
direction arrow and the exception reason as tooltip when applicable.

Parameters:

  • sms_message (SmsMessage)

    message whose status to render

Returns:

  • (String)

    badge HTML



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/helpers/crm/sms_messages_helper.rb', line 51

def sms_message_status_label(sms_message)
  label_class = { draft: 'light',
                  queued: 'warning',
                  sent: 'info',
                  delivered: 'success',
                  exception: 'danger',
                  received_queue: 'info',
                  received: 'info' }[sms_message.state.to_sym] || 'info'

  display_state = if sms_message.outbound?
                    sms_message.human_state_name.upcase + fa_icon('arrow-right-long', class: 'ms-2')
                  else
                    fa_icon('arrow-left-long', class: 'me-2') + sms_message.human_state_name.upcase
                  end
   :span, display_state.html_safe, class: "badge bg-#{label_class}",
              title: sms_message.exception? ? sms_message.exception_reason : nil
end

#sms_participants_collectionArray<Array(String, String)>

Builds the select collection of SMS participants: global numbers plus
active employee numbers, extended with any numbers already selected via
the current filter params.

Returns:

  • (Array<Array(String, String)>)

    [label, number] pairs for select options



119
120
121
122
123
124
125
126
# File 'app/helpers/crm/sms_messages_helper.rb', line 119

def sms_participants_collection
  # Create the participant collection based on passed in filters combined with our employee sms numbers
  participant_collection = SmsMessage.global_numbers_for_select + Employee.all_active_employees_sms_numbers(format_for_select: true)
  filter_participant_collection = (params.dig(:q, :recipient_or_sender_in) || []).filter_map(&:presence).uniq
  unlisted_participants = filter_participant_collection - participant_collection.map(&:last)
  participant_collection += unlisted_participants.map { |n| [n, n] }
  participant_collection.filter_map(&:presence).uniq
end