Module: ContactPointsHelper

Defined in:
app/helpers/contact_points_helper.rb

Overview

View helper: contact points.

Instance Method Summary collapse

Instance Method Details

#can_click_to_call?Object?

Whether the context user has a Switchvox account configured for click-to-call.

Returns:

  • (Object, nil)

    the switchvox account id, or nil when not configured



254
255
256
# File 'app/helpers/contact_points_helper.rb', line 254

def can_click_to_call?
  @context_user.employee_phone_status.try(:switchvox_account_id)
end

#contact_point_category_icon(category, options = {}) ⇒ String

Renders the Font Awesome icon for a contact point category.

Parameters:

  • category (String, Symbol)

    contact point category

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

    options forwarded to fa_icon

Options Hash (options):

  • text (String)

    text rendered next to the icon

  • class (String)

    additional CSS classes for the icon

Returns:

  • (String)

    the icon HTML



12
13
14
# File 'app/helpers/contact_points_helper.rb', line 12

def contact_point_category_icon(category, options = {})
  fa_icon(contact_point_icon_for_category(category), options)
end

#contact_point_delete_button(contact_point) ⇒ String

Renders a delete button for a contact point.

Persisted records link to the destroy endpoint with a Turbo confirm;
unpersisted (nested form) records get a client-side row remover.

Parameters:

  • contact_point (ContactPoint)

    the contact point to delete

Returns:

  • (String)

    the delete button HTML



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/helpers/contact_points_helper.rb', line 47

def contact_point_delete_button(contact_point)
  if contact_point.persisted?
    link_to(fa_icon('trash'), contact_point_path(contact_point),
            data: {
              controller: 'ui-blocker',
              turbo_confirm: 'Are you sure you want to delete this contact point?',
              turbo_method: :delete,
              'class-to-remove': '.contact_point'
            },
            class: 'btn btn-outline-primary')
  else
    link_to(fa_icon('trash'), '#', class: 'btn btn-outline-primary trash-remove', data: { 'class-to-remove': '.contact_point' })
  end
end

#contact_point_email_preferences(contact_point) ⇒ String?

Renders an "Email preferences" button for an email contact point.

Parameters:

Returns:

  • (String, nil)

    the button HTML, or nil when not applicable



79
80
81
82
83
84
85
86
# File 'app/helpers/contact_points_helper.rb', line 79

def contact_point_email_preferences(contact_point)
  return if contact_point.new_record?
  return unless contact_point.email?

  link_to fa_icon('microphone-slash', title: 'Email preferences'),
          by_email_email_preferences_path(email: contact_point.detail, return_path: request.fullpath),
          class: 'btn btn-outline-primary'
end

#contact_point_icon_for_category(category) ⇒ String

Maps a contact point category to its Font Awesome icon name.

Parameters:

  • category (String, Symbol)

    contact point category (fax, email, cell, phone)

Returns:

  • (String)

    the Font Awesome icon name, or 'question' for unknown categories



20
21
22
23
24
25
26
27
28
# File 'app/helpers/contact_points_helper.rb', line 20

def contact_point_icon_for_category(category)
  sym_matrix = {
    fax: 'fax',
    email: 'envelope',
    cell: 'mobile-alt',
    phone: 'phone'
  }
  sym_matrix[category.to_sym] || 'question'
end

#contact_point_mark_as_working(contact_point, btn_class = 'btn btn-outline-primary btn-success', label = nil, return_path = request.fullpath) ⇒ String?

Renders a "Mark email as working again" button for a failed email contact point.

Parameters:

  • contact_point (ContactPoint)

    the contact point

  • btn_class (String) (defaults to: 'btn btn-outline-primary btn-success')

    CSS classes for the button

  • label (String, nil) (defaults to: nil)

    optional text rendered next to the icon

  • return_path (String) (defaults to: request.fullpath)

    path to return to after marking as working

Returns:

  • (String, nil)

    the button HTML, or nil when not applicable



