Created
August 25, 2017 11:43
-
-
Save pjmartorell/b5fa2fcaba335fb1d8d1e435f307e26b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment