so im using the aasm gem for the first time and im emulating a credit card transaction.
The initial state is "Pending" and i need a callback that right after creating a transaction it checks if the amount of the transaction is bigger than the limit. If its lower then transition from pending to paid, and if not, from pending to failed.
The after_transaction callback is used when transitioning right? It wouldn't work here. I would need a callback that calls the method "approve_transaction" right after the object is created.
aasm :column_name, column: 'status' do # default column: aasm_state
state :pending, initial: true
state :paid, :failed
after
event :fail do
transitions from: :pending, to: :failed
end
event :approve do
transitions from: :pending, to: :paid
end
def approve_transaction
if limit_left
approve
else
fail
end
end
def limit_left
remaining_limit = self.credit_card.limit - self.amount
remaining_limit >= 0 ? true : false
end
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…