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 `discontinue_on` to the `not_discontinued` products scope (#12282)
Damian Kaczmarczyk950298f9b1a9
diff --git a/core/app/models/concerns/spree/product_scopes.rb b/core/app/models/concerns/spree/product_scopes.rbindex ccfec73946..c0ae78942a 100644--- a/core/app/models/concerns/spree/product_scopes.rb+++ b/core/app/models/concerns/spree/product_scopes.rb@@ -246,7 +246,7 @@ module Spreedef self.not_discontinued(only_not_discontinued = true)if only_not_discontinued != '0' && only_not_discontinued- not_archived+ where(discontinue_on: [nil, Time.current..])elseallend@@ -263,11 +263,11 @@ module Spree# Can't use add_search_scope for this as it needs a default argumentdef self.available(available_on = nil, currency = nil)- if available_on- scope = not_discontinued.where("#{Product.quoted_table_name}.available_on <= ?", available_on)- else- scope = where(status: 'active')- end+ scope = if available_on+ not_discontinued.where("#{Product.quoted_table_name}.available_on <= ?", available_on)+ else+ not_discontinued.where(status: 'active')+ endunless Spree::Config.show_products_without_pricecurrency ||= Spree::Store.default.default_currencydiff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rbindex aa059ff163..2f3134f776 100644--- a/core/app/models/spree/product.rb+++ b/core/app/models/spree/product.rb@@ -420,7 +420,7 @@ module Spree# deleted products and products with status different than active# are not availabledef available?- active? && !deleted?+ active? && !deleted? && (available_on.nil? || available_on <= Time.current)enddef discontinue!diff --git a/core/spec/models/spree/product_spec.rb b/core/spec/models/spree/product_spec.rbindex 2f6a1b4bfb..23e8fc0eb5 100644--- a/core/spec/models/spree/product_spec.rb+++ b/core/spec/models/spree/product_spec.rb@@ -197,6 +197,12 @@ describe Spree::Product, type: :model doproduct.destroyexpect(product).not_to be_availableend++ it 'is not available when available_on is in the future' do+ product.available_on = 1.day.from_now++ expect(product).not_to be_available+ endenddescribe '#can_supply?' do@@ -1138,4 +1144,95 @@ describe Spree::Product, type: :model doendendend++ describe 'scopes' do+ describe '.not_discontinued' do+ let(:product) { create(:product, stores: [store]) }+ let(:discontinued_product) { create(:product, stores: [store], discontinue_on: Time.current - 1.day) }++ context 'when nothing is passed as an argument' do+ it 'returns only not discontinued products' do+ products = Spree::Product.not_discontinued+ expect(products).to include(product)+ expect(products).not_to include(discontinued_product)+ end+ end++ context 'when false is passed as an argument' do+ it 'returns all products' do+ products = Spree::Product.not_discontinued(false)++ expect(products).to include(product,discontinued_product)+ end+ end+ end++ describe '.available' do+ let!(:discontinued_product) { create(:product, discontinue_on: 1.day.ago) }+ let!(:future_product) { create(:product, available_on: 1.day.from_now, status: 'active') }+ let!(:active_product) { create(:product, available_on: 1.day.ago, status: 'active') }++ context 'when available_on is specified' do+ let(:current_time) { Time.current }++ it 'returns products available before or on the specified date' do+ available_products = described_class.available(current_time)++ expect(available_products).to include(active_product)+ expect(available_products).not_to include(future_product)+ expect(available_products).not_to include(discontinued_product)+ end+ end++ context 'when available_on is not specified' do+ it 'returns active, not discontinued products' do+ available_products = described_class.available++ expect(available_products).to include(active_product)+ expect(available_products).to include(future_product)+ expect(available_products).not_to include(discontinued_product)+ end+ end++ context 'when show_products_without_price is false' do+ before do+ Spree::Config.show_products_without_price = false+ end++ let!(:product_without_price) { create(:product, status: 'active') }+ let!(:product_with_price) { create(:product, status: 'active') }++ before do+ product_without_price.master.prices.delete_all+ end++ it 'only returns products with prices in the specified currency' do+ available_products = described_class.available(nil, 'USD')++ expect(available_products).to include(product_with_price)+ expect(available_products).not_to include(product_without_price)+ end+ end++ context 'when show_products_without_price is true' do+ before do+ Spree::Config.show_products_without_price = true+ end++ let!(:product_without_price) { create(:product, status: 'active') }+ let!(:product_with_price) { create(:product, status: 'active') }++ before do+ product_without_price.master.prices.delete_all+ end++ it 'returns products regardless of price' do+ available_products = described_class.available(nil, 'USD')++ expect(available_products).to include(product_with_price)+ expect(available_products).to include(product_without_price)+ end+ end+ end+ endend