From 160b535e2fffa3d59ff98c93449b06cb3b86dc7e Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Tue, 18 Jun 2019 13:29:17 +0100 Subject: [PATCH] Make weight calculator compute 0 for variants with unit different from weight --- app/models/calculator/weight.rb | 3 +++ spec/models/calculator/weight_spec.rb | 8 +++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/app/models/calculator/weight.rb b/app/models/calculator/weight.rb index fdac781c94..13885dadcc 100644 --- a/app/models/calculator/weight.rb +++ b/app/models/calculator/weight.rb @@ -16,6 +16,8 @@ module Calculator total_weight(line_items) * preferred_per_kg end + private + def total_weight(line_items) line_items.sum do |line_item| line_item_weight(line_item) @@ -23,6 +25,7 @@ module Calculator end def line_item_weight(line_item) + return 0 if line_item.variant.product.andand.variant_unit != 'weight' if line_item.final_weight_volume.present? # Divided by 1000 because grams is the base weight unit and the calculator price is per_kg line_item.final_weight_volume / 1000 diff --git a/spec/models/calculator/weight_spec.rb b/spec/models/calculator/weight_spec.rb index dd6e2c22fe..0dad5167aa 100644 --- a/spec/models/calculator/weight_spec.rb +++ b/spec/models/calculator/weight_spec.rb @@ -29,10 +29,16 @@ describe Calculator::Weight do end describe "and with final_weight_volume defined" do + before { line_item.update_attribute :final_weight_volume, '18000' } + it "computes fee using final_weight_volume, not the variant weight" do - line_item.final_weight_volume = "18000" expect(subject.compute(line_item)).to eq(10 * 18) end + + it "returns zero for variant where unit type is not weight" do + line_item.variant.product.update_attribute :variant_unit, 'items' + expect(subject.compute(line_item)).to eq(0) + end end end