oddvalue / laravel-drafts

MIT License
390 stars 18 forks source link

Multiple published records when draft is published #13

Closed jaysson closed 1 year ago

jaysson commented 1 year ago

Hello, thank you for your work on this library.

Here's a failing test, which I thought would pass:

it('does not create multiple published records', function () {
    $post = Post::factory()->create();
    $post->saveAsDraft();
    $post->draft->publish()->save();
    $this->assertCount(1, Post::withoutDrafts()->get());
    // Failed asserting that actual size 2 matches expected size 1.
});

If this is the expected behaviour, how do I get only the latest published instances?

SergkeiM commented 1 year ago

Hi @jaysson have same issue, when Publishing, Draft becomes published, but is not current, and old revision becomes, current and marked as published :/

oddvalue commented 1 year ago

Yeah, something's definitely not right. Will take a look, thanks :slightly_smiling_face:

SergkeiM commented 1 year ago

Hi @oddvalue

I think the problem is here:

public function setLive(): void
    {
        $published = $this->revisions()->excludeRevision($this)->published()->first();

If I'm editing a draft and call ->publish() it will exclude it and get the previous (currently published) and mark it as current.

So prevous published record always becomes current and published.

oddvalue commented 1 year ago

Right, been through it all and found a bug in the separation of the Publishes and HasDrafts traits. The Publishes trait was setting the is_published column to true on the record that the publish method was called on. I've moved things around to prevent that and added @jaysson's test. Use v1.0.2 and you should be good to go :slightly_smiling_face: