Skip to content

Commit 41cd352

Browse files
committed
Adds morph method helper to pending turbo stream responses
1 parent df58d03 commit 41cd352

File tree

2 files changed

+75
-7
lines changed

2 files changed

+75
-7
lines changed

src/Http/PendingTurboStreamResponse.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Illuminate\Contracts\Support\Responsable;
1313
use Illuminate\Contracts\View\View;
1414
use Illuminate\Database\Eloquent\Model;
15+
use Illuminate\Support\Arr;
1516
use Illuminate\Support\HtmlString;
1617
use Illuminate\Support\Traits\Macroable;
1718

@@ -37,7 +38,7 @@ class PendingTurboStreamResponse implements Htmlable, Renderable, Responsable
3738

3839
public static function forModel(Model $model, ?string $action = null): self
3940
{
40-
$builder = new self();
41+
$builder = new self;
4142

4243
// We're treating soft-deleted models as they were deleted. In other words, we
4344
// will render the remove Turbo Stream. If you need to treat a soft-deleted
@@ -108,6 +109,22 @@ public function attributes(array $attributes): self
108109
return $this;
109110
}
110111

112+
public function morph(): self
113+
{
114+
return $this->method('morph');
115+
}
116+
117+
public function method(?string $method = null): self
118+
{
119+
if ($method) {
120+
return $this->attributes(array_merge($this->useCustomAttributes, [
121+
'method' => $method,
122+
]));
123+
}
124+
125+
return $this->attributes(Arr::except($this->useCustomAttributes, 'method'));
126+
}
127+
111128
public function append(Model|string $target, $content = null): self
112129
{
113130
return $this->buildAction(
@@ -267,8 +284,7 @@ private function buildActionAll(string $action, Model|string $targets, $content
267284

268285
public function broadcastTo($channel, ?callable $callback = null)
269286
{
270-
$callback = $callback ?? function () {
271-
};
287+
$callback = $callback ?? function () {};
272288

273289
return tap($this, function () use ($channel, $callback) {
274290
$callback($this->asPendingBroadcast($channel));
@@ -277,8 +293,7 @@ public function broadcastTo($channel, ?callable $callback = null)
277293

278294
public function broadcastToPrivateChannel($channel, ?callable $callback = null)
279295
{
280-
$callback = $callback ?? function () {
281-
};
296+
$callback = $callback ?? function () {};
282297

283298
return $this->broadcastTo(null, function (PendingBroadcast $broadcast) use ($channel, $callback) {
284299
$broadcast->toPrivateChannel($channel);
@@ -288,8 +303,7 @@ public function broadcastToPrivateChannel($channel, ?callable $callback = null)
288303

289304
public function broadcastToPresenceChannel($channel, ?callable $callback = null)
290305
{
291-
$callback = $callback ?? function () {
292-
};
306+
$callback = $callback ?? function () {};
293307

294308
return $this->broadcastTo(null, function (PendingBroadcast $broadcast) use ($channel, $callback) {
295309
$callback($broadcast->toPresenceChannel($channel));

tests/FunctionsTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,33 @@ public function namespaced_turbo_stream_fn()
6464
HTML),
6565
trim(turbo_stream($this->article)),
6666
);
67+
68+
$expected = trim(view('articles._article', [
69+
'article' => $this->article,
70+
])->render());
71+
72+
$this->assertEquals(
73+
trim(<<<HTML
74+
<turbo-stream target="article_{$this->article->id}" action="replace" method="morph">
75+
<template>{$expected}</template>
76+
</turbo-stream>
77+
HTML),
78+
trim(turbo_stream($this->article->fresh())->morph()),
79+
);
80+
81+
// Unsets method
82+
$expected = trim(view('articles._article', [
83+
'article' => $this->article,
84+
])->render());
85+
86+
$this->assertEquals(
87+
trim(<<<HTML
88+
<turbo-stream target="article_{$this->article->id}" action="replace">
89+
<template>{$expected}</template>
90+
</turbo-stream>
91+
HTML),
92+
trim(turbo_stream($this->article->fresh())->morph()->method()),
93+
);
6794
}
6895

6996
/** @test */
@@ -105,6 +132,33 @@ public function global_turbo_stream_fn()
105132
HTML),
106133
trim(\turbo_stream($this->article)),
107134
);
135+
136+
$expected = trim(view('articles._article', [
137+
'article' => $this->article,
138+
])->render());
139+
140+
$this->assertEquals(
141+
trim(<<<HTML
142+
<turbo-stream target="article_{$this->article->id}" action="replace" method="morph">
143+
<template>{$expected}</template>
144+
</turbo-stream>
145+
HTML),
146+
trim(\turbo_stream($this->article->fresh())->morph()),
147+
);
148+
149+
// Unsets method
150+
$expected = trim(view('articles._article', [
151+
'article' => $this->article,
152+
])->render());
153+
154+
$this->assertEquals(
155+
trim(<<<HTML
156+
<turbo-stream target="article_{$this->article->id}" action="replace">
157+
<template>{$expected}</template>
158+
</turbo-stream>
159+
HTML),
160+
trim(\turbo_stream($this->article->fresh())->morph()->method()),
161+
);
108162
}
109163

110164
/** @test */

0 commit comments

Comments
 (0)