Skip to content

Commit be5c785

Browse files
authored
Merge pull request #485 from dirkjonker/main
Convert DOM IDs before submitting ActionBroadcastJob
2 parents 9232499 + 1cb3d06 commit be5c785

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

app/channels/turbo/streams/broadcasts.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ def broadcast_action_later_to(*streamables, action:, target: nil, targets: nil,
7979
streamables.compact_blank!
8080

8181
if streamables.present?
82+
target = convert_to_turbo_stream_dom_id(target)
83+
targets = convert_to_turbo_stream_dom_id(targets, include_selector: true)
8284
Turbo::Streams::ActionBroadcastJob.perform_later \
8385
stream_name_from(streamables), action: action, target: target, targets: targets, attributes: attributes, **rendering
8486
end

test/streams/streams_channel_test.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,36 @@ class Turbo::StreamsChannelTest < ActionCable::Channel::TestCase
236236
end
237237
end
238238

239+
test "broadcasting action later with ActiveModel array target" do
240+
options = { partial: "messages/message", locals: { message: "hello!" } }
241+
242+
message = Message.new(id: 42)
243+
target = [message, "opt"]
244+
expected_target = "opt_message_42"
245+
246+
assert_broadcast_on "stream", turbo_stream_action_tag("prepend", target: expected_target, template: render(options)) do
247+
perform_enqueued_jobs do
248+
Turbo::StreamsChannel.broadcast_action_later_to \
249+
"stream", action: "prepend", target: target, **options
250+
end
251+
end
252+
end
253+
254+
test "broadcasting action later with multiple ActiveModel targets" do
255+
options = { partial: "messages/message", locals: { message: "hello!" } }
256+
257+
one = Message.new(id: 1)
258+
targets = [one, "messages"]
259+
expected_targets = "#messages_message_1"
260+
261+
assert_broadcast_on "stream", turbo_stream_action_tag("prepend", targets: expected_targets, template: render(options)) do
262+
perform_enqueued_jobs do
263+
Turbo::StreamsChannel.broadcast_action_later_to \
264+
"stream", action: "prepend", targets: targets, **options
265+
end
266+
end
267+
end
268+
239269
test "broadcasting render now" do
240270
assert_broadcast_on "stream", turbo_stream_action_tag("replace", target: "message_1", template: "Goodbye!") do
241271
Turbo::StreamsChannel.broadcast_render_to "stream", partial: "messages/message"

0 commit comments

Comments
 (0)