diff --git a/app/services/weights_and_measures.rb b/app/services/weights_and_measures.rb index c2bc8c313cd..79da11cf525 100644 --- a/app/services/weights_and_measures.rb +++ b/app/services/weights_and_measures.rb @@ -24,16 +24,25 @@ def system UNITS = { 'weight' => { + 0.001 => { 'name' => 'mg', 'system' => 'metric' }, 1.0 => { 'name' => 'g', 'system' => 'metric' }, + 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' }, 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' }, + 0.01 => { 'name' => 'cL', 'system' => 'metric' }, + 0.1 => { 'name' => 'dL', 'system' => 'metric' }, 1.0 => { 'name' => 'L', 'system' => 'metric' }, - 1000.0 => { 'name' => 'kL', 'system' => 'metric' } + 1000.0 => { 'name' => 'kL', 'system' => 'metric' }, + + 0.25 => { 'name' => 'cu', 'system' => 'imperial' }, + 4.54609 => { 'name' => 'gal', 'system' => 'imperial' }, } }.freeze diff --git a/engines/dfc_provider/app/services/quantitative_value_builder.rb b/engines/dfc_provider/app/services/quantitative_value_builder.rb index 5d7ac7e1424..9903aa5965f 100644 --- a/engines/dfc_provider/app/services/quantitative_value_builder.rb +++ b/engines/dfc_provider/app/services/quantitative_value_builder.rb @@ -31,7 +31,7 @@ def self.apply(quantity, product) measure, unit_name, unit_scale = map_unit(quantity.unit) product.variant_unit = measure - product.variant_unit_name = unit_name + product.variant_unit_name = unit_name if measure == "items" product.variant_unit_scale = unit_scale product.unit_value = quantity.value * unit_scale end @@ -69,34 +69,38 @@ def self.apply(quantity, product) def self.map_unit(unit) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength quantity_unit = DfcLoader.connector.MEASURES.UNIT.QUANTITYUNIT + # The unit name is only set for items. The name is implied for weight and + # volume and filled in by `WeightsAndMeasures`. case unit when quantity_unit.LITRE - ["volume", "liter", 1] + ["volume", nil, 1] when quantity_unit.MILLILITRE - ["volume", "ml", 0.001] + ["volume", nil, 0.001] when quantity_unit.CENTILITRE - ["volume", "cl", 0.01] + ["volume", nil, 0.01] when quantity_unit.DECILITRE - ["volume", "dl", 0.1] + ["volume", nil, 0.1] when quantity_unit.CUP # Interpreted as metric cup, not US legal cup. # https://github.com/datafoodconsortium/taxonomies/issues/8 - ["volume", "cu", 0.25] + ["volume", nil, 0.25] when quantity_unit.GALLON - ["volume", "gal", 4.54609] + ["volume", nil, 4.54609] + when quantity_unit.MILLIGRAM - ["weight", "mg", 0.001] + ["weight", nil, 0.001] when quantity_unit.GRAM - ["weight", "gram", 1] + ["weight", nil, 1] when quantity_unit.KILOGRAM - ["weight", "kg", 1_000] + ["weight", nil, 1_000] when quantity_unit.TONNE - ["weight", "kg", 1_000_000] + ["weight", nil, 1_000_000] # Not part of the DFC yet: # when quantity_unit.OUNCE - # ["weight", "oz", 28.349523125] + # ["weight", nil, 28.349523125] when quantity_unit.POUNDMASS - ["weight", "lb", 453.59237] + ["weight", nil, 453.59237] + when quantity_unit.PAIR ["items", "pair", 2] when quantity_unit._4PACK diff --git a/engines/dfc_provider/spec/services/quantitative_value_builder_spec.rb b/engines/dfc_provider/spec/services/quantitative_value_builder_spec.rb index ed9a4a09e35..d09067f8166 100644 --- a/engines/dfc_provider/spec/services/quantitative_value_builder_spec.rb +++ b/engines/dfc_provider/spec/services/quantitative_value_builder_spec.rb @@ -71,7 +71,7 @@ builder.apply(quantity, product) expect(product.variant_unit).to eq "volume" - expect(product.variant_unit_name).to eq "liter" + expect(product.variant_unit_name).to eq nil expect(product.variant_unit_scale).to eq 1 expect(product.unit_value).to eq 2 end @@ -85,7 +85,7 @@ builder.apply(quantity, product) expect(product.variant_unit).to eq "weight" - expect(product.variant_unit_name).to eq "kg" + expect(product.variant_unit_name).to eq nil expect(product.variant_unit_scale).to eq 1_000 expect(product.unit_value).to eq 4_000 end @@ -99,7 +99,7 @@ builder.apply(quantity, product) expect(product.variant_unit).to eq "weight" - expect(product.variant_unit_name).to eq "mg" + expect(product.variant_unit_name).to eq nil expect(product.variant_unit_scale).to eq 0.001 expect(product.unit_value).to eq 0.005 end @@ -113,7 +113,7 @@ builder.apply(quantity, product) expect(product.variant_unit).to eq "weight" - expect(product.variant_unit_name).to eq "lb" + expect(product.variant_unit_name).to eq nil expect(product.variant_unit_scale).to eq 453.59237 expect(product.unit_value).to eq 4_535.9237 end diff --git a/spec/services/variant_units/option_value_namer_spec.rb b/spec/services/variant_units/option_value_namer_spec.rb index a902d353064..cf9178c2443 100644 --- a/spec/services/variant_units/option_value_namer_spec.rb +++ b/spec/services/variant_units/option_value_namer_spec.rb @@ -115,8 +115,8 @@ module VariantUnits p = double(:product, variant_unit: 'volume', variant_unit_scale: scale) allow(v).to receive(:product) { p } allow(p).to receive(:persisted?) { true } - 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) { 3 * scale } + expect(subject.send(:option_value_value_unit)).to eq [3, unit] end end