Merge pull request #6926 from andrewpbrett/check-nan

Ensure unit_value is a number
This commit is contained in:
Maikel
2021-02-23 10:40:00 +11:00
committed by GitHub
2 changed files with 11 additions and 2 deletions

View File

@@ -63,8 +63,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
@@ -249,7 +249,8 @@ module Spree
end
def ensure_unit_value
return unless product&.variant_unit == "items" && unit_value.nil?
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
end

View File

@@ -733,5 +733,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