diff --git a/lib/statesman/adapters/active_record.rb b/lib/statesman/adapters/active_record.rb index a0f4ac63..27509d61 100644 --- a/lib/statesman/adapters/active_record.rb +++ b/lib/statesman/adapters/active_record.rb @@ -78,12 +78,20 @@ def create_transition(from, to, metadata) transition.save! @last_transition = transition @observer.execute(:after, from, to, transition) + add_after_commit_callback(from, to, transition) end - @observer.execute(:after_commit, from, to, transition) transition end + def add_after_commit_callback(from, to, transition) + ::ActiveRecord::Base.connection.add_transaction_record( + ActiveRecordAfterCommitWrap.new do + @observer.execute(:after_commit, from, to, transition) + end, + ) + end + def transitions_for_parent @parent_model.send(@association_name) end @@ -149,5 +157,31 @@ def with_updated_timestamp(params) params.merge(column => timestamp) end end + + class ActiveRecordAfterCommitWrap + def initialize + @callback = Proc.new + @connection = ::ActiveRecord::Base.connection + end + + # rubocop: disable Naming/PredicateName + def has_transactional_callbacks? + true + end + # rubocop: enable Naming/PredicateName + + def committed!(*) + @callback.call + end + + def before_committed!(*); end + + def rolledback!(*); end + + # Required for +transaction(requires_new: true)+ + def add_to_transaction(*) + @connection.add_transaction_record(self) + end + end end end