Sha256: 3599dd027edff6076bccd5d657777fabec96f4093c73bf645ed182ea058ea86d

Contents?: true

Size: 1.27 KB

Versions: 22

Compression:

Stored size: 1.27 KB

Contents

module Spree
  class Promotion
    module Rules
      module OptionValueWithNumerificationSupport
        def preferred_eligible_values
          values = super || {}
          Hash[values.keys.zip(
            values.values.map do |v|
              (v.is_a?(Array) ? v : v.split(','))
            end
          )]
        end
      end

      class OptionValue < PromotionRule
        prepend OptionValueWithNumerificationSupport

        MATCH_POLICIES = %w(any)
        preference :match_policy, :string, default: MATCH_POLICIES.first
        preference :eligible_values, :hash

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

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

        def actionable?(line_item)
          product_id = line_item.product_id
          option_values_ids = line_item.variant.option_value_ids
          eligible_product_ids = preferred_eligible_values.keys
          eligible_value_ids = preferred_eligible_values[product_id]

          eligible_product_ids.include?(product_id) && (eligible_value_ids & option_values_ids).present?
        end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
spree_core-4.10.1 app/models/spree/promotion/rules/option_value.rb
spree_core-4.10.0 app/models/spree/promotion/rules/option_value.rb
spree_core-4.9.0 app/models/spree/promotion/rules/option_value.rb
spree_core-4.8.3 app/models/spree/promotion/rules/option_value.rb
spree_core-4.8.2 app/models/spree/promotion/rules/option_value.rb
spree_core-4.7.3 app/models/spree/promotion/rules/option_value.rb
spree_core-4.6.6 app/models/spree/promotion/rules/option_value.rb
spree_core-4.5.5 app/models/spree/promotion/rules/option_value.rb
spree_core-4.5.4 app/models/spree/promotion/rules/option_value.rb
spree_core-4.6.5 app/models/spree/promotion/rules/option_value.rb
spree_core-4.7.2 app/models/spree/promotion/rules/option_value.rb
spree_core-4.7.1 app/models/spree/promotion/rules/option_value.rb
spree_core-4.6.4 app/models/spree/promotion/rules/option_value.rb
spree_core-4.7.0 app/models/spree/promotion/rules/option_value.rb
spree_core-4.6.3 app/models/spree/promotion/rules/option_value.rb
spree_core-4.6.2 app/models/spree/promotion/rules/option_value.rb
spree_core-4.6.1 app/models/spree/promotion/rules/option_value.rb
spree_core-4.6.0 app/models/spree/promotion/rules/option_value.rb
spree_core-4.5.3 app/models/spree/promotion/rules/option_value.rb
spree_core-4.5.2 app/models/spree/promotion/rules/option_value.rb