95
96
97
98
99
100
101
102
# File 'app/helpers/contact_points_helper.rb', line 95

def contact_point_mark_as_working(contact_point, btn_class = 'btn btn-outline-primary btn-success', label = nil, return_path = request.fullpath)
  return if contact_point.new_record?
  return unless contact_point.email? && contact_point.failed_before?

  link_to fa_icon('check', text: label, title: 'Mark email as working again'),
          mark_as_working_contact_point_path(contact_point, return_path: return_path),
          class: btn_class
end

#contact_point_options(cp, communication_options: {}, options: {}) {|OpenStruct| ... } ⇒ Object?

Creates actions and options for contact points based on their state
will yield a structure you must consume and render

Parameters:

  • cp (ContactPoint)

    the contact point

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

    extra params merged into new communication links

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

    display options

Options Hash (options:):

  • return_path (String)

    path to return to after actions (defaults to the party path)

  • link_class (String)

    CSS class applied to action links

Yields:

  • (OpenStruct)

    display hash with :category, :title, and :actions

Returns:

  • (Object, nil)

    the block's return value, or nil when not displayable



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'app/helpers/contact_points_helper.rb', line 191

def contact_point_options(cp, communication_options: {}, options: {})
  return unless cp
  return if cp.detail.blank?

  display_title = cp.formatted_dial_string
  return if display_title.blank?

  display_title = tag.del(display_title) if cp.call_blocked? # Only when calls are blocked put this treatment
  # Because hash are by reference
  options = options.dup

  return_path = options.delete(:return_path)
  return_path ||= polymorphic_path(cp.party) if cp.party
  return_path ||= cms_link(request.path) unless request.xhr?

  link_class = options.delete(:link_class)
  nil
  actions = []

  if cp.transmittable?
    communication_params = { recipient_contact_point_id: cp.id, return_path: return_path }.merge(communication_options)
    actions << contact_point_mark_as_working(cp, "text-success #{link_class}") if cp.email? && cp.failed_before?

    actions << link_to(fa_icon((cp.fax? ? 'fax' : 'envelope')),
                       new_communication_path(communication_params),
                       class: link_class)
    if cp.email?
      actions << link_to(fa_icon('gear', title: 'Email preferences'),
                         by_email_email_preferences_path(email: cp.detail, return_path: return_path),
                         class: "#{'text-danger' if cp.email_blocked?} #{link_class}")
    end

  elsif cp.callable?
    actions << sip_dial_link(fa_icon('headset', title: 'Call'), cp.dial_string, party_id: cp.party_id, class: link_class) unless cp.call_blocked?
    if cp.sms_enabled? && current_user&.sms_enabled? && !cp.text_blocked?
      actions << link_to(fa_icon('comment-sms', title: 'Send SMS/Text'),
                  new_sms_message_path(sms_message: { recipient: cp.dial_string, recipient_party_id: cp.party_id },
                  return_path: polymorphic_path(cp.party, tab: "sms")),
                  class: link_class)
    end

    actions << link_to(fa_icon('gear', title: 'Do Not Call/Text/Fax Preferences'),
                        new_do_not_call_path(do_not_call: { number: cp.detail }, return_path: return_path),
                        class: "#{'text-danger' if cp.dnc_blocked?} #{link_class}")

  elsif cp.website?
    actions << link_to(fa_icon('up-right-from-square', title: 'Open site in new window'), cp.detail, target: '_blank', rel: "noopener noreferrer", class: link_class)
  end

  display_title << (:span, cp.notes, class: 'ms-2 badge rounded-pill bg-secondary') if cp.notes.present?

  display_hash = {
    category: cp.category,
    title: display_title.html_safe,
    actions: actions
  }

  yield OpenStruct.new(display_hash).freeze
end

#contact_point_phone_preferences(contact_point) ⇒ String?

Renders a "Put on Do Not Call List" button for a dialable phone contact point.

Parameters:

Returns:

  • (String, nil)

    the button HTML, or nil when not applicable



