Skip to content

Commit 34c2e41

Browse files
committed
feat: Version::isLatest()
1 parent 3fbdbdd commit 34c2e41

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

src/Diff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ protected function getContents(bool $stripTags = false): array
7777
// all versions before this version.
7878
$versionsBeforeThis = $this->newVersion->previousVersions()->get();
7979

80-
foreach ($versionsBeforeThis->reverse() as $version) { // DIFF show previous and current updated data
80+
foreach ($versionsBeforeThis->reverse() as $version) { // DIFF show previous and current updated data
8181
if (! empty($version->contents)) {
8282
$oldContents = array_merge($oldContents, $version->contents);
8383
}

src/Version.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@ public function nextVersion(): ?static
181181
->first();
182182
}
183183

184+
public function isLatest(): bool
185+
{
186+
return $this->getKey() === $this->versionable->latestVersion()->getKey();
187+
}
188+
184189
public function diff(?Version $toVersion = null, array $differOptions = [], array $renderOptions = []): Diff
185190
{
186191
if (! $toVersion) {

tests/FeatureTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,18 @@ public function user_can_get_previous_version()
194194
$this->assertNull($post->latestVersion->previousVersion()->previousVersion()->previousVersion());
195195
}
196196

197+
public function user_can_detect_whether_the_version_is_the_latest_version(): void
198+
{
199+
$post = Post::create(['title' => 'version1', 'content' => 'version1 content']);
200+
$post->update(['title' => 'version2']);
201+
$post->update(['title' => 'version3']);
202+
203+
$post->refresh();
204+
205+
$this->assertTrue($post->latestVersion->isLatest());
206+
$this->assertFalse($post->latestVersion->previousVersion()->isLatest());
207+
}
208+
197209
/**
198210
* @test
199211
*/

0 commit comments

Comments
 (0)