Class: Spree::Promotion::Rules::Taxon

Inherits:
Spree::PromotionRule show all
Defined in:
app/models/spree/promotion/rules/taxon.rb

Constant Summary collapse

MATCH_POLICIES =
%w(any all none)

Instance Method Summary collapse

Methods inherited from Spree::PromotionRule

#eligibility_errors, for, #to_partial_path

Methods inherited from Base

display_includes

Methods included from Core::Permalinks

#generate_permalink, #save_permalink

Instance Method Details

#actionable?(line_item) ⇒ Boolean

Returns:

  • (Boolean)

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/spree/promotion/rules/taxon.rb', line 47

def actionable?(line_item)
  found = Spree::Classification.where(
    product_id: line_item.variant.product_id,
    taxon_id: rule_taxon_ids_with_children
  ).exists?

  case preferred_match_policy
  when 'any', 'all'
    found
  when 'none'
    !found
  else
    raise "unexpected match policy: #{preferred_match_policy.inspect}"
  end
end

#applicable?(promotable) ⇒ Boolean

Returns:

  • (Boolean)

16
17
18
# File 'app/models/spree/promotion/rules/taxon.rb', line 16

def applicable?(promotable)
  promotable.is_a?(Spree::Order)
end

#eligible?(order, _options = {}) ⇒ Boolean

Returns:

  • (Boolean)

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
# File 'app/models/spree/promotion/rules/taxon.rb', line 20

def eligible?(order, _options = {})
  order_taxons = taxons_in_order(order)

  case preferred_match_policy
  when 'all'
    matches_all = taxons.all? do |rule_taxon|
      order_taxons.where(id: rule_taxon.self_and_descendants.ids).exists?
    end

    unless matches_all
      eligibility_errors.add(:base, eligibility_error_message(:missing_taxon), error_code: :missing_taxon)
    end
  when 'any'
    unless order_taxons.where(id: rule_taxon_ids_with_children).exists?
      eligibility_errors.add(:base, eligibility_error_message(:no_matching_taxons), error_code: :no_matching_taxons)
    end
  when 'none'
    if order_taxons.where(id: rule_taxon_ids_with_children).exists?
      eligibility_errors.add(:base, eligibility_error_message(:has_excluded_taxon), error_code: :has_excluded_taxon)
    end
  else
    raise "unexpected match policy: #{preferred_match_policy.inspect}"
  end

  eligibility_errors.empty?
end

#taxon_ids_stringObject


63
64
65
# File 'app/models/spree/promotion/rules/taxon.rb', line 63

def taxon_ids_string
  taxons.pluck(:id).join(',')
end

#taxon_ids_string=(taxon_ids) ⇒ Object


67
68
69
70
# File 'app/models/spree/promotion/rules/taxon.rb', line 67

def taxon_ids_string=(taxon_ids)
  taxon_ids = taxon_ids.to_s.split(',').map(&:strip)
  self.taxons = Spree::Taxon.find(taxon_ids)
end