Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add except record event #1244

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 6 additions & 0 deletions src/Traits/LogsActivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ protected static function eventsToBeRecorded(): Collection
$events->push('restored');
}

if (isset(static::$exceptRecordEvents)) {
$events = $events->filter(function (string $event) {
return !in_array($event, static::$exceptRecordEvents);
});
}

return $events;
}

Expand Down
21 changes: 21 additions & 0 deletions tests/LogsActivityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,27 @@ public function getActivitylogOptions(): LogOptions
$this->assertNull($this->getLastActivity());
});

it('except created log event', function () {
$articleClass = new class() extends Article {
use LogsActivity;

protected $exceptRecordEvents = [
'created',
];

public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()->logOnly(['name']);
}
};
$article = new $articleClass();
$article->name = 'my name';
$article->save();

$this->assertCount(0, Activity::all());
$this->assertNull($this->getLastActivity());
});

it('can switch on activity logging after disabling it', function () {
$article = new $this->article();

Expand Down