Class: Spree::InventoryUnit

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/spree/inventory_unit.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.backordered_for_stock_item(stock_item) ⇒ Object

This was refactored from a simpler query because the previous implementation lead to issues once users tried to modify the objects returned. That’s due to ActiveRecord ‘joins(shipment: :stock_location)` only return readonly objects

Returns an array of backordered inventory units as per a given stock item



40
41
42
43
44
# File 'app/models/spree/inventory_unit.rb', line 40

def self.backordered_for_stock_item(stock_item)
  backordered_per_variant(stock_item).select do |unit|
    unit.shipment.stock_location == stock_item.stock_location
  end
end

.finalize_units!(inventory_units) ⇒ Object



46
47
48
# File 'app/models/spree/inventory_unit.rb', line 46

def self.finalize_units!(inventory_units)
  inventory_units.map { |iu| iu.update_column(:pending, false) }
end

Instance Method Details

#find_stock_itemObject



50
51
52
53
# File 'app/models/spree/inventory_unit.rb', line 50

def find_stock_item
  Spree::StockItem.where(stock_location_id: shipment.stock_location_id,
    variant_id: variant_id).first
end

#variantObject

Remove variant default_scope ‘deleted_at: nil`



56
57
58
# File 'app/models/spree/inventory_unit.rb', line 56

def variant
  Spree::Variant.unscoped { super }
end