Skip to content

About

No description, website, or topics provided.

Resources

Code of conduct

Stars

0 stars

Watchers

0 watching

Forks

 
 

Repository files navigation

StateMachines::AfterTransitionCommit

Add #after_transition_commit to your active record state machines. This ensures that queueing up async jobs happens after the transaction is committed to the database.

Installation

Add this line to your application's Gemfile:

gem 'state_machines-after_transition_commit'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install state_machines-after_transition_commit

Usage

Anywhere you use after_transition you can replace with after_transition_commit. The block is registered on the open transaction and runs, with the record that transitioned, once the outermost transaction commits. If the transaction rolls back, the block is dropped. Outside a transaction, it runs immediately. Requires Active Record 7.2 or later.

state_machine :state, initial: :created do
  state :created
  state :complete

  event(:finish) do
    transition :created => :complete
  end

  after_transition_commit :created => :complete do |record|
    YourAsyncJob.perform_later(record)
  end
end

add_after_transition_commit_callback is no longer needed. It is kept as a no-op so existing state machines keep loading.

Gotchas

Don't trigger saves in your after_transition_commit block or you'll end up in an infinite loop. The typical use case is for queueing async jobs.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/hoverinc/state_machines-after_transition_commit. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the StateMachines::AfterTransitionCommit project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

About

No description, website, or topics provided.

Resources

Code of conduct

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages