diff --git a/app/models/order.rb b/app/models/order.rb index 2171857f5..eaa47e8a9 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -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 diff --git a/app/models/product.rb b/app/models/product.rb index 9f318107c..830880478 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -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 diff --git a/spec/models/order_spec.rb b/spec/models/order_spec.rb index 57a878701..54d67d16f 100644 --- a/spec/models/order_spec.rb +++ b/spec/models/order_spec.rb @@ -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