From 1ea2f37c18efa845974d41f8fae9473aa78be3a2 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Mon, 18 Mar 2019 17:29:58 +1100 Subject: [PATCH 1/3] Support international decimals in weight calculator A previous pull request added support for flexible decimal characters when editing money amounts. https://github.com/openfoodfoundation/openfoodnetwork/pull/1831 This pull request applies the same principle to the weight calculator which was missed in the previous pull request. --- app/models/calculator/weight.rb | 4 ++++ spec/models/calculator/weight_spec.rb | 2 ++ 2 files changed, 6 insertions(+) diff --git a/app/models/calculator/weight.rb b/app/models/calculator/weight.rb index 410a53b2f8..a10245bc55 100644 --- a/app/models/calculator/weight.rb +++ b/app/models/calculator/weight.rb @@ -1,7 +1,11 @@ +require 'spree/localized_number' + module Calculator class Weight < Spree::Calculator + extend Spree::LocalizedNumber preference :per_kg, :decimal, default: 0.0 attr_accessible :preferred_per_kg + localize_number :preferred_per_kg def self.description I18n.t('spree.weight') diff --git a/spec/models/calculator/weight_spec.rb b/spec/models/calculator/weight_spec.rb index c45da8850a..028e82d829 100644 --- a/spec/models/calculator/weight_spec.rb +++ b/spec/models/calculator/weight_spec.rb @@ -1,6 +1,8 @@ require 'spec_helper' describe Calculator::Weight do + it_behaves_like "a model using the LocalizedNumber module", [:preferred_per_kg] + it "computes shipping cost for an order by total weight" do variant1 = build(:variant, weight: 10) variant2 = build(:variant, weight: 20) From 3ef68782332ef2cec96b159da7b447693c571f58 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Tue, 19 Mar 2019 11:32:18 +1100 Subject: [PATCH 2/3] Avoid global state change by using config stub --- spec/support/localized_number_helper.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/spec/support/localized_number_helper.rb b/spec/support/localized_number_helper.rb index 1b1192b3d4..996374d549 100644 --- a/spec/support/localized_number_helper.rb +++ b/spec/support/localized_number_helper.rb @@ -1,10 +1,6 @@ shared_examples "a model using the LocalizedNumber module" do |attributes| before do - Spree::Config[:enable_localized_number?] = true - end - - after do - Spree::Config[:enable_localized_number?] = false + allow(Spree::Config).to receive(:enable_localized_number?).and_return true end attributes.each do |attribute| From 38030944099511a7c04f189cc084d3941f7911dc Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Tue, 19 Mar 2019 12:03:34 +1100 Subject: [PATCH 3/3] Add more examples to LocalizedNumber spec --- spec/lib/spree/localized_number_spec.rb | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/spec/lib/spree/localized_number_spec.rb b/spec/lib/spree/localized_number_spec.rb index 8f2bbc7794..6112b953fd 100644 --- a/spec/lib/spree/localized_number_spec.rb +++ b/spec/lib/spree/localized_number_spec.rb @@ -1,7 +1,19 @@ require 'spec_helper' describe Spree::LocalizedNumber do - context ".parse" do + describe ".parse" do + context "with point separator" do + it "captures the proper amount for a formatted string" do + expect(described_class.parse('5.67')).to eql 5.67 + end + end + + context "with comma separator" do + it "captures the proper amount for a formatted string" do + expect(described_class.parse('5,67')).to eql 5.67 + end + end + context "with decimal point" do it "captures the proper amount for a formatted string" do expect(described_class.parse('1,599.99')).to eql 1599.99 @@ -19,9 +31,15 @@ describe Spree::LocalizedNumber do expect(described_class.parse(1599.99)).to eql 1599.99 end end + + context "with a string having 2 digits between separators" do + it "ignores the left separator" do + expect(described_class.parse('1,59.99')).to eql 159.99 + end + end end - context ".valid_localizable_number?" do + describe ".valid_localizable_number?" do context "with a properly formatted string" do it "returns true" do expect(described_class.valid_localizable_number?('1.599,99')).to eql true