added method to update paid acct details after product purchase

This commit is contained in:
Skud
2013-05-18 11:16:45 +10:00
parent fac1b9cc01
commit 75e2913203
2 changed files with 38 additions and 0 deletions

View File

@@ -11,4 +11,16 @@ class Product < ActiveRecord::Base
def to_s
name
end
# 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.
def update_account(member)
member.account.account_type = account_type
if paid_months
start_date = member.account.paid_until || Time.zone.now
member.account.paid_until = start_date + paid_months.months
end
end
end

View File

@@ -6,4 +6,30 @@ describe Product do
@product = FactoryGirl.create(:product)
@product.to_s.should eq @product.name
end
context "update account" do
before(:each) do
@product = FactoryGirl.create(:product,
:paid_months => 3
)
@member = FactoryGirl.create(:member)
end
it "sets account_type" do
@product.update_account(@member)
@member.account.account_type.should eq @product.account_type
end
it "sets paid_until" do
@member.account.paid_until = nil # blank for now, as if never paid before
@product.update_account(@member)
# stringify to avoid millisecond problems...
@member.account.paid_until.to_s.should eq (Time.zone.now + 3.months).to_s
# and again to make sure it works for currently paid accounts
@product.update_account(@member)
@member.account.paid_until.to_s.should eq (Time.zone.now + 6.months).to_s
end
end
end