Skip to content

Commit

Permalink
Merge pull request #78 from benms/fix_latest_versions_list
Browse files Browse the repository at this point in the history
Fix latest versions list.
  • Loading branch information
nonoesp authored Apr 30, 2021
2 parents b3cbcff + 176a2d9 commit 434f1d1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 29 deletions.
22 changes: 12 additions & 10 deletions src/Mpociot/Versionable/VersionableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ public function versions()
*/
public function currentVersion()
{
$class = $this->getVersionClass();
return $this->versions()->orderBy( $class::CREATED_AT, 'DESC')->first();
return $this->getLatestVersions()->first();
}

/**
Expand All @@ -123,8 +122,7 @@ public function currentVersion()
*/
public function previousVersion()
{
$class = $this->getVersionClass();
return $this->versions()->latest()->limit(1)->offset(1)->first();
return $this->getLatestVersions()->limit(1)->offset(1)->first();
}

/**
Expand Down Expand Up @@ -200,8 +198,7 @@ private function purgeOldVersions()
$count = $this->versions()->count();

if ($count > $keep) {
$oldVersions = $this->versions()
->latest()
$this->getLatestVersions()
->take($count)
->skip($keep)
->get()
Expand Down Expand Up @@ -234,10 +231,15 @@ private function isValidForVersioning()
*/
protected function getAuthUserId()
{
if (Auth::check()) {
return Auth::id();
}
return null;
return Auth::check() ? Auth::id() : null;
}

/**
* @return \Illuminate\Database\Eloquent\Builder
*/
protected function getLatestVersions()
{
return $this->versions()->orderByDesc('version_id');
}


Expand Down
19 changes: 0 additions & 19 deletions tests/VersionableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ public function testRetrievePreviousVersionExists()
$user->password = "12345";
$user->last_login = $user->freshTimestamp();
$user->save();
// Needed because otherwise timestamps are exactly the same
sleep(1);

$user->name = "John";
$user->save();
Expand Down Expand Up @@ -122,9 +120,6 @@ public function testGetResponsibleUserAttribute()

auth()->login($responsibleOrigUser);

// Needed because otherwise timestamps are exactly the same
sleep(1);

$user = new TestVersionableUser();
$user->name = "John";
$user->email = "[email protected]";
Expand Down Expand Up @@ -341,7 +336,6 @@ public function testDiffTwoVersions()
$user->password = "12345";
$user->last_login = $user->freshTimestamp();
$user->save();
sleep(1);

$user->name = "John";
$user->save();
Expand All @@ -361,7 +355,6 @@ public function testDiffIgnoresTimestamps()
$user->password = "12345";
$user->last_login = $user->freshTimestamp();
$user->save();
sleep(1);

$user->name = "John";
$user->created_at = Carbon::now();
Expand All @@ -385,12 +378,10 @@ public function testDiffSpecificVersions()
$user->password = "12345";
$user->last_login = $user->freshTimestamp();
$user->save();
sleep(1);

$user->name = "John";
$user->email = "[email protected]";
$user->save();
sleep(1);

$user->name = "Julia";
$user->save();
Expand Down Expand Up @@ -419,8 +410,6 @@ public function testDynamicVersionModel()
$model->name = $name_v1 ;
$model->save();

sleep(1);

$model->name = $name_v2 ;
$model->save();

Expand Down Expand Up @@ -455,8 +444,6 @@ public function testItUsesConfigurableVersionClass()
$model->password = $name_v1 ;
$model->save();

sleep(1);

$model->name = $name_v2 ;
$model->save();

Expand All @@ -480,18 +467,12 @@ public function testKeepMaxVersionCount()
$model->name = $name_v1 ;
$model->save();

sleep(1);

$model->name = $name_v2 ;
$model->save();

sleep(1);

$model->name = $name_v3 ;
$model->save();

sleep(1);

$model->name = $name_v4 ;
$model->save();

Expand Down

0 comments on commit 434f1d1

Please sign in to comment.