From e06843c44555d107887bb909b08b104a0eb0059f Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Fri, 19 Feb 2021 13:39:40 -0800 Subject: [PATCH 1/3] ensure unit_value is a number --- app/models/spree/variant.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/spree/variant.rb b/app/models/spree/variant.rb index bd72ac17ec..beac8eaf4b 100644 --- a/app/models/spree/variant.rb +++ b/app/models/spree/variant.rb @@ -64,8 +64,8 @@ module Spree } before_validation :set_cost_currency - before_validation :update_weight_from_unit_value, if: ->(v) { v.product.present? } before_validation :ensure_unit_value + before_validation :update_weight_from_unit_value, if: ->(v) { v.product.present? } after_save :save_default_price after_save :update_units @@ -300,7 +300,7 @@ module Spree end def ensure_unit_value - return unless product&.variant_unit == "items" && unit_value.nil? + return unless (product&.variant_unit == "items" && unit_value.nil?) || unit_value&.nan? self.unit_value = 1.0 end From dec6d2189fd1ec443c9a038947613b4a308c8348 Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Sun, 21 Feb 2021 22:26:36 -0800 Subject: [PATCH 2/3] add unit test for unit_value --- spec/models/spree/variant_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/models/spree/variant_spec.rb b/spec/models/spree/variant_spec.rb index cfd0d41d8b..a2c2b47f22 100644 --- a/spec/models/spree/variant_spec.rb +++ b/spec/models/spree/variant_spec.rb @@ -799,5 +799,13 @@ module Spree expect(variant.unit_value).to eq 1 end end + + context "trying to set an invalid unit_value" do + it "does not allow NaN" do + variant.update(unit_value: Float::NAN) + + expect(variant.reload.unit_value).to eq(1.0) + end + end end end From 5dcda317d28285da209d8a80828d794b101e2e3b Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Sun, 21 Feb 2021 22:27:58 -0800 Subject: [PATCH 3/3] add bugsnag alert --- app/models/spree/variant.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/spree/variant.rb b/app/models/spree/variant.rb index beac8eaf4b..234f6ee248 100644 --- a/app/models/spree/variant.rb +++ b/app/models/spree/variant.rb @@ -300,6 +300,7 @@ module Spree end def ensure_unit_value + Bugsnag.notify("Trying to set unit_value to NaN") if unit_value&.nan? return unless (product&.variant_unit == "items" && unit_value.nil?) || unit_value&.nan? self.unit_value = 1.0