state-machines / state_machines

Adds support for creating state machines for attributes on any Ruby class
https://github.com/state-machines/state_machines
MIT License
814 stars 91 forks source link

Unable to call the calling method of the state machine from within the state machine #24

Closed detournemint closed 9 years ago

detournemint commented 9 years ago

Sorry if this is in the wrong place.

I have the following state machine and I'm trying to send an email after the state changes:

state_machine :state, initial: :unapproved do
 event :reject_it do
   transition [:approved, :unapproved] => :rejected
  PurchaseRequestsMailer.purchase_request_rejected(self).send
 end
end

My problem is that self refers to the statemachine object, not the object that is calling the state machine, is there anyway to access that object that's calling the state machine?

seuros commented 9 years ago

If i'm not wrong you need to do

state_machine :state, initial: :unapproved do
  event :reject_it do
    transition [:approved, :unapproved] => :rejected 
  end
  after_transition [:approved, :unapproved] => :rejected do |rejected_purchase_request, _|
    PurchaseRequestsMailer.purchase_request_rejected(rejected_purchase_request).send
  end
end
detournemint commented 9 years ago

That does it, thank you!