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.

fix product and variant scopes

Łukasz Adamczykfb28dce04599
diff --git a/api/spec/models/spree/stock_movement_webhooks_spec.rb b/api/spec/models/spree/stock_movement_webhooks_spec.rb
index 337a2d722f..805a5ef305 100644
--- a/api/spec/models/spree/stock_movement_webhooks_spec.rb
+++ b/api/spec/models/spree/stock_movement_webhooks_spec.rb
@@ -204,20 +204,6 @@ describe Spree::StockMovement::Webhooks do
end
end
end
-
- context 'when stock item was in stock' do
- it do
- expect do
- # make in_stock? return false based on track_inventory, the easiest case
- variant.update(track_inventory: false)
- stock_location.stock_movements.new.tap do |stock_movement|
- stock_movement.quantity = 2
- stock_movement.stock_item = stock_location.set_up_stock_item(variant)
- stock_movement.save
- end
- end.not_to emit_webhook_event(event_name, variant)
- end
- end
end
describe 'emitting variant.out_of_stock' do
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb
index a0857f608a..edf3456c42 100644
--- a/core/app/models/spree/variant.rb
+++ b/core/app/models/spree/variant.rb
@@ -71,8 +71,8 @@ module Spree
after_touch :clear_in_stock_cache
- scope :in_stock, -> { joins(:stock_items).where("#{Spree::StockItem.table_name}.count_on_hand > ? OR #{Spree::Variant.table_name}.track_inventory = ?", 0, false) }
- scope :backorderable, -> { joins(:stock_items).where(spree_stock_items: { backorderable: true }) }
+ scope :in_stock, -> { left_joins(:stock_items).where("#{Spree::StockItem.table_name}.count_on_hand > ? OR #{Spree::Variant.table_name}.track_inventory = ?", 0, false) }
+ scope :backorderable, -> { left_joins(:stock_items).where(spree_stock_items: { backorderable: true }) }
scope :in_stock_or_backorderable, -> { in_stock.or(backorderable) }
scope :eligible, -> {
diff --git a/core/spec/models/spree/product_spec.rb b/core/spec/models/spree/product_spec.rb
index bada43593d..2f6a1b4bfb 100644
--- a/core/spec/models/spree/product_spec.rb
+++ b/core/spec/models/spree/product_spec.rb
@@ -103,8 +103,8 @@ describe Spree::Product, type: :model do
end
it 'saves the master' do
- expect(product.master).to receive(:save!)
product.save
+ expect(product.master.reload.sku).to eq('Something changed')
end
end
@@ -933,7 +933,7 @@ describe Spree::Product, type: :model do
context 'when all non-master variant stock items have track_inventory = false' do
before { variant.update(track_inventory: false) }
- it { expect(subject).to eq(true) }
+ it { expect(subject).to be(true) }
end
context 'when all non-master variant stock items have track_inventory = true' do
diff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rb
index dfd1dfaacc..02f76fddfa 100644
--- a/core/spec/models/spree/variant_spec.rb
+++ b/core/spec/models/spree/variant_spec.rb
@@ -108,6 +108,32 @@ describe Spree::Variant, type: :model do
end
end
+ describe 'after_update_commit :handle_track_inventory_change' do
+ let!(:product) { create(:product, stores: [store]) }
+
+ context 'when not tracking inventory' do
+ subject { variant.update!(track_inventory: false) }
+
+ let!(:variant) { create(:variant, product: product, track_inventory: true) }
+ let!(:stock_item) { create(:stock_item, variant: variant, count_on_hand: 100) }
+
+ it 'deletes stock items' do
+ expect { subject }.to change(variant.stock_items, :count).from(2).to(0)
+ end
+ end
+
+ context 'when tracking inventory' do
+ subject { variant.update!(track_inventory: true) }
+
+ let!(:variant) { create(:variant, product: product, track_inventory: false) }
+ let!(:stock_item) { create(:stock_item, variant: variant, count_on_hand: 100) }
+
+ it 'keeps stock items' do
+ expect { subject }.to_not change(variant.stock_items, :count)
+ end
+ end
+ end
+
describe 'scope' do
describe '.eligible' do
context 'when only master variants' do