Skip to content

Commit d85f2f0

Browse files
afraseseanpdoyle
authored andcommitted
Add support for Model Class in Action and Frame helpers
Updated turbo_stream_action_tag and turbo_frame_tag helpers to accept a Model Class as their target.
1 parent a3336b4 commit d85f2f0

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

app/helpers/turbo/frames_helper.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@ module Turbo::FramesHelper
3333
# <%= turbo_frame_tag(Article.find(1)) %>
3434
# # => <turbo-frame id="article_1"></turbo-frame>
3535
#
36+
# <%= turbo_frame_tag(Article) %>
37+
# # => <turbo-frame id="new_article"></turbo-frame>
38+
#
3639
# <%= turbo_frame_tag(Article.find(1), "comments") %>
3740
# # => <turbo-frame id="comments_article_1"></turbo-frame>
3841
def turbo_frame_tag(*ids, src: nil, target: nil, **attributes, &block)
39-
id = ids.first.respond_to?(:to_key) ? ActionView::RecordIdentifier.dom_id(*ids) : ids.join('_')
42+
id = ids.first.respond_to?(:to_key) || ids.first.is_a?(Class) ? ActionView::RecordIdentifier.dom_id(*ids) : ids.join('_')
4043
src = url_for(src) if src.present?
4144

4245
tag.turbo_frame(**attributes.merge(id: id, src: src, target: target).compact, &block)

app/helpers/turbo/streams/action_helper.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ module Turbo::Streams::ActionHelper
1919
# turbo_stream_action_tag "remove", target: message
2020
# # => <turbo-stream action="remove" target="message_1"></turbo-stream>
2121
#
22+
# turbo_stream_action_tag "remove", target: Message
23+
# # => <turbo-stream action="remove" target="new_message"></turbo-stream>
24+
#
2225
# message = Message.find(1)
2326
# turbo_stream_action_tag "remove", target: [message, :special]
2427
# # => <turbo-stream action="remove" target="special_message_1"></turbo-stream>
@@ -45,7 +48,7 @@ def turbo_stream_refresh_tag(request_id: Turbo.current_request_id, **attributes)
4548
private
4649
def convert_to_turbo_stream_dom_id(target, include_selector: false)
4750
target_array = Array.wrap(target)
48-
if target_array.any? { |value| value.respond_to?(:to_key) }
51+
if target_array.any? { |value| value.respond_to?(:to_key) || value.is_a?(Class) }
4952
"#{"#" if include_selector}#{ActionView::RecordIdentifier.dom_id(*target_array)}"
5053
else
5154
target

test/frames/frames_helper_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ class Turbo::FramesHelperTest < ActionView::TestCase
2121
assert_dom_equal %(<turbo-frame id="message_1"></turbo-frame>), turbo_frame_tag(record)
2222
end
2323

24+
test "frame with model class argument" do
25+
assert_dom_equal %(<turbo-frame id="new_message"></turbo-frame>), turbo_frame_tag(Message)
26+
end
27+
2428
test "string frame within a model frame" do
2529
record = Article.new(id: 1)
2630

test/streams/action_helper_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ class Turbo::ActionHelperTest < ActionCable::Channel::TestCase
1111
assert_equal stream, turbo_stream_action_tag("append", target: message)
1212
end
1313

14+
test "target is a model class" do
15+
message = Message
16+
17+
stream = "<turbo-stream action=\"append\" target=\"new_message\"><template></template></turbo-stream>"
18+
19+
assert_equal stream, turbo_stream_action_tag("append", target: message)
20+
end
21+
1422
test "target passed as array of dom_id arguments" do
1523
message = Message.new(id: 1)
1624

0 commit comments

Comments
 (0)