Class: Spree::Promotion::Rules::OptionValue

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

Constant Summary collapse

MATCH_POLICIES =
%w(any)

Instance Method Summary collapse

Methods inherited from Spree::PromotionRule

#eligibility_errors, #preload_relations, #to_partial_path

Instance Method Details

#actionable?(line_item) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
# File 'app/models/spree/promotion/rules/option_value.rb', line 22

def actionable?(line_item)
  pid = line_item.product.id
  ovids = line_item.variant.option_values.pluck(:id)

  product_ids.include?(pid) && (value_ids(pid) & ovids).present?
end

#applicable?(promotable) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'app/models/spree/promotion/rules/option_value.rb', line 11

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

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

Returns:

  • (Boolean)


15
16
17
18
19
20
# File 'app/models/spree/promotion/rules/option_value.rb', line 15

def eligible?(promotable, _options = {})
  case preferred_match_policy
  when 'any'
    promotable.line_items.any? { |item| actionable?(item) }
  end
end

#preferred_eligible_valuesObject



29
30
31
32
33
34
35
36
# File 'app/models/spree/promotion/rules/option_value.rb', line 29

def preferred_eligible_values
  values = preferences[:eligible_values] || {}
  Hash[values.keys.map(&:to_i).zip(
    values.values.map do |value|
      (value.is_a?(Array) ? value : value.split(",")).map(&:to_i)
    end
  )]
end