Sha256: e1ea890aa49c41e55636128a64d3a8b51dc41fb1aff16b0d1436ec352466c962

Contents?: true

Size: 1.79 KB

Versions: 43

Compression:

Stored size: 1.79 KB

Contents

# 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

43 entries across 43 versions & 1 rubygems

Version Path
solidus_core-2.5.2 app/models/spree/variant_property_rule.rb
solidus_core-2.5.1 app/models/spree/variant_property_rule.rb
solidus_core-2.5.0 app/models/spree/variant_property_rule.rb
solidus_core-2.5.0.rc1 app/models/spree/variant_property_rule.rb
solidus_core-2.5.0.beta2 app/models/spree/variant_property_rule.rb
solidus_core-2.5.0.beta1 app/models/spree/variant_property_rule.rb
solidus_core-2.4.2 app/models/spree/variant_property_rule.rb
solidus_core-2.3.1 app/models/spree/variant_property_rule.rb
solidus_core-2.4.1 app/models/spree/variant_property_rule.rb
solidus_core-2.2.2 app/models/spree/variant_property_rule.rb
solidus_core-2.1.1 app/models/spree/variant_property_rule.rb
solidus_core-2.0.3 app/models/spree/variant_property_rule.rb
solidus_core-1.4.2 app/models/spree/variant_property_rule.rb
solidus_core-1.3.2 app/models/spree/variant_property_rule.rb
solidus_core-2.4.0 app/models/spree/variant_property_rule.rb
solidus_core-2.4.0.rc1 app/models/spree/variant_property_rule.rb
solidus_core-2.4.0.beta1 app/models/spree/variant_property_rule.rb
solidus_core-2.3.0 app/models/spree/variant_property_rule.rb
solidus_core-2.3.0.rc3 app/models/spree/variant_property_rule.rb
solidus_core-2.3.0.rc2 app/models/spree/variant_property_rule.rb