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) ⇒ Quantifier


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

def initialize(variant)
  @variant = variant
  @stock_items = @variant.stock_items.with_active_stock_location
end

Instance Attribute Details

#stock_itemsObject (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

#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


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

def backorderable?
  stock_items.any?(&:backorderable)
end

#can_supply?(required = 1) ⇒ 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

#total_on_handObject


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

def total_on_hand
  if variant.should_track_inventory?
    stock_items.sum(:count_on_hand)
  else
    Float::INFINITY
  end
end