Skip to content

Commit

Permalink
Tap subject when using the logger (#1031)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancisMawn committed Apr 7, 2022
1 parent 3419620 commit a177e7e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/ActivityLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ public function log(string $description): ?ActivityContract
$activity
);

if (isset($activity->subject) && method_exists($activity->subject, 'tapActivity')) {
$this->tap([$activity->subject, 'tapActivity'], $activity->event ?? '');
}

$activity->save();

$this->activity = null;
Expand Down
25 changes: 25 additions & 0 deletions tests/ActivityLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
use Illuminate\Support\Collection;
use Spatie\Activitylog\Exceptions\CouldNotLogActivity;
use Spatie\Activitylog\Facades\CauserResolver;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Models\Activity;
use Spatie\Activitylog\Test\Models\Article;
use Spatie\Activitylog\Test\Models\User;
use Spatie\Activitylog\Traits\LogsActivity;

beforeEach(function () {
$this->activityDescription = 'My activity';
Expand Down Expand Up @@ -301,6 +303,29 @@
expect($firstActivity->created_at->format('Y-m-d H:i:s'))->toEqual(Carbon::yesterday()->startOfDay()->format('Y-m-d H:i:s'));
});

it('will tap a subject', function () {
$model = new class() extends Article {
use LogsActivity;

public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults();
}

public function tapActivity(Activity $activity, string $eventName)
{
$activity->description = 'my custom description';
}
};

activity()
->on($model)
->log($this->activityDescription);

$firstActivity = Activity::first();
$this->assertEquals('my custom description', $firstActivity->description);
});

it('will log a custom created at date time', function () {
$activityDateTime = now()->subDays(10);

Expand Down

0 comments on commit a177e7e

Please sign in to comment.