Skip to content

Commit

Permalink
Rework Quantifier initialization
Browse files Browse the repository at this point in the history
We're going to use the param `stock_location_or_id` in the next commit
  • Loading branch information
spaghetticode committed Mar 7, 2024
1 parent c197214 commit 5a21d93
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
27 changes: 18 additions & 9 deletions core/app/models/spree/stock/quantifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,16 @@ class Quantifier
# If unspecified it will check inventory in all available StockLocations
def initialize(variant, stock_location_or_id = nil)
@variant = variant
@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
@stock_location_or_id = stock_location_or_id
@stock_items = get_stock_items
end

# Returns the total number of inventory units on hand for the variant.
#
# @return [Fixnum] number of inventory units on hand, or infinity if
# inventory is not tracked on the variant.
def total_on_hand
if @variant.should_track_inventory?
if variant.should_track_inventory?
stock_items.sum(&:count_on_hand)
else
Float::INFINITY
Expand All @@ -48,6 +42,21 @@ def backorderable?
def can_supply?(required)
total_on_hand >= required || backorderable?
end

private

attr_reader :variant, :stock_location_or_id

def get_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
end
end
end
15 changes: 7 additions & 8 deletions core/spec/models/spree/stock/quantifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@

require 'rails_helper'

RSpec.shared_examples_for 'unlimited supply' do
it 'can_supply? any amount' do
expect(subject.can_supply?(1)).to eq true
expect(subject.can_supply?(101)).to eq true
expect(subject.can_supply?(100_001)).to eq true
end
end

module Spree
module Stock
RSpec.describe Quantifier, type: :model do
shared_examples_for 'unlimited supply' do
it 'can_supply? any amount' do
expect(subject.can_supply?(1)).to eq true
expect(subject.can_supply?(101)).to eq true
expect(subject.can_supply?(100_001)).to eq true
end
end
let(:target_stock_location) { nil }
let!(:stock_location) { create :stock_location_with_items }
let!(:stock_item) { stock_location.stock_items.order(:id).first }
Expand Down

0 comments on commit 5a21d93

Please sign in to comment.