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
[VEN-105] Product states - pre-Orders support - drop available_on logic - with both fields (#11605)
Konrad Gorazde46e71fdd288
diff --git a/api/app/controllers/spree/api/v2/platform/orders_controller.rb b/api/app/controllers/spree/api/v2/platform/orders_controller.rbindex 74ec151f4e..8bf794dbba 100644--- a/api/app/controllers/spree/api/v2/platform/orders_controller.rb+++ b/api/app/controllers/spree/api/v2/platform/orders_controller.rb@@ -100,7 +100,7 @@ module Spreeenddef allowed_sort_attributes- super.push(:available_on, :total, :payment_total, :item_total, :shipment_total,+ super.push(:available_on, :make_active_at, :total, :payment_total, :item_total, :shipment_total,:adjustment_total, :promo_total, :included_tax_total, :additional_tax_total,:item_count, :tax_total, :completed_at)enddiff --git a/api/app/controllers/spree/api/v2/platform/products_controller.rb b/api/app/controllers/spree/api/v2/platform/products_controller.rbindex e808b1d209..4f77285994 100644--- a/api/app/controllers/spree/api/v2/platform/products_controller.rb+++ b/api/app/controllers/spree/api/v2/platform/products_controller.rb@@ -16,7 +16,7 @@ module Spreeenddef allowed_sort_attributes- super << :available_on+ super.push(:available_on, :make_active_at)enddef sorted_collectiondiff --git a/api/spec/requests/spree/api/v2/platform/products_spec.rb b/api/spec/requests/spree/api/v2/platform/products_spec.rbindex 36503b869d..90db92f32d 100644--- a/api/spec/requests/spree/api/v2/platform/products_spec.rb+++ b/api/spec/requests/spree/api/v2/platform/products_spec.rb@@ -196,6 +196,32 @@ describe 'API V2 Platform Products Spec' doendendend++ context 'sorting by make_active_at' do+ before { store.products.each_with_index { |p, i| p.update(make_active_at: Time.current - i.days) } }++ context 'ascending order' do+ before { get '/api/v2/platform/products?sort=make_active_at', headers: bearer_token }++ it_behaves_like 'returns 200 HTTP status'++ it 'returns products sorted by make_active_at' do+ expect(json_response['data'].count).to eq store.products.count+ expect(json_response['data'].pluck(:id)).to eq store.products.order(:make_active_at).map(&:id).map(&:to_s)+ end+ end++ context 'descending order' do+ before { get '/api/v2/platform/products?sort=-make_active_at', headers: bearer_token }++ it_behaves_like 'returns 200 HTTP status'++ it 'returns products sorted by make_active_at with descending order' do+ expect(json_response['data'].count).to eq store.products.count+ expect(json_response['data'].pluck(:id)).to eq store.products.order(make_active_at: :desc).map(&:id).map(&:to_s)+ end+ end+ endendcontext 'paginate products' dodiff --git a/api/spec/serializers/spree/api/v2/platform/product_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/product_serializer_spec.rbindex 4f4dc9913e..bc1137a000 100644--- a/api/spec/serializers/spree/api/v2/platform/product_serializer_spec.rb+++ b/api/spec/serializers/spree/api/v2/platform/product_serializer_spec.rb@@ -26,6 +26,7 @@ describe Spree::Api::V2::Platform::ProductSerializer doname: product.name,description: product.description,available_on: product.available_on,+ make_active_at: product.make_active_at,status: product.status,deleted_at: product.deleted_at,slug: product.slug,diff --git a/core/app/helpers/spree/products_helper.rb b/core/app/helpers/spree/products_helper.rbindex de8bc8462c..3746019e1f 100644--- a/core/app/helpers/spree/products_helper.rb+++ b/core/app/helpers/spree/products_helper.rb@@ -76,15 +76,20 @@ module Spree# will return a human readable stringdef available_status(product)- return Spree.t(:discontinued) if product.discontinued?+ ActiveSupport::Deprecation.warn(<<-DEPRECATION, caller)+ `Spree::ProductsHelper#available_status` method from spree/core is deprecated and will be removed.+ Please use `Spree::Admin::ProductsHelper#available_status` from spree_backend instead.+ DEPRECATION++ return Spree.t(:archived) if product.discontinued?return Spree.t(:deleted) if product.deleted?if product.available?- Spree.t(:available)- elsif product.available_on&.future?+ Spree.t(:active)+ elsif product.make_active_at&.future?Spree.t(:pending_sale)else- Spree.t(:no_available_date_set)+ Spree.t(:draft)endenddiff --git a/core/app/models/concerns/spree/product_scopes.rb b/core/app/models/concerns/spree/product_scopes.rbindex 014f7d63a7..0fc4ad70c2 100644--- a/core/app/models/concerns/spree/product_scopes.rb+++ b/core/app/models/concerns/spree/product_scopes.rb@@ -284,7 +284,7 @@ module Spreeif user.try(:has_spree_role?, 'admin')with_deletedelse- not_deleted.not_discontinued.where("#{Product.quoted_table_name}.available_on <= ?", Time.current)+ not_deleted.where(status: 'active')endenddiff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rbindex 66aa7f7fee..a0e5c11928 100644--- a/core/app/models/spree/product.rb+++ b/core/app/models/spree/product.rb@@ -124,7 +124,7 @@ module Spreeendvalidates :slug, presence: true, uniqueness: { allow_blank: true, case_sensitive: true, scope: spree_base_uniqueness_scope }- validate :discontinue_on_must_be_later_than_available_on, if: -> { available_on && discontinue_on }+ validate :discontinue_on_must_be_later_than_make_active_at, if: -> { make_active_at && discontinue_on }scope :for_store, ->(store) { joins(:store_products).where(StoreProduct.table_name => { store_id: store.id }) }@@ -497,8 +497,8 @@ module Spreeremoved_classifications.each &:remove_from_listend- def discontinue_on_must_be_later_than_available_on- if discontinue_on < available_on+ def discontinue_on_must_be_later_than_make_active_at+ if discontinue_on < make_active_aterrors.add(:discontinue_on, :invalid_date_range)endenddiff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rbindex 21b887226f..65525d0252 100644--- a/core/app/models/spree/variant.rb+++ b/core/app/models/spree/variant.rb@@ -14,7 +14,7 @@ module Spreebelongs_to :product, -> { with_deleted }, touch: true, class_name: 'Spree::Product', inverse_of: :variantsbelongs_to :tax_category, class_name: 'Spree::TaxCategory', optional: true- delegate :name, :name=, :description, :slug, :available_on, :shipping_category_id,+ delegate :name, :name=, :description, :slug, :available_on, :make_active_at, :shipping_category_id,:meta_description, :meta_keywords, :shipping_category, to: :product# we need to have this callback before any dependent: :destroy associationsdiff --git a/core/config/locales/en.yml b/core/config/locales/en.ymlindex a526baf2a3..a235391b18 100644--- a/core/config/locales/en.yml+++ b/core/config/locales/en.yml@@ -91,6 +91,7 @@ en:name: Namespree/product:available_on: Available On+ make_active_at: Make Active Atdiscontinue_on: Discontinue Oncost_currency: Cost Currencycost_price: Cost Price@@ -583,6 +584,7 @@ en:auto_capture: Auto Captureavailability: availabilityavailable_on: Available On+ make_active_at: Make Active Atavailable: Availableaverage_order_value: Average Order Valueavs_response: AVS Responsediff --git a/core/db/migrate/20220117100333_add_make_active_at_to_spree_products.rb b/core/db/migrate/20220117100333_add_make_active_at_to_spree_products.rbnew file mode 100644index 0000000000..01ffe9d5d4--- /dev/null+++ b/core/db/migrate/20220117100333_add_make_active_at_to_spree_products.rb@@ -0,0 +1,17 @@+class AddMakeActiveAtToSpreeProducts < ActiveRecord::Migration[5.2]+ def change+ add_column :spree_products, :make_active_at, :datetime+ add_index :spree_products, :make_active_at++ Spree::Product.+ where('discontinue_on IS NULL or discontinue_on > ?', Time.current).+ where('available_on <= ?', Time.current).+ where(status: 'draft').+ update_all(status: 'active', updated_at: Time.current)++ Spree::Product.+ where('discontinue_on <= ?', Time.current).+ where.not(status: 'archived').+ update_all(status: 'archived', updated_at: Time.current)+ end+enddiff --git a/core/lib/spree/permitted_attributes.rb b/core/lib/spree/permitted_attributes.rbindex a099ee519d..5c32ed2f35 100644--- a/core/lib/spree/permitted_attributes.rb+++ b/core/lib/spree/permitted_attributes.rb@@ -89,7 +89,7 @@ module Spree@@product_properties_attributes = [:property_name, :value, :position]@@product_attributes = [- :name, :description, :available_on, :discontinue_on, :permalink, :meta_description,+ :name, :description, :available_on, :make_active_at, :discontinue_on, :permalink, :meta_description,:meta_keywords, :price, :sku, :deleted_at, :prototype_id,:option_values_hash, :weight, :height, :width, :depth,:shipping_category_id, :tax_category_id,diff --git a/core/lib/spree/testing_support/factories/product_factory.rb b/core/lib/spree/testing_support/factories/product_factory.rbindex 5fad238945..aeb3ecd5e2 100644--- a/core/lib/spree/testing_support/factories/product_factory.rb+++ b/core/lib/spree/testing_support/factories/product_factory.rb@@ -6,6 +6,7 @@ FactoryBot.define docost_price { 17.00 }sku { generate(:sku) }available_on { 1.year.ago }+ make_active_at { 1.year.ago }deleted_at { nil }shipping_category { |r| Spree::ShippingCategory.first || r.association(:shipping_category) }status { 'active' }diff --git a/core/lib/tasks/core.rake b/core/lib/tasks/core.rakeindex 9e221d98ee..5c40dd5ccf 100644--- a/core/lib/tasks/core.rake+++ b/core/lib/tasks/core.rake@@ -187,9 +187,9 @@ use rake db:load_file[/absolute/path/to/sample/filename.rb]}endnamespace :core do- desc 'Set "active" status on draft products where available_on is in the past'+ desc 'Set "active" status on draft products where make_active_at is in the past'task activate_products: :environment do |_t, _args|- Spree::Product.where('available_on <= ?', Time.current).where(status: 'draft').update_all(status: 'active', updated_at: Time.current)+ Spree::Product.where('make_active_at <= ?', Time.current).where(status: 'draft').update_all(status: 'active', updated_at: Time.current)enddesc 'Set "archived" status on active products where discontinue_on is in the past'diff --git a/core/spec/finders/spree/products/find_spec.rb b/core/spec/finders/spree/products/find_spec.rbindex a8902b8cd2..38babe9996 100644--- a/core/spec/finders/spree/products/find_spec.rb+++ b/core/spec/finders/spree/products/find_spec.rb@@ -507,7 +507,7 @@ module Spreeparams: params).execute.map(&:id)- expect(product_ids).to eq Spree::Product.available.order(available_on: :desc).map(&:id)+ expect(product_ids).to eq Spree::Product.available.order(make_active_at: :desc).map(&:id)endit 'returns products in price-high-to-low order' dodiff --git a/core/spec/helpers/products_helper_spec.rb b/core/spec/helpers/products_helper_spec.rbindex 4dcefbb802..c35697a31a 100644--- a/core/spec/helpers/products_helper_spec.rb+++ b/core/spec/helpers/products_helper_spec.rb@@ -294,7 +294,7 @@ module Spreecontext 'product is available' doit 'has available status' do- expect(status).to eq(Spree.t(:available))+ expect(status).to eq(Spree.t(:active))endend@@ -314,20 +314,30 @@ module Spreeendit 'has discontinued status' do- expect(status).to eq(Spree.t(:discontinued))+ expect(status).to eq(Spree.t(:archived))endendcontext 'product will be available soon' dobefore doproduct.status = 'draft'- product.available_on = 1.month.from_now+ product.make_active_at = 1.month.from_nowendit 'has pending sale status' doexpect(status).to eq(Spree.t(:pending_sale))endend++ context 'draft product' do+ before do+ product.status = 'draft'+ end++ it 'has draft status' do+ expect(status).to eq(Spree.t(:draft))+ end+ endendendenddiff --git a/core/spec/lib/tasks/core_spec.rb b/core/spec/lib/tasks/core_spec.rbindex d6f972f91d..54c5a95da8 100644--- a/core/spec/lib/tasks/core_spec.rb+++ b/core/spec/lib/tasks/core_spec.rb@@ -9,26 +9,26 @@ describe 'core:activate_products' doit { expect(subject.prerequisites).to include('environment') }end- it 'draft, available_on in the past -> active' do- product.update(status: 'draft', available_on: 1.day.ago)+ it 'draft, make_active_at in the past -> active' do+ product.update(status: 'draft', make_active_at: 1.day.ago)subject.invokeexpect(product.reload.status).to eq('active')end- it 'draft, available_on in the future -> draft' do- product.update(status: 'draft', available_on: 1.day.from_now)+ it 'draft, make_active_at in the future -> draft' do+ product.update(status: 'draft', make_active_at: 1.day.from_now)subject.invokeexpect(product.reload.status).to eq('draft')end- it 'archived, available_on in the past -> archived' do- product.update(status: 'archived', available_on: 1.day.ago)+ it 'archived, make_active_at in the past -> archived' do+ product.update(status: 'archived', make_active_at: 1.day.ago)subject.invokeexpect(product.reload.status).to eq('archived')end- it 'archived, available_on in the future -> archived' do- product.update(status: 'archived', available_on: 1.day.from_now)+ it 'archived, make_active_at in the future -> archived' do+ product.update(status: 'archived', make_active_at: 1.day.from_now)subject.invokeexpect(product.reload.status).to eq('archived')enddiff --git a/core/spec/models/spree/product/scopes_spec.rb b/core/spec/models/spree/product/scopes_spec.rbindex 80e1ebb909..0ecd2bacca 100644--- a/core/spec/models/spree/product/scopes_spec.rb+++ b/core/spec/models/spree/product/scopes_spec.rb@@ -18,7 +18,7 @@ describe 'Product scopes', type: :model doendcontext 'when available' do- let!(:product_2) { create(:product, available_on: Time.current - 1.day,stores: [store]) }+ let!(:product_2) { create(:product, status: 'active', stores: [store]) }it { expect(Spree::Product.available).to include(product_2) }enddiff --git a/core/spec/models/spree/product_spec.rb b/core/spec/models/spree/product_spec.rbindex 0f0f2eddde..3abd7317d3 100644--- a/core/spec/models/spree/product_spec.rb+++ b/core/spec/models/spree/product_spec.rb@@ -275,10 +275,10 @@ describe Spree::Product, type: :model doendend- describe '#discontinue_on_must_be_later_than_available_on' do- before { product.available_on = Date.today }+ describe '#discontinue_on_must_be_later_than_make_active_at' do+ before { product.make_active_at = Date.today }- context 'available_on is a date earlier than discontinue_on' do+ context 'make_active_at is a date earlier than discontinue_on' dobefore { product.discontinue_on = 5.days.from_now }it 'is valid' do@@ -286,7 +286,7 @@ describe Spree::Product, type: :model doendend- context 'available_on is a date earlier than discontinue_on' do+ context 'make_active_at is a date earlier than discontinue_on' dobefore { product.discontinue_on = 5.days.ago }context 'is not valid' do@@ -297,10 +297,10 @@ describe Spree::Product, type: :model doendend- context 'available_on and discontinue_on are nil' do+ context 'make_active_at and discontinue_on are nil' dobefore doproduct.discontinue_on = nil- product.available_on = nil+ product.make_active_at = nilendit 'is valid' dodiff --git a/sample/db/samples/products.rb b/sample/db/samples/products.rbindex bd852698ea..7beac2b2fd 100644--- a/sample/db/samples/products.rb+++ b/sample/db/samples/products.rb@@ -23,6 +23,7 @@ PRODUCTS.each do |(parent_name, taxon_name, product_name)|product.price = rand(10...100) + 0.99product.description = FFaker::Lorem.paragraphproduct.available_on = Time.zone.now+ product.make_active_at = Time.zone.nowproduct.status = 'active'if parent_name == 'Women' and %w[Dresses Skirts].include?(taxon_name)product.option_types = [color, length, size]