Skip to content

Commit

Permalink
[Fix] Prevent multiple current records after publishing draft (#39)
Browse files Browse the repository at this point in the history
* [Fix] Prevent multiple current records after publishing draft

* Fix styling

---------

Co-authored-by: oddvalue <[email protected]>
  • Loading branch information
oddvalue and oddvalue authored Jun 23, 2023
1 parent 1a4e8fa commit cf35953
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 35 deletions.
31 changes: 20 additions & 11 deletions src/Concerns/HasDrafts.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,19 +147,19 @@ public function getDraftableAttributes(): array

public function setCurrent(): void
{
$oldCurrent = $this->revisions()->withDrafts()->current()->excludeRevision($this)->first();
$this->{$this->getIsCurrentColumn()} = true;

static::saved(function (Model $model) use ($oldCurrent): void {
if ($model->isNot($this) || ! $oldCurrent) {
static::saved(function (Model $model): void {
if ($model->isNot($this)) {
return;
}

$oldCurrent->{$this->getIsCurrentColumn()} = false;
$oldCurrent->timestamps = false;
$oldCurrent->saveQuietly();
$this->revisions()
->withDrafts()
->current()
->excludeRevision($this)
->update([$this->getIsCurrentColumn() => false]);
});

$this->{$this->getIsCurrentColumn()} = true;
}

public function setLive(): void
Expand Down Expand Up @@ -199,7 +199,9 @@ public function setLive(): void
if ($related = $this->{$relationName}) {
$replicated = $related->replicate();

$method = method_exists($replicated, 'getDraftableAttributes') ? 'getDraftableAttributes' : 'getAttributes';
$method = method_exists($replicated, 'getDraftableAttributes')
? 'getDraftableAttributes'
: 'getAttributes';

$published->{$relationName}()->create($replicated->$method());
}
Expand All @@ -209,7 +211,9 @@ public function setLive(): void
$this->{$relationName}()->get()->each(function ($model) use ($published, $relationName) {
$replicated = $model->replicate();

$method = method_exists($replicated, 'getDraftableAttributes') ? 'getDraftableAttributes' : 'getAttributes';
$method = method_exists($replicated, 'getDraftableAttributes')
? 'getDraftableAttributes'
: 'getAttributes';

$published->{$relationName}()->create($replicated->$method());
});
Expand Down Expand Up @@ -380,11 +384,16 @@ public function getIsCurrentColumn(): string

public function getUuidColumn(): string
{
return defined(static::class.'::UUID')
return defined(static::class . '::UUID')
? static::UUID
: config('drafts.column_names.uuid', 'uuid');
}

public function isCurrent(): bool
{
return $this->{$this->getIsCurrentColumn()} ?? false;
}

/*
|--------------------------------------------------------------------------
| RELATIONS
Expand Down
48 changes: 24 additions & 24 deletions src/Scopes/PublishingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,30 @@ public function extend(Builder $builder): void
}
}

// protected function addPublish(Builder $builder): void
// {
// $builder->macro('publish', function (Builder $builder) {
// $builder->withDrafts();
//
// return $builder->update([$builder->getModel()->getIsPublishedColumn() => now()]);
// });
// }
//
// protected function addUnpublish(Builder $builder): void
// {
// $builder->macro('unpublish', function (Builder $builder) {
// return $builder->update([$builder->getModel()->getIsPublishedColumn() => null]);
// });
// }
//
// protected function addSchedule(Builder $builder): void
// {
// $builder->macro('schedule', function (Builder $builder, string | \DateTimeInterface $date) {
// $builder->withDrafts();
//
// return $builder->update([$builder->getModel()->getIsPublishedColumn() => $date]);
// });
// }
// protected function addPublish(Builder $builder): void
// {
// $builder->macro('publish', function (Builder $builder) {
// $builder->withDrafts();
//
// return $builder->update([$builder->getModel()->getIsPublishedColumn() => now()]);
// });
// }
//
// protected function addUnpublish(Builder $builder): void
// {
// $builder->macro('unpublish', function (Builder $builder) {
// return $builder->update([$builder->getModel()->getIsPublishedColumn() => null]);
// });
// }
//
// protected function addSchedule(Builder $builder): void
// {
// $builder->macro('schedule', function (Builder $builder, string | \DateTimeInterface $date) {
// $builder->withDrafts();
//
// return $builder->update([$builder->getModel()->getIsPublishedColumn() => $date]);
// });
// }

protected function addPublished(Builder $builder): void
{
Expand Down
25 changes: 25 additions & 0 deletions tests/PublishingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,28 @@
expect(Post::withoutDrafts()->pluck('id'))
->toHaveCount(1);
});

it('can publish a draft that is not the current one', function () {
\Oddvalue\LaravelDrafts\Facades\LaravelDrafts::withDrafts();

Post::factory()->create(['title' => 'a']);

$post = Post::where('title', 'a')->first();
$post->title = 'b';
$b = $post->saveAsDraft();

$post = Post::where('title', 'b')->first();
$post->title = 'c';
$post->saveAsDraft();

expect(Post::where('title', 'a')->first()->isPublished())->toBeTrue();
expect(Post::where('title', 'c')->first()->isCurrent())->toBeTrue();

$draftB = Post::where('title', 'b')->first();
$draftB->setLive();
$draftB->save();

expect(Post::where('title', 'a')->first()->isPublished())->toBeFalse();
expect(Post::where('title', 'b')->first()->isPublished())->toBeTrue();
expect(Post::current()->count())->toBe(1);
});

0 comments on commit cf35953

Please sign in to comment.