Class: Spree::Promotion::Rules::Product

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

Constant Summary collapse

MATCH_POLICIES =
%w(any all none)

Instance Method Summary collapse

Methods inherited from Spree::PromotionRule

#eligibility_errors, for

Methods inherited from Base

page

Methods included from Spree::Preferences::Preferable

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

Instance Method Details

#actionable?(line_item) ⇒ Boolean

Returns:

  • (Boolean)

42
43
44
45
46
47
48
49
50
51
# File 'app/models/spree/promotion/rules/product.rb', line 42

def actionable?(line_item)
  case preferred_match_policy
  when 'any', 'all'
    product_ids.include? line_item.variant.product_id
  when 'none'
    product_ids.exclude? line_item.variant.product_id
  else
    raise "unexpected match policy: #{preferred_match_policy.inspect}"
  end
end

#applicable?(promotable) ⇒ Boolean

Returns:

  • (Boolean)

18
19
20
# File 'app/models/spree/promotion/rules/product.rb', line 18

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

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

Returns:

  • (Boolean)

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/models/spree/promotion/rules/product.rb', line 22

def eligible?(order, options = {})
  return true if eligible_products.empty?

  if preferred_match_policy == 'all'
    unless eligible_products.all? {|p| order.products.include?(p) }
      eligibility_errors.add(:base, eligibility_error_message(:missing_product))
    end
  elsif preferred_match_policy == 'any'
    unless order.products.any? {|p| eligible_products.include?(p) }
      eligibility_errors.add(:base, eligibility_error_message(:no_applicable_products))
    end
  else
    unless order.products.none? {|p| eligible_products.include?(p) }
      eligibility_errors.add(:base, eligibility_error_message(:has_excluded_product))
    end
  end

  eligibility_errors.empty?
end

#eligible_productsObject

scope/association that is used to test eligibility


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

def eligible_products
  products
end

#product_ids_stringObject


53
54
55
# File 'app/models/spree/promotion/rules/product.rb', line 53

def product_ids_string
  product_ids.join(',')
end

#product_ids_string=(s) ⇒ Object


57
58
59
# File 'app/models/spree/promotion/rules/product.rb', line 57

def product_ids_string=(s)
  self.product_ids = s.to_s.split(',').map(&:strip)
end