summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--billing/app/controllers/customers_controller.rb3
-rw-r--r--billing/app/controllers/subscriptions_controller.rb7
-rw-r--r--billing/app/models/customer.rb26
-rw-r--r--billing/app/views/customers/_subscription.html.haml1
-rw-r--r--billing/app/views/customers/show.html.haml8
-rw-r--r--billing/app/views/subscriptions/index.html.haml2
-rw-r--r--billing/config/routes.rb1
7 files changed, 27 insertions, 21 deletions
diff --git a/billing/app/controllers/customers_controller.rb b/billing/app/controllers/customers_controller.rb
index 3479448..b0184af 100644
--- a/billing/app/controllers/customers_controller.rb
+++ b/billing/app/controllers/customers_controller.rb
@@ -4,8 +4,7 @@ class CustomersController < BillingBaseController
def show
- @subscriptions = @customer.active_subscriptions(@braintree_data)
-
+ @active_subscription = @customer.subscriptions(@braintree_data)
# UGLY Braintree::ResourceCollection to array.
# might want method
@transactions = []
diff --git a/billing/app/controllers/subscriptions_controller.rb b/billing/app/controllers/subscriptions_controller.rb
index b248be5..9217979 100644
--- a/billing/app/controllers/subscriptions_controller.rb
+++ b/billing/app/controllers/subscriptions_controller.rb
@@ -6,7 +6,7 @@ class SubscriptionsController < ApplicationController
# don't show link to subscribe if they are already subscribed?
customer = Customer.find_by_user_id(current_user.id)
- if subscription = customer.single_subscription
+ if subscription = customer.subscriptions # will return active subscription, if it exists
redirect_to subscription_path(subscription.id), :notice => 'You already have an active subscription'
else
credit_card = customer.default_credit_card #safe to assume default?
@@ -25,6 +25,11 @@ class SubscriptionsController < ApplicationController
@result = Braintree::Subscription.cancel params[:id]
end
+ def index
+ customer = Customer.find_by_user_id(current_user.id)
+ @subscriptions = customer.subscriptions(nil, false)
+ end
+
private
def fetch_subscription
diff --git a/billing/app/models/customer.rb b/billing/app/models/customer.rb
index fa43b96..161f763 100644
--- a/billing/app/models/customer.rb
+++ b/billing/app/models/customer.rb
@@ -30,28 +30,28 @@ class Customer < CouchRest::Model::Base
#slow to get Braintree Customer data, so pass it if have already retrieved it
# won't really have multiple credit cards on file
+ # instead of having method, should just be able to call braintree_data.credit_cards.first if just one is allowed
def default_credit_card(braintree_data = nil)
return unless has_payment_info?
braintree_data = braintree_data || Braintree::Customer.find(braintree_customer_id)
braintree_data.credit_cards.find { |cc| cc.default? }
end
- #todo will this be plural?
- def active_subscriptions(braintree_data=nil)
- subscriptions = Array.new
+
+ # based on 2nd parameter, either returns the single active subscription (or nil if there isn't one), or an array of all subsciptions
+ def subscriptions(braintree_data=nil, only_active=true)
+ return unless has_payment_info?
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'
+
+ subscriptions = []
+ braintree_data.credit_cards.first.subscriptions.each do |sub|
+ if only_active and sub.status == 'Active'
+ return sub
+ else
+ subscriptions << sub
end
end
- subscriptions
+ only_active ? nil : subscriptions
end
- def single_subscription(braintree_data=nil)
- self.active_subscriptions(braintree_data).first
- end
-
-
-
end
diff --git a/billing/app/views/customers/_subscription.html.haml b/billing/app/views/customers/_subscription.html.haml
deleted file mode 100644
index a57f6e9..0000000
--- a/billing/app/views/customers/_subscription.html.haml
+++ /dev/null
@@ -1 +0,0 @@
-= render :partial => "subscriptions/subscription_details", :locals => {:subscription => subscription}
diff --git a/billing/app/views/customers/show.html.haml b/billing/app/views/customers/show.html.haml
index 7416682..b36ebaf 100644
--- a/billing/app/views/customers/show.html.haml
+++ b/billing/app/views/customers/show.html.haml
@@ -2,7 +2,7 @@
= link_to 'Make Payment', new_payment_path, :class => :btn
%h3 Transaction History
= render(:partial => "transaction", :collection => @transactions) # show subset with link to see more
-- if @subscriptions.any?
- %h3 Active Subscriptions
- = # todo: won't really have multiple subscriptions
- = render(:partial => "subscription", :collection => @subscriptions) \ No newline at end of file
+- if @active_subscription
+ %h3 Active Subscription
+ = render :partial => "subscriptions/subscription_details", :locals => {:subscription => @active_subscription}
+ = link_to 'All subscriptions', subscriptions_path \ No newline at end of file
diff --git a/billing/app/views/subscriptions/index.html.haml b/billing/app/views/subscriptions/index.html.haml
new file mode 100644
index 0000000..c885f90
--- /dev/null
+++ b/billing/app/views/subscriptions/index.html.haml
@@ -0,0 +1,2 @@
+- @subscriptions.each do |s|
+ = render :partial => "subscription_details", :locals => {:subscription => s} \ No newline at end of file
diff --git a/billing/config/routes.rb b/billing/config/routes.rb
index 1c7bb30..9b29aef 100644
--- a/billing/config/routes.rb
+++ b/billing/config/routes.rb
@@ -2,6 +2,7 @@ Rails.application.routes.draw do
match 'payments/new' => 'payments#new', :as => :new_payment
match 'payments/confirm' => 'payments#confirm', :as => :confirm_payment
+ resources :payments, :only => [:index]
resources :customers, :only => [:new, :edit, :show]
resources :credit_card_info, :only => [:edit]