Skip to content

Commit

Permalink
Allow returning custom turbo streams without template on responses
Browse files Browse the repository at this point in the history
  • Loading branch information
tonysm committed Aug 26, 2024
1 parent 8b7f8c7 commit 5158503
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Http/PendingTurboStreamResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class PendingTurboStreamResponse implements Htmlable, Renderable, Responsable
{
use Macroable;

private array $defaultActions = ['append', 'prepend', 'update', 'replace', 'before', 'after', 'remove', 'refresh'];

private string $useAction;

private ?string $useTarget = null;
Expand Down Expand Up @@ -341,7 +343,7 @@ private function contentAsRendering()
*/
public function toResponse($request)
{
if (! in_array($this->useAction, ['remove', 'refresh']) && ! $this->partialView && $this->inlineContent === null) {
if (! in_array($this->useAction, ['remove', 'refresh']) && in_array($this->useAction, $this->defaultActions) && ! $this->partialView && $this->inlineContent === null) {
throw TurboStreamResponseFailedException::missingPartial();
}

Expand Down
22 changes: 22 additions & 0 deletions tests/Http/TurboStreamResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,26 @@ public function turbo_assert_has_turbo_stream()
))
));
}

/** @test */
public function turbo_allows_custom_actions_with_no_view()
{
$this->assertEquals(
<<<'HTML'
<turbo-stream target="contact-edit-drawer" action="close-drawer">
</turbo-stream>

HTML,
(string) turbo_stream()->action('close-drawer')->target('contact-edit-drawer'),
);

$this->assertEquals(
<<<'HTML'
<turbo-stream target="contact-edit-drawer" action="close-drawer">
</turbo-stream>

HTML,
turbo_stream()->action('close-drawer')->target('contact-edit-drawer')->toResponse(request())->getContent(),
);
}
}

0 comments on commit 5158503

Please sign in to comment.