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

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

Constant Summary

MATCH_POLICIES =
%w(any)

Instance Method Summary collapse

Methods inherited from Spree::PromotionRule

#eligibility_errors, for, #to_partial_path

Methods inherited from Base

display_includes, #initialize_preference_defaults, page, preference

Methods included from Spree::Preferences::Preferable

#admin_form_preference_names, #default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference

Instance Method Details

#actionable?(line_item) ⇒ Boolean

Returns:

  • (Boolean)


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

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)


9
10
11
# File 'app/models/spree/promotion/rules/option_value.rb', line 9

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

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

Returns:

  • (Boolean)


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

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

#preferred_eligible_valuesObject



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

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