Module: Crm::ResourcesHelper

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

Overview

View helpers for rendering links to arbitrary CRM resources (opportunities,
quotes, orders, invoices, etc.) as warning badges, used by CRM views that
display mixed collections of related records.

See Also:

  • ResourcesController

Instance Method Summary collapse

Instance Method Details

Renders a badge-styled link to a CRM resource, choosing a label and path
from the resource's class. Unknown classes fall back to a polymorphic
link, then to a static label when no route resolves.

Parameters:

  • resource (ApplicationRecord)

    the record to link to (Opportunity,
    Communication, Quote, Order, RoomConfiguration, Invoice, SupportCase,
    CreditApplication, SpiffEnrollment, PurchaseOrder, CreditMemo, Rma,
    Delivery, LocatorRecord, Payment, or any routable record)

Returns:

  • (ActiveSupport::SafeBuffer)

    the badge link or label HTML



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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/helpers/crm/resources_helper.rb', line 18

def resource_label_link(resource)
  with_options(class: 'badge bg-warning', style: 'white-space: normal;', data: { turbo_frame: '_top' }) do |options|
    case resource.class.name
    when 'Opportunity'
      options.link_to resource.name.to_s, opportunity_path(resource.id)
    when 'Communication'
      if resource.draft? && can?(:update, resource)
        options.link_to 'Communication', edit_communication_path(resource.id)
      else
        options.link_to 'Communication', communication_path(resource.id)
      end
    when 'Quote'
      options.link_to "Quote ##{resource.reference_number}", quote_path(resource.id)
    when 'Order'
      options.link_to "Order ##{resource.cart_identifier}", order_path(resource.id)
    when 'RoomConfiguration'
      options.link_to resource.name_with_room.to_s, room_configuration_path(resource.id)
    when 'Invoice'
      options.link_to "Invoice ##{resource.reference_number}", invoice_path(resource.id)
    when 'SupportCase'
      options.link_to "Support Case ##{resource.case_number}", support_case_path(resource.id)
    when 'CreditApplication'
      options.link_to "Credit Application ##{resource.reference_number}", credit_application_path(resource.id)
    when 'SpiffEnrollment'
      options.link_to "Spiff Enrollment ##{resource.id}", spiff_enrollment_path(resource.id)
    when 'PurchaseOrder'
      options.link_to "Purchase Order ##{resource.reference_number}", purchase_order_path(resource.id)
    when 'CreditMemo'
      options.link_to "Credit Memo ##{resource.reference_number}", credit_memo_path(resource.id)
    when 'Rma'
      options.link_to resource.rma_number, rma_path(resource.id)
    when 'Delivery'
      options.link_to "Delivery ##{resource.id}", delivery_path(resource.id)
    when 'LocatorRecord'
      options.link_to "Locator Record ##{resource.id}", locator_record_path(resource.id)
    when 'Payment'
      if resource.order
        options.link_to resource.to_s, order_path(resource.order)
      else
        options.(:span, resource.to_s)
      end
    else # Try a polymorphic link
      if (link = begin
        options.link_to(resource.to_s, polymorphic_path(resource))
      rescue StandardError
        nil
      end)
        link
      else # render static label
        options.(:span, resource.to_s)
      end
    end
  end
end