Skip to content

Commit

Permalink
Restore turbo_frame_tag support for Array arguments
Browse files Browse the repository at this point in the history
Closes [hotwired#503][]

When the positional arguments to `turbo_frame_tag` respond are
compatible with `dom_id` (they respond to `#to_key`), pass them to
`dom_id`, otherwise, check them individually.

This approach melds the behavior introduced in [hotwired#476][] with the
original support.

[hotwired#503]: hotwired#503
[hotwired#476]: hotwired#476
  • Loading branch information
seanpdoyle committed Oct 16, 2023
1 parent 097d8f9 commit aefdf47
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
17 changes: 16 additions & 1 deletion app/helpers/turbo/frames_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ module Turbo::FramesHelper
# <div>My tray frame!</div>
# <% end %>
# # => <turbo-frame id="tray"><div>My tray frame!</div></turbo-frame>

# <%= turbo_frame_tag [user_id, "tray"], src: tray_path(tray) %>
# # => <turbo-frame id="1_tray" src="http://example.com/trays/1"></turbo-frame>
#
# The `turbo_frame_tag` helper will convert the arguments it receives to their
# `dom_id` if applicable to easily generate unique ids for Turbo Frames:
Expand All @@ -36,7 +39,19 @@ module Turbo::FramesHelper
# <%= turbo_frame_tag(Article.find(1), Comment.new) %>
# # => <turbo-frame id="article_1_new_comment"></turbo-frame>
def turbo_frame_tag(*ids, src: nil, target: nil, **attributes, &block)
id = ids.first.respond_to?(:to_key) ? ActionView::RecordIdentifier.dom_id(*ids) : ids.first
id =
if ids.first.respond_to?(:to_key)
ActionView::RecordIdentifier.dom_id(*ids)
else
ids.map do |id|
if id.respond_to?(:to_key)
ActionView::RecordIdentifier.dom_id(*id)
else
id
end
end.join("_")
end

src = url_for(src) if src.present?

tag.turbo_frame(**attributes.merge(id: id, src: src, target: target).compact, &block)
Expand Down
6 changes: 6 additions & 0 deletions test/frames/frames_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ class Turbo::FramesHelperTest < ActionView::TestCase
assert_dom_equal %(<turbo-frame id="message_1"></turbo-frame>), turbo_frame_tag(record)
end

test "frame with Array argument" do
target = [1, 2, "string"]

assert_dom_equal %(<turbo-frame id="1_2_string"></turbo-frame>), turbo_frame_tag(target)
end

test "string frame nested withing a model frame" do
record = Article.new(id: 1)

Expand Down

0 comments on commit aefdf47

Please sign in to comment.