Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Grove is using the ActiveRecord hook
after_create
for sendingcreate
events in River. This means that external systems are notified before the object is available in the database, because the transaction has not completed by the timeafter_create
triggers:http://apidock.com/rails/ActiveRecord/Callbacks/after_create
The fix changes the Observer hook to
after_commit
, which fixes this issue. However,after_commit
triggers after every commit to the database, so in order to distinguish create events, we need to reach into ActiveRecord internals to query the transaction type. Normally one would specify a condition to the hook in an ActiveRecord model like thisThis is not possible in an Observer because hooks are specified as declared methods:
So we introduce a call to the private method
transaction_include_any_action?
which accepts an array of actions and returnstrue
is the transaction involves any of them:The pull request wraps this hackery in a private method that checks for availability of
transaction_include_any_action?
and raises an error if it disappears in a future upgrade.In order for transactional tests to pass with this way of specifying hooks, we need to explicitly run the commit callbacks in the test