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 full_in_stock/full_in_stock? examples.

Sebastian Palmaa6ff2a427d97
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb
index 1f963048c7..a39b5adf72 100644
--- a/core/app/models/spree/variant.rb
+++ b/core/app/models/spree/variant.rb
@@ -83,15 +83,6 @@ module Spree
)
}
- scope :not_discontinued, -> do
- where(
- arel_table[:discontinue_on].eq(nil).or(
- arel_table[:discontinue_on].gteq(Time.current)
- )
- )
- end
-
- # [TODO]: test
scope :full_in_stock, -> do
joins(:stock_items).where(<<~SQL)
#{Spree::StockItem.table_name}.count_on_hand > 0 OR
@@ -100,9 +91,12 @@ module Spree
SQL
end
- # [TODO]: test
- def full_in_stock?
- Spree::Variant.full_in_stock.exists?(id: id)
+ scope :not_discontinued, -> do
+ where(
+ arel_table[:discontinue_on].eq(nil).or(
+ arel_table[:discontinue_on].gteq(Time.current)
+ )
+ )
end
scope :not_deleted, -> { where("#{Spree::Variant.quoted_table_name}.deleted_at IS NULL") }
@@ -146,6 +140,10 @@ module Spree
!discontinued? && product.available?
end
+ def full_in_stock?
+ Spree::Variant.full_in_stock.exists?(id: id)
+ end
+
def tax_category
@tax_category ||= if self[:tax_category_id].nil?
product.tax_category
@@ -275,10 +273,6 @@ module Spree
end
end
- def out_of_stock?
- !in_stock?
- end
-
def backorderable?
@backorderable ||= Rails.cache.fetch(['variant-backorderable', cache_key_with_version]) do
quantifier.backorderable?
diff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rb
index 2333a2d803..77548e8a07 100644
--- a/core/spec/models/spree/variant_spec.rb
+++ b/core/spec/models/spree/variant_spec.rb
@@ -98,14 +98,12 @@ describe Spree::Variant, type: :model do
context 'when product is created without variants but with stock' do
it { expect(product.master).to be_in_stock }
- it { expect(product.master).not_to be_out_of_stock }
end
context 'when a variant is created' do
let!(:new_variant) { create(:variant, product: product) }
it { expect(product.master).not_to be_in_stock }
- it { expect(product.master).to be_out_of_stock }
end
end
end
@@ -133,6 +131,54 @@ 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) }
@@ -320,6 +366,54 @@ describe Spree::Variant, type: :model do
end
end
+ context '#full_in_stock?' do
+ 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) }
+ 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(variant.full_in_stock?).to eq(true) }
+ it { expect(variant2.full_in_stock?).to eq(false) }
+ 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) }
+
+ context 'when variant track_inventory = false' do
+ before { variant.update(track_inventory: false) }
+
+ it { expect(variant.full_in_stock?).to eq(true) }
+ end
+
+ context 'when variant track_inventory = true' do
+ it { expect(variant.full_in_stock?).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) }
+ end
+ end
+ end
+ end
+ end
+
context '#price=' do
it 'uses LocalizedNumber.parse' do
expect(Spree::LocalizedNumber).to receive(:parse).with('1,599.99')
@@ -536,7 +630,6 @@ describe Spree::Variant, type: :model do
it 'returns true if stock_items in stock' do
expect(variant.in_stock?).to be true
- expect(variant.out_of_stock?).to be false
end
end
@@ -548,7 +641,6 @@ describe Spree::Variant, type: :model do
it 'return false if stock_items out of stock' do
expect(variant.in_stock?).to be false
- expect(variant.out_of_stock?).to be true
end
end
end
@@ -573,7 +665,6 @@ describe Spree::Variant, type: :model do
it 'in_stock? returns false' do
expect(variant.in_stock?).to be false
- expect(variant.out_of_stock?).to be true
end
it 'can_supply? return true' do