From 427dc549458a7ee9ac30eb8654cb6ee2f0f6b9ea Mon Sep 17 00:00:00 2001 From: blainebillings Date: Thu, 20 Feb 2020 09:28:43 -0500 Subject: [PATCH 1/4] Change Result of PriceSack Calculation from Integers to Floats --- app/models/spree/calculator/price_sack_decorator.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/spree/calculator/price_sack_decorator.rb b/app/models/spree/calculator/price_sack_decorator.rb index 41aeabf330..b474f77861 100644 --- a/app/models/spree/calculator/price_sack_decorator.rb +++ b/app/models/spree/calculator/price_sack_decorator.rb @@ -17,9 +17,9 @@ module Spree order_amount = line_items_for(object).map { |x| x.price * x.quantity }.sum if order_amount < min - cost = preferred_normal_amount.to_i + cost = preferred_normal_amount.to_f elsif order_amount >= min - cost = preferred_discount_amount.to_i + cost = preferred_discount_amount.to_f end cost From 993a684e446e20e5333b71ef38eb929de5a3a19b Mon Sep 17 00:00:00 2001 From: blainebillings Date: Tue, 25 Feb 2020 09:28:54 -0500 Subject: [PATCH 2/4] Add Price Sack Spec for Float Amounts --- spec/models/spree/calculator/price_sack_spec.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/spec/models/spree/calculator/price_sack_spec.rb b/spec/models/spree/calculator/price_sack_spec.rb index 9b0a38d0e8..f77cd01da4 100644 --- a/spec/models/spree/calculator/price_sack_spec.rb +++ b/spec/models/spree/calculator/price_sack_spec.rb @@ -8,6 +8,13 @@ describe Spree::Calculator::PriceSack do calculator.preferred_discount_amount = 1 calculator end + let(:floatCalculator) do + floatCalculator = Spree::Calculator::PriceSack.new + floatCalculator.preferred_minimal_amount = 5 + floatCalculator.preferred_normal_amount = 10.4 + floatCalculator.preferred_discount_amount = 1.2 + floatCalculator + end let(:line_item) { build(:line_item, price: price, quantity: 2) } @@ -16,6 +23,11 @@ describe Spree::Calculator::PriceSack do it "uses the preferred normal amount" do expect(calculator.compute(line_item)).to eq(10) end + context "and preferred normal amount is float" do + it "uses the float preferred normal amount" do + expect(floatCalculator.compute(line_item)).to eq(10.4) + end + end end context 'when the order amount is above preferred minimal' do @@ -23,6 +35,11 @@ describe Spree::Calculator::PriceSack do it "uses the preferred discount amount" do expect(calculator.compute(line_item)).to eq(1) end + context "and preferred discount amount is float" do + it "uses the float preferred discount amount" do + expect(floatCalculator.compute(line_item)).to eq(1.2) + end + end end context "extends LocalizedNumber" do From d23397f250b68dd023deb8ec17e2bba17fc3fdd6 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Fri, 17 Apr 2020 14:49:09 +0100 Subject: [PATCH 3/4] Move float test to a separate context --- .../spree/calculator/price_sack_spec.rb | 40 ++++++++++++------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/spec/models/spree/calculator/price_sack_spec.rb b/spec/models/spree/calculator/price_sack_spec.rb index f77cd01da4..751f337cee 100644 --- a/spec/models/spree/calculator/price_sack_spec.rb +++ b/spec/models/spree/calculator/price_sack_spec.rb @@ -8,36 +8,46 @@ describe Spree::Calculator::PriceSack do calculator.preferred_discount_amount = 1 calculator end - let(:floatCalculator) do - floatCalculator = Spree::Calculator::PriceSack.new - floatCalculator.preferred_minimal_amount = 5 - floatCalculator.preferred_normal_amount = 10.4 - floatCalculator.preferred_discount_amount = 1.2 - floatCalculator - end - let(:line_item) { build(:line_item, price: price, quantity: 2) } context 'when the order amount is below preferred minimal' do let(:price) { 2 } + it "uses the preferred normal amount" do expect(calculator.compute(line_item)).to eq(10) end - context "and preferred normal amount is float" do - it "uses the float preferred normal amount" do - expect(floatCalculator.compute(line_item)).to eq(10.4) - end - end end context 'when the order amount is above preferred minimal' do let(:price) { 6 } + it "uses the preferred discount amount" do expect(calculator.compute(line_item)).to eq(1) end - context "and preferred discount amount is float" do + end + + context "preferred discount amount is float" do + let(:float_calculator) do + float_calculator = Spree::Calculator::PriceSack.new + float_calculator.preferred_minimal_amount = 5 + float_calculator.preferred_normal_amount = 10.4 + float_calculator.preferred_discount_amount = 1.2 + float_calculator + end + + context 'when the order amount is below preferred minimal' do + let(:price) { 2 } + + it "uses the float preferred normal amount" do + expect(float_calculator.compute(line_item)).to eq(10.4) + end + end + + context 'when the order amount is above preferred minimal' do + let(:price) { 6 } + it "uses the float preferred discount amount" do - expect(floatCalculator.compute(line_item)).to eq(1.2) + expect(float_calculator.compute(line_item)).to eq(1.2) end end end From 914751842258fe025246cf2ddb056777bf4273b3 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Fri, 17 Apr 2020 14:51:06 +0100 Subject: [PATCH 4/4] Remove some unnecessary code --- spec/models/spree/calculator/price_sack_spec.rb | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/spec/models/spree/calculator/price_sack_spec.rb b/spec/models/spree/calculator/price_sack_spec.rb index 751f337cee..921b657f88 100644 --- a/spec/models/spree/calculator/price_sack_spec.rb +++ b/spec/models/spree/calculator/price_sack_spec.rb @@ -27,19 +27,16 @@ describe Spree::Calculator::PriceSack do end context "preferred discount amount is float" do - let(:float_calculator) do - float_calculator = Spree::Calculator::PriceSack.new - float_calculator.preferred_minimal_amount = 5 - float_calculator.preferred_normal_amount = 10.4 - float_calculator.preferred_discount_amount = 1.2 - float_calculator + before do + calculator.preferred_normal_amount = 10.4 + calculator.preferred_discount_amount = 1.2 end context 'when the order amount is below preferred minimal' do let(:price) { 2 } it "uses the float preferred normal amount" do - expect(float_calculator.compute(line_item)).to eq(10.4) + expect(calculator.compute(line_item)).to eq(10.4) end end @@ -47,7 +44,7 @@ describe Spree::Calculator::PriceSack do let(:price) { 6 } it "uses the float preferred discount amount" do - expect(float_calculator.compute(line_item)).to eq(1.2) + expect(calculator.compute(line_item)).to eq(1.2) end end end