-
Notifications
You must be signed in to change notification settings - Fork 10
{WIP} Add on_rollback hook to resources
#26
Conversation
|
@richmolj I'm not ready to merge this yet as I still want to do some clean up of the @Startouf This addresses jsonapi-suite/jsonapi_compliable#137 (at least as far as I described and intended in jsonapi-suite/jsonapi_compliable#72 (review) ). Please let us know whether this addresses the problem you were trying to solve? |
on_rollback hook to resourceson_rollback hook to resources
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love that you took this on ❤️ 🎉 !
Had a few comments on particulars but the broad strokes look correct to me 👍
lib/graphiti/util/hooks.rb
Outdated
| # the graph, working inwards | ||
| def self.add(prc) | ||
| _hooks.unshift(prc) | ||
| def self.add(before_commit, rollback) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI this is Hook.add but is specific to before_commit
lib/graphiti/util/hooks.rb
Outdated
| _hooks.each { |h| h.call } | ||
| begin | ||
| _hooks[:before_commit].each_with_index do |before_commit, idx| | ||
| result = before_commit.call |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this is existing behavior, but we should try to run in the resource instance context if possible.
| class DepartmentResource < ApplicationResource | ||
| self.model = ::Department | ||
|
|
||
| on_rollback do |record| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going back and forth on it myself, but I wonder how much this should be tied to a given before_commit hook. Something like:
before_commit :update_service do
perform do
# ... logic ...
end
rollback do
# ... logic ...
end
endThe fact that the add_hook method is accepting both makes it seem like they should be tied together, and would be easier to separate concerns
|
|
||
| controller(ApplicationController) do | ||
| def create | ||
| employee = IntegrationHooks::EmployeeResource.build(params) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this is how the existing spec is written, but I think that's compliable legacy. We can call .build and .save independent of a controller here, I think. Probably fits the "resource testing" context actually.
lib/graphiti/util/hooks.rb
Outdated
| rollback = _hooks[:rollback][idx] | ||
|
|
||
| # Want to run rollbacks in reverse order from before_commit hooks | ||
| _hooks[:staged_rollbacks].unshift(-> { rollback.call(result) }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My comment on perform do ... end; rollback do ... end might make this easier - take the same array and reverse it.
Previously there was a controller-level hack to make request specs work in rails 4. Instead, we need to differentiate which rails versions when we are calling the underlying request/controller spec helpers, as these changes between major rails versions 4 and 5.
|
closing in favor of #67 |
From @jsonapi-suite/jsonapi_compliable#137, this allows an API
maintainer to run a rollback hook on a given resource if an error occurs
after the resource's
before_commithook is successful, but beforethe transaction actually completes. This will be helpful in situations
where a user would like to rollback something that cannot be
automatically undone in the transaction. The
on_rollbackhook willreceive as its argument the return value of its
before_callbackhook(or the created/updated/deleted record if no
before_callbackhook isprovided).
If creating an employee and a nested postion, the position will blow up,
and the rollback hook will clean up after itself.