added update_account method to order

This commit is contained in:
Skud
2013-05-18 11:26:29 +10:00
parent 75e2913203
commit 7c2fc52009
3 changed files with 31 additions and 1 deletions

View File

@@ -5,4 +5,12 @@ class Order < ActiveRecord::Base
has_many :order_items, :dependent => :destroy
default_scope order('created_at DESC')
# 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|
i.product.update_account(member)
end
end
end

View File

@@ -14,7 +14,9 @@ class Product < ActiveRecord::Base
# when purchasing a product that gives you a paid account, this method
# does all the messing around to actually make sure the account is
# updated correctly -- account type, paid until, etc.
# updated correctly -- account type, paid until, etc. Usually this is
# called by order.update_account, which loops through all order items
# and does this for each one.
def update_account(member)
member.account.account_type = account_type
if paid_months

View File

@@ -17,4 +17,24 @@ describe Order do
Order.all.should eq [@order2, @order]
end
it 'updates the account details' do
@member = FactoryGirl.create(:member)
@order = FactoryGirl.create(:order, :member => @member)
@account_type = FactoryGirl.create(:account_type, :name => 'paid')
@product = FactoryGirl.create(:product,
:account_type => @account_type,
:paid_months => 3
)
@order_item = FactoryGirl.create(:order_item,
:order_id => @order.id, :product_id => @product.id)
@member.account.account_type.should be_nil
@member.account.paid_until.should be_nil
@order.update_account
@member.account.account_type.should eq @account_type
@member.account.paid_until.should_not be_nil
end
end