From 38113e6024df8875f25cd7378a77ffea4bbc75dc Mon Sep 17 00:00:00 2001 From: Daniel Orner Date: Fri, 13 Oct 2023 16:17:22 -0400 Subject: [PATCH] Fix unintentionally calling `to_a` on the target --- app/helpers/turbo/streams/action_helper.rb | 5 +++-- test/streams/action_helper_test.rb | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/app/helpers/turbo/streams/action_helper.rb b/app/helpers/turbo/streams/action_helper.rb index 37e6e545..a02d69fe 100644 --- a/app/helpers/turbo/streams/action_helper.rb +++ b/app/helpers/turbo/streams/action_helper.rb @@ -37,8 +37,9 @@ def turbo_stream_action_tag(action, target: nil, targets: nil, template: nil, ** private def convert_to_turbo_stream_dom_id(target, include_selector: false) - if Array(target).any? { |value| value.respond_to?(:to_key) } - "#{"#" if include_selector}#{ActionView::RecordIdentifier.dom_id(*target)}" + target_array = Array.wrap(target) + if target_array.any? { |value| value.respond_to?(:to_key) } + "#{"#" if include_selector}#{ActionView::RecordIdentifier.dom_id(*target_array)}" else target end diff --git a/test/streams/action_helper_test.rb b/test/streams/action_helper_test.rb index d1818cd0..a91ba9b7 100644 --- a/test/streams/action_helper_test.rb +++ b/test/streams/action_helper_test.rb @@ -19,6 +19,16 @@ class Turbo::ActionHelperTest < ActionCable::Channel::TestCase assert_equal stream, turbo_stream_action_tag("append", target: [message, :special]) end + test "target uses custom to_a" do + klass = Class.new(Message) do + def to_a; raise "DO NOT CALL ME"; end + def self.name; "CustomToAClass"; end + end + + stream = "" + assert_equal stream, turbo_stream_action_tag("append", target: klass.new) + end + test "no template" do stream = ""