Hello list,

I have a subscription model that has a upgrade event. This changes the state
from free->paying and triggers a charge method in the subscription model.
This charge method then creates a new instance of the payment model (just in
memory, using .new), and triggers the pay event on it. The pay event tries
to transition the payment from pending to paid and calls the do_pay method
before the transition (before_transition).

Everything would be 100% if it weren't for a small details. The payment
should be persisted even if the subscription transition from free->paying
has failed. In the do_pay method, if the payment failed, I do self.decline
(a FSM event that simply changes from pending->declined) and I have an
after_transition callback setup like: "after_transition any =>  any, do {|p|
p.save }" which gets triggered. However, since I return false in the do_pay
method to prevent the pendign->paid transition to complete (the payment
failed, remember?), state_machine then rollbacks the payment's save.

What I did was to call:

self.class.connection.commit_db_transaction

after self.decline and before return false in the payment's do_pay method.
This works, but is kind of ugly.

If anyone out there has any suggestions on how I could improve this design
or maybe just tell me that this is ok to do, alleviating my worries about
what seems to be a hack, I would be grateful!

Thanks,

Marcelo.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to