pluginaweek / state_machine

Adds support for creating state machines for attributes on any Ruby class
http://www.pluginaweek.org
MIT License
3.74k stars 510 forks source link

use_transactions: false does not work on activerecord #264

Open trobrock opened 11 years ago

trobrock commented 11 years ago

I have something like the following:

  state_machine initial: :new, use_transactions: false do
    event :queue do
      transition :new => :queued
    end

    event :launch do
      transition :queued => :launching
    end

    event :provision do
      transition :launching => :provisioning
    end

    event :finish do
      transition :provisioning => :finished
    end

    after_transition on: :launch, :do => :launch_server
    after_transition on: :provision, :do => :provision_server
  end

I have use_transactions false, because I want the state transitions to be persisted during the callback methods so I can give the client updates on the state of the object. But when I added the use_transactions: false at the top it seems they are still ran within a transaction. Am I misunderstanding what that option does?

johnnaegle commented 11 years ago

I also have this issue:

class User < ActiveRecord::Base

  state_machine :state, :initial => :idle, :use_transactions => false do
    before_transition :idle => :calculating do |obj|
      puts "In Before IDLE => CALCULATING"
    end

    after_transition :idle => :calculating do |obj|
      puts "In After IDLE => CALCULATING"
    end

    event :start do
      transition :idle => :calculating
    end

    event :finish do
      transition :calculating => :idle
    end    
  end

end

When I start this I did not expect this to be wrapped in a transaction:

user.fire_events!(:start)
   (0.2ms)  BEGIN
In Before IDLE => CALCULATING
   (0.6ms)  UPDATE "users" SET "state" = 'calculating' WHERE "users"."id" = 21
In After IDLE => CALCULATING
   (1.4ms)  COMMIT

I'd like to see this:

user.fire_events!(:start)
In Before IDLE => CALCULATING
   (0.2ms)  BEGIN
   (0.6ms)  UPDATE "users" SET "state" = 'calculating' WHERE "users"."id" = 21
   (1.4ms)  COMMIT
In After IDLE => CALCULATING

Am I also mis-understanding use_transactions?

johnnaegle commented 11 years ago

I believe this is resolved by: #255