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

Raise exception when no id is passed to the turbo_frame_tag helper #551

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
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
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