Skip to content

Commit 54e6984

Browse files
committed
Handle soft-deletes as regular deletes for Turbo Stream
Reference: #5 (comment)
1 parent 2df19d7 commit 54e6984

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/TurboStreamResponseMacro.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ public function __construct(TurboStreamModelRenderer $renderer)
1616

1717
public function handle(Model $model, string $action = null)
1818
{
19-
if (! $model->exists) {
19+
if (! $model->exists ||
20+
(method_exists($model, 'trashed') && $model->trashed())
21+
) {
2022
return $this->renderModelDeletedStream($model);
2123
}
2224

tests/Http/ResponseMacrosTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tonysm\TurboLaravel\Tests\Http;
44

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

115+
/** @test */
116+
public function streams_model_on_soft_delete()
117+
{
118+
$testModelSoftDelete = tap(TestModelSoftDelete::create(['name' => 'test']))->delete();
119+
120+
$expected = <<<html
121+
<turbo-stream target="test_model_soft_delete_{$testModelSoftDelete->getKey()}" action="remove"></turbo-stream>
122+
html;
123+
124+
$resp = response()->turboStream($testModelSoftDelete);
125+
126+
$this->assertEquals($expected, trim($resp->getContent()));
127+
$this->assertEquals(Turbo::TURBO_STREAM_FORMAT, $resp->headers->get('Content-Type'));
128+
}
129+
114130
/** @test */
115131
public function streams_broadcastable_models_for_deleted()
116132
{
@@ -190,6 +206,16 @@ public function hotwirePartialName()
190206
}
191207
}
192208

209+
class TestModelSoftDelete extends TestModel
210+
{
211+
use SoftDeletes;
212+
213+
public function hotwirePartialName()
214+
{
215+
return "_test_model_soft_delete";
216+
}
217+
}
218+
193219
class BroadcastTestModel extends \Tonysm\TurboLaravel\Tests\TestModel
194220
{
195221
use Broadcasts;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div id="@domid($testModelSoftDelete)">hello</div>

0 commit comments

Comments
 (0)