From 16fa512b471f935b9fef40e9ec4148a0b89f8249 Mon Sep 17 00:00:00 2001 From: jim Date: Tue, 7 Feb 2023 07:46:51 +0000 Subject: [PATCH] Replace `withoutSelf` with `excludeRevision` --- src/Concerns/HasDrafts.php | 24 +++++++++++++++++++----- tests/DraftsTest.php | 11 +++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/Concerns/HasDrafts.php b/src/Concerns/HasDrafts.php index 8b49a67..602c96a 100644 --- a/src/Concerns/HasDrafts.php +++ b/src/Concerns/HasDrafts.php @@ -3,6 +3,7 @@ namespace Oddvalue\LaravelDrafts\Concerns; use Illuminate\Contracts\Database\Query\Builder; +use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasOne; @@ -13,6 +14,11 @@ use JetBrains\PhpStorm\ArrayShape; use Oddvalue\LaravelDrafts\Facades\LaravelDrafts; +/** + * @method void Current(Builder $query) + * @method void WithoutCurrent(Builder $query) + * @method void ExcludeRevision(Builder $query, int | Model $exclude) + */ trait HasDrafts { use Publishes; @@ -86,7 +92,7 @@ protected function newRevision(): void return; } - $revision = $this->fresh()->replicate(); + $revision = $this->fresh()?->replicate(); static::saved(function () use ($revision) { $revision->created_at = $this->created_at; @@ -118,7 +124,7 @@ public function generateUuid(): void public function setCurrent(): void { - $oldCurrent = $this->revisions()->withDrafts()->current()->withoutSelf()->first(); + $oldCurrent = $this->revisions()->withDrafts()->current()->excludeRevision($this)->first(); static::saved(function () use ($oldCurrent) { if ($oldCurrent) { @@ -133,7 +139,7 @@ public function setCurrent(): void public function setLive(): void { - $published = $this->revisions()->withoutSelf()->published()->first(); + $published = $this->revisions()->excludeRevision($this)->published()->first(); if (! $published) { $this->{$this->getPublishedAtColumn()} ??= now(); @@ -278,7 +284,7 @@ public function setPublisher(): static public function pruneRevisions() { - $this->withoutEvents(function () { + self::withoutEvents(function () { $revisionsToKeep = $this->revisions() ->orderByDesc('updated_at') ->onlyDrafts() @@ -374,7 +380,15 @@ public function scopeWithoutCurrent(Builder $query): void $query->where($this->getIsCurrentColumn(), false); } - public function scopeWithoutSelf(Builder $query) + public function scopeExcludeRevision(Builder $query, int | Model $exclude): void + { + $query->where($this->getKeyName(), '!=', is_int($exclude) ? $exclude : $exclude->getKey()); + } + + /** + * @deprecated This doesn't actually work, will be removed in next version + */ + public function scopeWithoutSelf(Builder $query): void { $query->where('id', '!=', $this->id); } diff --git a/tests/DraftsTest.php b/tests/DraftsTest.php index 934a560..6858c82 100644 --- a/tests/DraftsTest.php +++ b/tests/DraftsTest.php @@ -1,5 +1,6 @@ title)->toBe('Qux'); }); + +it('can get all revisions excluding self', function (): void { + $post = Post::factory()->create(['title' => 'Foo']); + $post->updateAsDraft(['title' => 'Bar']); + $post->updateAsDraft(['title' => 'Baz']); + $post->updateAsDraft(['title' => 'Qux']); + + expect($post->revisions()->excludeRevision($post)->pluck('id')) + ->not()->toContain($post->id); +});