Module: Crm::EdiCommunicationLogsHelper

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

Overview

View helpers for the CRM EDI communication logs screens: state badges,
row highlighting, payload formatting, and per-flow run actions.

Constant Summary collapse

ECL_STATE_BADGE =

Bootstrap badge CSS class per EdiCommunicationLog state.

{
  'ready'      => 'text-bg-info',
  'processing' => 'text-bg-primary',
  'processed'  => 'text-bg-success',
  'retry'      => 'text-bg-warning',
  'exception'  => 'text-bg-danger',
  'archived'   => 'text-bg-secondary'
}.freeze
ECL_ROW_CLASS =

Bootstrap table-row CSS class per EdiCommunicationLog state.

{
  'exception'  => 'table-danger',
  'processing' => 'table-primary',
  'retry'      => 'table-warning'
}.freeze

Instance Method Summary collapse

Instance Method Details

#all_orchestrators_all_partners {|oc_name, partner| ... } ⇒ void

This method returns an undefined value.

Yields each active EDI orchestrator/partner pair across all orchestrator
classes.

Yield Parameters:

  • oc_name (String)

    the orchestrator class name

  • partner (String)

    the partner handled by that orchestrator



97
98
99
100
101
102
103
104
# File 'app/helpers/crm/edi_communication_logs_helper.rb', line 97

def all_orchestrators_all_partners
  Edi::BaseOrchestrator.all_orchestrators_class.each do |orchestrator_class|
    oc_name = orchestrator_class.name
    orchestrator_class.orchestrators.select(&:active).map(&:partner).each do |partner|
      yield(oc_name, partner)
    end
  end
end

#ecl_row_class(ecl) ⇒ String

Returns the Bootstrap table-row CSS class highlighting the log's state.

Parameters:

Returns:

  • (String)

    the row class, or an empty string for unhighlighted states



42
43
44
# File 'app/helpers/crm/edi_communication_logs_helper.rb', line 42

def ecl_row_class(ecl)
  ECL_ROW_CLASS.fetch(ecl.state.to_s, '')
end

#ecl_state_badge(ecl) ⇒ ActiveSupport::SafeBuffer

Renders a Bootstrap badge for the log's state, appending a timer glyph
when the log is scheduled to transmit in the future.

Parameters:

Returns:

  • (ActiveSupport::SafeBuffer)

    the badge <span> tag



31
32
33
34
35
36
# File 'app/helpers/crm/edi_communication_logs_helper.rb', line 31

def ecl_state_badge(ecl)
  css = ECL_STATE_BADGE.fetch(ecl.state.to_s, 'text-bg-secondary')
  label = ecl.human_state_name
  label = "#{label}" if ecl.transmit_after&.future?
  tag.span(label, class: "badge #{css}")
end

#edi_communication_log_actions(ecl, include_show_action: true, return_path: nil) ⇒ Array<ActiveSupport::SafeBuffer>

Builds the list of action links (status, process, show, edit, clone,
download, delete) for a log, filtered by state and authorization.

Parameters:

  • ecl (EdiCommunicationLog)

    the log the actions operate on

  • include_show_action (Boolean) (defaults to: true)

    unused; kept for call-site compatibility

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

    optional path to return to after the action

Returns:

  • (Array<ActiveSupport::SafeBuffer>)

    the rendered action links



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

def edi_communication_log_actions(ecl, include_show_action: true, return_path: nil) # rubocop:disable Lint/UnusedMethodArgument
  links = []
  with_options(controller: 'crm/edi_communication_logs', return_path: return_path) do |defaults|
    links << link_to('Status', defaults.url_for(action: :do_status, id: ecl.id), data: { turbo_method: :post }) if ecl.processing?
    links << link_to('Process', defaults.url_for(action: :do_process, id: ecl.id), data: { turbo_method: :post }) if ecl.ready?
    links << link_to('Show', defaults.url_for(action: :show, id: ecl.id))
    links << link_to('Edit', defaults.url_for(action: :edit, id: ecl.id)) if can?(:update, ecl)
    links << link_to('Clone', defaults.url_for(action: :clone, id: ecl.id), data: { turbo_method: :post }) if can?(:create, EdiCommunicationLog)
    links << link_to('Download', defaults.url_for(action: :download, id: ecl.id))
    links << link_to('Delete', defaults.url_for(action: :destroy, id: ecl.id), data: { turbo_method: :delete, turbo_confirm: 'Please confirm you want to delete this record?' }) if can?(:destroy, ecl)
  end
  links
