Skip to content

Commit e01a91d

Browse files
committed
When activity changes has been emptied by pipes, discard it.
1 parent ffadece commit e01a91d

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

src/Traits/LogsActivity.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,6 @@ protected static function bootLogsActivity(): void
6262
return;
6363
}
6464

65-
if ($model->isLogEmpty($changes) && ! $model->activitylogOptions->submitEmptyLogs) {
66-
return;
67-
}
68-
6965
// User can define a custom pipelines to mutate, add or remove from changes
7066
// each pipe receives the event carrier bag with changes and the model in
7167
// question every pipe should manipulate new and old attributes.
@@ -74,6 +70,10 @@ protected static function bootLogsActivity(): void
7470
->through(static::$changesPipes)
7571
->thenReturn();
7672

73+
if ($model->isLogEmpty($event->changes) && ! $model->activitylogOptions->submitEmptyLogs) {
74+
return;
75+
}
76+
7777
// Actual logging
7878
$logger = app(ActivityLogger::class)
7979
->useLog($logName)

tests/DetectsChangesTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,50 @@ public function handle(EventLogBag $event, Closure $next): EventLogBag
382382
$this->assertEquals($expectedChanges, $this->getLastActivity()->changes()->toArray());
383383
});
384384

385+
it('can block activity entry when pipe modification empties changes', function () {
386+
$articleClass = new class() extends Article {
387+
use LogsActivity;
388+
389+
public function getActivitylogOptions(): LogOptions
390+
{
391+
return LogOptions::defaults()
392+
->dontSubmitEmptyLogs()
393+
->logOnly(['name', 'text', 'user.name']);
394+
}
395+
};
396+
397+
$user = User::create([
398+
'name' => 'user name',
399+
]);
400+
401+
$articleClass::addLogChange(new class() implements LoggablePipe {
402+
public function handle(EventLogBag $event, Closure $next): EventLogBag
403+
{
404+
if ('updated' === $event->event) {
405+
$event->changes = [
406+
'attributes' => [],
407+
'old' => [],
408+
];
409+
}
410+
411+
return $next($event);
412+
}
413+
});
414+
415+
$article = $articleClass::create([
416+
'name' => 'original name',
417+
'text' => 'original text',
418+
'user_id' => $user->id,
419+
]);
420+
421+
$article->name = 'updated name';
422+
$article->text = 'updated text';
423+
$article->save();
424+
425+
$this->assertEquals(1, Activity::query()->count(), 'Only the created event should be present');
426+
$this->assertEquals('created', $this->getLastActivity()->event);
427+
});
428+
385429
it('can store empty relation when creating a model', function () {
386430
$articleClass = new class() extends Article {
387431
use LogsActivity;

0 commit comments

Comments
 (0)