Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unintentionally calling to_a on the target #502

Merged
merged 1 commit into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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
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
Loading