Class: Item::Materials::CompatibleAccessories

Inherits:
BaseCompatibleFinder
  • Object
show all
Defined in:
app/services/item/materials/compatible_accessories.rb

Overview

This class is responsible for finding a catalog item's compatible controls

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.item_product_line_ids_for_accessories(item) ⇒ Object

Was handing an id ARRAY to ProductLine.self_and_ancestors_ids, which takes a
single id and picks one unordered row — the scope was whichever line
Postgres happened to return first. See ProductLine#compatibility_scope_ids.



67
68
69
# File 'app/services/item/materials/compatible_accessories.rb', line 67

def self.item_product_line_ids_for_accessories(item)
  item.primary_product_line&.compatibility_scope_ids || []
end

Instance Method Details

#process(item:, catalog_items_scope: nil, product_line: false, context: nil) ⇒ Object

At a minimum you need an item
If you provide a catalog item scope, such as Catalog.catalog_items.for_online_catalog
It will be used to populate the catalog_items results
Without a catalog item scope only the items will be populated in the result
Context is either :sales, :support or nil for either



9
10
11
12
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/services/item/materials/compatible_accessories.rb', line 9

def process(item:, catalog_items_scope: nil, product_line: false, context: nil)
  CatalogItem.none
  items = Item.active.non_publications
  item_ids = []
  # 'Replacement Part' links are service inventory (screws, anchors, hex keys);
  # the sales surface honors only the deliberate 'Accessory' ones, which is how
  # cold leads and mounting kits still reach a product page.
  explicit = context == :sales ? item.target_item_relations.accessories : item.target_item_relations.accessories_or_parts
  item_ids += explicit.pluck(:target_item_id)

  if !item.disable_heuristic_accessories && (pl_ids = self.class.item_product_line_ids_for_accessories(item)).present?
    item_scope = Item.by_product_line_id(pl_ids)
    item_ids += item_scope.accessories.ids
    # Spare parts (incl. goods.spare_parts.cold_leads) are a support/CRM lookup,
    # not merchandising — the public site never heuristically suggests them. An
    # explicit accessories_or_parts relation above still wins.
    item_ids += item_scope.spare_parts.ids unless context == :sales
    item_ids += item_scope.sensors.ids
    item_ids += item_scope.upgrades.ids
    pl_slt = item.primary_product_line&.slug_ltree.to_s
    if item.primary_product_line && pl_slt.match?(/\Afloor_heating\.control\.(nhance|nspire|ntrust)/)
      item_ids += item_scope.where(sku: ItemConstants::POWER_MODULE_SKUS).ids # add on relay
    end
    item_ids += item_scope.relay_panels.ids if pl_slt.match?(/\Asnow_melting\.control\./) && pl_slt != 'snow_melting.control.relay'
    # We exclude variants of ourselves, strips, and other items rendered in separate relation types
    exclude_ids = item.variants&.ids
    # Batch lookup strip item IDs in single query instead of N+1
    exclude_ids += Item.where(sku: ItemConstants::TZ_CABLE_STRIP_SKUS).ids
    # Sibling products from our own line (other mats, other cables) aren't add-ons.
    # Excluding the whole item_scope instead also subtracted every accessory,
    # sensor and upgrade the heuristic had just found — plus any explicit relation
    # that fell inside it — which made this entire block a no-op.
    own_line_ids = item.primary_product_line&.self_and_descendants_ids
    exclude_ids += Item.by_product_line_id(own_line_ids).ids if own_line_ids.present?
    # Infrared heating panel overrides
    exclude_ids += Item.where(sku: %w[IP-EM-ACC-CEI IP-EM-ACC-BAR]).ids if item.sku == 'IP-EM-FLX-WHT-0300-W' && !product_line
    # The IPE towel bar is only available for infrared heating panels with a width of 60 cm
    exclude_ids += Item.where(sku: ['IPE-T-01']).ids if item.spec_value(:width).to_i != 60
    item_ids -= item.target_item_relations.not_accessories_or_parts.pluck(:target_item_id)
    item_ids -= exclude_ids
  end
  if item_ids.present?
    items = items.where(id: item_ids.compact.uniq)
    items = items.joins(:product_category).order(ProductCategory[:priority], ProductCategory[:name], Item[:sku])
  else
    items = Item.none
  end
  if context == :support
    # There's so much junk items that probably shouldn't be 'visible_for_support'
    # to quickly render some relevant i filter only to public items
    items = items.includes(:primary_product_line, :primary_image).available_to_public.available_to_public_for_support
  end
  return_results(item:, items:, catalog_items_scope:)
end