Skip to content

Commit

Permalink
Merge pull request #735 from spatie/issue-733
Browse files Browse the repository at this point in the history
do not call fresh() if retrieved event is processed
  • Loading branch information
Gummibeer committed May 19, 2020
2 parents e895bde + 990c675 commit c5ee012
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Traits/DetectsChanges.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,13 @@ public function attributeValuesToBeLogged(string $processingEvent): array
}

$properties['attributes'] = static::logChanges(
$this->exists
? $this->fresh() ?? $this
: $this
$processingEvent == 'retrieved'
? $this
: (
$this->exists
? $this->fresh() ?? $this
: $this
)
);

if (static::eventsToBeRecorded()->contains('updated') && $processingEvent == 'updated') {
Expand Down
16 changes: 16 additions & 0 deletions tests/LogsActivityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\Collection;
use Spatie\Activitylog\Models\Activity;
use Spatie\Activitylog\Test\Models\Article;
use Spatie\Activitylog\Test\Models\Issue733;
use Spatie\Activitylog\Test\Models\User;
use Spatie\Activitylog\Traits\LogsActivity;

Expand Down Expand Up @@ -423,6 +424,21 @@ public function it_will_submit_a_log_with_json_changes()
$this->assertSame($expectedChanges, $changes);
}

/** @test */
public function it_will_log_the_retrieval_of_the_model()
{
$article = Issue733::create(['name' => 'my name']);

$retrieved = Issue733::whereKey($article->getKey())->first();
$this->assertTrue($article->is($retrieved));

$activity = $this->getLastActivity();

$this->assertInstanceOf(get_class($article), $activity->subject);
$this->assertTrue($article->is($activity->subject));
$this->assertEquals('retrieved', $activity->description);
}

public function loginWithFakeUser()
{
$user = new $this->user();
Expand Down
18 changes: 18 additions & 0 deletions tests/Models/Issue733.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Spatie\Activitylog\Test\Models;

use Spatie\Activitylog\Traits\LogsActivity;

class Issue733 extends Article
{
use LogsActivity;

protected static $recordEvents = [
'retrieved',
];

protected static $submitEmptyLogs = false;
protected static $logAttributes = ['name'];
public static $logOnlyDirty = false;
}

0 comments on commit c5ee012

Please sign in to comment.