Sha256: 3c11227f805f2cdbeb7723ec52f419b3cfb1d85039aac394e255597d14be24be

Contents?: true

Size: 899 Bytes

Versions: 4

Compression:

Stored size: 899 Bytes

Contents

# frozen_string_literal: true

module SolidusFriendlyPromotions
  module Conditions
    class LineItemOptionValue < Condition
      include LineItemLevelCondition

      preference :eligible_values, :hash

      def eligible?(line_item, _options = {})
        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] || {}
        values.keys.map(&:to_i).zip(
          values.values.map do |value|
            (value.is_a?(Array) ? value : value.split(",")).map(&:to_i)
          end
        ).to_h
      end

      private

      def product_ids
        preferred_eligible_values.keys
      end

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
solidus_friendly_promotions-1.9.3 app/models/solidus_friendly_promotions/conditions/line_item_option_value.rb
solidus_friendly_promotions-1.9.2 app/models/solidus_friendly_promotions/conditions/line_item_option_value.rb
solidus_friendly_promotions-1.9.1 app/models/solidus_friendly_promotions/conditions/line_item_option_value.rb
solidus_friendly_promotions-1.9.0 app/models/solidus_friendly_promotions/conditions/line_item_option_value.rb