Skip to content

Commit 29fe9c3

Browse files
committed
Fix publish method when called on a draft
1 parent a45624f commit 29fe9c3

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

src/Concerns/HasDrafts.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@ public function initializeHasDrafts()
3737
{
3838
$this->mergeCasts([
3939
$this->getIsCurrentColumn() => 'boolean',
40+
$this->getIsPublishedColumn() => 'boolean',
4041
$this->getPublishedAtColumn() => 'datetime',
4142
]);
4243
}
4344

4445
public static function bootHasDrafts(): void
4546
{
46-
static::creating(function ($model) {
47+
static::creating(function (Model $model): void {
4748
$model->{$model->getIsCurrentColumn()} = true;
4849
$model->setPublisher();
4950
$model->generateUuid();
@@ -52,26 +53,32 @@ public static function bootHasDrafts(): void
5253
}
5354
});
5455

55-
static::updating(function ($model) {
56+
static::updating(function (Model $model): void {
5657
$model->newRevision();
5758
});
5859

59-
static::publishing(function ($model) {
60+
static::publishing(function (Model $model): bool {
6061
$model->setLive();
62+
63+
static::saved(function (Model $model): void {
64+
$model->fireModelEvent('published');
65+
});
66+
67+
return false;
6168
});
6269

63-
static::deleted(function ($model) {
70+
static::deleted(function (Model $model): void {
6471
$model->revisions()->delete();
6572
});
6673

6774
if (method_exists(static::class, 'restored')) {
68-
static::restored(function ($model) {
75+
static::restored(function (Model $model): void {
6976
$model->revisions()->restore();
7077
});
7178
}
7279

7380
if (method_exists(static::class, 'forceDeleted')) {
74-
static::forceDeleted(function ($model) {
81+
static::forceDeleted(function (Model $model): void {
7582
$model->revisions()->forceDelete();
7683
});
7784
}

src/Concerns/Publishes.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ public function publish(): static
4949
$this->{$this->getPublishedAtColumn()} ??= now();
5050
$this->{$this->getIsPublishedColumn()} = true;
5151

52-
static::saved(function (Model $record): void {
53-
if ($record->getKey() !== $this->getKey()) {
52+
static::saved(function (Model $model): void {
53+
if ($model->is($this)) {
5454
return;
5555
}
5656

tests/PublishingTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
it('can use `withDrafts` scope to select drafts', function () {
4040
Post::factory()->count(5)->published()->create();
4141
Post::factory()->count(5)->draft()->create();
42-
$this->assertCount(10, Post::withDrafts()->get());
42+
expect(Post::withDrafts()->pluck('id'))
43+
->toHaveCount(10);
4344
});
4445

4546
it('generates a uuid', function () {
@@ -54,3 +55,14 @@
5455
$post = Post::factory()->draft()->create();
5556
expect($post->isPublished())->toBeFalse();
5657
});
58+
59+
it('does not create multiple published records', function () {
60+
$post = Post::factory()->create();
61+
$post->title = 'b';
62+
$post->saveAsDraft();
63+
$post->draft->publish()->save();
64+
expect(Post::withDrafts()->pluck('id'))
65+
->toHaveCount(2);
66+
expect(Post::withoutDrafts()->pluck('id'))
67+
->toHaveCount(1);
68+
});

0 commit comments

Comments
 (0)