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.

Remove Variant.full_in_stock. Add Variant#in_stock_or_backorderable?.

Sebastian Palma6469fe0316b2
diff --git a/api/app/models/spree/api/webhooks/stock_item_decorator.rb b/api/app/models/spree/api/webhooks/stock_item_decorator.rb
index 44393648ae..e957eeddd7 100644
--- a/api/app/models/spree/api/webhooks/stock_item_decorator.rb
+++ b/api/app/models/spree/api/webhooks/stock_item_decorator.rb
@@ -9,7 +9,7 @@ module Spree
private
def queue_webhooks_requests_for_variant_backorderable!
- was_out_of_stock = !variant.full_in_stock?
+ was_out_of_stock = !variant.in_stock_or_backorderable?
was_not_backorderable = !variant_backorderable?
yield
touch # changes must be reflected before instantiating the serializer
diff --git a/api/app/models/spree/api/webhooks/stock_movement_decorator.rb b/api/app/models/spree/api/webhooks/stock_movement_decorator.rb
index c89785503c..8001fa58c2 100644
--- a/api/app/models/spree/api/webhooks/stock_movement_decorator.rb
+++ b/api/app/models/spree/api/webhooks/stock_movement_decorator.rb
@@ -17,9 +17,9 @@ module Spree
private
def queue_webhooks_requests_for_variant_back_in_stock!
- variant_was_out_of_stock = !variant.full_in_stock?
+ variant_was_out_of_stock = !variant.in_stock_or_backorderable?
yield
- if variant_was_out_of_stock && variant.full_in_stock?
+ if variant_was_out_of_stock && variant.in_stock_or_backorderable?
variant.queue_webhooks_requests!('variant.back_in_stock')
end
end
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb
index a39b5adf72..301c9ee4cb 100644
--- a/core/app/models/spree/variant.rb
+++ b/core/app/models/spree/variant.rb
@@ -83,14 +83,6 @@ module Spree
)
}
- scope :full_in_stock, -> do
- joins(:stock_items).where(<<~SQL)
- #{Spree::StockItem.table_name}.count_on_hand > 0 OR
- #{Spree::Variant.table_name}.track_inventory = FALSE OR
- #{Spree::StockItem.table_name}.backorderable = TRUE
- SQL
- end
-
scope :not_discontinued, -> do
where(
arel_table[:discontinue_on].eq(nil).or(
@@ -140,8 +132,8 @@ module Spree
!discontinued? && product.available?
end
- def full_in_stock?
- Spree::Variant.full_in_stock.exists?(id: id)
+ def in_stock_or_backorderable?
+ self.class.in_stock_or_backorderable.exists?(id: id)
end
def tax_category
diff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rb
index 77548e8a07..ec5da032d0 100644
--- a/core/spec/models/spree/variant_spec.rb
+++ b/core/spec/models/spree/variant_spec.rb
@@ -131,54 +131,6 @@ describe Spree::Variant, type: :model do
end
end
- describe '.full_in_stock' do
- let!(:variant) { create(:variant) }
-
- context 'when variant has no stock items' do
- before { Spree::StockItem.delete_all }
-
- it { expect(described_class.full_in_stock).to eq([]) }
- end
-
- context 'when variant has stock items' do
- let!(:variant2) { create(:variant) }
-
- before { Spree::StockItem.update_all(backorderable: false) }
-
- context 'when variant stock items count_on_hand > 0' do
- before do
- variant.stock_items.first.set_count_on_hand(1)
- variant2.stock_items.first.set_count_on_hand(0)
- end
-
- it { expect(described_class.full_in_stock).to include(variant) }
- it { expect(described_class.full_in_stock).not_to include(variant2) }
- end
-
- context 'when variant stock items count_on_hand <= 0' do
- before { variant.stock_items.first.set_count_on_hand(0) }
-
- it { expect(described_class.full_in_stock).not_to include(variant) }
-
- context 'when variant track_inventory = false' do
- before { variant.update(track_inventory: false) }
-
- it { expect(described_class.full_in_stock).to include(variant) }
- end
-
- context 'when variant track_inventory = true' do
- it { expect(described_class.full_in_stock).not_to include(variant) }
-
- context 'with some variant stock item having backorderable = true' do
- before { variant.stock_items.first.update(backorderable: true) }
-
- it { expect(described_class.full_in_stock).to include(variant) }
- end
- end
- end
- end
- end
-
describe '.not_discontinued' do
context 'when discontinued' do
let!(:discontinued_variant) { create(:variant, discontinue_on: Time.current - 1.day) }
@@ -366,13 +318,15 @@ describe Spree::Variant, type: :model do
end
end
- context '#full_in_stock?' do
+ context '#in_stock_or_backorderable?' do
+ subject { variant.in_stock_or_backorderable? }
+
let!(:variant) { create(:variant) }
context 'when variant has no stock items' do
before { Spree::StockItem.delete_all }
- it { expect(variant.full_in_stock?).to eq(false) }
+ it { expect(subject).to eq(false) }
end
context 'when variant has stock items' do
@@ -381,33 +335,29 @@ describe Spree::Variant, type: :model do
before { Spree::StockItem.update_all(backorderable: false) }
context 'when variant stock items count_on_hand > 0' do
- before do
- variant.stock_items.first.set_count_on_hand(1)
- variant2.stock_items.first.set_count_on_hand(0)
- end
+ before { variant.stock_items.first.set_count_on_hand(1) }
- it { expect(variant.full_in_stock?).to eq(true) }
- it { expect(variant2.full_in_stock?).to eq(false) }
+ it { expect(subject).to eq(true) }
end
context 'when variant stock items count_on_hand <= 0' do
before { variant.stock_items.first.set_count_on_hand(0) }
- it { expect(variant.full_in_stock?).to eq(false) }
+ it { expect(subject).to eq(false) }
context 'when variant track_inventory = false' do
before { variant.update(track_inventory: false) }
- it { expect(variant.full_in_stock?).to eq(true) }
+ it { expect(subject).to eq(true) }
end
context 'when variant track_inventory = true' do
- it { expect(variant.full_in_stock?).to eq(false) }
+ it { expect(variant.in_stock_or_backorderable?).to eq(false) }
context 'with some variant stock item having backorderable = true' do
before { variant.stock_items.first.update(backorderable: true) }
- it { expect(variant.full_in_stock?).to eq(true) }
+ it { expect(subject).to eq(true) }
end
end
end