Class: Item::Materials::CompatibleControls

Inherits:
BaseCompatibleFinder
  • Object
show all
Defined in:
app/services/item/materials/compatible_controls.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_controls(item) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'app/services/item/materials/compatible_controls.rb', line 77

def self.item_product_line_ids_for_controls(item)
  pl_ids = []
  pl_ids << item.primary_product_line&.id
  pl_ids << item.primary_product_line&.get_first_show_in_sales_portal_ancestor_id
  pl_ids += item.root_system_product_line&.complimentary_product_line_ids || [] # The complimentaries of the root system
  pl_ids += item.primary_product_line&.root&.complimentary_product_line_ids || [] # The complimentaries of the root
  pl_ids += item.primary_product_line&.complimentary_product_line_ids || [] # The complimentaries of only this product line
  ProductLine.self_and_descendants_ids(*pl_ids.compact).uniq
end

Instance Method Details

#process(item:, 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



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

def process(item:, context: nil)
  items = Item.none
  messages = []

  return_results(item:, items:) if skip_item?(item)

  item_relations = item.kit_target_item_relations.includes(:target_item)
  control_item_relations = item_relations.select { |tir| tir.target_item.is_control? }
  pl_ids_for_controls = self.class.item_product_line_ids_for_controls(item)
  exclude_item_ids = item.target_item_relations.not_controls.pluck(:target_item_id)
  if item.controllable?
    if control_item_relations.present?
      messages << "Using item's kit target item relations pulling controls"
      item_ids = control_item_relations.map(&:target_item_id)
      items = Item.where(id: item_ids)
    elsif item.is_towel_warmer_dual_connect?
      messages << 'Using towel warmer plug in and hardwired control item scope'
      items = Item.towel_warmer_controls_dual_connect
    elsif item.is_towel_warmer_plug_in?
      messages << 'Using towel warmer plug in control item scope'
      items = Item.towel_warmer_controls_plug_in
    elsif item.is_towel_warmer_hardwired?
      messages << 'Using towel warmer hardwire control item scope'
      items = Item.towel_warmer_controls_hardwired
    elsif item.is_radiant_panel_dual_connect?
      messages << 'Using radiant panel plug in and hardwired control item scope'
      items = Item.radiant_panel_controls_dual_connect
    elsif item.is_radiant_panel_plug_in?
      messages << 'Using radiant panel plug in control item scope'
      items = Item.radiant_panel_controls_plug_in
    elsif item.is_radiant_panel_hardwired?
      messages << 'Using radiant panel hardwire control item scope'
      items = Item.radiant_panel_controls_hardwired
    elsif item.is_heating_element? && item.primary_product_line_slug_ltree.to_s.start_with?('floor_heating')
      items = Item.floor_heating_controls
    elsif pl_ids_for_controls.present?
      messages << "Using item's product line controls. #{pl_ids_for_controls.inspect} pulling relays and controls from product line"
      # Any item that is not a publication, not discontinued and belonging to the same product line id,
      # that includes any of the product line ids tagged to this item not just the primary
      # and we'll bring complimentaries of the root product system line as well
      items = Item.non_publications.active.by_product_line_id(pl_ids_for_controls)
      # Only controls or relays are invited to the party
      items = items.merge(Item.controls.or(Item.relay_panels)) # In reality, support priority is a sales priority, most popular items first
      # Only compatible power types, plug-in vs hardwired.
      if (connection_type = item.spec_value(:connection_method))
        items = items.with_product_specification(:connection_method, connection_type, :electrical, true)
      end
    end
    # We further can test for voltage compatibility if we have it, but only against items who have that specs
    # This is not quite possible because voltage are not clearly defined all the time, e.g SCA DUAL and others
    # have a range of voltage, so it doesn't compare in this case
    # voltage = item.spec_value(:voltage)
    # items = items.with_product_specification(:voltage, voltage, :electrical, true) if voltage
  else
    messages << 'Could not determine any path for compatible controls'
  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.available_to_public.available_to_public_for_support
  end
  items = items.where.not(id: exclude_item_ids)
  items = items.includes(:primary_product_line, :primary_image).reorder(Item[:popularity].desc, :sku)
  return_results(item:, items:)
end

#skip_item?(item) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'app/services/item/materials/compatible_controls.rb', line 73

def skip_item?(item)
  !item.controllable?
end