Class: Spree::Stock::Quantifier
- Inherits:
-
Object
- Object
- Spree::Stock::Quantifier
- Defined in:
- app/models/spree/stock/quantifier.rb
Instance Attribute Summary collapse
-
#stock_items ⇒ Object
readonly
Returns the value of attribute stock_items.
Instance Method Summary collapse
-
#backorderable? ⇒ Boolean
Checks if any of its stock items are backorderable.
-
#can_supply?(required) ⇒ Boolean
Checks if it is possible to supply a given number of units.
-
#initialize(variant, stock_location = nil) ⇒ Quantifier
constructor
A new instance of Quantifier.
-
#total_on_hand ⇒ Fixnum
Returns the total number of inventory units on hand for the variant.
Constructor Details
#initialize(variant, stock_location = nil) ⇒ Quantifier
Returns a new instance of Quantifier.
6 7 8 9 10 11 12 13 14 15 |
# File 'app/models/spree/stock/quantifier.rb', line 6 def initialize(variant, stock_location = nil) @variant = variant where_args = { variant_id: @variant } if stock_location where_args.merge!(stock_location: stock_location) else where_args.merge!(Spree::StockLocation.table_name => { active: true }) end @stock_items = Spree::StockItem.joins(:stock_location).where(where_args) end |
Instance Attribute Details
#stock_items ⇒ Object (readonly)
Returns the value of attribute stock_items.
4 5 6 |
# File 'app/models/spree/stock/quantifier.rb', line 4 def stock_items @stock_items end |
Instance Method Details
#backorderable? ⇒ Boolean
Checks if any of its stock items are backorderable.
32 33 34 |
# File 'app/models/spree/stock/quantifier.rb', line 32 def backorderable? stock_items.any?(&:backorderable) end |
#can_supply?(required) ⇒ Boolean
Checks if it is possible to supply a given number of units.
41 42 43 |
# File 'app/models/spree/stock/quantifier.rb', line 41 def can_supply?(required) total_on_hand >= required || backorderable? end |
#total_on_hand ⇒ Fixnum
Returns the total number of inventory units on hand for the variant.
21 22 23 24 25 26 27 |
# File 'app/models/spree/stock/quantifier.rb', line 21 def total_on_hand if @variant.should_track_inventory? stock_items.sum(:count_on_hand) else Float::INFINITY end end |