Permalink
Browse files

Use a cached status on Subscription when provided

  • Loading branch information...
1 parent e3cd30d commit bf3ef034ee1ebd389cb8b425656d8589c154f55d @jferris jferris committed Dec 7, 2011
Showing with 64 additions and 14 deletions.
  1. +19 −3 lib/ashmont/subscription.rb
  2. +45 −11 spec/ashmont/subscription_spec.rb
@@ -6,12 +6,17 @@ module Ashmont
class Subscription
attr_reader :token, :errors
- def initialize(token = nil)
+ def initialize(token = nil, cached_attributes = {})
@token = token
+ @cached_attributes = cached_attributes
@errors = {}
end
- delegate :transactions, :status, :to => :remote_subscription
+ delegate :transactions, :to => :remote_subscription, :allow_nil => true
+
+ def status
+ @cached_attributes[:status] || remote_status
+ end
def save(attributes)
attributes_for_merchant = add_merchant_to_attributes(attributes)
@@ -44,11 +49,16 @@ def next_billing_date
def reload
@remote_subscription = nil
+ @cached_attributes = {}
self
end
private
+ def remote_status
+ remote_subscription.status if remote_subscription
+ end
+
def add_merchant_to_attributes(attributes)
if Ashmont.merchant_account_id
attributes.merge(:merchant_account_id => Ashmont.merchant_account_id)
@@ -74,7 +84,13 @@ def update(attributes)
end
def remote_subscription
- @remote_subscription ||= Braintree::Subscription.find(token)
+ @remote_subscription ||= find_remote_subscription
+ end
+
+ def find_remote_subscription
+ if token
+ Braintree::Subscription.find(token)
+ end
end
def merchant_account_time_zone
@@ -1,7 +1,7 @@
require 'spec_helper'
describe Ashmont::Subscription do
- %w(transactions status).each do |delegated_method|
+ %w(transactions).each do |delegated_method|
it "delegates ##{delegated_method} to the remote subscription" do
token = 'xyz'
remote_subscription = stub("remote-subscription", delegated_method => "expected")
@@ -17,7 +17,7 @@
unconverted_date = "2011-01-20"
remote_subscription = stub("remote-subscription", :next_billing_date => unconverted_date)
Braintree::Subscription.stubs(:find => remote_subscription)
- subscription = Ashmont::Subscription.new
+ subscription = Ashmont::Subscription.new("xyz")
result = subscription.next_billing_date
result.utc_offset.should == ActiveSupport::TimeZone[Ashmont.merchant_account_time_zone].utc_offset
result.strftime("%Y-%m-%d").should == unconverted_date
@@ -70,8 +70,8 @@
transaction = stub("transaction", :id => 'abc')
retry_result = stub("retry-result", :transaction => transaction)
settlement_result = stub("settlement-result", :success? => true)
- past_due_subscription = stub("subscription", :status=> "past_due")
- active_subscription = stub("subscription", :status=> "active")
+ past_due_subscription = stub("subscription", :status => "past_due")
+ active_subscription = stub("subscription", :status => "active")
Braintree::Subscription.stubs(:retry_charge => retry_result)
Braintree::Transaction.stubs(:submit_for_settlement => settlement_result)
Braintree::Subscription.stubs(:find).returns(past_due_subscription).then.returns(active_subscription)
@@ -137,19 +137,43 @@
remote_subscription = stub("remote-subscription", :transactions => transactions)
Braintree::Subscription.stubs(:find => remote_subscription)
- subscription = Ashmont::Subscription.new
+ subscription = Ashmont::Subscription.new("xyz")
subscription.most_recent_transaction.created_at.should == 1.day.ago
end
end
it "reloads remote data" do
- old_remote_subscription = stub("old-remote-subscription", :status => "old")
- new_remote_subscription = stub("new-remote-subscription", :status => "new")
- Braintree::Subscription.stubs(:find).returns(old_remote_subscription).then.returns(new_remote_subscription)
- subscription = Ashmont::Subscription.new(:token => 'xyz')
- subscription.status.should == "old"
- subscription.reload.status.should == "new"
+ old_remote_subscription = stub("old-remote-subscription", :status => "old")
+ new_remote_subscription = stub("new-remote-subscription", :status => "new")
+ Braintree::Subscription.stubs(:find).returns(old_remote_subscription).then.returns(new_remote_subscription)
+ subscription = Ashmont::Subscription.new(:token => 'xyz')
+ subscription.status.should == "old"
+ subscription.reload.status.should == "new"
+ end
+
+ it "finds status from the remote subscription" do
+ remote_subscription = stub_remote_subscription(:status => "a-ok")
+ subscription = Ashmont::Subscription.new("xyz")
+ subscription.status.should == "a-ok"
+ end
+
+ it "doesn't have status without a remote subscription" do
+ subscription = Ashmont::Subscription.new(nil)
+ subscription.status.should be_nil
+ end
+
+ it "uses a cached status when provided" do
+ remote_subscription = stub_remote_subscription(:status => "past-due")
+ subscription = Ashmont::Subscription.new("xyz", :status => "a-ok")
+ subscription.status.should == "a-ok"
+ end
+
+ it "updates the cached status when reloading" do
+ remote_subscription = stub_remote_subscription(:status => "active")
+ subscription = Ashmont::Subscription.new("xyz", :status => "past-due")
+ subscription.reload
+ subscription.status.should == "active"
end
def with_configured_merchant_acount_id
@@ -159,4 +183,14 @@ def with_configured_merchant_acount_id
ensure
Ashmont.merchant_account_id = nil
end
+
+ def stub_remote_subscription(options = {})
+ stub(
+ "remote_subscription",
+ {
+ :status => "active",
+ :id => "abcdef"
+ }.update(options)
+ ).tap { |subscription| Braintree::Subscription.stubs(:find => subscription) }
+ end
end

0 comments on commit bf3ef03

Please sign in to comment.