Skip to content

Commit

Permalink
written pending specs for order totaling methods. sketched out the im…
Browse files Browse the repository at this point in the history
…plementation of those methods
  • Loading branch information
David North committed Aug 16, 2010
1 parent af22972 commit 24b2a74
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 11 deletions.
29 changes: 29 additions & 0 deletions core/app/models/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,35 @@ def contains?(variant)
# self
# end

def calculate_totals
update_adjustments
self.payment_total = payments..total
self.item_total = line_items.total
self.adjustment_total = adjustments..total
self.total = item_total + adjustment_total
# self.outstanding_balance = total - payment_total
end

def update_adjustments
destroy_inapplicable_adjustments
adjustments.each(&:update_amount)
end


def destroy_inapplicable_adjustments
applicable_adjustments, adjustments_to_destroy = adjustments.partition{|a| a.applicable?}
self.adjustments = applicable_adjustments
adjustments_to_destroy.each(&:destroy)
end

def update_totals!
calculate_totals
# then commit to db without callbacks etc.
end




def name
address = bill_address || ship_address
"#{address.firstname} #{address.lastname}" if address
Expand Down
58 changes: 47 additions & 11 deletions core/spec/models/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,55 @@
end
end

it "should indicate whether its user is a guest" do
order.user = mock_model(User, :guest? => true)
order.should be_guest
order.user = mock_model(User, :guest? => false)
order.should_not be_guest
context "#guest?" do
it "should indicate whether its user is a guest" do
order.user = mock_model(User, :guest? => true)
order.should be_guest
order.user = mock_model(User, :guest? => false)
order.should_not be_guest
end
end

context "#complete?" do
it "should indicate if order is complete" do
order.completed_at = nil
order.complete?.should be_false

order.completed_at = Time.now
order.complete?.should be_true
end
end

it "should indicate if order is complete" do
order.completed_at = nil
order.complete?.should be_false

order.completed_at = Time.now
order.complete?.should be_true



context "Totaling" do

context "#calculate_totals" do
it "should call update_adjustments"
it "should set item_total to the sum of line_item amounts"
it "should set payments_total to the sum of payment amounts"
it "should set adjustment_total to the sum of adjustment amounts"
it "should set the total to the sum of item and adjustment totals"
it "should set outstanding_balance to the difference between the total and payment_total"
end

context "#update_adjustments" do
it "should destroy inapplicatable adjustments"
it "should force the adjustments to recalculate their amounts"
end

context "#update_totals" do
it "should update the relevant database columns sucessfully"
it "should not save associated objects"
end

context "#destroy_inapplicable_adjustments" do
it "should destroy adjustments for which applicable? is false"
it "should remove the destroyed adjustments from the association collection"
end

end

end
end

0 comments on commit 24b2a74

Please sign in to comment.