Sha256: eb50bdd1dbdbbbe120af5ab43153ec65010c9cc4851824f520a7ba8623168547

Contents?: true

Size: 1.33 KB

Versions: 4

Compression:

Stored size: 1.33 KB

Contents

module SpreeMultiVendor::Spree::VariantDecorator
  def self.prepended(base)
    base.before_create :assign_vendor

    base.scope :for_vendor_user, ->(user) { where(vendor_id: user.vendor_ids) }
  end

  def vendor
    @vendor ||= if self.class.reflect_on_association(:vendor) && self[:vendor_id].present?
                  ::Spree::Vendor.unscoped.find(self[:vendor_id])
                elsif Spree::Product.reflect_on_association(:vendor) && product.vendor_id.present?
                  product.vendor
                end
  end

  def vendor_id
    @vendor_id ||= if self.class.reflect_on_association(:vendor) && self[:vendor_id].present?
                     self[:vendor_id]
                   elsif Spree::Product.reflect_on_association(:vendor) && product.vendor_id.present?
                     product.vendor_id
                   end
  end

  private

  def assign_vendor
    return if !product.class.reflect_on_association(:vendor) || product.vendor.blank?
    return if !self.class.reflect_on_association(:vendor) || self[:vendor_id].present?

    self.vendor = product.vendor
  end

  def create_stock_items
    Spree::StockLocation.where(propagate_all_variants: true, vendor_id: vendor_id).each do |stock_location|
      stock_location.propagate_variant(self)
    end
  end
end

Spree::Variant.prepend SpreeMultiVendor::Spree::VariantDecorator

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
spree_multi_vendor-2.5.0 app/models/spree_multi_vendor/spree/variant_decorator.rb
spree_multi_vendor-2.4.1 app/models/spree_multi_vendor/spree/variant_decorator.rb
atkhayar-dev-spree-multi-vendor-0.0.0 app/models/spree_multi_vendor/spree/variant_decorator.rb
spree_multi_vendor-2.4.0 app/models/spree_multi_vendor/spree/variant_decorator.rb