Sha256: 95495a5c963d409a668fa27c90bc12f58b7d55033e709d76c08adc980e48c597

Contents?: true

Size: 1.32 KB

Versions: 34

Compression:

Stored size: 1.32 KB

Contents

module Spree
  class Promotion
    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).empty?
        end

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

        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

34 entries across 34 versions & 1 rubygems

Version Path
spree_core-3.1.14 app/models/spree/promotion/rules/option_value.rb
spree_core-3.1.13 app/models/spree/promotion/rules/option_value.rb
spree_core-3.1.12 app/models/spree/promotion/rules/option_value.rb
spree_core-3.1.11 app/models/spree/promotion/rules/option_value.rb
spree_core-3.1.10 app/models/spree/promotion/rules/option_value.rb
spree_core-3.1.9 app/models/spree/promotion/rules/option_value.rb
spree_core-3.1.8 app/models/spree/promotion/rules/option_value.rb
spree_core-3.1.7 app/models/spree/promotion/rules/option_value.rb
spree_core-3.1.6 app/models/spree/promotion/rules/option_value.rb
spree_core-3.1.5 app/models/spree/promotion/rules/option_value.rb
spree_core-3.1.4 app/models/spree/promotion/rules/option_value.rb
spree_core-3.1.3 app/models/spree/promotion/rules/option_value.rb
spree_core-3.1.2 app/models/spree/promotion/rules/option_value.rb
spree_core-3.0.10 app/models/spree/promotion/rules/option_value.rb
spree_core-3.1.1 app/models/spree/promotion/rules/option_value.rb
spree_core-3.1.0 app/models/spree/promotion/rules/option_value.rb
spree_core-3.1.0.rc4 app/models/spree/promotion/rules/option_value.rb
spree_core-3.0.9 app/models/spree/promotion/rules/option_value.rb
spree_core-3.1.0.rc3 app/models/spree/promotion/rules/option_value.rb
spree_core-3.1.0.rc2 app/models/spree/promotion/rules/option_value.rb