Skip to content

Commit 4e97b27

Browse files
committed
Feat: Ability to disable auditable
1 parent b548659 commit 4e97b27

File tree

3 files changed

+21
-23
lines changed

3 files changed

+21
-23
lines changed

src/AuditableTrait.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
*/
1313
trait AuditableTrait
1414
{
15+
16+
public $auditable = true;
17+
1518
/**
1619
* Boot the audit trait for a model.
1720
*/

src/AuditableTraitObserver.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ class AuditableTraitObserver
1111
*/
1212
public function creating(Model $model): void
1313
{
14-
if (method_exists($model, 'getCreatedByColumn')) {
14+
if (method_exists($model, 'getCreatedByColumn') && $model->auditable) {
1515
$createdBy = $model->getCreatedByColumn();
1616

1717
if (! $model->$createdBy) {
1818
$model->$createdBy = $this->getAuthenticatedUserId();
1919
}
2020
}
2121

22-
if (method_exists($model, 'getUpdatedByColumn')) {
22+
if (method_exists($model, 'getUpdatedByColumn') && $model->auditable) {
2323
$updatedBy = $model->getUpdatedByColumn();
2424

2525
if (! $model->$updatedBy) {
@@ -41,7 +41,7 @@ protected function getAuthenticatedUserId(): int|string|null
4141
*/
4242
public function updating(Model $model): void
4343
{
44-
if (method_exists($model, 'getUpdatedByColumn')) {
44+
if (method_exists($model, 'getUpdatedByColumn') && $model->auditable) {
4545
$updatedBy = $model->getUpdatedByColumn();
4646

4747
if (! $model->isDirty($updatedBy)) {
@@ -55,7 +55,7 @@ public function updating(Model $model): void
5555
*/
5656
public function saved(Model $model): void
5757
{
58-
if (method_exists($model, 'getUpdatedByColumn')) {
58+
if (method_exists($model, 'getUpdatedByColumn') && $model->auditable) {
5959
$updatedBy = $model->getUpdatedByColumn();
6060

6161
if ($this->getAuthenticatedUserId() && $this->getAuthenticatedUserId() != $model->$updatedBy && $model->isDirty()) {

tests/Feature/AuditableModelTest.php

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,32 +48,27 @@
4848
expect($post->deleted_by)->toBe($user->id);
4949
});
5050

51-
test('a model wont be updated if edited by another user without it being dirty', function () {
52-
$user = User::create([
51+
test('a post can be created without audit', function () {
52+
$user = User::forceCreate([
5353
'name' => 'John Doe',
5454
'email' => '[email protected]',
5555
]);
5656

5757
actingAs($user);
5858

59-
$anotherUser = User::create([
60-
'name' => 'Jane Doe',
61-
'email' => '[email protected]',
62-
]);
59+
$post = new Post();
60+
$post->title = 'Hello World';
61+
$post->auditable = false;
62+
$post->save();
6363

64-
DB::table('posts')->insert([
65-
'title' => 'Hello World',
66-
'created_by' => $anotherUser->id,
67-
'updated_by' => $anotherUser->id,
68-
]);
69-
70-
$model = Post::first();
71-
72-
expect($model->created_by)->toBe($anotherUser->id);
73-
expect($model->updated_by)->toBe($anotherUser->id);
64+
expect($post->created_by)->toBe(null);
65+
expect($post->updated_by)->toBe(null);
66+
expect($post->deleted_by)->toBe(null);
7467

75-
$model->save();
68+
$post->auditable = true;
69+
$post->title = 'Hello World 2';
70+
$post->save();
7671

77-
expect($model->created_by)->toBe($anotherUser->id);
78-
expect($model->updated_by)->toBe($anotherUser->id);
72+
expect($post->updated_by)->toBe($user->id);
73+
expect($post->deleted_by)->toBe(null);
7974
});

0 commit comments

Comments
 (0)