Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Correct issue with per item calculator when used with shipping method #1601

Closed
wants to merge 1 commit into from

2 participants

@laurens

Relates to #1596

see commented on commit a5c07bf

@radar
Collaborator

Merged into 1-1-stable and master. Thank you for fixing my stupidity.

@radar radar closed this
@laurens laurens deleted the unknown repository branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 25, 2012
  1. @laurens
This page is out of date. Refresh to see the latest.
View
2  core/app/models/spree/calculator/per_item.rb
@@ -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
View
32 core/spec/models/calculator/per_item_spec.rb
@@ -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
Something went wrong with that request. Please try again.