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 01/20] 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. From ae0b76e6104d79e4e2f03f7f856c97c937f3f9e2 Mon Sep 17 00:00:00 2001 From: Zee Spencer <50284+zspencer@users.noreply.github.com> Date: Wed, 27 May 2020 11:45:55 -0700 Subject: [PATCH 02/20] Support imperial units when scaling the unit value We are pretty sure this is not the correct final implementation, but we wanted to get some tests failing so we can start to fix them. --- lib/open_food_network/option_value_namer.rb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/open_food_network/option_value_namer.rb b/lib/open_food_network/option_value_namer.rb index 22ef439fc4..896412856a 100644 --- a/lib/open_food_network/option_value_namer.rb +++ b/lib/open_food_network/option_value_namer.rb @@ -63,9 +63,15 @@ module OpenFoodNetwork end def scale_for_unit_value - # 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' }, + # TODO: We _think_ this will cause the following weird results: + # 29g of product would use the `oz` measurement + # 445g of product would use the `lb` measurement + # So we probably want to keep the metric and imperial measures + # in their own lanes; perhaps using a configurable value on a per + # shop or producer basis? + + units = { 'weight' => { 1.0 => 'g', 1000.0 => 'kg', 1_000_000.0 => 'T', + 28.34952 => 'oz', 453.59237 => 'lb'}, '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. From f5e300a5debe44740f96db48d921d346ceec9b25 Mon Sep 17 00:00:00 2001 From: Zee Spencer <50284+zspencer@users.noreply.github.com> Date: Wed, 27 May 2020 11:47:07 -0700 Subject: [PATCH 03/20] Presenting a line item or variants options_text uses overriden values This changes how we display the description of weight, but it doesn't change the `Spree::OptionValue`s that are being created when someone adds a product to their cart. This takes us closer by making the UI look more correct; but it feels odd compared to settiong the `Spree::OptionValue` to the correct unit on creation. But on the other hand, that could possibly make things worse for the shipping calculation bits. --- .../variant_and_line_item_naming.rb | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/open_food_network/variant_and_line_item_naming.rb b/lib/open_food_network/variant_and_line_item_naming.rb index 7eed59b892..b38538a1a0 100644 --- a/lib/open_food_network/variant_and_line_item_naming.rb +++ b/lib/open_food_network/variant_and_line_item_naming.rb @@ -18,7 +18,18 @@ module OpenFoodNetwork order("#{Spree::OptionType.table_name}.position asc") end - values.map(&:presentation).to_sentence(words_connector: ", ", two_words_connector: ", ") + values.map { |option_value| presentation(option_value) }.to_sentence(words_connector: ", ", two_words_connector: ", ") + end + + def presentation(option_value) + if option_value.option_type.name == "unit_weight" + if has_attribute?(:display_as) && display_as.present? + return display_as + elsif respond_to?(:variant) && variant.present? && variant.respond_to?(:display_as) && variant.display_as.present? + return variant.display_as + end + end + option_value.presentation end def product_and_full_name @@ -48,9 +59,13 @@ module OpenFoodNetwork end def unit_to_display - return options_text if !has_attribute?(:display_as) || display_as.blank? - - display_as + if has_attribute?(:display_as) && display_as.present? + display_as + elsif respond_to?(:variant) && variant.present? && variant.respond_to?(:display_as) && variant.display_as.present? + variant.display_as + else + options_text + end end def update_units From e99799bca2195692c022481ec2943ae908dd8faa Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Fri, 7 Aug 2020 17:12:40 -0700 Subject: [PATCH 04/20] add ounces and round up for lbs --- .../products/services/variant_unit_manager.js.coffee | 12 +++--------- app/models/product_import/unit_converter.rb | 6 ++---- lib/open_food_network/option_value_namer.rb | 2 +- 3 files changed, 6 insertions(+), 14 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 845827adc4..f54b9bcbb1 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,15 +4,9 @@ angular.module("admin.products").factory "VariantUnitManager", -> 'weight': 1.0: 'g' 1000.0: 'kg' - 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' + 1000000.0: 'T' + 453.6: 'lb' + 28.34952: 'oz' '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 c7e5464329..f9467f4ac5 100644 --- a/app/models/product_import/unit_converter.rb +++ b/app/models/product_import/unit_converter.rb @@ -32,10 +32,8 @@ 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' }, + 'oz' => { scale: 28.34952, unit: 'weight' }, + 'lb' => { scale: 453.6, 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 896412856a..4a2f18010f 100644 --- a/lib/open_food_network/option_value_namer.rb +++ b/lib/open_food_network/option_value_namer.rb @@ -71,7 +71,7 @@ module OpenFoodNetwork # shop or producer basis? units = { 'weight' => { 1.0 => 'g', 1000.0 => 'kg', 1_000_000.0 => 'T', - 28.34952 => 'oz', 453.59237 => 'lb'}, + 28.34952 => 'oz', 453.6 => 'lb'}, '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. From 2f216039ac3b4035f88e8c11564696a083d9e9f6 Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Fri, 7 Aug 2020 17:25:37 -0700 Subject: [PATCH 05/20] update variantunitmanager test expectation --- .../admin/services/variant_unit_manager_spec.js.coffee | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/spec/javascripts/unit/admin/services/variant_unit_manager_spec.js.coffee b/spec/javascripts/unit/admin/services/variant_unit_manager_spec.js.coffee index ff70e2a1d1..b2948f38d5 100644 --- a/spec/javascripts/unit/admin/services/variant_unit_manager_spec.js.coffee +++ b/spec/javascripts/unit/admin/services/variant_unit_manager_spec.js.coffee @@ -28,16 +28,18 @@ describe "VariantUnitManager", -> expect(VariantUnitManager.getUnitName(1000, "volume")).toEqual "kL" describe "unitScales", -> - it "returns a set of scales for unit type weight", -> - expect(VariantUnitManager.unitScales('weight')).toEqual [1.0, 1000.0, 1000000.0] + it "returns a sorted set of scales for unit type weight", -> + expect(VariantUnitManager.unitScales('weight')).toEqual [1.0, 28.34952, 453.6, 1000.0, 1000000.0] - it "returns a set of scales for unit type volume", -> + it "returns a sorted set of scales for unit type volume", -> expect(VariantUnitManager.unitScales('volume')).toEqual [0.001, 1.0, 1000.0] describe "variantUnitOptions", -> it "returns an array of options", -> expect(VariantUnitManager.variantUnitOptions()).toEqual [ ["Weight (g)", "weight_1"], + ["Weight (oz)", "weight_28.34952"], + ["Weight (lb)", "weight_453.6"], ["Weight (kg)", "weight_1000"], ["Weight (T)", "weight_1000000"], ["Volume (mL)", "volume_0.001"], From e5e7e12a329b43561029db9fdeff1316cfe4260f Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Fri, 7 Aug 2020 17:28:44 -0700 Subject: [PATCH 06/20] green tests for sorted scales --- .../admin/products/services/variant_unit_manager.js.coffee | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 f54b9bcbb1..fc24c07f56 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 @@ -35,4 +35,5 @@ angular.module("admin.products").factory "VariantUnitManager", -> @unitNames[unitType][scale] @unitScales: (unitType) -> - (parseFloat(scale) for scale in Object.keys(@unitNames[unitType])).sort() + (parseFloat(scale) for scale in Object.keys(@unitNames[unitType])).sort (a, b) -> + a - b From 9b9b6ded092efbc1489782e326d273e798bd3e0a Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Fri, 7 Aug 2020 20:23:04 -0700 Subject: [PATCH 07/20] add each scales system in option_value_namer.rb --- lib/open_food_network/option_value_namer.rb | 41 +++++++++++++-------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/lib/open_food_network/option_value_namer.rb b/lib/open_food_network/option_value_namer.rb index 4a2f18010f..2c497b6c1a 100644 --- a/lib/open_food_network/option_value_namer.rb +++ b/lib/open_food_network/option_value_namer.rb @@ -63,25 +63,34 @@ module OpenFoodNetwork end def scale_for_unit_value - # TODO: We _think_ this will cause the following weird results: - # 29g of product would use the `oz` measurement - # 445g of product would use the `lb` measurement - # So we probably want to keep the metric and imperial measures - # in their own lanes; perhaps using a configurable value on a per - # shop or producer basis? + units = { + 'weight' => { + 1.0 => { 'name' => 'g', 'system' => 'metric' }, + 28.34952 => { 'name' => 'oz', 'system' => 'imperial' }, + 453.6 => { 'name' => 'lb', 'system' => 'imperial' }, + 1000.0 => { 'name' => 'kg', 'system' => 'metric' }, + 1_000_000.0 => { 'name' => 'T', 'system' => 'metric' } + }, + 'volume' => { + 0.001 => { 'name' => 'mL', 'system' => 'metric' }, + 1.0 => { 'name' => 'L', 'system' => 'metric' }, + 1000.0 => { 'name' => 'kL', 'system' => 'metric' } + } + } - units = { 'weight' => { 1.0 => 'g', 1000.0 => 'kg', 1_000_000.0 => 'T', - 28.34952 => 'oz', 453.6 => 'lb'}, - '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. + # Find the largest available and compatible unit where unit_value comes + # to >= 1 when expressed in it. # If there is none available where this is true, use the smallest available unit. - unit = units[@variant.product.variant_unit].select { |scale, _unit_name| - @variant.unit_value / scale >= 1 - }.to_a.last - unit = units[@variant.product.variant_unit].first if unit.nil? + scales = units[@variant.product.variant_unit] + product_scale = @variant.product.variant_unit_scale + product_scale_system = scales[product_scale.to_f]['system'] + largest_unit = scales.select { |scale, unit_info| + unit_info['system'] == product_scale_system && + @variant.unit_value/scale >= 1 + }.sort.last + largest_unit = units[@variant.product.variant_unit].first if largest_unit.nil? - unit + [largest_unit[0], largest_unit[1]["name"]] end def pluralize(unit_name, count) From 0018ef6eb4e678b5e287fea2a8e5c1535120d9ec Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Fri, 7 Aug 2020 20:54:25 -0700 Subject: [PATCH 08/20] refactor variant_unit_manager.coffee and add systems to scales --- .../services/variant_unit_manager.js.coffee | 42 +++++++++++++------ 1 file changed, 30 insertions(+), 12 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 fc24c07f56..2d2922924c 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 @@ -1,19 +1,35 @@ angular.module("admin.products").factory "VariantUnitManager", -> class VariantUnitManager - @unitNames: + @units: 'weight': - 1.0: 'g' - 1000.0: 'kg' - 1000000.0: 'T' - 453.6: 'lb' - 28.34952: 'oz' + 1.0: + name: 'g' + system: 'metric' + 1000.0: + name: 'kg' + system: 'metric' + 1000000.0: + name: 'T' + system: 'metric' + 453.6: + name: 'lb' + system: 'imperial' + 28.34952: + name: 'oz' + system: 'imperial' 'volume': - 0.001: 'mL' - 1.0: 'L' - 1000.0: 'kL' + 0.001: + name: 'mL' + system: 'metric' + 1.0: + name: 'L' + system: 'metric' + 1000.0: + name: 'kL' + system: 'metric' @variantUnitOptions: -> - options = for unit_type, scale_with_name of @unitNames + options = for unit_type, _ of @units for scale in @unitScales(unit_type) name = @getUnitName(scale, unit_type) ["#{I18n.t(unit_type)} (#{name})", "#{unit_type}_#{scale}"] @@ -32,8 +48,10 @@ angular.module("admin.products").factory "VariantUnitManager", -> unitScales[0] @getUnitName: (scale, unitType) -> - @unitNames[unitType][scale] + @units[unitType][scale]['name'] @unitScales: (unitType) -> - (parseFloat(scale) for scale in Object.keys(@unitNames[unitType])).sort (a, b) -> + (parseFloat(scale) for scale in Object.keys(@units[unitType])).sort (a, b) -> a - b + + @compatibleUnitScales From 08e6e5a45958f7bd70d434408b2185a0e8f971a9 Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Fri, 7 Aug 2020 22:10:46 -0700 Subject: [PATCH 09/20] add compatibleUnitScales function and spec --- .../admin/products/services/variant_unit_manager.js.coffee | 5 ++++- .../unit/admin/services/variant_unit_manager_spec.js.coffee | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) 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 2d2922924c..46e08c45b9 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 @@ -54,4 +54,7 @@ angular.module("admin.products").factory "VariantUnitManager", -> (parseFloat(scale) for scale in Object.keys(@units[unitType])).sort (a, b) -> a - b - @compatibleUnitScales + @compatibleUnitScales: (scale, unitType) -> + scaleSystem = @units[unitType][scale]['system'] + (parseFloat(scale) for scale, scaleInfo of @units[unitType] when scaleInfo['system'] == scaleSystem).sort (a, b) -> + a - b diff --git a/spec/javascripts/unit/admin/services/variant_unit_manager_spec.js.coffee b/spec/javascripts/unit/admin/services/variant_unit_manager_spec.js.coffee index b2948f38d5..f9664ad55b 100644 --- a/spec/javascripts/unit/admin/services/variant_unit_manager_spec.js.coffee +++ b/spec/javascripts/unit/admin/services/variant_unit_manager_spec.js.coffee @@ -34,6 +34,11 @@ describe "VariantUnitManager", -> it "returns a sorted set of scales for unit type volume", -> expect(VariantUnitManager.unitScales('volume')).toEqual [0.001, 1.0, 1000.0] + describe "compatibleUnitScales", -> + it "returns a sorted set of compatible scales based on the scale and unit type provided", -> + expect(VariantUnitManager.compatibleUnitScales(1, "weight")).toEqual [1.0, 1000.0, 1000000.0] + expect(VariantUnitManager.compatibleUnitScales(453.6, "weight")).toEqual [28.34952, 453.6] + describe "variantUnitOptions", -> it "returns an array of options", -> expect(VariantUnitManager.variantUnitOptions()).toEqual [ From a2993652c12374d0b668a9126bf310b733b79dbb Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Sat, 8 Aug 2020 08:19:16 -0700 Subject: [PATCH 10/20] get only compatible scales in option_value_namer.js.coffee --- .../services/option_value_namer.js.coffee | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/app/assets/javascripts/admin/products/services/option_value_namer.js.coffee b/app/assets/javascripts/admin/products/services/option_value_namer.js.coffee index 49a2f470be..8993e6a230 100644 --- a/app/assets/javascripts/admin/products/services/option_value_namer.js.coffee +++ b/app/assets/javascripts/admin/products/services/option_value_namer.js.coffee @@ -53,17 +53,18 @@ angular.module("admin.products").factory "OptionValueNamer", (VariantUnitManager [value, unit_name] scale_for_unit_value: -> - # Find the largest available unit where unit_value comes to >= 1 when expressed in it. - # If there is none available where this is true, use the smallest available unit. - unit = ([scale, unit_name] for scale, unit_name of VariantUnitManager.unitNames[@variant.product.variant_unit] when @variant.unit_value / scale >= 1).reduce (unit, [scale, unit_name]) -> - if (unit && scale > unit[0]) || !unit? - [scale, unit_name] - else - unit - , null - if !unit? - unit = ([scale, unit_name] for scale, unit_name of VariantUnitManager.unitNames[@variant.product.variant_unit]).reduce (unit, [scale, unit_name]) -> - if scale < unit[0] then [scale, unit_name] else unit - , [Infinity,""] + # Find the largest available and compatible unit where unit_value comes + # to >= 1 when expressed in it. + # If there is none available where this is true, use the smallest + # available unit. + product = @variant.product + scales = VariantUnitManager.compatibleUnitScales(product.variant_unit_scale, product.variant_unit) + variantUnitValue = @variant.unit_value - unit + # sets largestScale = last element in filtered scales array + [_, ..., largestScale] = (scales.filter (s) -> variantUnitValue / s >= 1) + + if (largestScale) + [largestScale, VariantUnitManager.getUnitName(largestScale, product.variant_unit)] + else + [scales[0], VariantUnitManager.getUnitName(scales[0], product.variant_unit)] From d7a8873ee9eedafaa473ab0a545a8f93ba7cf307 Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Sat, 8 Aug 2020 08:36:18 -0700 Subject: [PATCH 11/20] return empty string for unitName if no scale matches --- .../admin/products/services/variant_unit_manager.js.coffee | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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 46e08c45b9..d48ca458cd 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 @@ -48,7 +48,10 @@ angular.module("admin.products").factory "VariantUnitManager", -> unitScales[0] @getUnitName: (scale, unitType) -> - @units[unitType][scale]['name'] + if @units[unitType][scale] + @units[unitType][scale]['name'] + else + '' @unitScales: (unitType) -> (parseFloat(scale) for scale in Object.keys(@units[unitType])).sort (a, b) -> From 2fe9f4abc858c76b9d75b012f85852b875557fae Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Sat, 8 Aug 2020 17:35:05 -0700 Subject: [PATCH 12/20] style updates for rubocop --- lib/open_food_network/option_value_namer.rb | 16 ++++++++-------- .../variant_and_line_item_naming.rb | 10 +++++++--- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/lib/open_food_network/option_value_namer.rb b/lib/open_food_network/option_value_namer.rb index 2c497b6c1a..e7b929a029 100644 --- a/lib/open_food_network/option_value_namer.rb +++ b/lib/open_food_network/option_value_namer.rb @@ -65,15 +65,15 @@ module OpenFoodNetwork def scale_for_unit_value units = { 'weight' => { - 1.0 => { 'name' => 'g', 'system' => 'metric' }, - 28.34952 => { 'name' => 'oz', 'system' => 'imperial' }, - 453.6 => { 'name' => 'lb', 'system' => 'imperial' }, - 1000.0 => { 'name' => 'kg', 'system' => 'metric' }, + 1.0 => { 'name' => 'g', 'system' => 'metric' }, + 28.34952 => { 'name' => 'oz', 'system' => 'imperial' }, + 453.6 => { 'name' => 'lb', 'system' => 'imperial' }, + 1000.0 => { 'name' => 'kg', 'system' => 'metric' }, 1_000_000.0 => { 'name' => 'T', 'system' => 'metric' } }, 'volume' => { - 0.001 => { 'name' => 'mL', 'system' => 'metric' }, - 1.0 => { 'name' => 'L', 'system' => 'metric' }, + 0.001 => { 'name' => 'mL', 'system' => 'metric' }, + 1.0 => { 'name' => 'L', 'system' => 'metric' }, 1000.0 => { 'name' => 'kL', 'system' => 'metric' } } } @@ -86,8 +86,8 @@ module OpenFoodNetwork product_scale_system = scales[product_scale.to_f]['system'] largest_unit = scales.select { |scale, unit_info| unit_info['system'] == product_scale_system && - @variant.unit_value/scale >= 1 - }.sort.last + @variant.unit_value / scale >= 1 + }.max largest_unit = units[@variant.product.variant_unit].first if largest_unit.nil? [largest_unit[0], largest_unit[1]["name"]] diff --git a/lib/open_food_network/variant_and_line_item_naming.rb b/lib/open_food_network/variant_and_line_item_naming.rb index b38538a1a0..3b5a7de47c 100644 --- a/lib/open_food_network/variant_and_line_item_naming.rb +++ b/lib/open_food_network/variant_and_line_item_naming.rb @@ -18,14 +18,17 @@ module OpenFoodNetwork order("#{Spree::OptionType.table_name}.position asc") end - values.map { |option_value| presentation(option_value) }.to_sentence(words_connector: ", ", two_words_connector: ", ") + values.map { |option_value| + presentation(option_value) + }.to_sentence(words_connector: ", ", two_words_connector: ", ") end def presentation(option_value) if option_value.option_type.name == "unit_weight" if has_attribute?(:display_as) && display_as.present? return display_as - elsif respond_to?(:variant) && variant.present? && variant.respond_to?(:display_as) && variant.display_as.present? + elsif respond_to?(:variant) && variant.present? && + variant.respond_to?(:display_as) && variant.display_as.present? return variant.display_as end end @@ -61,7 +64,8 @@ module OpenFoodNetwork def unit_to_display if has_attribute?(:display_as) && display_as.present? display_as - elsif respond_to?(:variant) && variant.present? && variant.respond_to?(:display_as) && variant.display_as.present? + elsif respond_to?(:variant) && variant.present? && + variant.respond_to?(:display_as) && variant.display_as.present? variant.display_as else options_text From 55e448897f8b463368970dbbc7ca74f0918a14ce Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Wed, 12 Aug 2020 10:38:38 -0700 Subject: [PATCH 13/20] use fewer sigfigs for ounces; add spec to option_value_namer --- .../services/variant_unit_manager.js.coffee | 2 +- app/models/product_import/unit_converter.rb | 2 +- lib/open_food_network/option_value_namer.rb | 3 +-- .../services/variant_unit_manager_spec.js.coffee | 6 +++--- .../open_food_network/option_value_namer_spec.rb | 15 ++++++++++++--- 5 files changed, 18 insertions(+), 10 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 d48ca458cd..244d51cde6 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 @@ -14,7 +14,7 @@ angular.module("admin.products").factory "VariantUnitManager", -> 453.6: name: 'lb' system: 'imperial' - 28.34952: + 28.35: name: 'oz' system: 'imperial' 'volume': diff --git a/app/models/product_import/unit_converter.rb b/app/models/product_import/unit_converter.rb index f9467f4ac5..c5321a4990 100644 --- a/app/models/product_import/unit_converter.rb +++ b/app/models/product_import/unit_converter.rb @@ -32,7 +32,7 @@ module ProductImport { 'g' => { scale: 1, unit: 'weight' }, 'kg' => { scale: 1000, unit: 'weight' }, - 'oz' => { scale: 28.34952, unit: 'weight' }, + 'oz' => { scale: 28.35, unit: 'weight' }, 'lb' => { scale: 453.6, unit: 'weight' }, 't' => { scale: 1_000_000, unit: 'weight' }, 'ml' => { scale: 0.001, unit: 'volume' }, diff --git a/lib/open_food_network/option_value_namer.rb b/lib/open_food_network/option_value_namer.rb index e7b929a029..f47bddc20a 100644 --- a/lib/open_food_network/option_value_namer.rb +++ b/lib/open_food_network/option_value_namer.rb @@ -39,7 +39,6 @@ module OpenFoodNetwork if @variant.unit_value.present? if %w(weight volume).include? @variant.product.variant_unit value, unit_name = option_value_value_unit_scaled - else value = @variant.unit_value unit_name = pluralize(@variant.product.variant_unit_name, value) @@ -66,7 +65,7 @@ module OpenFoodNetwork units = { 'weight' => { 1.0 => { 'name' => 'g', 'system' => 'metric' }, - 28.34952 => { 'name' => 'oz', 'system' => 'imperial' }, + 28.35 => { 'name' => 'oz', 'system' => 'imperial' }, 453.6 => { 'name' => 'lb', 'system' => 'imperial' }, 1000.0 => { 'name' => 'kg', 'system' => 'metric' }, 1_000_000.0 => { 'name' => 'T', 'system' => 'metric' } diff --git a/spec/javascripts/unit/admin/services/variant_unit_manager_spec.js.coffee b/spec/javascripts/unit/admin/services/variant_unit_manager_spec.js.coffee index f9664ad55b..40f03cf8cb 100644 --- a/spec/javascripts/unit/admin/services/variant_unit_manager_spec.js.coffee +++ b/spec/javascripts/unit/admin/services/variant_unit_manager_spec.js.coffee @@ -29,7 +29,7 @@ describe "VariantUnitManager", -> describe "unitScales", -> it "returns a sorted set of scales for unit type weight", -> - expect(VariantUnitManager.unitScales('weight')).toEqual [1.0, 28.34952, 453.6, 1000.0, 1000000.0] + expect(VariantUnitManager.unitScales('weight')).toEqual [1.0, 28.35, 453.6, 1000.0, 1000000.0] it "returns a sorted set of scales for unit type volume", -> expect(VariantUnitManager.unitScales('volume')).toEqual [0.001, 1.0, 1000.0] @@ -37,13 +37,13 @@ describe "VariantUnitManager", -> describe "compatibleUnitScales", -> it "returns a sorted set of compatible scales based on the scale and unit type provided", -> expect(VariantUnitManager.compatibleUnitScales(1, "weight")).toEqual [1.0, 1000.0, 1000000.0] - expect(VariantUnitManager.compatibleUnitScales(453.6, "weight")).toEqual [28.34952, 453.6] + expect(VariantUnitManager.compatibleUnitScales(453.6, "weight")).toEqual [28.35, 453.6] describe "variantUnitOptions", -> it "returns an array of options", -> expect(VariantUnitManager.variantUnitOptions()).toEqual [ ["Weight (g)", "weight_1"], - ["Weight (oz)", "weight_28.34952"], + ["Weight (oz)", "weight_28.35"], ["Weight (lb)", "weight_453.6"], ["Weight (kg)", "weight_1000"], ["Weight (T)", "weight_1000000"], diff --git a/spec/lib/open_food_network/option_value_namer_spec.rb b/spec/lib/open_food_network/option_value_namer_spec.rb index 1e3ec2944c..88e215d5b6 100644 --- a/spec/lib/open_food_network/option_value_namer_spec.rb +++ b/spec/lib/open_food_network/option_value_namer_spec.rb @@ -83,12 +83,21 @@ module OpenFoodNetwork expect(subject.send(:option_value_value_unit)).to eq [1, 'kg'] end + it "returns only values that are in the same measurement systems" do + p = double(:product, variant_unit: 'weight', variant_unit_scale: 1.0) + allow(v).to receive(:product) { p } + allow(v).to receive(:unit_value) { 500 } + # 500g would convert to > 1 pound, but we don't want the namer to use + # pounds since it's in a different measurement system. + expect(subject.send(:option_value_value_unit)).to eq [500, 'g'] + end + it "generates values for all weight scales" do - [[1.0, 'g'], [1000.0, 'kg'], [1_000_000.0, 'T']].each do |scale, unit| + [[1.0, 'g'], [28.35, 'oz'], [453.6, 'lb'], [1000.0, 'kg'], [1_000_000.0, 'T']].each do |scale, unit| p = double(:product, variant_unit: 'weight', variant_unit_scale: scale) allow(v).to receive(:product) { p } - allow(v).to receive(:unit_value) { 100 * scale } - expect(subject.send(:option_value_value_unit)).to eq [100, unit] + allow(v).to receive(:unit_value) { 10.0 * scale } + expect(subject.send(:option_value_value_unit)).to eq [10, unit] end end From 404d7bbc43f200cbaa545c42c6ca305c2e41f5fa Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Wed, 12 Aug 2020 11:46:23 -0700 Subject: [PATCH 14/20] add test for options_text using g/lb --- spec/models/spree/line_item_spec.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/spec/models/spree/line_item_spec.rb b/spec/models/spree/line_item_spec.rb index 2836c2c51c..4410a00975 100644 --- a/spec/models/spree/line_item_spec.rb +++ b/spec/models/spree/line_item_spec.rb @@ -545,11 +545,24 @@ module Spree end describe "getting unit for display" do + let(:o) { create(:order) } + let(:p1) { create(:product, name: 'Clear Honey', variant_unit_scale: 1) } + let(:v1) { create(:variant, product: p1, unit_value: 500) } + let(:li1) { create(:line_item, order: o, product: p1, variant: v1) } + let(:p2) { create(:product, name: 'Clear United States Honey', variant_unit_scale: 453.6) } + let(:v2) { create(:variant, product: p2, unit_value: 453.6) } + let(:li2) { create(:line_item, order: o, product: p2, variant: v2) } + it "returns options_text" do li = create(:line_item) allow(li).to receive(:options_text).and_return "ponies" expect(li.unit_to_display).to eq("ponies") end + + it "returns options_text based on units" do + expect(li1.options_text).to eq("500g") + expect(li2.options_text).to eq("1lb") + end end context "when the line_item already has a final_weight_volume set (and all required option values do not exist)" do From 4bc3101f4d68206afb863db15a52a5fe71c50f3e Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Fri, 14 Aug 2020 15:05:09 -0700 Subject: [PATCH 15/20] Add specs for shipping weight calculations using lbs and oz --- spec/models/calculator/weight_spec.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/spec/models/calculator/weight_spec.rb b/spec/models/calculator/weight_spec.rb index 1ca1759b40..311e09775e 100644 --- a/spec/models/calculator/weight_spec.rb +++ b/spec/models/calculator/weight_spec.rb @@ -103,6 +103,28 @@ describe Calculator::Weight do expect(calculator.compute(line_item)).to eq(42_000) # 7000 * 6 end end + + context "when the product is in lb (1lb)" do + let!(:product_attributes) { { variant_unit: "weight", variant_unit_scale: 453.6 } } + let!(:variant_attributes) { { unit_value: 453.6, weight: 453.6 } } + + it "is correct" do + expect(line_item.final_weight_volume).to eq(907.2) # 2lb + line_item.final_weight_volume = 680.4 # 1.5lb + expect(calculator.compute(line_item)).to eq(4.08) # 0.6804 * 6 + end + end + + context "when the product is in oz (8oz)" do + let!(:product_attributes) { { variant_unit: "weight", variant_unit_scale: 28.35 } } + let!(:variant_attributes) { { unit_value: 226.8, weight: 226.8 } } + + it "is correct" do + expect(line_item.final_weight_volume).to eq(453.6) # 2 * 8oz == 1lb + line_item.final_weight_volume = 680.4 # 1.5lb + expect(calculator.compute(line_item)).to eq(4.08) # 0.6804 * 6 + end + end end context "when the product uses volume unit" do From 55f9fef2c3e9a3bd2200970502a3711335db5492 Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Fri, 14 Aug 2020 15:25:57 -0700 Subject: [PATCH 16/20] denote that weight is in grams on the bulk order management page --- config/locales/en.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/locales/en.yml b/config/locales/en.yml index 6cbd131c09..3eafb947d3 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -652,7 +652,7 @@ en: order_date: "Completed at" max: "Max" product_unit: "Product: Unit" - weight_volume: "Weight/Volume" + weight_volume: "Weight/Volume (g)" ask: "Ask?" page_title: "Bulk Order Management" actions_delete: "Delete Selected" From c6741dda3601bd280303a694c3a8cf4dc4569736 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Mon, 31 Aug 2020 15:54:39 +0100 Subject: [PATCH 17/20] Fix rubocop complexity issues by making code easier to read --- .../variant_and_line_item_naming.rb | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/open_food_network/variant_and_line_item_naming.rb b/lib/open_food_network/variant_and_line_item_naming.rb index 3b5a7de47c..c1c065efc2 100644 --- a/lib/open_food_network/variant_and_line_item_naming.rb +++ b/lib/open_food_network/variant_and_line_item_naming.rb @@ -24,17 +24,20 @@ module OpenFoodNetwork end def presentation(option_value) - if option_value.option_type.name == "unit_weight" - if has_attribute?(:display_as) && display_as.present? - return display_as - elsif respond_to?(:variant) && variant.present? && - variant.respond_to?(:display_as) && variant.display_as.present? - return variant.display_as - end - end + return option_value.presentation unless option_value.option_type.name == "unit_weight" + + return display_as if has_attribute?(:display_as) && display_as.present? + + return variant.display_as if variant_display_as? + option_value.presentation end + def variant_display_as? + respond_to?(:variant) && variant.present? && + variant.respond_to?(:display_as) && variant.display_as.present? + end + def product_and_full_name return "#{product.name} - #{full_name}" unless full_name.start_with? product.name @@ -62,14 +65,11 @@ module OpenFoodNetwork end def unit_to_display - if has_attribute?(:display_as) && display_as.present? - display_as - elsif respond_to?(:variant) && variant.present? && - variant.respond_to?(:display_as) && variant.display_as.present? - variant.display_as - else - options_text - end + return display_as if has_attribute?(:display_as) && display_as.present? + + return variant.display_as if variant_display_as? + + options_text end def update_units From 9204687e4db5f5aca637b0f452322a660cae0705 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Mon, 31 Aug 2020 15:59:30 +0100 Subject: [PATCH 18/20] Fix rubocop issues --- lib/open_food_network/option_value_namer.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/open_food_network/option_value_namer.rb b/lib/open_food_network/option_value_namer.rb index f47bddc20a..8d5fea6669 100644 --- a/lib/open_food_network/option_value_namer.rb +++ b/lib/open_food_network/option_value_namer.rb @@ -64,15 +64,15 @@ module OpenFoodNetwork def scale_for_unit_value units = { 'weight' => { - 1.0 => { 'name' => 'g', 'system' => 'metric' }, - 28.35 => { 'name' => 'oz', 'system' => 'imperial' }, - 453.6 => { 'name' => 'lb', 'system' => 'imperial' }, - 1000.0 => { 'name' => 'kg', 'system' => 'metric' }, - 1_000_000.0 => { 'name' => 'T', 'system' => 'metric' } + 1.0 => { 'name' => 'g', 'system' => 'metric' }, + 28.35 => { 'name' => 'oz', 'system' => 'imperial' }, + 453.6 => { 'name' => 'lb', 'system' => 'imperial' }, + 1000.0 => { 'name' => 'kg', 'system' => 'metric' }, + 1_000_000.0 => { 'name' => 'T', 'system' => 'metric' } }, 'volume' => { - 0.001 => { 'name' => 'mL', 'system' => 'metric' }, - 1.0 => { 'name' => 'L', 'system' => 'metric' }, + 0.001 => { 'name' => 'mL', 'system' => 'metric' }, + 1.0 => { 'name' => 'L', 'system' => 'metric' }, 1000.0 => { 'name' => 'kL', 'system' => 'metric' } } } From 83ae13d7c7af21db959155f8f0c2522b4feb6448 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Mon, 31 Aug 2020 16:18:36 +0100 Subject: [PATCH 19/20] Extract method to make code easier to read --- lib/open_food_network/option_value_namer.rb | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/open_food_network/option_value_namer.rb b/lib/open_food_network/option_value_namer.rb index 8d5fea6669..c6edb51d21 100644 --- a/lib/open_food_network/option_value_namer.rb +++ b/lib/open_food_network/option_value_namer.rb @@ -77,19 +77,25 @@ module OpenFoodNetwork } } - # Find the largest available and compatible unit where unit_value comes - # to >= 1 when expressed in it. - # If there is none available where this is true, use the smallest available unit. scales = units[@variant.product.variant_unit] product_scale = @variant.product.variant_unit_scale product_scale_system = scales[product_scale.to_f]['system'] + + largest_unit = find_largest_unit(scales, product_scale_system) + [largest_unit[0], largest_unit[1]["name"]] + end + + # Find the largest available and compatible unit where unit_value comes + # to >= 1 when expressed in it. + # If there is none available where this is true, use the smallest available unit. + def find_largest_unit(scales, product_scale_system) largest_unit = scales.select { |scale, unit_info| unit_info['system'] == product_scale_system && @variant.unit_value / scale >= 1 }.max - largest_unit = units[@variant.product.variant_unit].first if largest_unit.nil? + return scales.first if largest_unit.nil? - [largest_unit[0], largest_unit[1]["name"]] + largest_unit end def pluralize(unit_name, count) From e413920335a3c7efe407d84e8340e96667b05271 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Mon, 31 Aug 2020 16:30:23 +0100 Subject: [PATCH 20/20] Move both OptionValueNamer and VariantAndLineItemNaming to app/services/variant_units --- app/models/spree/line_item_decorator.rb | 4 ++-- app/models/spree/variant_decorator.rb | 4 ++-- .../services/variant_units}/option_value_namer.rb | 4 ++-- .../services/variant_units}/variant_and_line_item_naming.rb | 6 +++--- .../order_management/reports/bulk_coop/bulk_coop_report.rb | 4 ++-- lib/open_food_network/lettuce_share_report.rb | 5 +++-- spec/models/spree/variant_spec.rb | 6 +++--- .../variant_units}/option_value_namer_spec.rb | 2 +- 8 files changed, 18 insertions(+), 17 deletions(-) rename {lib/open_food_network => app/services/variant_units}/option_value_namer.rb (97%) rename {lib/open_food_network => app/services/variant_units}/variant_and_line_item_naming.rb (96%) rename spec/{lib/open_food_network => services/variant_units}/option_value_namer_spec.rb (99%) diff --git a/app/models/spree/line_item_decorator.rb b/app/models/spree/line_item_decorator.rb index c58b072892..558b2efc59 100644 --- a/app/models/spree/line_item_decorator.rb +++ b/app/models/spree/line_item_decorator.rb @@ -1,8 +1,8 @@ require 'open_food_network/scope_variant_to_hub' -require 'open_food_network/variant_and_line_item_naming' +require 'variant_units/variant_and_line_item_naming' Spree::LineItem.class_eval do - include OpenFoodNetwork::VariantAndLineItemNaming + include VariantUnits::VariantAndLineItemNaming include LineItemBasedAdjustmentHandling has_and_belongs_to_many :option_values, join_table: 'spree_option_values_line_items', class_name: 'Spree::OptionValue' diff --git a/app/models/spree/variant_decorator.rb b/app/models/spree/variant_decorator.rb index 41b1a4eb76..737b2a8da4 100644 --- a/app/models/spree/variant_decorator.rb +++ b/app/models/spree/variant_decorator.rb @@ -1,5 +1,5 @@ require 'open_food_network/enterprise_fee_calculator' -require 'open_food_network/variant_and_line_item_naming' +require 'variant_units/variant_and_line_item_naming' require 'concerns/variant_stock' Spree::Variant.class_eval do @@ -8,7 +8,7 @@ Spree::Variant.class_eval do # This file may be double-loaded in delayed job environment, so we check before # removing the Spree method to prevent error. remove_method :options_text if instance_methods(false).include? :options_text - include OpenFoodNetwork::VariantAndLineItemNaming + include VariantUnits::VariantAndLineItemNaming include VariantStock has_many :exchange_variants diff --git a/lib/open_food_network/option_value_namer.rb b/app/services/variant_units/option_value_namer.rb similarity index 97% rename from lib/open_food_network/option_value_namer.rb rename to app/services/variant_units/option_value_namer.rb index c6edb51d21..7cb8fc8547 100644 --- a/lib/open_food_network/option_value_namer.rb +++ b/app/services/variant_units/option_value_namer.rb @@ -2,7 +2,7 @@ require "open_food_network/i18n_inflections" -module OpenFoodNetwork +module VariantUnits class OptionValueNamer def initialize(variant = nil) @variant = variant @@ -99,7 +99,7 @@ module OpenFoodNetwork end def pluralize(unit_name, count) - I18nInflections.pluralize(unit_name, count) + OpenFoodNetwork::I18nInflections.pluralize(unit_name, count) end end end diff --git a/lib/open_food_network/variant_and_line_item_naming.rb b/app/services/variant_units/variant_and_line_item_naming.rb similarity index 96% rename from lib/open_food_network/variant_and_line_item_naming.rb rename to app/services/variant_units/variant_and_line_item_naming.rb index c1c065efc2..6e325c1fe5 100644 --- a/lib/open_food_network/variant_and_line_item_naming.rb +++ b/app/services/variant_units/variant_and_line_item_naming.rb @@ -2,9 +2,9 @@ # It contains all of our logic for creating and naming option values (which are associated # with both models) and methods for printing human readable "names" for instances of these models. -require 'open_food_network/option_value_namer' +require 'variant_units/option_value_namer' -module OpenFoodNetwork +module VariantUnits module VariantAndLineItemNaming # Copied and modified from Spree::Variant def options_text @@ -103,7 +103,7 @@ module OpenFoodNetwork if has_attribute?(:display_as) && display_as.present? display_as else - option_value_namer = OpenFoodNetwork::OptionValueNamer.new self + option_value_namer = VariantUnits::OptionValueNamer.new self option_value_namer.name end end diff --git a/engines/order_management/app/services/order_management/reports/bulk_coop/bulk_coop_report.rb b/engines/order_management/app/services/order_management/reports/bulk_coop/bulk_coop_report.rb index bc44c2b857..c9c8b822f1 100644 --- a/engines/order_management/app/services/order_management/reports/bulk_coop/bulk_coop_report.rb +++ b/engines/order_management/app/services/order_management/reports/bulk_coop/bulk_coop_report.rb @@ -194,11 +194,11 @@ module OrderManagement end def option_value_value(line_items) - OpenFoodNetwork::OptionValueNamer.new(line_items.first).value + VariantUnits::OptionValueNamer.new(line_items.first).value end def option_value_unit(line_items) - OpenFoodNetwork::OptionValueNamer.new(line_items.first).unit + VariantUnits::OptionValueNamer.new(line_items.first).unit end def order_billing_address_name(line_items) diff --git a/lib/open_food_network/lettuce_share_report.rb b/lib/open_food_network/lettuce_share_report.rb index c04a7b3adb..49fffbadac 100644 --- a/lib/open_food_network/lettuce_share_report.rb +++ b/lib/open_food_network/lettuce_share_report.rb @@ -1,4 +1,5 @@ require 'open_food_network/products_and_inventory_report_base' +require 'variant_units/option_value_namer' module OpenFoodNetwork class LettuceShareReport < ProductsAndInventoryReportBase @@ -27,8 +28,8 @@ module OpenFoodNetwork variant.product.name, variant.full_name, '', - OptionValueNamer.new(variant).value, - OptionValueNamer.new(variant).unit, + VariantUnits::OptionValueNamer.new(variant).value, + VariantUnits::OptionValueNamer.new(variant).unit, variant.price, '', gst(variant), diff --git a/spec/models/spree/variant_spec.rb b/spec/models/spree/variant_spec.rb index 7a3b2ac85c..86cbbf174c 100644 --- a/spec/models/spree/variant_spec.rb +++ b/spec/models/spree/variant_spec.rb @@ -1,5 +1,5 @@ require 'spec_helper' -require 'open_food_network/option_value_namer' +require 'variant_units/option_value_namer' module Spree describe Variant do @@ -436,7 +436,7 @@ module Spree let!(:v) { create(:variant, product: p, unit_value: 5, unit_description: 'bar', display_as: '') } it "requests the name of the new option_value from OptionValueName" do - expect_any_instance_of(OpenFoodNetwork::OptionValueNamer).to receive(:name).exactly(1).times.and_call_original + expect_any_instance_of(VariantUnits::OptionValueNamer).to receive(:name).exactly(1).times.and_call_original v.update(unit_value: 10, unit_description: 'foo') ov = v.option_values.last expect(ov.name).to eq("10g foo") @@ -448,7 +448,7 @@ module Spree let!(:v) { create(:variant, product: p, unit_value: 5, unit_description: 'bar', display_as: 'FOOS!') } it "does not request the name of the new option_value from OptionValueName" do - expect_any_instance_of(OpenFoodNetwork::OptionValueNamer).not_to receive(:name) + expect_any_instance_of(VariantUnits::OptionValueNamer).not_to receive(:name) v.update!(unit_value: 10, unit_description: 'foo') ov = v.option_values.last expect(ov.name).to eq("FOOS!") diff --git a/spec/lib/open_food_network/option_value_namer_spec.rb b/spec/services/variant_units/option_value_namer_spec.rb similarity index 99% rename from spec/lib/open_food_network/option_value_namer_spec.rb rename to spec/services/variant_units/option_value_namer_spec.rb index 88e215d5b6..3e153896fd 100644 --- a/spec/lib/open_food_network/option_value_namer_spec.rb +++ b/spec/services/variant_units/option_value_namer_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -module OpenFoodNetwork +module VariantUnits describe OptionValueNamer do describe "generating option value name" do let(:v) { Spree::Variant.new }