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.
This commit is contained in:
Gaetan Craig-Riou
2024-08-06 15:39:24 +10:00
parent 4ad6971121
commit 25171413ef
2 changed files with 21 additions and 0 deletions

View File

@@ -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',

View File

@@ -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