66
67
68
69
70
71
72
73
# File 'app/helpers/contact_points_helper.rb', line 66

def contact_point_phone_preferences(contact_point)
  return if contact_point.new_record?
  return unless contact_point.can_be_dialed?

  link_to fa_icon('microphone-slash', title: 'Put on Do Not Call List'),
          new_do_not_call_path(do_not_call: { number: contact_point.detail, country_code: contact_point.country_code }, return_path: polymorphic_path(contact_point.party)),
          class: 'btn btn-outline-primary'
end

#contact_point_status(status) ⇒ String

Renders a status badge for a contact point.

Parameters:

  • status (String)

    the contact point status

Returns:

  • (String)

    the status badge HTML



34
35
36
37
38
# File 'app/helpers/contact_points_helper.rb', line 34

def contact_point_status(status)
   :span, class: "contact_point_status #{status}" do
    fa_icon('circle-arrow-right', text: status)
  end
end

#display_contact_point(cp, communication_options = {}, in_options = {}) ⇒ String?

Legacy style contact point display
Use contact_point_options for a better option which yields a struct
you can render properly in a view or partial

Parameters:

  • cp (ContactPoint, nil)

    the contact point to display

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

    extra params merged into new communication links

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

    display options

Options Hash (in_options):

  • embed_icon (Boolean)

    embed the category icon in the title

  • return_path (String)

    path to return to after actions

  • style (String)

    additional inline CSS for the title

  • highlight (Boolean)

    emphasize the title with a bullseye icon

  • preferences_label (String)

    label for the email preferences link

Returns:

  • (String, nil)

    the display HTML, or nil when not displayable



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'app/helpers/contact_points_helper.rb', line 117

def display_contact_point(cp, communication_options = {}, in_options = {})
  return unless cp
  return if cp.detail.blank?

  options = in_options.dup
  embed_icon = options.delete(:embed_icon).to_b
  return_path = options.delete(:return_path)
  return_path ||= polymorphic_path(cp.party) if cp.party
  return_path ||= cms_link(request.path) unless request.xhr?
  display_title = cp.formatted_dial_string
  styles = [options.delete(:style)]
  info = [cp.notes]
  if cp.email?
    if cp.failed_before?
      styles << 'color:red'
      display_title += fa_icon('triangle-exclamation')
      info << cp.system_notes
    elsif cp.working?
      styles << 'color:green'
    end
  end
  if options.delete(:highlight)
    display_title = fa_icon('bullseye', text: display_title)
    styles << 'font-weight:bolder'
  end

  return if display_title.blank?

  display_title = display_title.html_safe

  nil
  preferences_link = ''
  mark_as_working_link = ''
  display_title = if embed_icon
                    contact_point_category_icon(cp.category, text: display_title, style: styles.compact.join(';').presence)
                  else
                     :span, display_title,
                                                style: styles.compact.join(';').presence
                  end
  display_title << (:span, cp.notes, class: 'ms-1 badge rounded-pill bg-secondary') if cp.notes.present?

  if cp.transmittable?
    display_title = link_to display_title, new_communication_path({ recipient_contact_point_id: cp.id, return_path: return_path }.merge(communication_options))
    if cp.email?
      preferences_link = link_to(fa_icon('gears', text: options[:preferences_label], title: 'Email preferences'),
                                 by_email_email_preferences_path(email: cp.detail, return_path: return_path))
    end
    mark_as_working_link = contact_point_mark_as_working(cp) if cp.email? && cp.failed_before?
  elsif cp.callable?
    display_title = sip_dial_link(display_title, cp.dial_string, party_id: cp.party_id)
  elsif cp.website?
    display_title = link_to(cp.detail, cp.detail, target: '_blank', rel: "noopener noreferrer")
  end

  copy_link = clipboard_copy(cp.detail, button_class: 'btn btn-link p-0')

  category_label = cp.category.capitalize
  if (info = info.filter_map(&:presence)).present?
    category_label << " (#{info.join(', ')})"
  end
  value_display = [display_title, preferences_link, mark_as_working_link, copy_link].compact.join(' ').html_safe
  attr_display(category_label, value_display, true, options)
