Sha256: 7bd80dc8ec06db8f815beb385c4e06708195c35f37d53af09618dcfd3a3ebccf

Contents?: true

Size: 1.82 KB

Versions: 42

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

# The reason for variant properties not being associated with variants
# (either directly or through an association table) is performance.
#
# Variant properties are intended to be applied to a group of variants based
# on their option values. If there were thousands of variants that shared the
# same option value, attempting to associate a variant property with that
# group of variants would be problematic in terms of performance.
#
# An added benefit to this approach is not having to associate existing variant
# properties with newly created variants. If the variant has the option values
# targeted by the rule, the properties will automatically apply to the variant.
module Spree
  class VariantPropertyRule < Spree::Base
    belongs_to :product, touch: true

    has_many :values, class_name: 'Spree::VariantPropertyRuleValue', dependent: :destroy
    has_many :properties, through: :values
    has_many :conditions, class_name: 'Spree::VariantPropertyRuleCondition', dependent: :destroy
    has_many :option_values, through: :conditions

    accepts_nested_attributes_for :values, allow_destroy: true, reject_if: lambda { |val| val[:property_name].blank? }

    # Checks whether the provided ids are the same as the rule's
    # condition's option value ids.
    #
    # @param option_value_ids [Array<Integer>] list of option value ids
    # @return [Boolean]
    def matches_option_value_ids?(option_value_ids)
      self.option_value_ids.sort == option_value_ids.sort
    end

    # Checks whether the rule applies to the variant by
    # checking the rule's conditions against the variant's
    # option values.
    #
    # @param variant [Spree::Variant] variant to check
    # @return [Boolean]
    def applies_to_variant?(variant)
      (option_value_ids & variant.option_value_ids).present?
    end
  end
end

Version data entries

42 entries across 42 versions & 2 rubygems

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