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

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

Overview

A rule to limit a promotion based on products in the order. Can require all or any of the products to be present. Valid products either come from assigned product group or are assingned directly to the rule.

Constant Summary collapse

MATCH_POLICIES =
%w(any all none)

Instance Method Summary collapse

Methods inherited from Spree::PromotionRule

#eligibility_errors, #to_partial_path

Instance Method Details

#actionable?(line_item) ⇒ Boolean

Returns:

  • (Boolean)


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

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)


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

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

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

Returns:

  • (Boolean)


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

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

  case preferred_match_policy
  when "all"
    unless eligible_products.all? { |product| order_products(order).include?(product) }
      eligibility_errors.add(:base, eligibility_error_message(:missing_product), error_code: :missing_product)
    end
  when "any"
    unless order_products(order).any? { |product| eligible_products.include?(product) }
      eligibility_errors.add(:base, eligibility_error_message(:no_applicable_products),
                             error_code: :no_applicable_products)
    end
  when "none"
    unless order_products(order).none? { |product| eligible_products.include?(product) }
      eligibility_errors.add(:base, eligibility_error_message(:has_excluded_product),
                             error_code: :has_excluded_product)
    end
  else
    raise "unexpected match policy: #{preferred_match_policy.inspect}"
  end

  eligibility_errors.empty?
end

#eligible_productsObject

scope/association that is used to test eligibility



29
30
31
# File 'app/models/spree/promotion/rules/product.rb', line 29

def eligible_products
  products
end

#preload_relationsObject



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

def preload_relations
  [:products]
end

#product_ids_stringObject



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

def product_ids_string
  product_ids.join(',')
end

#product_ids_string=(product_ids) ⇒ Object



77
78
79
# File 'app/models/spree/promotion/rules/product.rb', line 77

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