From 2829e7020abcf6d2f708fbc7dfb3db98ad174255 Mon Sep 17 00:00:00 2001 From: jessib Date: Mon, 15 Apr 2013 12:13:33 -0700 Subject: Displaying of user's subscription. Still not committed to whether user can have multiple subscriptions at a time or not. --- billing/app/models/customer.rb | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'billing/app/models/customer.rb') diff --git a/billing/app/models/customer.rb b/billing/app/models/customer.rb index 8085d32..c7f216c 100644 --- a/billing/app/models/customer.rb +++ b/billing/app/models/customer.rb @@ -5,7 +5,7 @@ class Customer < CouchRest::Model::Base use_database "customers" belongs_to :user property :braintree_customer_id - + design do view :by_user_id view :by_braintree_customer_id @@ -35,5 +35,22 @@ class Customer < CouchRest::Model::Base braintree_data.credit_cards.find { |cc| cc.default? } end + #todo will this be plural? + def active_subscriptions(braintree_data=nil) + subscriptions = Array.new + braintree_data = braintree_data || Braintree::Customer.find(braintree_customer_id) + braintree_data.credit_cards.each do |cc| + cc.subscriptions.each do |sub| + subscriptions << sub if sub.status == 'Active' + end + end + subscriptions + end + + def single_subscription(braintree_data=nil) + self.active_subscriptions(braintree_data).first + end + + end -- cgit v1.2.3