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



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

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



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

def self.finalize_units!(inventory_units)
  inventory_units.map do |iu|
    iu.update_columns(
      pending: false,
      updated_at: Time.now,
    )
  end
end

Instance Method Details

#find_stock_itemObject



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

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`



62
63
64
# File 'app/models/spree/inventory_unit.rb', line 62

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