Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Add Spree::StockItem.for_store scope; simplify admin controller

Damian Legawiecdcdb67fa3e77
diff --git a/spree/api/app/controllers/spree/api/v3/admin/stock_items_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/stock_items_controller.rb
index c0295e4fcf..c839e22a9f 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/stock_items_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/stock_items_controller.rb
@@ -21,6 +21,13 @@ module Spree
def collection_includes
[:stock_location, :variant]
end
+
+ # `StockItem.for_store` already applies its own `distinct`, and
+ # `id`-asc gives a stable order across edits (variant.position
+ # alone isn't unique — see git blame for the row-jumping bug).
+ def apply_collection_sort(collection)
+ collection.order(Spree::StockItem.arel_table[:id].asc)
+ end
end
end
end
diff --git a/spree/core/app/models/spree/stock_item.rb b/spree/core/app/models/spree/stock_item.rb
index 6df57dc51c..3030d28700 100644
--- a/spree/core/app/models/spree/stock_item.rb
+++ b/spree/core/app/models/spree/stock_item.rb
@@ -38,6 +38,20 @@ module Spree
scope :with_active_stock_location, -> { joins(:stock_location).merge(Spree::StockLocation.active) }
+ # Stock items for products assigned to `store`. Walks
+ # `variant → product → stores`, dedups via `distinct` so a product
+ # in multiple stores doesn't double-count its stock items.
+ #
+ # Used by the admin API as the base scope (`StockItem.for_store`)
+ # so the controller can filter directly by stock_location/variant
+ # without inheriting `Spree::Store#stock_items`'s extra joins or
+ # the variant default ordering.
+ scope :for_store, ->(store) {
+ joins(variant: { product: :stores }).
+ where(spree_stores: { id: store.id }).
+ distinct
+ }
+
def backordered_inventory_units
Spree::InventoryUnit.backordered_for_stock_item(self)
end
diff --git a/spree/core/spec/models/spree/stock_item_spec.rb b/spree/core/spec/models/spree/stock_item_spec.rb
index acba32ffdf..eafd0cdd50 100644
--- a/spree/core/spec/models/spree/stock_item_spec.rb
+++ b/spree/core/spec/models/spree/stock_item_spec.rb
@@ -476,5 +476,35 @@ describe Spree::StockItem, type: :model do
it { expect(stock_items_with_active_location).not_to include(subject) }
end
end
+
+ describe '.for_store' do
+ let(:store) { create(:store) }
+ let(:other_store) { create(:store) }
+ let(:product_in_store) { create(:product, stores: [store]) }
+ let(:product_in_other_store) { create(:product, stores: [other_store]) }
+ let(:product_in_both) { create(:product, stores: [store, other_store]) }
+
+ let!(:in_store_item) do
+ create(:stock_item, variant: product_in_store.master, stock_location: stock_location)
+ end
+ let!(:other_store_item) do
+ create(:stock_item, variant: product_in_other_store.master, stock_location: stock_location)
+ end
+ let!(:shared_item) do
+ create(:stock_item, variant: product_in_both.master, stock_location: stock_location)
+ end
+
+ it 'returns stock items for variants of products assigned to the store' do
+ expect(described_class.for_store(store)).to include(in_store_item, shared_item)
+ end
+
+ it 'excludes stock items for products not assigned to the store' do
+ expect(described_class.for_store(store)).not_to include(other_store_item)
+ end
+
+ it 'returns each row once even when the product belongs to multiple stores' do
+ expect(described_class.for_store(store).where(id: shared_item.id).count).to eq(1)
+ end
+ end
end
end