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

Support for multiple string arguments to frame helper #504

Merged
merged 1 commit into from
Oct 17, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/helpers/turbo/frames_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ module Turbo::FramesHelper
# <% 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,7 @@ 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I've pulled your example in.

id = ids.first.respond_to?(:to_key) ? ActionView::RecordIdentifier.dom_id(*ids) : ids.first
id = ids.first.respond_to?(:to_key) ? ActionView::RecordIdentifier.dom_id(*ids) : ids.join('_')
src = url_for(src) if src.present?

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

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

assert_dom_equal %(<turbo-frame id="comments_article_1"></turbo-frame>), turbo_frame_tag(record, "comments")
end

test "string frame with non-record array" do
assert_dom_equal %(<turbo-frame id="foo_1_2"></turbo-frame>), turbo_frame_tag(['foo', 1, 2])
end

test "block style" do
assert_dom_equal(%(<turbo-frame id="tray"><p>tray!</p></turbo-frame>), turbo_frame_tag("tray") { tag.p("tray!") })
end
Expand Down