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.

Keep a movement readable after its stock level is deleted

Łukasz Adamczykb3dfedafc794
diff --git a/spree/core/app/models/spree/stock_movement.rb b/spree/core/app/models/spree/stock_movement.rb
index a2505f5fde..a68e605a1a 100644
--- a/spree/core/app/models/spree/stock_movement.rb
+++ b/spree/core/app/models/spree/stock_movement.rb
@@ -16,7 +16,11 @@ module Spree
publishes_lifecycle_events
- belongs_to :stock_level, class_name: 'Spree::StockLevel', inverse_of: :stock_movements
+ # `with_deleted` because the ledger outlives what it describes: a level is
+ # soft-deleted whenever a product save stops listing its location, and
+ # without this the default scope propagates into every join — taking the
+ # history of those units out of `for_store` and out of the API with it.
+ belongs_to :stock_level, -> { with_deleted }, class_name: 'Spree::StockLevel', inverse_of: :stock_movements
# @deprecated Replaced by the concrete cause keys below; dropped in 6.1.
belongs_to :originator, polymorphic: true, optional: true
diff --git a/spree/core/spec/models/spree/stock_movement_spec.rb b/spree/core/spec/models/spree/stock_movement_spec.rb
index 9c6f10af8a..9d19f366e6 100644
--- a/spree/core/spec/models/spree/stock_movement_spec.rb
+++ b/spree/core/spec/models/spree/stock_movement_spec.rb
@@ -14,6 +14,20 @@ describe Spree::StockMovement, type: :model do
end
end
+ describe '.for_store' do
+ let(:store) { Spree::Store.default || create(:store) }
+
+ # A level is soft-deleted whenever a product save stops listing its
+ # location, and the ledger has to survive that: the units it accounts for
+ # are the ones a merchant most wants explained afterwards.
+ it 'still finds movements whose stock level was soft-deleted' do
+ movement = create(:stock_movement, kind: 'received', quantity: 4)
+ movement.stock_level.destroy
+
+ expect(described_class.for_store(store)).to include(movement)
+ end
+ end
+
describe 'Constants' do
describe 'KINDS' do
it 'names every movement kind' do