From 176a2d93066f73296d4b756039106b7c759d7d91 Mon Sep 17 00:00:00 2001 From: Dmitriy Ben Date: Thu, 25 Mar 2021 09:05:50 +0200 Subject: [PATCH] fix(versionable): latest versions list --- src/Mpociot/Versionable/VersionableTrait.php | 22 +++++++++++--------- tests/VersionableTest.php | 19 ----------------- 2 files changed, 12 insertions(+), 29 deletions(-) diff --git a/src/Mpociot/Versionable/VersionableTrait.php b/src/Mpociot/Versionable/VersionableTrait.php index c321b0f..a58d05f 100644 --- a/src/Mpociot/Versionable/VersionableTrait.php +++ b/src/Mpociot/Versionable/VersionableTrait.php @@ -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(); } /** @@ -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(); } /** @@ -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() @@ -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'); } diff --git a/tests/VersionableTest.php b/tests/VersionableTest.php index 7aa580c..c46baeb 100644 --- a/tests/VersionableTest.php +++ b/tests/VersionableTest.php @@ -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(); @@ -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 = "j.tester@test.php"; @@ -341,7 +336,6 @@ public function testDiffTwoVersions() $user->password = "12345"; $user->last_login = $user->freshTimestamp(); $user->save(); - sleep(1); $user->name = "John"; $user->save(); @@ -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(); @@ -385,12 +378,10 @@ public function testDiffSpecificVersions() $user->password = "12345"; $user->last_login = $user->freshTimestamp(); $user->save(); - sleep(1); $user->name = "John"; $user->email = "john@snow.com"; $user->save(); - sleep(1); $user->name = "Julia"; $user->save(); @@ -419,8 +410,6 @@ public function testDynamicVersionModel() $model->name = $name_v1 ; $model->save(); - sleep(1); - $model->name = $name_v2 ; $model->save(); @@ -455,8 +444,6 @@ public function testItUsesConfigurableVersionClass() $model->password = $name_v1 ; $model->save(); - sleep(1); - $model->name = $name_v2 ; $model->save(); @@ -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();