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



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

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



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

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

Instance Method Details

#find_stock_itemObject



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

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`



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

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