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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
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.rbindex 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]: testscope :full_in_stock, -> dojoins(:stock_items).where(<<~SQL)#{Spree::StockItem.table_name}.count_on_hand > 0 OR@@ -100,9 +91,12 @@ module SpreeSQLend- # [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)+ )+ )endscope :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 Spreeendend- def out_of_stock?- !in_stock?- end-def backorderable?@backorderable ||= Rails.cache.fetch(['variant-backorderable', cache_key_with_version]) doquantifier.backorderable?diff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rbindex 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 docontext 'when product is created without variants but with stock' doit { expect(product.master).to be_in_stock }- it { expect(product.master).not_to be_out_of_stock }endcontext 'when a variant is created' dolet!(:new_variant) { create(:variant, product: product) }it { expect(product.master).not_to be_in_stock }- it { expect(product.master).to be_out_of_stock }endendend@@ -133,6 +131,54 @@ describe Spree::Variant, type: :model doendend+ 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' docontext 'when discontinued' dolet!(:discontinued_variant) { create(:variant, discontinue_on: Time.current - 1.day) }@@ -320,6 +366,54 @@ describe Spree::Variant, type: :model doendend+ 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=' doit 'uses LocalizedNumber.parse' doexpect(Spree::LocalizedNumber).to receive(:parse).with('1,599.99')@@ -536,7 +630,6 @@ describe Spree::Variant, type: :model doit 'returns true if stock_items in stock' doexpect(variant.in_stock?).to be true- expect(variant.out_of_stock?).to be falseendend@@ -548,7 +641,6 @@ describe Spree::Variant, type: :model doit 'return false if stock_items out of stock' doexpect(variant.in_stock?).to be false- expect(variant.out_of_stock?).to be trueendendend@@ -573,7 +665,6 @@ describe Spree::Variant, type: :model doit 'in_stock? returns false' doexpect(variant.in_stock?).to be false- expect(variant.out_of_stock?).to be trueendit 'can_supply? return true' do