Skip to content

Commit

Permalink
Merge pull request #502 from dorner/fix-array-to-a-bug
Browse files Browse the repository at this point in the history
Fix unintentionally calling `to_a` on the target
  • Loading branch information
brunoprietog authored Jul 13, 2024
2 parents be5c785 + 38113e6 commit 669ba6d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
5 changes: 3 additions & 2 deletions app/helpers/turbo/streams/action_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ def turbo_stream_refresh_tag(request_id: Turbo.current_request_id, **attributes)

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
Expand Down
10 changes: 10 additions & 0 deletions test/streams/action_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<turbo-stream action=\"append\" target=\"new_custom_to_a_class\"><template></template></turbo-stream>"
assert_equal stream, turbo_stream_action_tag("append", target: klass.new)
end

test "no template" do
stream = "<turbo-stream action=\"append\" target=\"message_1\"><template></template></turbo-stream>"

Expand Down

0 comments on commit 669ba6d

Please sign in to comment.