Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion app/helpers/turbo/frames_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ module Turbo::FramesHelper
#
# <%= turbo_frame_tag(Article.find(1), "comments") %>
# # => <turbo-frame id="comments_article_1"></turbo-frame>
#
# Raises an +ArgumentError+ if called without a frame id.
# <%= turbo_frame_tag(nil) %> # => ArgumentError: You must supply a frame id
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('_')
raise ArgumentError, "You must supply a frame id" if ids.all?(&:blank?)

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
20 changes: 20 additions & 0 deletions test/frames/frames_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ class Turbo::FramesHelperTest < ActionView::TestCase
assert_dom_equal %(<turbo-frame id="message_1"></turbo-frame>), turbo_frame_tag(record)
end

test "frame with invalid argument should raise ArgumentError" do
Copy link
Contributor

Choose a reason for hiding this comment

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

The record variables could be ignored here and pass the argument directly. I don't see much value in declaring them

assert_raises ArgumentError, "ArgumentError: You must supply a frame id" do
record = nil

turbo_frame_tag(record)
end

assert_raises ArgumentError, "ArgumentError: You must supply a frame id" do
record = []

turbo_frame_tag(record)
end

assert_raises ArgumentError, "ArgumentError: You must supply a frame id" do
record = ''

turbo_frame_tag(record)
end
end

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

Expand Down