Class: SolidusPromotions::Conditions::LineItemTaxon

Inherits:
SolidusPromotions::Condition show all
Includes:
LineItemLevelCondition, TaxonCondition
Defined in:
app/models/solidus_promotions/conditions/line_item_taxon.rb

Constant Summary collapse

MATCH_POLICIES =
%w[include exclude].freeze

Instance Method Summary collapse

Methods included from TaxonCondition

included, #preload_relations, #taxon_ids_string, #taxon_ids_string=, #taxons_ids_with_children=

Methods included from LineItemLevelCondition

included, #level

Methods inherited from SolidusPromotions::Condition

#applicable?, applicable_to, #eligibility_errors, #eligible?, eligible_method_for, inherited, #level, #preload_relations, #to_partial_path, #updateable?

Instance Method Details

#line_item_eligible?(line_item, _options = {}) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/models/solidus_promotions/conditions/line_item_taxon.rb', line 17

def line_item_eligible?(line_item, _options = {})
  line_item_taxon_ids = line_item.variant.product.classifications.map(&:taxon_id)

  case preferred_match_policy
  when "include"
    taxon_ids_with_children.any? { |taxon_and_descendant_ids| (line_item_taxon_ids & taxon_and_descendant_ids).any? }
  when "exclude"
    taxon_ids_with_children.none? { |taxon_and_descendant_ids| (line_item_taxon_ids & taxon_and_descendant_ids).any? }
  else
    raise "unexpected match policy: #{preferred_match_policy.inspect}"
  end
end