From bba59c1ffd0085bfdafd328af6bbe7d2286be574 Mon Sep 17 00:00:00 2001 From: David Cook Date: Thu, 18 Jul 2024 11:06:42 +1000 Subject: [PATCH 1/3] Force significant: false for NumberHelper Although it defaults to false, somehow it seems to be evaluated as true on hu_prod...??!?!!1! (https://github.com/openfoodfoundation/openfoodnetwork/issues/12682) Maybe this will help. --- app/services/weights_and_measures.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/services/weights_and_measures.rb b/app/services/weights_and_measures.rb index 1cb70b4752..78846e6e85 100644 --- a/app/services/weights_and_measures.rb +++ b/app/services/weights_and_measures.rb @@ -25,7 +25,7 @@ class WeightsAndMeasures available_units_sorted.flat_map do |measurement, measurement_info| measurement_info.filter_map do |scale, unit_info| scale_clean = - ActiveSupport::NumberHelper.number_to_rounded(scale, precision: nil, + ActiveSupport::NumberHelper.number_to_rounded(scale, precision: nil, significant: false, strip_insignificant_zeros: true) [ "#{I18n.t(measurement)} (#{unit_info['name']})", # Label (eg "Weight (g)") From 4925e2088d5f844324888f8a67e8c442d07ffd49 Mon Sep 17 00:00:00 2001 From: David Cook Date: Thu, 18 Jul 2024 11:39:04 +1000 Subject: [PATCH 2/3] Test for corrupt data Ah ha, we found the problem. --- spec/services/weights_and_measures_spec.rb | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/spec/services/weights_and_measures_spec.rb b/spec/services/weights_and_measures_spec.rb index 22d337308c..3f6d742b33 100644 --- a/spec/services/weights_and_measures_spec.rb +++ b/spec/services/weights_and_measures_spec.rb @@ -58,6 +58,45 @@ RSpec.describe WeightsAndMeasures do expect(subject.system).to eq("custom") end end + + # In the event of corrupt data, we don't want an exception + context "corrupt data" do + it "when unit is invalid, scale is valid" do + allow(product).to receive(:variant_unit) { "blah" } + allow(product).to receive(:variant_unit_scale) { 1.0 } + expect(subject.system).to eq("custom") + end + + it "when unit is invalid, scale is nil" do + allow(product).to receive(:variant_unit) { "blah" } + allow(product).to receive(:variant_unit_scale) { nil } + expect(subject.system).to eq("custom") + end + + it "when unit is nil, scale is valid" do + allow(product).to receive(:variant_unit) { nil } + allow(product).to receive(:variant_unit_scale) { 1.0 } + expect(subject.system).to eq("custom") + end + + it "when unit is nil, scale is nil" do + allow(product).to receive(:variant_unit) { nil } + allow(product).to receive(:variant_unit_scale) { nil } + expect(subject.system).to eq("custom") + end + + it "when unit is valid, but scale is nil" do + allow(product).to receive(:variant_unit) { "weight" } + allow(product).to receive(:variant_unit_scale) { nil } + expect(subject.system).to eq("custom") + end + + pending "when unit is valid, but scale is 0" do + allow(product).to receive(:variant_unit) { "weight" } + allow(product).to receive(:variant_unit_scale) { 0.0 } + expect(subject.system).to eq("custom") + end + end end describe "#variant_unit_options" do From 918d4401ff661a14bb9647b7c9e94cfd6f3a193c Mon Sep 17 00:00:00 2001 From: David Cook Date: Thu, 18 Jul 2024 11:40:07 +1000 Subject: [PATCH 3/3] Gracefully handle empty string --- app/services/weights_and_measures.rb | 6 ++++-- spec/services/weights_and_measures_spec.rb | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app/services/weights_and_measures.rb b/app/services/weights_and_measures.rb index 78846e6e85..170dacb208 100644 --- a/app/services/weights_and_measures.rb +++ b/app/services/weights_and_measures.rb @@ -15,9 +15,11 @@ class WeightsAndMeasures def system return "custom" unless scales = scales_for_variant_unit(ignore_available_units: true) - return "custom" unless product_scale = @variant.product.variant_unit_scale - scales[product_scale.to_f]['system'] + product_scale = @variant.product.variant_unit_scale&.to_f + return "custom" unless product_scale.present? && product_scale.positive? + + scales[product_scale]['system'] end # @returns enumerable with label and value for select diff --git a/spec/services/weights_and_measures_spec.rb b/spec/services/weights_and_measures_spec.rb index 3f6d742b33..0992316886 100644 --- a/spec/services/weights_and_measures_spec.rb +++ b/spec/services/weights_and_measures_spec.rb @@ -91,7 +91,7 @@ RSpec.describe WeightsAndMeasures do expect(subject.system).to eq("custom") end - pending "when unit is valid, but scale is 0" do + it "when unit is valid, but scale is 0" do allow(product).to receive(:variant_unit) { "weight" } allow(product).to receive(:variant_unit_scale) { 0.0 } expect(subject.system).to eq("custom")