transitions icon indicating copy to clipboard operation
transitions copied to clipboard

Success callback not fired

Open alexhanh opened this issue 10 years ago • 1 comments

state_machine do
    state :unpaid
    state :paid, :enter => lambda { |tx| puts "callback fired!" }
    state :relayed
    state :cancelled
    state :refunded

    event :pay, timestamp: 'paid_at' do
      transitions to: :paid, from: :unpaid
    end

    event :relay, timestamp: 'relayed_at' do
      transitions to: :relayed, from: :paid
    end

    event :cancel, timestamp: 'cancelled_at' do
      transitions to: :cancelled, from: [:unpaid, :paid]
    end

    event :refund, timestamp: 'refunded_at' do
      transitions to: :refunded, from: :cancelled
    end
  end

  def event_fired(current_state, new_state, event)
    puts "EVENTFIRED #{current_state} #{new_state}"
    add_event!("transfer.#{new_state}")
  end

  def add_event!(type, msg=nil)
    # Or use ::Event, see https://github.com/troessner/transitions/issues/123#issuecomment-37759328
    self.events.create!(type: type)
  end

  def event_failed(event)
    puts "EVENT FAILED"
  end
def pay
    from_bank = transfer.main_from_bank
    from_bank.with_lock do
      from_bank.locked_balance += transfer.total
      from_bank.save!
      transfer.pay!
    end
    puts transfer.state
    puts transfer.paid?
    true

    rescue Transitions::InvalidTransition => e

    _error(:invalid_transition, "#{e.inspect}")
end

Results in the following output:

callback fired!
EVENTFIRED unpaid paid
  SQL (0.3ms)  INSERT INTO "events" ("type", "transfer_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["type", "transfer.paid"], ["transfer_id", 16597], ["created_at", "2015-09-07 15:02:13.526027"], ["updated_at", "2015-09-07 15:02:13.526027"]]
  SQL (0.3ms)  UPDATE "transfers" SET "paid_at" = $1, "state" = $2, "updated_at" = $3 WHERE "transfers"."id" = $4  [["paid_at", "2015-09-07 15:02:13.524531"], ["state", "paid"], ["updated_at", "2015-09-07 15:02:13.528612"], ["id", 16597]]
   (8.3ms)  COMMIT
paid
true

However, if the :enter => lambda { |tx| puts "callback fired!" } is changed to :success => lambda { |tx| puts "callback fired!" }, then callback fired! doesn't show up. That is, the success callback doesn't fire.

alexhanh avatar Sep 07 '15 15:09 alexhanh

Sounds like a legit bug. Unfortunately I'm on vacation currently and I have no idea when I can really look into it. Unless you come up with a pull request yourself (which I would highly appreciate) I'm afraid you'll have to wait 3 weeks until I can look into this :(

troessner avatar Sep 10 '15 17:09 troessner