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

fix(versionable): latest versions list #78

Merged
merged 1 commit into from
Apr 30, 2021
Merged
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
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 = "j.tester@test.php";
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 = "john@snow.com";
$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