Sha256: a2a87177927b9c76efdc3fd2d328bedb0dbc51268e3255e5c339defb92219691

Contents?: true

Size: 1.42 KB

Versions: 6

Compression:

Stored size: 1.42 KB

Contents

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

    auto_strip_attributes :value

    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?
        # don't use `find_by :name` to workaround globalize/globalize#423 bug
        self.property = Property.where(name: name).first_or_create(presentation: name)
      end
    end

    protected

    def param_candidate
      value
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
spree_core-4.5.5 app/models/spree/product_property.rb
spree_core-4.5.4 app/models/spree/product_property.rb
spree_core-4.5.3 app/models/spree/product_property.rb
spree_core-4.5.2 app/models/spree/product_property.rb
spree_core-4.5.1 app/models/spree/product_property.rb
spree_core-4.5.0 app/models/spree/product_property.rb