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

Returns a new instance of Quantifier.



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

def initialize(variant)
  @variant = variant
  @stock_items = Spree::StockItem.joins(:stock_location).where(:variant_id => @variant, Spree::StockLocation.table_name =>{ :active => true})
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

Instance Method Details

#backorderable?Boolean

Returns:

  • (Boolean)


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

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

#can_supply?(required) ⇒ Boolean

Returns:

  • (Boolean)


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

def can_supply?(required)
  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