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



54
55
56
57
58
59
60
# File 'app/services/item/materials/compatible_accessories.rb', line 54

def self.item_product_line_ids_for_accessories(item)
  pl_ids = []
  pl_ids << item.primary_product_line&.id
  pl_ids += item.root_system_product_line&.complimentary_product_line_ids || [] # The complimentaries of the root system
  pl_ids += item.primary_product_line&.complimentary_product_line_ids || [] # The complimentaries of only this product line
  ProductLine.self_and_ancestors_ids(pl_ids).compact.uniq
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



8
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
# File 'app/services/item/materials/compatible_accessories.rb', line 8

def process(item:, catalog_items_scope: nil, product_line: false, context: nil)
  catalog_items = CatalogItem.none #
  messages = []
  items = Item.active.non_publications
  item_ids = []
  if (accessories_item_ids = item.target_item_relations.accessories_or_parts.pluck(:target_item_id)).present?
    item_ids += accessories_item_ids
  end

  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.pluck(:id)
    item_ids += item_scope.spare_parts.pluck(:id)
    item_ids += item_scope.sensors.pluck(:id)
    item_ids += item_scope.upgrades.pluck(:id)
    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).pluck(:id) # add on relay
    end
    item_ids += item_scope.relay_panels.pluck(:id) 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&.pluck(:id)
    # Batch lookup strip item IDs in single query instead of N+1
    exclude_ids += Item.where(sku: ItemConstants::TZ_CABLE_STRIP_SKUS).pluck(:id)
    exclude_ids += item_scope.pluck(:id)
    # Radiant panel overrides
    exclude_ids += Item.where(sku: %w[IP-EM-ACC-CEI IP-EM-ACC-BAR]).pluck(:id) if item.sku == 'IP-EM-FLX-WHT-0300-W' && !product_line
    # The IPE towel bar is only available for radiant panels with a width of 60 cm
    exclude_ids += Item.where(sku: ['IPE-T-01']).pluck(:id) 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