From 2ef9e34f281cc5e884bb7d9bfa67484f4c28b485 Mon Sep 17 00:00:00 2001 From: David Cook Date: Wed, 21 Feb 2024 15:18:42 +1100 Subject: [PATCH] Re-arrange imperial units Who would have guessed it was this complicated. Fingers crossed this doesn't break any other functionality... --- app/services/weights_and_measures.rb | 13 ++++++++++--- spec/services/weights_and_measures_spec.rb | 7 +++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/app/services/weights_and_measures.rb b/app/services/weights_and_measures.rb index 7d9aa19b3d..059075b7f8 100644 --- a/app/services/weights_and_measures.rb +++ b/app/services/weights_and_measures.rb @@ -45,9 +45,16 @@ class WeightsAndMeasures def self.available_units_sorted self::UNITS.transform_values do |measurement_info| + # Filter to only include available units measurement_info.filter do |_scale, unit_info| available_units.include?(unit_info['name']) - end.sort.to_h # sort by unit (hash key) + end. + # Remove duplicates by name + uniq do |_scale, unit_info| + unit_info['name'] + end. + # Sort by unit number + sort.to_h end end @@ -60,10 +67,10 @@ class WeightsAndMeasures 1000.0 => { 'name' => 'kg', 'system' => 'metric' }, 1_000_000.0 => { 'name' => 'T', 'system' => 'metric' }, - 28.349523125 => { 'name' => 'oz', 'system' => 'imperial' }, 28.35 => { 'name' => 'oz', 'system' => 'imperial' }, - 453.59237 => { 'name' => 'lb', 'system' => 'imperial' }, + 28.349523125 => { 'name' => 'oz', 'system' => 'imperial' }, 453.6 => { 'name' => 'lb', 'system' => 'imperial' }, + 453.59237 => { 'name' => 'lb', 'system' => 'imperial' }, }, 'volume' => { 0.001 => { 'name' => 'mL', 'system' => 'metric' }, diff --git a/spec/services/weights_and_measures_spec.rb b/spec/services/weights_and_measures_spec.rb index 6748b3c034..59877062ec 100644 --- a/spec/services/weights_and_measures_spec.rb +++ b/spec/services/weights_and_measures_spec.rb @@ -30,6 +30,11 @@ describe WeightsAndMeasures do allow(product).to receive(:variant_unit_scale) { 28.35 } expect(subject.system).to eq("imperial") end + + it "when precise scale is for an imperial unit" do + allow(product).to receive(:variant_unit_scale) { 28.349523125 } + expect(subject.system).to eq("imperial") + end end context "volume" do @@ -79,7 +84,6 @@ describe WeightsAndMeasures do ["Volume (kL)", "volume_1000"], ["Items", "items"], ] - pending "imperial measurements are duplicated" expect(subject).to match_array expected_array # diff each element expect(subject).to eq expected_array # test ordering also end @@ -97,7 +101,6 @@ describe WeightsAndMeasures do ["Volume (L)", "volume_1"], ["Items", "items"], ] - pending "imperial measurements are duplicated" expect(subject).to match_array expected_array # diff each element expect(subject).to eq expected_array # test ordering also end