mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Merge pull request #12683 from dacook/buu-troubleshoot-12682
[BUU] Handle corrupt data and troubleshooting
This commit is contained in:
@@ -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
|
||||
@@ -25,7 +27,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)")
|
||||
|
||||
@@ -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
|
||||
|
||||
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")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#variant_unit_options" do
|
||||
|
||||
Reference in New Issue
Block a user