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

turbo_stream tag builder: support :partial with block #701

Open
wants to merge 2 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/models/turbo/streams/tag_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ def render_template(target, content = nil, allow_inferred_rendering: true, **ren
content.render_in(@view_context, &block)
when content
allow_inferred_rendering ? (render_record(content) || content) : content
when block_given? && (rendering.key?(:partial) || rendering.key?(:layout))
@view_context.render(formats: [ :html ], layout: rendering[:partial], **rendering, &block)
when block_given?
@view_context.capture(&block)
when rendering.any?
Expand Down
2 changes: 1 addition & 1 deletion bug_report_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
gem "rails"
gem "propshaft"
gem "puma"
gem "sqlite3", "~> 1.4"
gem "sqlite3"
gem "turbo-rails"

gem "capybara"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p><%= yield %></p>
42 changes: 42 additions & 0 deletions test/streams/streams_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,48 @@ def to_key

attr_accessor :formats

test "turbo_stream builder captures block when called without :partial keyword" do
rendered = turbo_stream.update "target_id" do
tag.span "Hello, world"
end

assert_dom_equal <<~HTML.strip, rendered
<turbo-stream action="update" target="target_id">
<template>
<span>Hello, world</span>
</template>
</turbo-stream>
HTML
end

test "turbo_stream builder forwards block to partial when called with :partial keyword" do
rendered = turbo_stream.update "target_id", partial: "application/partial_with_block" do
"Hello, from application/partial_with_block partial"
end

assert_dom_equal <<~HTML.strip, rendered
<turbo-stream action="update" target="target_id">
<template>
<p>Hello, from application/partial_with_block partial</p>
</template>
</turbo-stream>
HTML
end

test "turbo_stream builder forwards block to partial when called with :layout keyword" do
rendered = turbo_stream.update "target_id", layout: "application/partial_with_block" do
"Hello, from application/partial_with_block partial"
end

assert_dom_equal <<~HTML.strip, rendered
<turbo-stream action="update" target="target_id">
<template>
<p>Hello, from application/partial_with_block partial</p>
</template>
</turbo-stream>
HTML
end

test "supports valid :renderable option object with nil content" do
component = Component.new(id: 1, content: "Hello, world")

Expand Down
Loading