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



4
5
6
# File 'app/models/spree/shipping_manifest.rb', line 4

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

Instance Method Details

#for_order(order) ⇒ Object



8
9
10
11
12
# File 'app/models/spree/shipping_manifest.rb', line 8

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

#itemsObject



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

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).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 }

      line_item = units.first.line_item
      variant = units.first.variant
      ManifestItem.new(line_item, variant, units.length, states)
    end
  end.flatten
end