Skip to content

Commit 16fa512

Browse files
committed
Replace withoutSelf with excludeRevision
1 parent 991186f commit 16fa512

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

src/Concerns/HasDrafts.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Oddvalue\LaravelDrafts\Concerns;
44

55
use Illuminate\Contracts\Database\Query\Builder;
6+
use Illuminate\Database\Eloquent\Model;
67
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
78
use Illuminate\Database\Eloquent\Relations\HasMany;
89
use Illuminate\Database\Eloquent\Relations\HasOne;
@@ -13,6 +14,11 @@
1314
use JetBrains\PhpStorm\ArrayShape;
1415
use Oddvalue\LaravelDrafts\Facades\LaravelDrafts;
1516

17+
/**
18+
* @method void Current(Builder $query)
19+
* @method void WithoutCurrent(Builder $query)
20+
* @method void ExcludeRevision(Builder $query, int | Model $exclude)
21+
*/
1622
trait HasDrafts
1723
{
1824
use Publishes;
@@ -86,7 +92,7 @@ protected function newRevision(): void
8692
return;
8793
}
8894

89-
$revision = $this->fresh()->replicate();
95+
$revision = $this->fresh()?->replicate();
9096

9197
static::saved(function () use ($revision) {
9298
$revision->created_at = $this->created_at;
@@ -118,7 +124,7 @@ public function generateUuid(): void
118124

119125
public function setCurrent(): void
120126
{
121-
$oldCurrent = $this->revisions()->withDrafts()->current()->withoutSelf()->first();
127+
$oldCurrent = $this->revisions()->withDrafts()->current()->excludeRevision($this)->first();
122128

123129
static::saved(function () use ($oldCurrent) {
124130
if ($oldCurrent) {
@@ -133,7 +139,7 @@ public function setCurrent(): void
133139

134140
public function setLive(): void
135141
{
136-
$published = $this->revisions()->withoutSelf()->published()->first();
142+
$published = $this->revisions()->excludeRevision($this)->published()->first();
137143

138144
if (! $published) {
139145
$this->{$this->getPublishedAtColumn()} ??= now();
@@ -278,7 +284,7 @@ public function setPublisher(): static
278284

279285
public function pruneRevisions()
280286
{
281-
$this->withoutEvents(function () {
287+
self::withoutEvents(function () {
282288
$revisionsToKeep = $this->revisions()
283289
->orderByDesc('updated_at')
284290
->onlyDrafts()
@@ -374,7 +380,15 @@ public function scopeWithoutCurrent(Builder $query): void
374380
$query->where($this->getIsCurrentColumn(), false);
375381
}
376382

377-
public function scopeWithoutSelf(Builder $query)
383+
public function scopeExcludeRevision(Builder $query, int | Model $exclude): void
384+
{
385+
$query->where($this->getKeyName(), '!=', is_int($exclude) ? $exclude : $exclude->getKey());
386+
}
387+
388+
/**
389+
* @deprecated This doesn't actually work, will be removed in next version
390+
*/
391+
public function scopeWithoutSelf(Builder $query): void
378392
{
379393
$query->where('id', '!=', $this->id);
380394
}

tests/DraftsTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Illuminate\Support\Facades\DB;
34
use Oddvalue\LaravelDrafts\Tests\Post;
45

56
use function Spatie\PestPluginTestTime\testTime;
@@ -124,3 +125,13 @@
124125

125126
expect(Post::find($originalId)->title)->toBe('Qux');
126127
});
128+
129+
it('can get all revisions excluding self', function (): void {
130+
$post = Post::factory()->create(['title' => 'Foo']);
131+
$post->updateAsDraft(['title' => 'Bar']);
132+
$post->updateAsDraft(['title' => 'Baz']);
133+
$post->updateAsDraft(['title' => 'Qux']);
134+
135+
expect($post->revisions()->excludeRevision($post)->pluck('id'))
136+
->not()->toContain($post->id);
137+
});

0 commit comments

Comments
 (0)