Skip to content

Commit ee5e1f5

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 01ff576 commit ee5e1f5

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
@@ -228,6 +228,18 @@ def prepend_all(targets, content = nil, **rendering, &block)
228228
action_all :prepend, targets, content, **rendering, &block
229229
end
230230

231+
# Creates a `turbo-stream` tag with an `[action="refresh"`] attribute and a
232+
# `[request-id]` attribute that defaults to `Turbo.current_request_id`:
233+
#
234+
# turbo_stream.refresh
235+
# # => <turbo-stream action="refresh" request-id="ef083d55-7516-41b1-ad28-16f553399c6a"></turbo-stream>
236+
#
237+
# turbo_stream.refresh request_id: "abc123"
238+
# # => <turbo-stream action="refresh" request-id="abc123"></turbo-stream>
239+
def refresh(...)
240+
turbo_stream_refresh_tag(...)
241+
end
242+
231243
# Send an action of the type <tt>name</tt> to <tt>target</tt>. Options described in the concrete methods.
232244
def action(name, target, content = nil, allow_inferred_rendering: true, **rendering, &block)
233245
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)