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
Fixed method automatic type casting which doesn't work with UUIDs (#11507)
Damian Legawiec2622fc715910
diff --git a/core/app/models/spree/stock_location.rb b/core/app/models/spree/stock_location.rbindex 21b62fcd62..38d62c9350 100644--- a/core/app/models/spree/stock_location.rb+++ b/core/app/models/spree/stock_location.rb@@ -47,15 +47,13 @@ module Spreeend# Attempts to look up StockItem for the variant, and creates one if not found.- # This method accepts an instance of the variant.- # Other methods in this model attempt to pass a variant,- # but controller actions can pass just the variant id as a parameter.#- # @param variant [Variant] Variant instance.+ # @param variant Variant instance or Variant ID## @return [StockItem] Corresponding StockItem for the StockLocation's variant.def stock_item_or_create(variant)- stock_item(variant) || stock_items.create(variant_id: variant.id)+ variant_id = variant.is_a?(Spree::Variant) ? variant.id : variant+ stock_item(variant_id) || stock_items.create(variant_id: variant_id)enddef count_on_hand(variant)diff --git a/core/spec/models/spree/stock_location_spec.rb b/core/spec/models/spree/stock_location_spec.rbindex cfabbe3449..543b7dd483 100644--- a/core/spec/models/spree/stock_location_spec.rb+++ b/core/spec/models/spree/stock_location_spec.rb@@ -119,15 +119,43 @@ module Spreeenddescribe '#stock_item_or_create' do- before do- variant = create(:variant)- variant.stock_items.destroy_all- variant.save+ context 'without stock item' do+ let!(:variant) { create(:variant) }++ before { variant.stock_items.destroy_all }++ context 'variant instance passed' do+ it 'creates a stock_item if not found for a variant' do+ stock_item = subject.stock_item_or_create(variant)+ expect(stock_item.variant).to eq variant+ end++ it { expect { subject.stock_item_or_create(variant) }.to change(Spree::StockItem, :count) }+ end++ context 'variant ID passed' do+ it 'creates a stock_item if not found for a variant' do+ stock_item = subject.stock_item_or_create(variant.id)+ expect(stock_item.variant).to eq variant+ end++ it { expect { subject.stock_item_or_create(variant.id) }.to change(Spree::StockItem, :count) }+ endend- it 'creates a stock_item if not found for a variant' do- stock_item = subject.stock_item_or_create(variant)- expect(stock_item.variant).to eq variant+ context 'with stock item' do+ let!(:variant) { create(:variant) }+ let!(:stock_item) { create(:stock_item, variant: variant, stock_location: subject) }++ context 'variant instance passed' do+ it { expect { subject.stock_item_or_create(variant) }.not_to change(Spree::StockItem, :count) }+ it { expect(subject.stock_item_or_create(variant)).to eq(stock_item) }+ end++ context 'variant ID passed' do+ it { expect { subject.stock_item_or_create(variant.id) }.not_to change(Spree::StockItem, :count) }+ it { expect(subject.stock_item_or_create(variant.id)).to eq(stock_item) }+ endendend