Files
growstuff/app/models/order.rb
Lucy 778d7a68c3 Fix bug in order total; add conversion link to order show.
The old order total method did not account for quantities. The
view test for order totals was not catching this, so we've
strengthened it. We also added a conversion link to the order
summary page and created an additional test to check that orders
with more than one item were generating the correct total.
2013-07-03 14:10:37 +01:00

49 lines
1.1 KiB
Ruby

class Order < ActiveRecord::Base
attr_accessible :member_id, :completed_at
belongs_to :member
has_many :order_items, :dependent => :destroy
default_scope order('created_at DESC')
# total price of an order
def total
sum = 0
for i in order_items do
subtotal = i.price * i.quantity
sum += subtotal
end
return sum
end
# return items in the format ActiveMerchant/PayPal want them
def activemerchant_items
items = []
order_items.each do |i|
items.push({
:name => i.product.name,
:quantity => i.quantity,
:amount => i.price
})
end
return items
end
# record the paypal details for reference
def record_paypal_details(token)
self.paypal_express_token = token
details = EXPRESS_GATEWAY.details_for(token)
self.paypal_express_payer_id = details.payer_id
self.save
end
# when an order is completed, we update the member's account to mark
# them as paid, or whatever, based on what products they ordered
def update_account
order_items.each do |i|
member.update_account_after_purchase(i.product)
end
end
end