From cb09c935dc91e55f6eb9b4939333738ef9b13e13 Mon Sep 17 00:00:00 2001 From: Zee Spencer <50284+zspencer@users.noreply.github.com> Date: Wed, 20 May 2020 11:40:20 -0700 Subject: [PATCH] WIP: Products may be created with pounds for their weight unit_converter See: https://community.openfoodnetwork.org/t/hubs-managers-can-choose-the-adapted-weight-and-measure-units-for-their-shops-given-their-own-local-situation/1289/11 We're not entirely sure what needs to be changed in order for this to accurately work with shipping and other parts of the eCommerce platform. We are assuming that so long as we canonically store the weight scale in grams, that the shipping calculation will be able to do what it needs to. So if we put in values for "oz" as grams, we may not need to do much else in order to let product(s) be sold by the pound (or ounce). Next steps appear to be: - [ ] When looking at an order as a customer, do we want to show pounds instead of grams? (See: http://localhost:3000/orders/R125684626) - [ ] Compile a list of tests that are worth writing (because we have no confidence that we know what we are supposed to be doing in order for this feature to be "ready" to be used by people.) - [ ] Write a test that demonstrates when we create a product with a variant in pound that the product's shipping weight is correctly calculated? - [ ] Do we want to think about i18n? --- .../products/services/variant_unit_manager.js.coffee | 10 +++++++++- app/models/product_import/unit_converter.rb | 4 ++++ lib/open_food_network/option_value_namer.rb | 4 +++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/admin/products/services/variant_unit_manager.js.coffee b/app/assets/javascripts/admin/products/services/variant_unit_manager.js.coffee index ad16ae7cff..845827adc4 100644 --- a/app/assets/javascripts/admin/products/services/variant_unit_manager.js.coffee +++ b/app/assets/javascripts/admin/products/services/variant_unit_manager.js.coffee @@ -4,7 +4,15 @@ angular.module("admin.products").factory "VariantUnitManager", -> 'weight': 1.0: 'g' 1000.0: 'kg' - 1000000.0: 'T' + 1000000.0: 'T', + # This appears to be what needs to be set in order for + # products to have a mass value stored in the database + # when they are created. However, it does not appear to + # change the existing product(s), so if the scale value + # is changed, a data migration may be necessary to make sure + # the proper unit X to grams actually works. + # TODO: ^^^ Delete this + 453.592: 'lb' 'volume': 0.001: 'mL' 1.0: 'L' diff --git a/app/models/product_import/unit_converter.rb b/app/models/product_import/unit_converter.rb index e97cff43f9..c7e5464329 100644 --- a/app/models/product_import/unit_converter.rb +++ b/app/models/product_import/unit_converter.rb @@ -32,6 +32,10 @@ module ProductImport { 'g' => { scale: 1, unit: 'weight' }, 'kg' => { scale: 1000, unit: 'weight' }, + # We have _no idea_ what this is doing. It has units? + # And it maybe is connected to something related to shipping? + # TODO: DELETE THIS ^^^ + 'lb' => { scale: 453.592, unit: 'weight' }, 't' => { scale: 1_000_000, unit: 'weight' }, 'ml' => { scale: 0.001, unit: 'volume' }, 'l' => { scale: 1, unit: 'volume' }, diff --git a/lib/open_food_network/option_value_namer.rb b/lib/open_food_network/option_value_namer.rb index 706a0b443d..22ef439fc4 100644 --- a/lib/open_food_network/option_value_namer.rb +++ b/lib/open_food_network/option_value_namer.rb @@ -63,7 +63,9 @@ module OpenFoodNetwork end def scale_for_unit_value - units = { 'weight' => { 1.0 => 'g', 1000.0 => 'kg', 1_000_000.0 => 'T' }, + # We're not entirely sure what this is connected to either. + # TODO: DELETE THIS + units = { 'weight' => { 1.0 => 'g', 1000.0 => 'kg', 1_000_000.0 => 'T', 1.0 => 'oz' }, 'volume' => { 0.001 => 'mL', 1.0 => 'L', 1000.0 => 'kL' } } # Find the largest available unit where unit_value comes to >= 1 when expressed in it.