Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Success callback not fired #147

Open
alexhanh opened this issue Sep 7, 2015 · 1 comment
Open

Success callback not fired #147

alexhanh opened this issue Sep 7, 2015 · 1 comment
Labels

Comments

@alexhanh
Copy link
Contributor

alexhanh commented Sep 7, 2015

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.

@troessner
Copy link
Owner

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 :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants