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

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

Constant Summary collapse

MATCH_POLICIES =

Preferences

%w(any all none)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Spree::PromotionRule

#eligibility_errors, for, #human_description, #human_name, #key

Instance Attribute Details

#product_ids_to_addObject

Attributes



25
26
27
# File 'app/models/spree/promotion/rules/product.rb', line 25

def product_ids_to_add
  @product_ids_to_add
end

Instance Method Details

#actionable?(line_item) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
70
# File 'app/models/spree/promotion/rules/product.rb', line 61

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)


37
38
39
# File 'app/models/spree/promotion/rules/product.rb', line 37

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

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

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/spree/promotion/rules/product.rb', line 41

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



33
34
35
# File 'app/models/spree/promotion/rules/product.rb', line 33

def eligible_products
  products
end

#product_ids_stringObject



72
73
74
75
76
77
# File 'app/models/spree/promotion/rules/product.rb', line 72

def product_ids_string
  ActiveSupport::Deprecation.warn(
    'Please use `product_ids=` instead.'
  )
  product_ids.join(',')
end

#product_ids_string=(s) ⇒ Object



79
80
81
82
83
84
# File 'app/models/spree/promotion/rules/product.rb', line 79

def product_ids_string=(s)
  ActiveSupport::Deprecation.warn(
    'Please use `product_ids=` instead.'
  )
  self.product_ids = s
end