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

Ensure turbo-stream[action="remove"] does not render a view partial by default #680

Merged
merged 1 commit into from
Sep 18, 2024
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
18 changes: 13 additions & 5 deletions app/channels/turbo/streams/broadcasts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Turbo::Streams::Broadcasts
include Turbo::Streams::ActionHelper

def broadcast_remove_to(*streamables, **opts)
broadcast_action_to(*streamables, action: :remove, **opts)
broadcast_action_to(*streamables, action: :remove, render: false, **opts)
end

def broadcast_replace_to(*streamables, **opts)
Expand Down Expand Up @@ -38,10 +38,18 @@ def broadcast_refresh_to(*streamables, **opts)
end

def broadcast_action_to(*streamables, action:, target: nil, targets: nil, attributes: {}, **rendering)
broadcast_stream_to(*streamables, content: turbo_stream_action_tag(action, target: target, targets: targets, template:
rendering.delete(:content) || rendering.delete(:html) || (rendering[:render] != false && rendering.any? ? render_format(:html, **rendering) : nil),
**attributes
))
content = rendering.delete(:content)
html = rendering.delete(:html)
render = rendering.delete(:render)

template =
if render == false
nil
else
content || html || (render_format(:html, **rendering) if rendering.present?)
end

broadcast_stream_to(*streamables, content: turbo_stream_action_tag(action, target: target, targets: targets, template: template, **attributes))
end

def broadcast_replace_later_to(*streamables, **opts)
Expand Down
1 change: 1 addition & 0 deletions test/dummy/app/views/messages/_raises_error.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= raise "Failed to render" %>
22 changes: 22 additions & 0 deletions test/streams/broadcastable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
class Turbo::BroadcastableTest < ActionCable::Channel::TestCase
include ActiveJob::TestHelper, Turbo::Streams::ActionHelper

class MessageThatRendersError < Message
def to_partial_path
"messages/raises_error"
end
end

setup { @message = Message.new(id: 1, content: "Hello!") }

test "broadcasting ignores blank streamables" do
Expand Down Expand Up @@ -37,6 +43,14 @@ class Turbo::BroadcastableTest < ActionCable::Channel::TestCase
end
end

test "broadcasting remove does not render contents" do
message = MessageThatRendersError.new(id: 1)

assert_broadcast_on message.to_gid_param, turbo_stream_action_tag("remove", target: dom_id(message)) do
message.broadcast_remove
end
end

test "broadcasting replace to stream now" do
assert_broadcast_on "stream", turbo_stream_action_tag("replace", target: "message_1", template: render(@message)) do
@message.broadcast_replace_to "stream"
Expand Down Expand Up @@ -127,6 +141,14 @@ class Turbo::BroadcastableTest < ActionCable::Channel::TestCase
end
end

test "broadcasting refresh does not render contents" do
message = MessageThatRendersError.new(id: 1)

assert_broadcast_on message.to_gid_param, turbo_stream_action_tag("refresh") do
message.broadcast_refresh
end
end

test "broadcasting refresh later is debounced" do
assert_broadcast_on @message.to_gid_param, turbo_stream_refresh_tag do
assert_broadcasts(@message.to_gid_param, 1) do
Expand Down
Loading