Sha256: 6bcd9d1abe733d3b8a6c578eb5c9ad1de93a79d745ed75c66367239363119543

Contents?: true

Size: 1.26 KB

Versions: 43

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

module Spree
  class Promotion < Spree::Base
    module Rules
      class OptionValue < PromotionRule
        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)
          pid = line_item.product.id
          ovids = line_item.variant.option_values.pluck(:id)

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

        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

        private

        def product_ids
          preferred_eligible_values.keys
        end

        def value_ids(product_id)
          preferred_eligible_values[product_id]
        end
      end
    end
  end
end

Version data entries

43 entries across 43 versions & 2 rubygems

Version Path
solidus_core-2.9.6 app/models/spree/promotion/rules/option_value.rb
solidus_core-2.8.6 app/models/spree/promotion/rules/option_value.rb
solidus_core-2.9.5 app/models/spree/promotion/rules/option_value.rb
solidus_core-2.9.4 app/models/spree/promotion/rules/option_value.rb
solidus_core-2.6.6 app/models/spree/promotion/rules/option_value.rb
solidus_core-2.7.4 app/models/spree/promotion/rules/option_value.rb
solidus_core-2.8.5 app/models/spree/promotion/rules/option_value.rb
solidus_core-2.9.3 app/models/spree/promotion/rules/option_value.rb
solidus_core-2.9.2 app/models/spree/promotion/rules/option_value.rb
solidus_core-2.10.0.beta1 app/models/spree/promotion/rules/option_value.rb
solidus_core-2.7.3 app/models/spree/promotion/rules/option_value.rb
solidus_core-2.6.5 app/models/spree/promotion/rules/option_value.rb
solidus_core-2.9.1 app/models/spree/promotion/rules/option_value.rb
solidus_core-2.9.0 app/models/spree/promotion/rules/option_value.rb
solidus_core-2.9.0.rc.1 app/models/spree/promotion/rules/option_value.rb
solidus_core-2.8.4 app/models/spree/promotion/rules/option_value.rb
solidus_core-2.8.3 app/models/spree/promotion/rules/option_value.rb
solidus_core-2.7.2 app/models/spree/promotion/rules/option_value.rb
solidus_core-2.6.4 app/models/spree/promotion/rules/option_value.rb
solidus_core-2.8.2 app/models/spree/promotion/rules/option_value.rb