Module: Models::ItemPackageContentHelper

Extended by:
ActiveSupport::Concern
Included in:
Item
Defined in:
app/concerns/models/item_package_content_helper.rb

Instance Method Summary collapse

Instance Method Details

#build_package_content_entriesObject



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
# File 'app/concerns/models/item_package_content_helper.rb', line 32

def build_package_content_entries
  entries = []
  if is_kit?
    entries += target_item_relations.kit_components
                                    .includes(:target_item)
                                    .joins(target_item: :product_category)
                                    .sort_by do |tir|
      if tir.target_item.is_heating_element?
        0
      elsif tir.target_item.is_thermostat?
        10
      elsif tir.target_item.is_membrane?
        20
      else
        100
      end
    end.map do |tir|
      qty = tir.quantity.to_i
      name_part = tir.target_item.package_content_short_name
      name_form = "(#{qty}) #{name_part}"
      sku_form = "(#{qty}) #{tir.target_item.sku}"
      { name_form: name_form, sku_form: sku_form }
    end
    # TRT and TCT kits have two sensors, one in the thermostat box, one on the roll or cable. this should probably be a spec attribute
    if sku.start_with?('TCT', 'TRT', 'RCT')
      sensor_form = '(2) Floor Sensors'
      entries << { name_form: sensor_form, sku_form: sensor_form }
    end
  else
    name_part = package_content_short_name
    name_form = "(1) #{name_part}"
    sku_form = "(1) #{sku}"
    entries << { name_form: name_form, sku_form: sku_form }
  end
  entries
end

#condense_group_to_char_limit(group_entries, char_limit) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/concerns/models/item_package_content_helper.rb', line 75

def condense_group_to_char_limit(group_entries, char_limit)
  forms = group_entries.map { |e| { use: :name_form, name_len: e[:name_form].length, sku_len: e[:sku_form].length, ref: e } }
  current = forms.map { |f| f[:ref][f[:use]] }
  current_len = current.join('; ').length
  if current_len > char_limit
    candidates = forms.map { |f| [f, (f[:name_len] - f[:sku_len])] }
                      .select { |(_f, delta)| delta.positive? }
                      .sort_by { |(_f, delta)| -delta }
    candidates.each do |f, _delta|
      f[:use] = :sku_form
      current = forms.map { |ff| ff[:ref][ff[:use]] }
      current_len = current.join('; ').length
      break if current_len <= char_limit
    end
  end
  return nil if current_len > char_limit

  current.join('; ')
end

#group_entries_for_max_unique_items(entries, max_unique_items) ⇒ Object



69
70
71
72
73
# File 'app/concerns/models/item_package_content_helper.rb', line 69

def group_entries_for_max_unique_items(entries, max_unique_items)
  groups = Array.new(max_unique_items) { [] }
  entries.each_with_index { |val, idx| groups[idx % max_unique_items] << val }
  groups
end

#package_content_htmlObject



109
110
111
# File 'app/concerns/models/item_package_content_helper.rb', line 109

def package_content_html
  %(<ul>#{package_contents_array.map { |c| "<li>#{c}</li>" }.join}</ul>).html_safe
end

#package_content_short_nameObject



99
100
101
102
103
104
105
106
107
# File 'app/concerns/models/item_package_content_helper.rb', line 99

def package_content_short_name
  if sku.in?(ItemConstants::TZ_CABLE_STRIP_SKUS)
    n = 'Cable Fixing Strips'
  else
    n = public_name.gsub(/[,();™©Ⓒ]/, '').gsub('sq.ft.', 'sq ft').gsub('ft.', 'ft')
    n << " (#{sku})"
  end
  n
end

#package_contents_array(char_limit: 150, max_unique_items: nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/concerns/models/item_package_content_helper.rb', line 8

def package_contents_array(char_limit: 150, max_unique_items: nil)
  entries = build_package_content_entries

  # If no grouping required, return individual entries respecting char limit per entry
  if max_unique_items.blank?
    return entries.map { |e| e[:name_form].length > char_limit ? e[:sku_form] : e[:name_form] }
  end

  max_unique_items = max_unique_items.to_i
  return [] if max_unique_items <= 0

  groups = group_entries_for_max_unique_items(entries, max_unique_items)
  condensed = groups.map { |group_entries| condense_group_to_char_limit(group_entries, char_limit) }

  if condensed.any?(&:nil?)
    Rails.logger.warn(
      "package_contents_array: could not fit grouped contents within char_limit=#{char_limit} and max_unique_items=#{max_unique_items} for item id=#{id}, sku=#{sku}; returning empty array"
    )
    return []
  end

  condensed.reject(&:blank?)
end

#package_contents_friendlyObject



95
96
97
# File 'app/concerns/models/item_package_content_helper.rb', line 95

def package_contents_friendly
  package_contents_array.join('; ')
end