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

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

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary

MATCH_POLICIES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

%w(any all none)

Instance Method Summary collapse

Methods inherited from Spree::PromotionRule

#eligibility_errors, for

Methods inherited from Base

display_includes, #initialize_preference_defaults, page, preference

Methods included from Spree::Preferences::Preferable

#default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference

Instance Method Details

#actionable?(line_item) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/models/spree/promotion/rules/taxon.rb', line 45

def actionable?(line_item)
  case preferred_match_policy
  when 'any', 'all'
    taxon_product_ids.include?(line_item.variant.product_id)
  when 'none'
    taxon_product_ids.exclude? line_item.variant.product_id
  else
    # Change this to an exception in a future version of Solidus
    warn_invalid_match_policy(assume: 'any')
    taxon_product_ids.include?(line_item.variant.product_id)
  end
end

#applicable?(promotable) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


14
15
16
# File 'app/models/spree/promotion/rules/taxon.rb', line 14

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

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


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

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

  case preferred_match_policy
  when 'all'
    unless (taxons.to_a - order_taxons).empty?
      eligibility_errors.add(:base, eligibility_error_message(:missing_taxon))
    end
  when 'any'
    unless taxons.any?{ |taxon| order_taxons.include? taxon }
      eligibility_errors.add(:base, eligibility_error_message(:no_matching_taxons))
    end
  when 'none'
    unless taxons.none?{ |taxon| order_taxons.include? taxon }
      eligibility_errors.add(:base, eligibility_error_message(:has_excluded_taxon))
    end
  else
    # Change this to an exception in a future version of Solidus
    warn_invalid_match_policy(assume: 'any')
    unless taxons.any? { |taxon| order_taxons.include? taxon }
      eligibility_errors.add(:base, eligibility_error_message(:no_matching_taxons))
    end
  end

  eligibility_errors.empty?
end

#taxon_ids_stringObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



58
59
60
# File 'app/models/spree/promotion/rules/taxon.rb', line 58

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

#taxon_ids_string=(s) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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