Skip to content

Commit

Permalink
Replace withoutSelf with excludeRevision
Browse files Browse the repository at this point in the history
  • Loading branch information
oddvalue committed Feb 7, 2023
1 parent 991186f commit 16fa512
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/Concerns/HasDrafts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
Expand Down Expand Up @@ -278,7 +284,7 @@ public function setPublisher(): static

public function pruneRevisions()
{
$this->withoutEvents(function () {
self::withoutEvents(function () {
$revisionsToKeep = $this->revisions()
->orderByDesc('updated_at')
->onlyDrafts()
Expand Down Expand Up @@ -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);
}
Expand Down
11 changes: 11 additions & 0 deletions tests/DraftsTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Illuminate\Support\Facades\DB;
use Oddvalue\LaravelDrafts\Tests\Post;

use function Spatie\PestPluginTestTime\testTime;
Expand Down Expand Up @@ -124,3 +125,13 @@

expect(Post::find($originalId)->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);
});

0 comments on commit 16fa512

Please sign in to comment.