@@ -11,22 +11,16 @@ class Quantifier
1111 # If unspecified it will check inventory in all available StockLocations
1212 def initialize ( variant , stock_location_or_id = nil )
1313 @variant = variant
14- @stock_items = variant . stock_items . select do |stock_item |
15- if stock_location_or_id
16- stock_item . stock_location == stock_location_or_id ||
17- stock_item . stock_location_id == stock_location_or_id
18- else
19- stock_item . stock_location . active?
20- end
21- end
14+ @stock_location_or_id = stock_location_or_id
15+ @stock_items = variant_stock_items
2216 end
2317
2418 # Returns the total number of inventory units on hand for the variant.
2519 #
2620 # @return [Fixnum] number of inventory units on hand, or infinity if
2721 # inventory is not tracked on the variant.
2822 def total_on_hand
29- if @ variant. should_track_inventory?
23+ if variant . should_track_inventory?
3024 stock_items . sum ( &:count_on_hand )
3125 else
3226 Float ::INFINITY
@@ -48,6 +42,21 @@ def backorderable?
4842 def can_supply? ( required )
4943 total_on_hand >= required || backorderable?
5044 end
45+
46+ private
47+
48+ attr_reader :variant , :stock_location_or_id
49+
50+ def variant_stock_items
51+ variant . stock_items . select do |stock_item |
52+ if stock_location_or_id
53+ stock_item . stock_location == stock_location_or_id ||
54+ stock_item . stock_location_id == stock_location_or_id
55+ else
56+ stock_item . stock_location . active?
57+ end
58+ end
59+ end
5160 end
5261 end
5362end
0 commit comments