end

#edi_communication_log_actions_for_inventory_flowArray<ActiveSupport::SafeBuffer>

Builds links to run the EDI inventory flow, globally and per
orchestrator/partner pair.

Returns:

  • (Array<ActiveSupport::SafeBuffer>)

    the rendered action links



124
125
126
127
128
129
130
131
132
# File 'app/helpers/crm/edi_communication_logs_helper.rb', line 124

def edi_communication_log_actions_for_inventory_flow
  res = []
  res << link_to('Run inventory flow (all)', inventory_flow_edi_communication_logs_path, data: { turbo_method: :post })
  all_orchestrators_all_partners do |oc_name, partner|
    res << link_to("Run inventory flow #{oc_name} - #{partner}",
                   inventory_flow_edi_communication_logs_path(orchestrator_name: oc_name, partner:), data: { turbo_method: :post })
  end
  res
end

#edi_communication_log_actions_for_order_flowArray<ActiveSupport::SafeBuffer>

Builds links to run the EDI order flow, globally and per
orchestrator/partner pair.

Returns:

  • (Array<ActiveSupport::SafeBuffer>)

    the rendered action links



110
111
112
113
114
115
116
117
118
# File 'app/helpers/crm/edi_communication_logs_helper.rb', line 110

def edi_communication_log_actions_for_order_flow
  res = []
  res << link_to('Run order flow (all)', order_flow_edi_communication_logs_path, data: { turbo_method: :post })
  all_orchestrators_all_partners do |oc_name, partner|
    res << link_to("Run order flow #{oc_name} - #{partner}",
                   order_flow_edi_communication_logs_path(orchestrator_name: oc_name, partner:), data: { turbo_method: :post })
  end
  res
end

#edi_communication_log_actions_for_product_data_flowArray<ActiveSupport::SafeBuffer>

Builds links to run the EDI product data flow, globally and per
orchestrator/partner pair.

Returns:

  • (Array<ActiveSupport::SafeBuffer>)

    the rendered action links



138
139
140
141
142
143
144
145
146
# File 'app/helpers/crm/edi_communication_logs_helper.rb', line 138

def edi_communication_log_actions_for_product_data_flow
  res = []
  res << link_to('Run data flow (all)', product_data_flow_edi_communication_logs_path, data: { turbo_method: :post })
  all_orchestrators_all_partners do |oc_name, partner|
    res << link_to("Run product data flow #{oc_name} - #{partner}",
                   product_data_flow_edi_communication_logs_path(orchestrator_name: oc_name, partner:), data: { turbo_method: :post })
  end
  res
end

#edi_communication_log_format_data(ecl) ⇒ ActiveSupport::SafeBuffer?

Renders the log's payload: interactive pretty-printed JSON when the data
is JSON and small enough, otherwise a plain <pre> block.

Parameters:

Returns:

  • (ActiveSupport::SafeBuffer, nil)

    the payload tag, or nil when the log has no data



51
52
53
54
55
56
57
58
59
# File 'app/helpers/crm/edi_communication_logs_helper.rb', line 51

def edi_communication_log_format_data(ecl)
  return if ecl.data.blank?

  if ecl.json? && ecl.data.size < 30_000
    pretty_json_tag ecl.data_as_json, expand: 8
  else
    tag.pre ecl.data_as_json
  end
end

#setup_edi_communication_log(ecl) ⇒ EdiCommunicationLog

Prepares a log for its form by ensuring a nested upload row is available.

Parameters:

Returns:



65
66
67
68
# File 'app/helpers/crm/edi_communication_logs_helper.rb', line 65

def setup_edi_communication_log(ecl)
  ecl.uploads.build unless ecl.uploads.any?(&:new_record?)
  ecl
end