Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle soft-deletes as regular deletes for Turbo Stream #16

Merged
merged 1 commit into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/TurboStreamResponseMacro.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ public function __construct(TurboStreamModelRenderer $renderer)

public function handle(Model $model, string $action = null)
{
if (! $model->exists) {
if (! $model->exists ||
(method_exists($model, 'trashed') && $model->trashed())
) {
return $this->renderModelDeletedStream($model);
}

Expand Down
26 changes: 26 additions & 0 deletions tests/Http/ResponseMacrosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tonysm\TurboLaravel\Tests\Http;

use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\View;
use Tonysm\TurboLaravel\Models\Broadcasts;
use Tonysm\TurboLaravel\Tests\TestCase;
Expand Down Expand Up @@ -111,6 +112,21 @@ public function streams_model_on_delete()
$this->assertEquals(Turbo::TURBO_STREAM_FORMAT, $resp->headers->get('Content-Type'));
}

/** @test */
public function streams_model_on_soft_delete()
{
$testModelSoftDelete = tap(TestModelSoftDelete::create(['name' => 'test']))->delete();

$expected = <<<html
<turbo-stream target="test_model_soft_delete_{$testModelSoftDelete->getKey()}" action="remove"></turbo-stream>
html;

$resp = response()->turboStream($testModelSoftDelete);

$this->assertEquals($expected, trim($resp->getContent()));
$this->assertEquals(Turbo::TURBO_STREAM_FORMAT, $resp->headers->get('Content-Type'));
}

/** @test */
public function streams_broadcastable_models_for_deleted()
{
Expand Down Expand Up @@ -190,6 +206,16 @@ public function hotwirePartialName()
}
}

class TestModelSoftDelete extends TestModel
{
use SoftDeletes;

public function hotwirePartialName()
{
return "_test_model_soft_delete";
}
}

class BroadcastTestModel extends \Tonysm\TurboLaravel\Tests\TestModel
{
use Broadcasts;
Expand Down
1 change: 1 addition & 0 deletions tests/Stubs/views/_test_model_soft_delete.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div id="@domid($testModelSoftDelete)">hello</div>