Skip to content
This repository was archived by the owner on Apr 20, 2019. It is now read-only.

Conversation

@wadetandy
Copy link
Contributor

Replaces #26. No material change at the moment.

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_commit hook is successful, but before
the 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_rollback hook will
receive as its argument the return value of its before_callback hook
(or the created/updated/deleted record if no before_callback hook is
provided).

class EmployeeResource
  has_many :positions

  before_commit only: :create do |record|
    # Makes HTTP request and returns ID of created record
    PayrollSystem::Employee.create(record)
  end

  on_rollback only: :create do |sap_record_id|
    # Delete created record
    PayrollSystem::Employee.find(sap_record_id).destroy
  end
end

class PositionResource
  before_commit do |record|
    raise 'boom'
  end
end

If creating an employee and a nested postion, the position will blow up,
and the rollback hook will clean up after itself.

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_commit` hook is successful, but _before_
the 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_rollback` hook will
receive as its argument the return value of its `before_callback` hook
(or the created/updated/deleted record if no `before_callback` hook is
provided).

```ruby
class EmployeeResource
  has_many :positions

  before_commit only: :create do |record|
    # Makes HTTP request and returns ID of created record
    PayrollSystem::Employee.create(record)
  end

  on_rollback only: :create do |sap_record_id|
    # Delete created record
    PayrollSystem::Employee.find(sap_record_id).destroy
  end
end

class PositionResource
  before_commit do |record|
    raise 'boom'
  end
end
```

If creating an employee and a nested postion, the position will blow up,
and the rollback hook will clean up after itself.
@richmolj
Copy link
Contributor

richmolj commented Jan 7, 2019

@wadetandy let's move this to the main Graphiti repo when you get a chance. No rush, but I like the work here.

@richmolj richmolj closed this Jan 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants