Sha256: 1c830a4d95429acf1ffe786d0c207a8eeb362e725592cbdb2db6454a43202f6f

Contents?: true

Size: 1.94 KB

Versions: 9

Compression:

Stored size: 1.94 KB

Contents

module SpreeMultiVendor::Spree::LineItemDecorator
  def self.prepended(base)
    base.scope :for_vendor, ->(vendor) do
      if ::Spree::LineItem.reflect_on_association(:vendor)
        where(vendor_id: vendor.id)
      elsif ::Spree::Product.reflect_on_association(:vendor)
        joins(variant: :product).where('spree_products.vendor_id' => vendor.id)
      elsif ::Spree::Variant.reflect_on_association(:vendor)
        joins(:variant).where('spree_variants.vendor_id' => vendor.id)
      else
        none
      end
    end
    base.scope :not_for_vendor, ->(vendor) do
      if ::Spree::Product.reflect_on_association(:vendor)
        where.not(vendor_id: vendor.id)
      elsif ::Spree::Product.reflect_on_association(:vendor)
        joins(variant: :product).where.not('spree_products.vendor_id' => vendor.id)
      elsif ::Spree::Variant.reflect_on_association(:vendor)
        joins(:variant).where.not('spree_variants.vendor_id' => vendor.id)
      else
        none
      end
    end
  end

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

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

Spree::LineItem.prepend SpreeMultiVendor::Spree::LineItemDecorator

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
spree_multi_vendor-2.5.0 app/models/spree_multi_vendor/spree/line_item_decorator.rb
spree_multi_vendor-2.4.1 app/models/spree_multi_vendor/spree/line_item_decorator.rb
atkhayar-dev-spree-multi-vendor-0.0.0 app/models/spree_multi_vendor/spree/line_item_decorator.rb
spree_multi_vendor-2.4.0 app/models/spree_multi_vendor/spree/line_item_decorator.rb
spree_multi_vendor-2.3.0 app/models/spree_multi_vendor/spree/line_item_decorator.rb
spree_multi_vendor-2.2.0 app/models/spree_multi_vendor/spree/line_item_decorator.rb
spree_multi_vendor-2.1.0 app/models/spree_multi_vendor/spree/line_item_decorator.rb
spree_multi_vendor-2.0.2 app/models/spree_multi_vendor/spree/line_item_decorator.rb
spree_multi_vendor-2.0.1 app/models/spree_multi_vendor/spree/line_item_decorator.rb