end

#party_contact_points_panel(parties_or_hash, options = {}) ⇒ String?

Renders a panel of contact points grouped per party, active parties first.

Parameters:

  • parties_or_hash (Party, Array<Party>, Hash)

    parties to render, or a title => party hash

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

    render options merged into each panel's locals

Options Hash (options):

  • wrapper_class (String)

    CSS class for each panel wrapper

Returns:

  • (String, nil)

    the panel HTML



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'app/helpers/contact_points_helper.rb', line 288

def party_contact_points_panel(parties_or_hash, options = {})
  return if parties_or_hash.blank?

  render_hash = {}
  if parties_or_hash.is_a?(Hash)
    render_hash = parties_or_hash
  elsif %(Customer Contact Supplier).include?(parties_or_hash.class.name)
    render_hash = { parties_or_hash.full_name => parties_or_hash }
  elsif parties_or_hash.respond_to?(:to_a)
    render_hash = parties_or_hash.index_by(&:full_name)
  end

  # Sort entries: active contacts first, then inactive ones
  sorted_entries = render_hash.sort_by do |_title, party_or_hash|
    party = party_or_hash.is_a?(Hash) ? party_or_hash[:party] : party_or_hash
    party&.inactive ? 1 : 0
  end

  capture do
    (:div, id: 'contacts_panel', class: 'row') do
      sorted_entries.each do |title, party_or_hash|
        if party_or_hash.is_a?(Hash)
          party = party_or_hash.delete(:party)
          render_options = { title: title }.merge(party_or_hash)
        else
          party = party_or_hash
          render_options = options.merge(title: title)
        end

        concat render(partial: 'crm/shared/party_contact_points_panel', locals: {
                        party: party,
                        options: render_options,
                        wrapper_class: options[:wrapper_class]
                      })
      end
    end
  end
end

#setup_context_object_for_contact_points(context_object) ⇒ #contact_points

Ensures a context object (party form) has email and phone contact points built.

Builds an email and a phone contact point when missing, plus a blank one
once all existing contact points are persisted.

Parameters:

  • context_object (#contact_points)

    the object to build contact points on

Returns:

  • (#contact_points)

    the same context object



334
335
336
337
338
339
340
341
342
# File 'app/helpers/contact_points_helper.rb', line 334

def setup_context_object_for_contact_points(context_object)
  context_object.tap do |co|
    # Build new contact points if needed
    [ContactPoint::EMAIL, ContactPoint::PHONE].each do |mc|
      co.contact_points.build(category: mc) unless co.contact_points.find { |cp| cp.category == mc }
    end
    co.contact_points.build if co.contact_points.all?(&:persisted?)
  end
end

Renders a click-to-call dial link based on the current user's phone integration.

Parameters:

  • display_title (String)

    the link text

  • phone (String)

    the phone number to dial

  • in_additional_params (Hash, nil) (defaults to: nil)

    extra params for the link

Options Hash (in_additional_params):

  • class (String)

    CSS class for the link

Returns:

  • (String, nil)

    the link HTML, a fallback span, or nil without a current user



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'app/helpers/contact_points_helper.rb', line 265

def sip_dial_link(display_title, phone, in_additional_params = nil)
  return unless current_user

  additional_params = (in_additional_params || {}).dup
  link_class = additional_params.delete(:class)

  case current_user.employee_phone_status&.click_to_call_integration&.to_sym
  when :api
    link_to(display_title, outbound_calls_path(destination: phone, **additional_params), data: { turbo_method: :post }, class: link_class)
  when :callto, :tel
    # callto does not get intercepted each time by chrome but yields the same results
    link_to(display_title, "#{current_user.employee_phone_status.click_to_call_integration}:#{phone.split('x').first}", class: link_class)
  else
    (:span, display_title, title: 'Current user not setup for click to call')
  end
end