From 25171413efe7cfbb4c25abbd438c594c121a7444 Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Tue, 6 Aug 2024 15:39:24 +1000 Subject: [PATCH] Update Spree::Price parsing to match LocalizedNumber.parse Spree::Price parsing was returning 0.0 when given a an empty string as price, resulting in a variant being valid even if no price was given. It only happened if `Spree::LocalizedNumber` wasn't used. Spree::LocalizedNumber` return nil if given a blank number. --- app/models/spree/price.rb | 1 + spec/models/spree/price_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/app/models/spree/price.rb b/app/models/spree/price.rb index 0c2d12c285..9fcdac6459 100644 --- a/app/models/spree/price.rb +++ b/app/models/spree/price.rb @@ -38,6 +38,7 @@ module Spree # strips all non-price-like characters from the price, taking into account locale settings def parse_price(price) + return nil if price.blank? return price unless price.is_a?(String) separator, _delimiter = I18n.t([:'number.currency.format.separator', diff --git a/spec/models/spree/price_spec.rb b/spec/models/spree/price_spec.rb index 441a7a185b..047d5da268 100644 --- a/spec/models/spree/price_spec.rb +++ b/spec/models/spree/price_spec.rb @@ -34,5 +34,25 @@ module Spree expect(variant.reload.price).to eq 10.25 end end + + describe "#price=" do + subject { Spree::Price.new } + + context "with a number" do + it "returns the same number" do + subject.price = 12.5 + + expect(subject.price).to eq(12.5) + end + end + + context "with empty string" do + it "sets the price to nil" do + subject.price = "" + + expect(subject.price).to be_nil + end + end + end end end