Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save pjmartorell/b5fa2fcaba335fb1d8d1e435f307e26b to your computer and use it in GitHub Desktop.

Select an option

Save pjmartorell/b5fa2fcaba335fb1d8d1e435f307e26b to your computer and use it in GitHub Desktop.

Revisions

  1. pjmartorell created this gist Aug 25, 2017.
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    module Delivery
    class QuantitiesWithMaxUnitsAndShippingPackageCategoriesMapper
    def initialize(cart_products)
    @cart_products = cart_products
    end

    def quantities_with_max_units_and_shipping_package_categories
    @cart_products.map do |cart_product|
    variant = cart_product.sample_pack_product.presence || cart_product.variant.presence
    raise "Unsupported type! #{cart_product.inspect}" unless variant

    # Even if the variable max_units_per_correos_envelope leads to believe that it's related with the maximum number
    # of units that fit in an envelope, the real meaning is the maximum number of units that fit in its defined
    # shipping_package_category (envelope, box or bag). This naming is misleading and prone to errors when
    # shipping_package_category is changed but not its corresponding max_units_per_correos_envelope.
    # However in the case of UPS we just have ONE type of package (envelope) and the naming makes sense. For this
    # reason we may decided to maintain the same naming.
    # To solve it partially I give a better naming in this mapper.
    # SamplePackProducts just have max_units_per_ups_envelope column. In the case of correos package we assume that
    # only 1 fits in.
    {
    quantity: cart_product.quantity,
    max_units_per_correos_package: variant.try(:max_units_per_correos_envelope) || 1,
    max_units_per_ups_package: variant.max_units_per_ups_envelope,
    shipping_package_category: variant.shipping_package_category
    }
    end
    end
    end
    end