Created
April 23, 2010 17:41
-
-
Save markmcdonald51/376870 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-------- class SubscriptionTransaction | |
named_scope :today, | |
:conditions => ['created_at > ? and created_at < ?', | |
Time.new.beginning_of_day, Time.new.tomorrow.beginning_of_day] | |
--------class Subscription | |
named_scope :due_today, | |
#:include => [:customer], | |
:joins => [:transactions], | |
:conditions => ['subscription_transactions.created_at < ? | |
and next_transaction > ?', Time.new.at_beginning_of_day, 1.days.ago] | |
include AASM | |
# State Machine ############################################################# | |
aasm_column :current_state # defaults to aasm_state | |
aasm_initial_state :inactive | |
aasm_state :inactive | |
aasm_state :good_standing, :enter => :bill_for_renewal | |
aasm_state :suspened | |
aasm_event :activate do | |
transitions :to => :good_standing, :from => [:suspend, :inactive, :good_standing] | |
end | |
aasm_event :renew do | |
transitions :to => :good_standing, | |
:from => [:inactive, :good_standing, :suspended], | |
:guard => :overbill? | |
end | |
aasm_event :suspend do | |
transitions :to => :suspened, :from => [:good_standing, :inactive] | |
end | |
############################################################################ | |
def overbill? | |
transactions.reload.today.blank? | |
end | |
def bill_for_renewal | |
puts 'called bill_for_renew' | |
# run transaction code here | |
#if (true) | |
# self.suspend! | |
# return true | |
#end | |
#result = customer.purchase(c.monthly_cost * 100) | |
#unless result.success? | |
end | |
------------- rake task -------------- | |
desc "grab subscriptions that are due today and suspend accounts that fail transaction" | |
task :purge_subscriptions => :environment do | |
puts 'Start by grabing subscriptions that are due today' | |
Subscription.due_today.each do |subscription| | |
y subscription | |
y subscription.customer | |
subscription.renew! | |
------------ rspec | |
it 'should only bill customer once during a given pay period' do | |
@customer.subscription = Factory(:subscription, :customer => @customer) | |
s = @customer.subscription | |
s.inactive?.should be_true | |
s.renew! | |
s.good_standing?.should be_true | |
# this will happen in the transaction code I am sure.. do it here for testing | |
transactions << Factory(:subscription_transaction,:subscription => customer.subscription ) | |
debugger | |
s.renew!.should be_false | |
## COOLIO! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment