Merge pull request #3614 from mkllnk/3335-localized-number-for-weight-calculator

Support international decimals in weight calculator
This commit is contained in:
Pau Pérez Fabregat
2019-03-22 10:35:37 +01:00
committed by GitHub
4 changed files with 27 additions and 7 deletions

View File

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

View File

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

View File

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

View File

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