Class: Spree::ShippingManifest

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

Defined Under Namespace

Classes: ManifestItem

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inventory_units:, order: nil) ⇒ ShippingManifest



13
14
15
16
# File 'app/models/spree/shipping_manifest.rb', line 13

def initialize(inventory_units:, order: nil)
  @inventory_units = inventory_units
  @order = order
end

Instance Attribute Details

#inventory_unitsObject (readonly)

Returns the value of attribute inventory_units.



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

def inventory_units
  @inventory_units
end

#orderObject (readonly)

Returns the value of attribute order.



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

def order
  @order
end

Instance Method Details

#for_order(order) ⇒ Object



18
19
20
21
22
23
# File 'app/models/spree/shipping_manifest.rb', line 18

def for_order(order)
  self.class.new(
    inventory_units: inventory_units.select { |unit| unit.order_id == order.id },
    order: order
  )
end

#itemsObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/spree/shipping_manifest.rb', line 25

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, line_item_units|
      states = {}
      line_item_units.group_by(&:state).each { |state, iu| states[state] = iu.count }

      first_unit = line_item_units.first

      ManifestItem.new(
        line_item_for(line_item_id, first_unit),
        first_unit.variant,
        line_item_units.length,
        states
      )
    end
  end
end