Sha256: 1f904e1ec7fc75aff33c273f8eb3931ff0d67e72d60bfe1122e3535fdea66d1b

Contents?: true

Size: 1.67 KB

Versions: 7

Compression:

Stored size: 1.67 KB

Contents

module Spree
  class ProductProperty < Spree::Base
    include Spree::FilterParam
    include TranslatableResource

    TRANSLATABLE_FIELDS = %i[value filter_param].freeze
    translates(*TRANSLATABLE_FIELDS)

    self::Translation.class_eval do
      auto_strip_attributes :value
    end

    acts_as_list scope: :product

    with_options inverse_of: :product_properties do
      belongs_to :product, touch: true, class_name: 'Spree::Product'
      belongs_to :property, touch: true, class_name: 'Spree::Property'
    end

    validates :property, presence: true
    validates :property_id, uniqueness: { scope: :product_id }
    validates :value, presence: true

    default_scope { order(:position) }

    scope :filterable, -> { joins(:property).where(Property.table_name => { filterable: true }) }
    scope :for_products, ->(products) { joins(:product).merge(products) }

    self.whitelisted_ransackable_attributes = ['value', 'filter_param']
    self.whitelisted_ransackable_associations = ['property']

    # virtual attributes for use with AJAX completion stuff
    delegate :name, :presentation, to: :property, prefix: true, allow_nil: true

    def property_name=(name)
      ActiveSupport::Deprecation.warn(<<-DEPRECATION, caller)
        `ProductProperty#property_name=` is deprecated and will be removed in Spree 5.0.
      DEPRECATION
      if name.present?
        self.property = if Property.where(name: name).exists?
                          Property.where(name: name).first
                        else
                          Property.create(name: name, presentation: name)
                        end
      end
    end

    protected

    def param_candidate
      value
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
spree_core-4.6.6 app/models/spree/product_property.rb
spree_core-4.6.5 app/models/spree/product_property.rb
spree_core-4.6.4 app/models/spree/product_property.rb
spree_core-4.6.3 app/models/spree/product_property.rb
spree_core-4.6.2 app/models/spree/product_property.rb
spree_core-4.6.1 app/models/spree/product_property.rb
spree_core-4.6.0 app/models/spree/product_property.rb