Skip to content

Commit

Permalink
feat: Version::isLatest()
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Oct 22, 2024
1 parent 3fbdbdd commit 34c2e41
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected function getContents(bool $stripTags = false): array
// all versions before this version.
$versionsBeforeThis = $this->newVersion->previousVersions()->get();

foreach ($versionsBeforeThis->reverse() as $version) { // DIFF show previous and current updated data
foreach ($versionsBeforeThis->reverse() as $version) { // DIFF show previous and current updated data
if (! empty($version->contents)) {
$oldContents = array_merge($oldContents, $version->contents);
}
Expand Down
5 changes: 5 additions & 0 deletions src/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ public function nextVersion(): ?static
->first();
}

public function isLatest(): bool
{
return $this->getKey() === $this->versionable->latestVersion()->getKey();
}

public function diff(?Version $toVersion = null, array $differOptions = [], array $renderOptions = []): Diff
{
if (! $toVersion) {
Expand Down
12 changes: 12 additions & 0 deletions tests/FeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,18 @@ public function user_can_get_previous_version()
$this->assertNull($post->latestVersion->previousVersion()->previousVersion()->previousVersion());
}

public function user_can_detect_whether_the_version_is_the_latest_version(): void
{
$post = Post::create(['title' => 'version1', 'content' => 'version1 content']);
$post->update(['title' => 'version2']);
$post->update(['title' => 'version3']);

$post->refresh();

$this->assertTrue($post->latestVersion->isLatest());
$this->assertFalse($post->latestVersion->previousVersion()->isLatest());
}

/**
* @test
*/
Expand Down

0 comments on commit 34c2e41

Please sign in to comment.