diff --git a/app/channels/turbo/streams/broadcasts.rb b/app/channels/turbo/streams/broadcasts.rb index f3aabd39..3a975d5c 100644 --- a/app/channels/turbo/streams/broadcasts.rb +++ b/app/channels/turbo/streams/broadcasts.rb @@ -64,6 +64,8 @@ def broadcast_prepend_later_to(*streamables, **opts) end def broadcast_action_later_to(*streamables, action:, target: nil, targets: nil, **rendering) + target = convert_to_turbo_stream_dom_id(target) + targets = convert_to_turbo_stream_dom_id(targets, include_selector: true) Turbo::Streams::ActionBroadcastJob.perform_later \ stream_name_from(streamables), action: action, target: target, targets: targets, **rendering end diff --git a/test/streams/streams_channel_test.rb b/test/streams/streams_channel_test.rb index dea35c6f..5d473453 100644 --- a/test/streams/streams_channel_test.rb +++ b/test/streams/streams_channel_test.rb @@ -190,6 +190,35 @@ class Turbo::StreamsChannelTest < ActionCable::Channel::TestCase end end + test "broadcasting action later with ActiveModel array target" do + options = { partial: "messages/message", locals: { message: "hello!" } } + + message = Message.new(id: 42) + target = [message, "opt"] + expected_target = "opt_message_42" + + assert_broadcast_on "stream", turbo_stream_action_tag("prepend", target: expected_target, template: render(options)) do + perform_enqueued_jobs do + Turbo::StreamsChannel.broadcast_action_later_to \ + "stream", action: "prepend", target: target, **options + end + end + end + + test "broadcasting action later with multiple ActiveModel targets" do + options = { partial: "messages/message", locals: { message: "hello!" } } + + one = Message.new(id: 1) + targets = [one, "messages"] + expected_targets = "#messages_message_1" + + assert_broadcast_on "stream", turbo_stream_action_tag("prepend", targets: expected_targets, template: render(options)) do + perform_enqueued_jobs do + Turbo::StreamsChannel.broadcast_action_later_to \ + "stream", action: "prepend", targets: targets, **options + end + end + end test "broadcasting render now" do assert_broadcast_on "stream", turbo_stream_action_tag("replace", target: "message_1", template: "Goodbye!") do