Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Stock Quantifier to use Enumerable #4291

Merged
merged 2 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions core/app/models/spree/stock/quantifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ class Quantifier
attr_reader :stock_items

# @param [Variant] variant The variant to check inventory for.
# @param [StockLocation, Integer] stock_location The stock_location to check inventory in. If unspecified it will check inventory in all available StockLocations
def initialize(variant, stock_location = nil)
# @param [StockLocation, Integer] stock_location_or_id
# The stock_location or stock location ID to check inventory in.
# If unspecified it will check inventory in all available StockLocations
def initialize(variant, stock_location_or_id = nil)
@variant = variant
@stock_items = Spree::StockItem.where(variant_id: variant)
if stock_location
@stock_items.where!(stock_location: stock_location)
else
@stock_items.joins!(:stock_location).merge!(Spree::StockLocation.active)
@stock_items = variant.stock_items.select do |stock_item|
if stock_location_or_id
stock_item.stock_location == stock_location_or_id ||
stock_item.stock_location_id == stock_location_or_id
else
stock_item.stock_location.active?
end
end
end

Expand All @@ -23,7 +27,7 @@ def initialize(variant, stock_location = nil)
# inventory is not tracked on the variant.
def total_on_hand
if @variant.should_track_inventory?
stock_items.sum(:count_on_hand)
stock_items.sum(&:count_on_hand)
else
Float::INFINITY
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

let(:order) { Spree::TestingSupport::OrderWalkthrough.up_to(:payment, user: user) }
let(:order_product) { order.products.first }
let(:order_stock_item) { order_product.stock_items.first }
let(:order_stock_item) { order.line_items.first.variant.stock_items.first }

before do
order_stock_item.update! backorderable: false
Expand All @@ -25,6 +25,7 @@
before do
visit spree.checkout_state_path(:confirm)
order_stock_item.set_count_on_hand(0)
order.line_items.first.variant.stock_items.reload
end

it 'redirects to cart page and shows an unavailable product message' do
Expand All @@ -44,6 +45,7 @@
before do
visit spree.checkout_state_path(:confirm)
order_stock_item.set_count_on_hand(0)
order.line_items.first.variant.stock_items.reload
end

it "redirects to the address checkout page and shows an availability changed message" do
Expand Down