From 75e29132030f2e60b50d76900a7f75c1ee61de97 Mon Sep 17 00:00:00 2001 From: Skud Date: Sat, 18 May 2013 11:16:45 +1000 Subject: [PATCH] added method to update paid acct details after product purchase --- app/models/product.rb | 12 ++++++++++++ spec/models/product_spec.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/app/models/product.rb b/app/models/product.rb index 031f59916..9f318107c 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -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 diff --git a/spec/models/product_spec.rb b/spec/models/product_spec.rb index d8fc496a8..e56502413 100644 --- a/spec/models/product_spec.rb +++ b/spec/models/product_spec.rb @@ -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