Class: SolidusFriendlyPromotions::Conditions::LineItemProduct

Inherits:
SolidusFriendlyPromotions::Condition show all
Includes:
LineItemLevelCondition
Defined in:
app/models/solidus_friendly_promotions/conditions/line_item_product.rb

Overview

A condition to apply a promotion only to line items with or without a chosen product

Constant Summary collapse

MATCH_POLICIES =
%w[include exclude].freeze

Instance Method Summary collapse

Methods included from LineItemLevelCondition

#applicable?, #level

Methods inherited from SolidusFriendlyPromotions::Condition

#applicable?, #eligibility_errors, #level, #to_partial_path, #updateable?

Instance Method Details

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

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/models/solidus_friendly_promotions/conditions/line_item_product.rb', line 25

def eligible?(line_item, _options = {})
  order_includes_product = product_ids.include?(line_item.variant.product_id)
  success = inverse? ? !order_includes_product : order_includes_product

  unless success
    message_code = inverse? ? :has_excluded_product : :no_applicable_products
    eligibility_errors.add(
      :base,
      eligibility_error_message(message_code),
      error_code: message_code
    )
  end

  success
end

#preload_relationsObject



21
22
23
# File 'app/models/solidus_friendly_promotions/conditions/line_item_product.rb', line 21

def preload_relations
  [:products]
end

#product_ids_stringObject



41
42
43
# File 'app/models/solidus_friendly_promotions/conditions/line_item_product.rb', line 41

def product_ids_string
  product_ids.join(",")
end

#product_ids_string=(product_ids) ⇒ Object



45
46
47
# File 'app/models/solidus_friendly_promotions/conditions/line_item_product.rb', line 45

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