Weight shipping calculator treats variants without weight defined as zero weight

This commit is contained in:
Rohan Mitchell
2012-08-08 15:29:47 +10:00
parent 9f013f0e37
commit 9dd94eaf57
2 changed files with 4 additions and 2 deletions

View File

@@ -8,7 +8,7 @@ module OpenFoodWeb
end
def compute(object)
total_weight = object.line_items.inject(0) { |sum, li| sum + (li.variant.weight * li.quantity) }
total_weight = object.line_items.inject(0) { |sum, li| sum + ((li.variant.andand.weight || 0) * li.quantity) }
total_weight * self.preferred_per_kg
end
end

View File

@@ -4,11 +4,13 @@ describe OpenFoodWeb::Calculator::Weight do
it "computes shipping cost for an order by total weight" do
variant_1 = double(:variant, :weight => 10)
variant_2 = double(:variant, :weight => 20)
variant_3 = double(:variant, :weight => nil)
line_item_1 = double(:line_item, :variant => variant_1, :quantity => 1)
line_item_2 = double(:line_item, :variant => variant_2, :quantity => 3)
line_item_3 = double(:line_item, :variant => variant_3, :quantity => 5)
order = double(:order, :line_items => [line_item_1, line_item_2])
order = double(:order, :line_items => [line_item_1, line_item_2, line_item_3])
subject.set_preference(:per_kg, 10)
subject.compute(order).should == (10*1 + 20*3) * 10