Skip to content

Commit 0833e18

Browse files
committed
Add turbo_stream.refresh builder method
Closes [#579][] Extends the `turbo_stream` tag builder helper to create `<turbo-stream action="refresh">` elements through the pre-existing `turbo_stream_refresh_tag` method. [#579]: #579
1 parent 9b17a3b commit 0833e18

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

app/models/turbo/streams/tag_builder.rb

+12
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,18 @@ def morph_all(targets, content = nil, **rendering, &block)
254254
action_all :morph, targets, content, **rendering, &block
255255
end
256256

257+
# Creates a `turbo-stream` tag with an `[action="refresh"`] attribute and a
258+
# `[request-id]` attribute that defaults to `Turbo.current_request_id`:
259+
#
260+
# turbo_stream.refresh
261+
# # => <turbo-stream action="refresh" request-id="ef083d55-7516-41b1-ad28-16f553399c6a"></turbo-stream>
262+
#
263+
# turbo_stream.refresh request_id: "abc123"
264+
# # => <turbo-stream action="refresh" request-id="abc123"></turbo-stream>
265+
def refresh(...)
266+
turbo_stream_refresh_tag(...)
267+
end
268+
257269
# Send an action of the type <tt>name</tt> to <tt>target</tt>. Options described in the concrete methods.
258270
def action(name, target, content = nil, allow_inferred_rendering: true, **rendering, &block)
259271
template = render_template(target, content, allow_inferred_rendering: allow_inferred_rendering, **rendering, &block)

test/streams/streams_helper_test.rb

+12
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ class Turbo::StreamsHelperTest < ActionView::TestCase
3535
turbo_stream_from("messages", channel: "NonExistentChannel", data: {payload: 1})
3636
end
3737

38+
test "turbo_stream.refresh" do
39+
assert_dom_equal <<~HTML, turbo_stream.refresh
40+
<turbo-stream action="refresh"></turbo-stream>
41+
HTML
42+
assert_dom_equal <<~HTML, Turbo.with_request_id("abc123") { turbo_stream.refresh }
43+
<turbo-stream request-id="abc123" action="refresh"></turbo-stream>
44+
HTML
45+
assert_dom_equal <<~HTML, turbo_stream.refresh(request_id: "def456")
46+
<turbo-stream request-id="def456" action="refresh"></turbo-stream>
47+
HTML
48+
end
49+
3850
test "custom turbo_stream builder actions" do
3951
assert_dom_equal <<~HTML.strip, turbo_stream.highlight("an-id")
4052
<turbo-stream action="highlight" target="an-id"><template></template></turbo-stream>

0 commit comments

Comments
 (0)