Skip to content

Commit

Permalink
Merge pull request #680 from seanpdoyle/issue-679
Browse files Browse the repository at this point in the history
Ensure `turbo-stream[action="remove"]` does not render a view partial by default
  • Loading branch information
brunoprietog authored Sep 18, 2024
2 parents 780ee0d + 51aa2a5 commit 9562a65
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
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

0 comments on commit 9562a65

Please sign in to comment.