Class: SolidusFriendlyPromotions::Conditions::Taxon

Inherits:
SolidusFriendlyPromotions::Condition show all
Includes:
LineItemApplicableOrderCondition
Defined in:
app/models/solidus_friendly_promotions/conditions/taxon.rb

Constant Summary collapse

MATCH_POLICIES =
%w[any all none].freeze

Instance Method Summary collapse

Methods included from LineItemApplicableOrderCondition

#applicable?, #eligible?, included, #level

Methods inherited from SolidusFriendlyPromotions::Condition

#applicable?, #eligibility_errors, #eligible?, #level, #to_partial_path

Instance Method Details

#line_item_eligible?(line_item) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/models/solidus_friendly_promotions/conditions/taxon.rb', line 57

def line_item_eligible?(line_item)
  # The order level eligibility check happens first, and if none of the taxons
  # are in the order, then no line items should be available to check.
  raise "This should not happen" if preferred_match_policy == "none"

  raise "unexpected match policy: #{preferred_match_policy.inspect}" unless preferred_match_policy.in?(MATCH_POLICIES)

  Spree::Classification.where(
    product_id: line_item.variant.product_id,
    taxon_id: condition_taxon_ids_with_children
  ).exists?
end

#order_eligible?(order) ⇒ Boolean

Returns:

  • (Boolean)


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
# File 'app/models/solidus_friendly_promotions/conditions/taxon.rb', line 22

def order_eligible?(order)
  order_taxons = taxons_in_order(order)

  case preferred_match_policy
  when "all"
    matches_all = taxons.all? do |condition_taxon|
      order_taxons.where(id: condition_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: condition_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: condition_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

#preload_relationsObject



12
13
14
# File 'app/models/solidus_friendly_promotions/conditions/taxon.rb', line 12

def preload_relations
  [:taxons]
end

#taxon_ids_stringObject



70
71
72
# File 'app/models/solidus_friendly_promotions/conditions/taxon.rb', line 70

def taxon_ids_string
  taxon_ids.join(",")
end

#taxon_ids_string=(taxon_ids) ⇒ Object



74
75
76
# File 'app/models/solidus_friendly_promotions/conditions/taxon.rb', line 74

def taxon_ids_string=(taxon_ids)
  self.taxon_ids = taxon_ids.to_s.split(",").map(&:strip)
end

#updateable?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'app/models/solidus_friendly_promotions/conditions/taxon.rb', line 78

def updateable?
  true
end