Skip to content

Success callback not fired #147

@alexhanh

Description

@alexhanh
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions