Skip to content

Commit 09ed58f

Browse files
Fix capture block issue (#193)
Co-authored-by: Will Cosgrove <will@cosgrove.email>
1 parent f180318 commit 09ed58f

File tree

10 files changed

+77
-4
lines changed

10 files changed

+77
-4
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
55

66
gemspec
77

8-
gem "phlex", github: "phlex-ruby/phlex"
8+
gem "phlex", github: "phlex-ruby/phlex", branch: "1.10"
99
gem "rubocop"
1010
gem "solargraph"
1111
gem "view_component"

lib/phlex/rails/sgml.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ def render(*args, **kwargs, &block)
2929
when Enumerable
3030
return super unless renderable.is_a?(ActiveRecord::Relation)
3131
else
32-
captured_block = -> { capture(&block) } if block
33-
@_context.target << @_view_context.render(*args, **kwargs, &captured_block)
32+
if block
33+
@_context.target << @_view_context.render(*args, **kwargs) { capture(&block) }
34+
else
35+
@_context.target << @_view_context.render(*args, **kwargs)
36+
end
3437
end
3538

3639
nil
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
class RenderingController < ApplicationController
4+
def partial_from_phlex
5+
render Rendering::PartialFromPhlex.new
6+
end
7+
8+
def view_component_from_phlex
9+
render Rendering::ViewComponentFromPhlex.new
10+
end
11+
end

test/dummy/app/views/posts/index_view.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
class Posts::IndexView < ApplicationView
44
def view_template
55
h1 { "Posts::Index" }
6-
p { "Find me in app/views/posts/index_view.rb" }
6+
aside { "Find me in app/views/posts/index_view.rb" }
77
end
88
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div id="erb">
2+
<%= yield %>
3+
</div>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
module Rendering
4+
class PartialFromPhlex < ApplicationView
5+
def view_template
6+
render "partial" do
7+
h1(id: "phlex") { "Partial from Phlex" }
8+
end
9+
end
10+
end
11+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
module Rendering
4+
class VcComponent < ViewComponent::Base
5+
renders_one :slot
6+
7+
def call
8+
if slot?
9+
slot
10+
end
11+
end
12+
end
13+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
module Rendering
4+
class ViewComponentFromPhlex < ApplicationView
5+
def view_template
6+
render VcComponent.new do |component|
7+
component.slot do
8+
h1 { "Rendered from Phlex" }
9+
end
10+
end
11+
end
12+
end
13+
end

test/dummy/config/routes.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@
1616
get "/helpers/form_with", to: "helpers#form_with"
1717
get "/helpers/tag", to: "helpers#tag"
1818
get "/helpers/missing_helper", to: "helpers#missing_helper"
19+
20+
get "/rendering/partial_from_phlex", to: "rendering#partial_from_phlex"
21+
get "/rendering/view_component_from_phlex", to: "rendering#view_component_from_phlex"
1922
end

test/phlex/render_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
class RenderTest < ActionDispatch::IntegrationTest
6+
test "rendering partial from Phlex view" do
7+
get "/rendering/partial_from_phlex"
8+
assert_response :success
9+
assert_select "#erb>h1#phlex", "Partial from Phlex"
10+
end
11+
12+
test "rendering view_component component from Phlex view" do
13+
get "/rendering/view_component_from_phlex"
14+
assert_response :success
15+
end
16+
end

0 commit comments

Comments
 (0)