From 90c58ddbe2beea0b34da8b7c5e912856a3c92cb2 Mon Sep 17 00:00:00 2001 From: MadelineCollier Date: Thu, 17 Dec 2020 14:37:07 -0800 Subject: [PATCH 1/2] Allow Variant#can_supply? to take stock_location Quantifier already has the ability to restrict stock checks to certain locations. This change just exposes that ability at the Variant level. This allows applications with multiple stock locations to check variant stock for a specific location without explicitly having to instantiate a new Quantifier with the stock location. --- core/app/models/spree/variant.rb | 7 +++++-- core/spec/models/spree/variant_spec.rb | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb index 9e8b13cf467..f827b6bb39a 100644 --- a/core/app/models/spree/variant.rb +++ b/core/app/models/spree/variant.rb @@ -346,9 +346,12 @@ def in_stock? end # @param quantity [Fixnum] how many are desired + # @param stock_location [Spree::StockLocation] Optionally restrict stock + # quantity check to a specific stock location. If unspecified it will + # check inventory in all available StockLocations. # @return [Boolean] true if the desired quantity can be supplied - def can_supply?(quantity = 1) - Spree::Stock::Quantifier.new(self).can_supply?(quantity) + def can_supply?(quantity = 1, stock_location = nil) + Spree::Stock::Quantifier.new(self, stock_location).can_supply?(quantity) end # Fetches the on-hand quantity of the variant. diff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rb index f98d4c469f3..3e6e81fff03 100644 --- a/core/spec/models/spree/variant_spec.rb +++ b/core/spec/models/spree/variant_spec.rb @@ -580,6 +580,22 @@ expect(quantifier).to receive(:can_supply?).with(10) variant.can_supply?(10) end + + context "with a stock_location specified" do + subject { variant.can_supply?(10, stock_location) } + + let(:quantifier) { instance_double(Spree::Stock::Quantifier) } + let(:stock_location) { build_stubbed(:stock_location) } + + it "initializes the quantifier with the stock location" do + expect(Spree::Stock::Quantifier). + to receive(:new). + with(variant, stock_location). + and_return(quantifier) + allow(quantifier).to receive(:can_supply?).with(10) + subject + end + end end context 'when stock_items are backorderable' do From 38b32b0eeec261e1782f98fb191e9c26fce8b419 Mon Sep 17 00:00:00 2001 From: MadelineCollier Date: Tue, 22 Dec 2020 14:15:46 -0800 Subject: [PATCH 2/2] Allow Variant#total_on_hand to take stock_location Similar to the previous change, we wanted to allow for applications to specify the stock location when calling variant.total_on_hand. --- core/app/models/spree/variant.rb | 7 +++++-- core/spec/models/spree/variant_spec.rb | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb index f827b6bb39a..79f9ae67ba2 100644 --- a/core/app/models/spree/variant.rb +++ b/core/app/models/spree/variant.rb @@ -356,9 +356,12 @@ def can_supply?(quantity = 1, stock_location = nil) # Fetches the on-hand quantity of the variant. # + # @param stock_location [Spree::StockLocation] Optionally restrict stock + # quantity check to a specific stock location. If unspecified it will + # check inventory in all available StockLocations. # @return [Fixnum] the number currently on-hand - def total_on_hand - Spree::Stock::Quantifier.new(self).total_on_hand + def total_on_hand(stock_location = nil) + Spree::Stock::Quantifier.new(self, stock_location).total_on_hand end # Shortcut method to determine if inventory tracking is enabled for this diff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rb index 3e6e81fff03..55c4c58ebed 100644 --- a/core/spec/models/spree/variant_spec.rb +++ b/core/spec/models/spree/variant_spec.rb @@ -649,6 +649,22 @@ variant = build(:variant) expect(variant.total_on_hand).to eq(Spree::Stock::Quantifier.new(variant).total_on_hand) end + + context "with a stock_location specified" do + subject { variant.total_on_hand(stock_location) } + + let(:quantifier) { instance_double(Spree::Stock::Quantifier) } + let(:stock_location) { build_stubbed(:stock_location) } + + it "initializes the quantifier with the stock location" do + expect(Spree::Stock::Quantifier). + to receive(:new). + with(variant, stock_location). + and_return(quantifier) + allow(quantifier).to receive(:total_on_hand) + subject + end + end end describe '#tax_category' do