From 7f5cf9d4dd3ca7db0b7e2acd2bf67f0f70ea3df0 Mon Sep 17 00:00:00 2001 From: Roman Shterenzon Date: Thu, 1 Aug 2024 12:16:21 +0300 Subject: [PATCH] Add documentation for performing a transition in a transaction --- README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/README.md b/README.md index 39b2956..b7f64ef 100644 --- a/README.md +++ b/README.md @@ -185,6 +185,33 @@ You can have a look at an advanced [`on_transition`][] example in [`on_transition`]: https://github.com/geekq/workflow#on_transition [advanced_hooks_and_validation_test]: http://github.com/geekq/workflow-activerecord/blob/develop/test/advanced_hooks_and_validation_test.rb + +### Handling the state transition in a transaction + +You might want to perform the state transition in a database transaction, +so that the state is persisted atomically with all the other attributes. +To do so, define a module: + +```ruby +module TransitionTransaction + def process_event!(name, *, **) + transaction { super(name, *, **) } + end +end +``` + +and then prepend it in your model: + +```ruby +class Task + workflow do + ... + end + + prepend TransitionTransaction +end +``` + Changelog ---------