Skip to content

Commit

Permalink
Correct issue with per item calculator when used with shipping method
Browse files Browse the repository at this point in the history
Relates to #1596

Merges #1601
  • Loading branch information
laurens authored and radar committed May 29, 2012
1 parent bc88123 commit 3e626c5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion core/app/models/spree/calculator/per_item.rb
Expand Up @@ -11,7 +11,7 @@ def self.description
def compute(object=nil)
return 0 if object.nil?
self.preferred_amount * object.line_items.reduce(0) do |sum, value|
if matching_products && matching_products.include?(value.product)
if !matching_products || matching_products.include?(value.product)
value_to_add = value.quantity
else
value_to_add = 0
Expand Down
32 changes: 22 additions & 10 deletions core/spec/models/calculator/per_item_spec.rb
Expand Up @@ -2,24 +2,36 @@

describe Spree::Calculator::PerItem do
# Like an order object, but not quite...
let!(:product) { double("Product") }
let!(:line_items) { [double("LineItem", :quantity => 5, :product => product)] * 3 }
let!(:product1) { double("Product") }
let!(:product2) { double("Product") }
let!(:line_items) { [double("LineItem", :quantity => 5, :product => product1), double("LineItem", :quantity => 3, :product => product2)] }
let!(:object) { double("Order", :line_items => line_items) }
let!(:calculable) { double("Calculable", :promotion => promotion) }
let!(:promotion) { double("Promotion", :rules => [double("Rule", :products => [product])]) }

let!(:shipping_calculable) { double("Calculable") }
let!(:promotion_calculable) { double("Calculable", :promotion => promotion) }

let!(:promotion) { double("Promotion", :rules => [double("Rule", :products => [product1])]) }

let!(:calculator) do
calculator = Spree::Calculator::PerItem.new(:preferred_amount => 10)
calculator.stub(:calculable => calculable)
calculator
end
let!(:calculator) { Spree::Calculator::PerItem.new(:preferred_amount => 10) }

# regression test for #1414
it "correctly calculates per item shipping" do
calculator.compute(object).to_f.should == 150 # 5 x 3 x 10
calculator.stub(:calculable => shipping_calculable)
calculator.compute(object).to_f.should == 80 # 5 x 10 + 3 x 10
end

it "correctly calculates per item promotion" do
calculator.stub(:calculable => promotion_calculable)
calculator.compute(object).to_f.should == 50 # 5 x 10
end

it "returns 0 when no object passed" do
calculator.stub(:calculable => shipping_calculable)
calculator.compute.should == 0
end

it "returns 0 when no object passed" do
calculator.stub(:calculable => promotion_calculable)
calculator.compute.should == 0
end

Expand Down

0 comments on commit 3e626c5

Please sign in to comment.