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

Add turbo_stream.refresh builder method #595

Open
wants to merge 1 commit 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
12 changes: 12 additions & 0 deletions app/models/turbo/streams/tag_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,18 @@ def morph_all(targets, content = nil, **rendering, &block)
action_all :morph, targets, content, **rendering, &block
end

# Creates a `turbo-stream` tag with an `[action="refresh"`] attribute and a
# `[request-id]` attribute that defaults to `Turbo.current_request_id`:
#
# turbo_stream.refresh
# # => <turbo-stream action="refresh" request-id="ef083d55-7516-41b1-ad28-16f553399c6a"></turbo-stream>
#
# turbo_stream.refresh request_id: "abc123"
# # => <turbo-stream action="refresh" request-id="abc123"></turbo-stream>
def refresh(...)
turbo_stream_refresh_tag(...)
end

# Send an action of the type <tt>name</tt> to <tt>target</tt>. Options described in the concrete methods.
def action(name, target, content = nil, allow_inferred_rendering: true, **rendering, &block)
template = render_template(target, content, allow_inferred_rendering: allow_inferred_rendering, **rendering, &block)
Expand Down
12 changes: 12 additions & 0 deletions test/streams/streams_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ class Turbo::StreamsHelperTest < ActionView::TestCase
turbo_stream_from("messages", channel: "NonExistentChannel", data: {payload: 1})
end

test "turbo_stream.refresh" do
assert_dom_equal <<~HTML, turbo_stream.refresh
<turbo-stream action="refresh"></turbo-stream>
HTML
assert_dom_equal <<~HTML, Turbo.with_request_id("abc123") { turbo_stream.refresh }
<turbo-stream request-id="abc123" action="refresh"></turbo-stream>
HTML
assert_dom_equal <<~HTML, turbo_stream.refresh(request_id: "def456")
<turbo-stream request-id="def456" action="refresh"></turbo-stream>
HTML
end

test "custom turbo_stream builder actions" do
assert_dom_equal <<~HTML.strip, turbo_stream.highlight("an-id")
<turbo-stream action="highlight" target="an-id"><template></template></turbo-stream>
Expand Down