Class: Spree::Promotion::Rules::Product
Constant Summary
collapse
- MATCH_POLICIES =
%w(any all none)
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#eligibility_errors, for, human_description, #human_description, human_name, #human_name, #key
Instance Attribute Details
#product_ids_to_add ⇒ Object
29
30
31
|
# File 'app/models/spree/promotion/rules/product.rb', line 29
def product_ids_to_add
@product_ids_to_add
end
|
Class Method Details
.additional_permitted_attributes ⇒ Object
16
17
18
|
# File 'app/models/spree/promotion/rules/product.rb', line 16
def self.additional_permitted_attributes
[product_ids: []]
end
|
Instance Method Details
#actionable?(line_item) ⇒ Boolean
69
70
71
72
73
74
75
76
77
78
|
# File 'app/models/spree/promotion/rules/product.rb', line 69
def actionable?(line_item)
case preferred_match_policy
when 'any', 'all'
eligible_product_ids.include? line_item.variant.product_id
when 'none'
eligible_product_ids.exclude? line_item.variant.product_id
else
raise "unexpected match policy: #{preferred_match_policy.inspect}"
end
end
|
#applicable?(promotable) ⇒ Boolean
45
46
47
|
# File 'app/models/spree/promotion/rules/product.rb', line 45
def applicable?(promotable)
promotable.is_a?(Spree::Order)
end
|
#eligible?(order, _options = {}) ⇒ Boolean
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'app/models/spree/promotion/rules/product.rb', line 49
def eligible?(order, _options = {})
return true if eligible_product_ids.empty?
if preferred_match_policy == 'all'
unless eligible_product_ids.all? { |p| order.product_ids.include?(p) }
eligibility_errors.add(:base, eligibility_error_message(:missing_product))
end
elsif preferred_match_policy == 'any'
unless order.product_ids.any? { |p| eligible_product_ids.include?(p) }
eligibility_errors.add(:base, eligibility_error_message(:no_applicable_products))
end
else
unless order.product_ids.none? { |p| eligible_product_ids.include?(p) }
eligibility_errors.add(:base, eligibility_error_message(:has_excluded_product))
end
end
eligibility_errors.empty?
end
|
#eligible_product_ids ⇒ Object
41
42
43
|
# File 'app/models/spree/promotion/rules/product.rb', line 41
def eligible_product_ids
@eligible_product_ids ||= product_promotion_rules.pluck(:product_id)
end
|
#eligible_products ⇒ Object
scope/association that is used to test eligibility
37
38
39
|
# File 'app/models/spree/promotion/rules/product.rb', line 37
def eligible_products
products
end
|
#product_ids_string ⇒ Object
80
81
82
83
84
85
|
# File 'app/models/spree/promotion/rules/product.rb', line 80
def product_ids_string
ActiveSupport::Deprecation.warn(
'Please use `product_ids=` instead.'
)
product_ids.join(',')
end
|
#product_ids_string=(s) ⇒ Object
87
88
89
90
91
92
|
# File 'app/models/spree/promotion/rules/product.rb', line 87
def product_ids_string=(s)
ActiveSupport::Deprecation.warn(
'Please use `product_ids=` instead.'
)
self.product_ids = s
end
|