From 5589ef858a19f1c46e7e8aa5b364914a5f14352a Mon Sep 17 00:00:00 2001 From: Azul Date: Tue, 2 Jul 2013 12:12:45 +0200 Subject: billing: fix issue with customer.braintree_customer The default CouchRest Model belongs_to does not respect the class option. So we need to write our own getter method. --- billing/app/models/customer.rb | 7 ++++++- billing/test/unit/customer_with_payment_info_test.rb | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/billing/app/models/customer.rb b/billing/app/models/customer.rb index 4788118..747f17f 100644 --- a/billing/app/models/customer.rb +++ b/billing/app/models/customer.rb @@ -5,7 +5,12 @@ class Customer < CouchRest::Model::Base use_database "customers" belongs_to :user - belongs_to :braintree_customer, class: Braintree::Customer + belongs_to :braintree_customer + + # Braintree::Customer - stored on braintrees servers - we only have the id. + def braintree_customer + @braintree_customer ||= Braintree::Customer.find(braintree_customer_id) + end validates :user, presence: true diff --git a/billing/test/unit/customer_with_payment_info_test.rb b/billing/test/unit/customer_with_payment_info_test.rb index f887674..ca89e65 100644 --- a/billing/test/unit/customer_with_payment_info_test.rb +++ b/billing/test/unit/customer_with_payment_info_test.rb @@ -20,6 +20,16 @@ class CustomerWithPaymentInfoTest < ActiveSupport::TestCase assert_equal Hash.new, @customer.custom_fields end + test "can access braintree_customer after reload" do + @customer.save + @customer = Customer.find_by_user_id(@customer.user_id) + @customer.with_braintree_data! + assert_equal 'Big', @customer.first_name + assert_equal 'Spender', @customer.last_name + assert_equal 1, @customer.credit_cards.size + assert_equal Hash.new, @customer.custom_fields + end + test "sets default_credit_card" do @customer.with_braintree_data! assert_equal @customer.credit_cards.first, @customer.default_credit_card -- cgit v1.2.3