Class: Spree::Stock::Quantifier

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/stock/quantifier.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(variant, stock_location = nil) ⇒ Quantifier

Returns a new instance of Quantifier.



6
7
8
9
# File 'app/models/spree/stock/quantifier.rb', line 6

def initialize(variant, stock_location = nil)
  @variant        = variant
  @stock_location = stock_location
end

Instance Attribute Details

#stock_locationObject (readonly)

Returns the value of attribute stock_location.



4
5
6
# File 'app/models/spree/stock/quantifier.rb', line 4

def stock_location
  @stock_location
end

#variantObject (readonly)

Returns the value of attribute variant.



4
5
6
# File 'app/models/spree/stock/quantifier.rb', line 4

def variant
  @variant
end

Instance Method Details

#backorderable?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'app/models/spree/stock/quantifier.rb', line 19

def backorderable?
  @backorderable ||= stock_items.any?(&:backorderable)
end

#can_supply?(required = 1) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'app/models/spree/stock/quantifier.rb', line 23

def can_supply?(required = 1)
  variant.available? && (total_on_hand >= required || backorderable?)
end

#stock_itemsObject



27
28
29
# File 'app/models/spree/stock/quantifier.rb', line 27

def stock_items
  @stock_items ||= scope_to_location(variant.stock_items)
end

#total_on_handObject



11
12
13
14
15
16
17
# File 'app/models/spree/stock/quantifier.rb', line 11

def total_on_hand
  @total_on_hand ||= if variant.should_track_inventory?
                       stock_items.sum(:count_on_hand)
                     else
                       BigDecimal::INFINITY
                     end
end