Skip to content

Commit

Permalink
update_totals! will now recalculate then save the necessary attribute…
Browse files Browse the repository at this point in the history
…s to the database without any callbacks etc. V2
  • Loading branch information
David North committed Aug 16, 2010
1 parent c819356 commit 39621d3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
7 changes: 6 additions & 1 deletion core/app/models/order.rb
Expand Up @@ -345,7 +345,12 @@ def destroy_inapplicable_adjustments

def update_totals!
calculate_totals
# then commit to db without callbacks etc.
changes = {
:item_total => item_total,
:adjustment_total => adjustment_total,
:payment_total => payment_total
}
self.class.update_all(changes, { :id => id }) == 1
end


Expand Down
41 changes: 27 additions & 14 deletions core/spec/models/order_spec.rb
Expand Up @@ -81,20 +81,24 @@
context "Totaling" do
before(:all) do
order.save
# add line items
3.times { Fabricate(:line_item, :price => 100, :order => order) }
# payments
payment = order.payments.build(:amount => 300)
payment.order.stub!(:outstanding_balance).and_return(300) # so payment will validate
payment.save!
# and adjustments
order.tax_charges.create!(:description => 'tax', :adjustment_source => order, :amount => 10)
order.shipping_charges.create!(:description => 'shipping', :amount => 20)
order.reload

order.calculate_totals
end

context "#calculate_totals" do
before(:all) do
# add line items
3.times { Fabricate(:line_item, :price => 100, :order => order) }
# payments
payment = order.payments.build(:amount => 300)
payment.order.stub!(:outstanding_balance).and_return(300) # so payment will validate
payment.save!
# and adjustments
order.tax_charges.create!(:description => 'tax', :adjustment_source => order, :amount => 10)
order.shipping_charges.create!(:description => 'shipping', :amount => 20)
order.reload

order.calculate_totals
end

it "should set item_total to the sum of line_item amounts" do
order.item_total.to_i.should == 300
end
Expand All @@ -116,8 +120,17 @@
end

context "#update_totals" do
it "should update the relevant database columns sucessfully"
it "should not save associated objects"
it "should update the relevant database columns sucessfully" do
order.stub!(:calculate_totals)
order.item_total = 1
order.adjustment_total = 2
order.payment_total = 3
order.update_totals!
order.reload
order.item_total.to_i.should == 1
order.adjustment_total.to_i.should == 2
order.payment_total.to_i.should == 3
end
end

context "#destroy_inapplicable_adjustments" do
Expand Down

0 comments on commit 39621d3

Please sign in to comment.