Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/Traits/LogsActivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ protected static function bootLogsActivity(): void
return;
}

if ($model->isLogEmpty($changes) && ! $model->activitylogOptions->submitEmptyLogs) {
return;
}

// User can define a custom pipelines to mutate, add or remove from changes
// each pipe receives the event carrier bag with changes and the model in
// question every pipe should manipulate new and old attributes.
Expand All @@ -74,6 +70,11 @@ protected static function bootLogsActivity(): void
->through(static::$changesPipes)
->thenReturn();

// Check for empty logs after pipeline has run
if ($model->isLogEmpty($event->changes) && ! $model->activitylogOptions->submitEmptyLogs) {
return;
}

// Actual logging
$logger = app(ActivityLogger::class)
->useLog($logName)
Expand Down
39 changes: 39 additions & 0 deletions tests/DetectsChangesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,45 @@ public function handle(EventLogBag $event, Closure $next): EventLogBag
$this->assertEquals($expectedChanges, $this->getLastActivity()->changes()->toArray());
});

it('will not log when pipe removes all changes and dontSubmitEmptyLogs is set', function () {
$articleClass = new class() extends Article {
use LogsActivity;

public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logOnly(['name'])
->logOnlyDirty()
->dontSubmitEmptyLogs();
}
};

// Add a pipe that removes all changes only for update events
$articleClass::addLogChange(new class() implements LoggablePipe {
public function handle(EventLogBag $event, Closure $next): EventLogBag
{
if ($event->event === 'updated') {
$event->changes = [];
}

return $next($event);
}
});

$article = $articleClass::create([
'name' => 'original name',
]);

// Created event should be logged
$this->assertCount(1, Activity::all());

$article->name = 'updated name';
$article->save();

// Updated event should NOT be logged because pipe removed all changes
$this->assertCount(1, Activity::all());
});

it('can store empty relation when creating a model', function () {
$articleClass = new class() extends Article {
use LogsActivity;
Expand Down