Skip to content

Commit

Permalink
feat: Pass data to roll-back event
Browse files Browse the repository at this point in the history
docs: Update docs
  • Loading branch information
cjmellor committed Oct 10, 2023
1 parent 6b1d928 commit 423f66a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,16 @@ A roll-back can be conditional, so you can roll back an approval if a condition
Approval::first()->rollback(fn () => true);
```

### Events

When a Model has been rolled back, a `ModelRolledBack` event will be fired with the Approval Model that was rolled back, as well as the User that rolled it back.

```php
// ModelRolledBackEvent::class

public Model $approval,
public Authenticatable|null $user,
````
## Disable Approvals

If you don't want Model data to be approved, you can bypass it with the `withoutApproval` method.
Expand Down
9 changes: 6 additions & 3 deletions src/Events/ModelRolledBackEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

namespace Cjmellor\Approval\Events;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Events\Dispatchable;

class ModelRolledBackEvent
{
use Dispatchable;

public function __construct()
{
//
public function __construct(
public Model $approval,
public Authenticatable|null $user,
) {
}
}
2 changes: 1 addition & 1 deletion src/Models/Approval.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,6 @@ public function rollback(Closure $condition = null): void
'rolled_back_at' => now(),
]);

Event::dispatch(new ModelRolledBackEvent($this));
Event::dispatch(new ModelRolledBackEvent(approval: $this, user: auth()->user()));
}
}
7 changes: 5 additions & 2 deletions tests/Feature/Models/ApprovalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@
->rolled_back_at->not->toBeNull();

// Assert the Events were fired
Event::assertDispatched(ModelRolledBackEvent::class);
Event::assertDispatched(function (ModelRolledBackEvent $event) use ($fakeModel): bool {
return $event->approval->is($fakeModel->fresh()->approvals()->first())
&& $event->user === null;
});
});

test('a rolled back Approval can be conditionally set', function () {
test(description: 'a rolled back Approval can be conditionally set', closure: function () {
// Build a query
$fakeModel = new FakeModel();

Expand Down

0 comments on commit 423f66a

Please sign in to comment.