module Turbo::FramesHelper
# Returns a frame tag that can either be used simply to encapsulate frame content or as a lazy-loading container that starts empty but
# fetches the URL supplied in the +src+ attribute.
#
# === Examples
#
# <%= turbo_frame_tag "tray", src: tray_path(tray) %>
# # =>
#
# <%= turbo_frame_tag tray, src: tray_path(tray) %>
# # =>
#
# <%= turbo_frame_tag "tray", src: tray_path(tray), target: "_top" %>
# # =>
#
# <%= turbo_frame_tag "tray", target: "other_tray" %>
# # =>
#
# <%= turbo_frame_tag "tray", src: tray_path(tray), loading: "lazy" %>
# # =>
#
# <%= turbo_frame_tag "tray" do %>
#
My tray frame!
# <% end %>
# # => My tray frame!
#
# <%= turbo_frame_tag [user_id, "tray"], src: tray_path(tray) %>
# # =>
#
# 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:
#
# <%= turbo_frame_tag(Article.find(1)) %>
# # =>
#
# <%= turbo_frame_tag(Article.find(1), "comments") %>
# # =>
#
# <%= turbo_frame_tag(Article.find(1), Comment.new) %>
# # =>
def turbo_frame_tag(*ids, src: nil, target: nil, **attributes, &block)
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)
end
end