Class: Spree::ShippingManifest

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/shipping_manifest.rb

Defined Under Namespace

Classes: ManifestItem

Instance Method Summary collapse

Constructor Details

#initialize(inventory_units:) ⇒ ShippingManifest

Returns a new instance of ShippingManifest.



6
7
8
# File 'app/models/spree/shipping_manifest.rb', line 6

def initialize(inventory_units:)
  @inventory_units = inventory_units.to_a
end

Instance Method Details

#for_order(order) ⇒ Object



10
11
12
13
14
# File 'app/models/spree/shipping_manifest.rb', line 10

def for_order(order)
  Spree::ShippingManifest.new(
    inventory_units: @inventory_units.select { |iu| iu.order_id == order.id }
  )
end

#itemsObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/spree/shipping_manifest.rb', line 16

def items
  # Grouping by the ID means that we don't have to call out to the association accessor
  # This makes the grouping by faster because it results in less SQL cache hits.
  @inventory_units.group_by(&:variant_id).flat_map do |_variant_id, variant_units|
    variant_units.group_by(&:line_item_id).map do |_line_item_id, units|
      states = {}
      units.group_by(&:state).each { |state, iu| states[state] = iu.count }

      first_unit = units.first

      ManifestItem.new(first_unit.line_item, first_unit.variant, units.length, states)
    end
  end
end