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
Disable propagating all variants for new stock locations by default (#11586)
Damian Legawiecc1aebea02b2b
diff --git a/core/app/models/spree/inventory_unit.rb b/core/app/models/spree/inventory_unit.rbindex 689b4fb3f5..f9c6a406b6 100644--- a/core/app/models/spree/inventory_unit.rb+++ b/core/app/models/spree/inventory_unit.rb@@ -63,8 +63,7 @@ module Spreeenddef find_stock_item- Spree::StockItem.where(stock_location_id: shipment.stock_location_id,- variant_id: variant_id).first+ shipment.stock_location.stock_item_or_create(variant)enddef self.split(original_inventory_unit, extract_quantity)diff --git a/core/app/models/spree/stock_location.rb b/core/app/models/spree/stock_location.rbindex 4113c7b885..59fb11ea17 100644--- a/core/app/models/spree/stock_location.rb+++ b/core/app/models/spree/stock_location.rb@@ -60,9 +60,15 @@ module Spree# @param variant Variant instance or Variant ID## @return [StockItem] Corresponding StockItem for the StockLocation's variant.- def stock_item_or_create(variant)- variant_id = variant.is_a?(Spree::Variant) ? variant.id : variant- stock_item(variant_id) || stock_items.create(variant_id: variant_id)+ def stock_item_or_create(variant_or_variant_id)+ if variant_or_variant_id.is_a?(Spree::Variant)+ variant_id = variant_or_variant_id.id+ variant = variant_or_variant_id+ else+ variant_id = variant_or_variant_id+ variant = Spree::Variant.find(variant_or_variant_id)+ end+ stock_item(variant_id) || propagate_variant(variant)enddef count_on_hand(variant)@@ -95,8 +101,7 @@ module Spreeenddef fill_status(variant, quantity)- if item = stock_item(variant)-+ if item = stock_item_or_create(variant)if item.count_on_hand >= quantityon_hand = quantitybackordered = 0diff --git a/core/db/migrate/20211229162122_disable_propagate_all_variants_by_default.rb b/core/db/migrate/20211229162122_disable_propagate_all_variants_by_default.rbnew file mode 100644index 0000000000..c3fd8c938c--- /dev/null+++ b/core/db/migrate/20211229162122_disable_propagate_all_variants_by_default.rb@@ -0,0 +1,5 @@+class DisablePropagateAllVariantsByDefault < ActiveRecord::Migration[5.2]+ def change+ change_column_default :spree_stock_locations, :propagate_all_variants, false+ end+enddiff --git a/core/lib/spree/testing_support/factories/order_factory.rb b/core/lib/spree/testing_support/factories/order_factory.rbindex bd74868524..da159b8604 100644--- a/core/lib/spree/testing_support/factories/order_factory.rb+++ b/core/lib/spree/testing_support/factories/order_factory.rb@@ -55,7 +55,8 @@ FactoryBot.define doorder.line_items.reloadend- create(:shipment, order: order, cost: evaluator.shipment_cost)+ stock_location = order.line_items&.first&.variant&.stock_items&.first&.stock_location || create(:stock_location)+ create(:shipment, order: order, cost: evaluator.shipment_cost, stock_location: stock_location)order.shipments.reloadorder.update_with_updater!diff --git a/core/lib/spree/testing_support/factories/product_factory.rb b/core/lib/spree/testing_support/factories/product_factory.rbindex 189f17632d..2eea8ad1a2 100644--- a/core/lib/spree/testing_support/factories/product_factory.rb+++ b/core/lib/spree/testing_support/factories/product_factory.rb@@ -21,6 +21,9 @@ FactoryBot.define doproduct.stores << [store]endend+ after(:create) do |product|+ Spree::StockLocation.all.each { |stock_location| stock_location.propagate_variant(product.master) unless stock_location.stock_items.exists?(variant: product.master) }+ endfactory :custom_product doname { 'Custom Product' }diff --git a/core/lib/spree/testing_support/factories/stock_location_factory.rb b/core/lib/spree/testing_support/factories/stock_location_factory.rbindex 9767851921..6a92a8c992 100644--- a/core/lib/spree/testing_support/factories/stock_location_factory.rb+++ b/core/lib/spree/testing_support/factories/stock_location_factory.rb@@ -7,6 +7,7 @@ FactoryBot.define dophone { '(202) 456-1111' }active { true }backorderable_default { true }+ propagate_all_variants { false }country { |stock_location| Spree::Country.first || stock_location.association(:country) }state do |stock_location|@@ -21,8 +22,8 @@ FactoryBot.define doproduct_1 = create(:product, stores: [store])product_2 = create(:product, stores: [store])- stock_location.stock_items.where(variant_id: product_1.master.id).first.adjust_count_on_hand(10)- stock_location.stock_items.where(variant_id: product_2.master.id).first.adjust_count_on_hand(20)+ stock_location.stock_item_or_create(product_1.master).adjust_count_on_hand(10)+ stock_location.stock_item_or_create(product_2.master).adjust_count_on_hand(20)endendenddiff --git a/core/lib/spree/testing_support/factories/variant_factory.rb b/core/lib/spree/testing_support/factories/variant_factory.rbindex ab1b4debc4..3e8d11d63f 100644--- a/core/lib/spree/testing_support/factories/variant_factory.rb+++ b/core/lib/spree/testing_support/factories/variant_factory.rb@@ -18,6 +18,10 @@ FactoryBot.define do# ensure stock item will be created for this variantbefore(:create) { create(:stock_location) unless Spree::StockLocation.any? }+ after(:create) do |variant|+ Spree::StockLocation.all.each { |stock_location| stock_location.propagate_variant(variant) }+ end+factory :variant do# on_hand 5product { |p| p.association(:product, stores: [create(:store)]) }diff --git a/core/spec/models/spree/customer_return_spec.rb b/core/spec/models/spree/customer_return_spec.rbindex a5d786a10a..1221a71b62 100644--- a/core/spec/models/spree/customer_return_spec.rb+++ b/core/spec/models/spree/customer_return_spec.rb@@ -199,7 +199,7 @@ describe Spree::CustomerReturn, type: :model doendcontext 'to a different stock location' do- let(:new_stock_location) { create(:stock_location, name: 'other') }+ let(:new_stock_location) { create(:stock_location, name: 'other', propagate_all_variants: true) }it 'updates the stock item counts in new stock location' doexpect dodiff --git a/core/spec/models/spree/fulfilment_changer_spec.rb b/core/spec/models/spree/fulfilment_changer_spec.rbindex 15466313a3..e56a7c6437 100644--- a/core/spec/models/spree/fulfilment_changer_spec.rb+++ b/core/spec/models/spree/fulfilment_changer_spec.rb@@ -62,12 +62,7 @@ describe Spree::FulfilmentChanger docontext 'when transferring to another stock location' dolet(:desired_stock_location) { create(:stock_location) }- let!(:stock_item) do- variant.stock_items.find_or_create_by!(- stock_location: desired_stock_location,- variant: variant- )- end+ let!(:stock_item) { desired_stock_location.stock_item_or_create(variant) }before dostock_item.set_count_on_hand(desired_count_on_hand)@@ -196,7 +191,7 @@ describe Spree::FulfilmentChanger doendcontext 'when the original shipment had some backordered units' do- let(:current_stock_item) { current_shipment.stock_location.stock_items.find_by(variant: variant) }+ let(:current_stock_item) { current_shipment.stock_location.stock_item_or_create(variant) }let(:desired_stock_item) { desired_shipment.stock_location.stock_items.find_by(variant: variant) }let(:backordered_units) { 6 }diff --git a/core/spec/models/spree/order_inventory_spec.rb b/core/spec/models/spree/order_inventory_spec.rbindex 2ae14107dd..edac2956f1 100644--- a/core/spec/models/spree/order_inventory_spec.rb+++ b/core/spec/models/spree/order_inventory_spec.rb@@ -61,7 +61,7 @@ describe Spree::OrderInventory, type: :model docontext 'variant doesnt track inventory' dolet(:variant) { create(:variant) }- before { variant.track_inventory = false }+ before { variant.update(track_inventory: false) }it 'creates only on hand inventory units' dovariant.stock_items.destroy_alldiff --git a/core/spec/models/spree/reimbursement_spec.rb b/core/spec/models/spree/reimbursement_spec.rbindex 896342afaa..f58301e70d 100644--- a/core/spec/models/spree/reimbursement_spec.rb+++ b/core/spec/models/spree/reimbursement_spec.rb@@ -119,7 +119,9 @@ describe Spree::Reimbursement, type: :model doendcontext 'when exchange is required' do- let(:exchange_variant) { build(:variant) }+ # we need to have a variant that will pass eligibility checks+ # so it needs to be a variant of the same product+ let!(:exchange_variant) { create(:variant, is_master: true, product: inventory_unit.variant.product) }before { return_item.exchange_variant = exchange_variant }diff --git a/core/spec/models/spree/shipment_spec.rb b/core/spec/models/spree/shipment_spec.rbindex 9e2f21a2ff..9ca81d2093 100644--- a/core/spec/models/spree/shipment_spec.rb+++ b/core/spec/models/spree/shipment_spec.rb@@ -743,7 +743,7 @@ describe Spree::Shipment, type: :model docontext '#transfer_to_location' do# Order with 2 line items in order to be able to split one shipment into 2let(:order) { create(:completed_order_with_totals, line_items_count: 2) }- let(:stock_location) { create(:stock_location) }+ let(:stock_location) { create(:stock_location, propagate_all_variants: true, backorderable_default: true) }let(:variant) { order.line_items.first.variant }before dodiff --git a/core/spec/models/spree/stock/packer_spec.rb b/core/spec/models/spree/stock/packer_spec.rbindex da21a15ef1..9620b44110 100644--- a/core/spec/models/spree/stock/packer_spec.rb+++ b/core/spec/models/spree/stock/packer_spec.rb@@ -26,11 +26,17 @@ module Spreelet!(:inventory_units) { Array.new(2) { InventoryUnit.new variant: create(:variant) } }it 'contains all the items' do+ inventory_units.each do |inventory_unit|+ stock_location.propagate_variant(inventory_unit.variant)+ endpackage = subject.default_packageexpect(package.contents.size).to eq 2endit 'variants are added as backordered without enough on_hand' do+ inventory_units.each do |inventory_unit|+ stock_location.propagate_variant(inventory_unit.variant)+ endexpect(stock_location).to receive(:fill_status).twice.and_return(*(Array.new(1, [1, 0]) + Array.new(1, [0, 1])))diff --git a/core/spec/models/spree/stock/quantifier_spec.rb b/core/spec/models/spree/stock/quantifier_spec.rbindex fefc82631f..ca28da6f1e 100644--- a/core/spec/models/spree/stock/quantifier_spec.rb+++ b/core/spec/models/spree/stock/quantifier_spec.rb@@ -76,8 +76,8 @@ module Spreeendcontext 'with multiple stock locations/items' do- let!(:stock_location_2) { create :stock_location }- let!(:stock_location_3) { create :stock_location, active: false }+ let!(:stock_location_2) { create :stock_location, propagate_all_variants: true }+ let!(:stock_location_3) { create :stock_location, active: false, propagate_all_variants: true }before dostock_location_2.stock_items.where(variant_id: stock_item.variant).update_all(count_on_hand: 5, backorderable: false)diff --git a/core/spec/models/spree/stock_item_spec.rb b/core/spec/models/spree/stock_item_spec.rbindex 38721f1aaa..75930afd29 100644--- a/core/spec/models/spree/stock_item_spec.rb+++ b/core/spec/models/spree/stock_item_spec.rb@@ -250,7 +250,7 @@ describe Spree::StockItem, type: :model docontext 'when a new stock location is added' doit 'touches its variant' doexpect do- create(:stock_location)+ create(:stock_location, propagate_all_variants: true)end.to change { subject.variant.reload.updated_at }endenddiff --git a/core/spec/models/spree/stock_location_spec.rb b/core/spec/models/spree/stock_location_spec.rbindex 543b7dd483..ee5b1ba2c7 100644--- a/core/spec/models/spree/stock_location_spec.rb+++ b/core/spec/models/spree/stock_location_spec.rb@@ -10,14 +10,14 @@ module Spreecontext 'handling the stock items creation after create' dolet!(:variant) { create(:variant) }- before { StockLocation.destroy_all }+ before { described_class.destroy_all }it 'creates stock_items for all variants' doexpect do- create(:stock_location)+ create(:stock_location, propagate_all_variants: true)end.to(change { Variant.count }.by(0).and(- change { StockLocation.count }.from(0).to(1).and(+ change { described_class.count }.from(0).to(1).and(change { StockItem.count }.from(0).to(2)))@@ -26,15 +26,15 @@ module Spreeendit 'validates uniqueness' do- StockLocation.create(name: 'Test')- expect(StockLocation.new(name: 'Test')).not_to be_valid+ described_class.create(name: 'Test')+ expect(described_class.new(name: 'Test')).not_to be_validendcontext 'handling stock items' dolet!(:variant) { create(:variant) }context 'given a variant' do- subject { StockLocation.create(name: 'testing', propagate_all_variants: false) }+ subject { described_class.create(name: 'testing', propagate_all_variants: false) }context 'set up' doit 'creates stock item' do@@ -76,7 +76,7 @@ module Spreeendcontext 'propagate all variants' do- subject { StockLocation.new(name: 'testing') }+ subject { described_class.new(name: 'testing') }context 'true' dobefore { subject.propagate_all_variants = true }@@ -188,7 +188,7 @@ module Spreeit 'can be deactivated' docreate(:stock_location, active: true)create(:stock_location, active: false)- expect(Spree::StockLocation.active.count).to eq 1+ expect(described_class.active.count).to eq 1endit 'ensures only one stock location is default at a time' do@@ -223,7 +223,7 @@ module Spreeendit 'zero on_hand with all backordered' do- expect(subject).to receive(:stock_item).with(variant).and_return(zero_stock_item)+ expect(subject).to receive(:stock_item_or_create).with(variant).and_return(zero_stock_item)on_hand, backordered = subject.fill_status(variant, 20)expect(on_hand).to eq 0@@ -233,7 +233,7 @@ module Spreecontext 'when backordering is not allowed' dobefore doallow(stock_item).to receive_messages backorderable?: false- expect(subject).to receive(:stock_item).with(variant).and_return(stock_item)+ expect(subject).to receive(:stock_item_or_create).with(variant).and_return(stock_item)endit 'all on_hand' do@@ -266,25 +266,25 @@ module Spreelet(:variant) { create(:base_variant) }- it 'zero on_hand and backordered' do+ it 'zero on_hand and one backordered' dosubjectvariant.stock_items.destroy_allon_hand, backordered = subject.fill_status(variant, 1)expect(on_hand).to eq 0- expect(backordered).to eq 0+ expect(backordered).to eq 1endendendcontext '#state_text' docontext 'state is blank' do- subject { StockLocation.create(name: 'testing', state: nil, state_name: 'virginia') }+ subject { described_class.create(name: 'testing', state: nil, state_name: 'virginia') }specify { expect(subject.state_text).to eq('virginia') }endcontext 'both name and abbr is present' do- subject { StockLocation.create(name: 'testing', state: state, state_name: nil) }+ subject { described_class.create(name: 'testing', state: state, state_name: nil) }let(:state) { create(:state, name: 'virginia', abbr: 'va') }@@ -292,7 +292,7 @@ module Spreeendcontext 'only name is present' do- subject { StockLocation.create(name: 'testing', state: state, state_name: nil) }+ subject { described_class.create(name: 'testing', state: state, state_name: nil) }let(:state) { create(:state, name: 'virginia', abbr: nil) }diff --git a/sample/db/samples/stock.rb b/sample/db/samples/stock.rbindex 3fc0a39ec7..4e8c765644 100644--- a/sample/db/samples/stock.rb+++ b/sample/db/samples/stock.rb@@ -16,8 +16,8 @@ Spree::StockLocations::StockItems::Create.call(stock_location: location)product_1 = Spree::Product.find_by!(name: 'Denim Shirt')product_2 = Spree::Product.find_by!(name: 'Checked Shirt')-product_1.master.stock_items.find_by!(stock_location: location).update!(count_on_hand: 1)-product_2.master.stock_items.find_by!(stock_location: location).update!(count_on_hand: 1)+location.stock_item_or_create(product_1.master).update(count_on_hand: 1)+location.stock_item_or_create(product_2.master).update(count_on_hand: 1)Spree::Variant.all.each do |variant|next if variant.is_master? && variant.product.has_variants?