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/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 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) 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|