Sha256: ebd56531e7a58920a17d6be7349f59402fea09a022b1636f8aa7312badb5e944

Contents?: true

Size: 989 Bytes

Versions: 42

Compression:

Stored size: 989 Bytes

Contents

# frozen_string_literal: true

class Spree::ShippingManifest
  ManifestItem = Struct.new(:line_item, :variant, :quantity, :states)

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

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

  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
end

Version data entries

42 entries across 42 versions & 2 rubygems

Version Path
solidus_core-2.9.6 app/models/spree/shipping_manifest.rb
solidus_core-2.8.6 app/models/spree/shipping_manifest.rb
solidus_core-2.9.5 app/models/spree/shipping_manifest.rb
solidus_core-2.9.4 app/models/spree/shipping_manifest.rb
solidus_core-2.6.6 app/models/spree/shipping_manifest.rb
solidus_core-2.7.4 app/models/spree/shipping_manifest.rb
solidus_core-2.8.5 app/models/spree/shipping_manifest.rb
solidus_core-2.9.3 app/models/spree/shipping_manifest.rb
solidus_core-2.9.2 app/models/spree/shipping_manifest.rb
solidus_core-2.7.3 app/models/spree/shipping_manifest.rb
solidus_core-2.6.5 app/models/spree/shipping_manifest.rb
solidus_core-2.9.1 app/models/spree/shipping_manifest.rb
solidus_core-2.9.0 app/models/spree/shipping_manifest.rb
solidus_core-2.9.0.rc.1 app/models/spree/shipping_manifest.rb
solidus_core-2.8.4 app/models/spree/shipping_manifest.rb
solidus_core-2.8.3 app/models/spree/shipping_manifest.rb
solidus_core-2.7.2 app/models/spree/shipping_manifest.rb
solidus_core-2.6.4 app/models/spree/shipping_manifest.rb
solidus_core-2.8.2 app/models/spree/shipping_manifest.rb
solidus_core-2.8.1 app/models/spree/shipping_manifest.rb