Skip to content

Commit d941453

Browse files
authored
Merge pull request #199 from phlex-ruby/fix-view-component-yield-issue
Fix issue with ViewComponent yields
2 parents f157fbf + db0fd20 commit d941453

File tree

6 files changed

+18
-11
lines changed

6 files changed

+18
-11
lines changed

lib/phlex/rails/buffered.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def respond_to_missing?(...)
5050

5151
def method_missing(*args, **kwargs, &block)
5252
output = if block
53-
@object.public_send(*args, **kwargs) { @view.capture(&block) }
53+
@object.public_send(*args, **kwargs) { |*a| @view.capture(*a, &block) }
5454
else
5555
@object.public_send(*args, **kwargs)
5656
end

lib/phlex/rails/sgml.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ def render(*args, **kwargs, &block)
3030
return super unless renderable.is_a?(ActiveRecord::Relation)
3131
else
3232
if block
33-
@_context.target << @_view_context.render(*args, **kwargs) { capture(&block) }
33+
@_context.target << @_view_context.render(*args, **kwargs) do |*yielded_args|
34+
if yielded_args.length == 1 && defined?(ViewComponent::Base) && ViewComponent::Base === yielded_args[0]
35+
capture(Phlex::Rails::Buffered.new(yielded_args[0], view: self), &block)
36+
else
37+
capture(*yielded_args, &block)
38+
end
39+
end
3440
else
3541
@_context.target << @_view_context.render(*args, **kwargs)
3642
end
@@ -70,7 +76,7 @@ def render_in(view_context, &block)
7076
end
7177
end
7278

73-
def capture
79+
def capture(...)
7480
super&.html_safe
7581
end
7682

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
<%= slot %>

test/dummy/app/views/rendering/vc_component.rb

-6
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,5 @@
33
module Rendering
44
class VcComponent < ViewComponent::Base
55
renders_one :slot
6-
7-
def call
8-
if slot?
9-
slot
10-
end
11-
end
126
end
137
end

test/dummy/app/views/rendering/view_component_from_phlex.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
module Rendering
44
class ViewComponentFromPhlex < ApplicationView
55
def view_template
6+
h1 { "Before" }
7+
68
render VcComponent.new do |component|
7-
component.slot do
8-
h1 { "Rendered from Phlex" }
9+
component.with_slot do
10+
h1(id: "phlex") { "Rendered from Phlex" }
911
end
1012
end
13+
14+
h1 { "After" }
1115
end
1216
end
1317
end

test/phlex/render_test.rb

+1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ class RenderTest < ActionDispatch::IntegrationTest
1212
test "rendering view_component component from Phlex view" do
1313
get "/rendering/view_component_from_phlex"
1414
assert_response :success
15+
assert_select "#phlex", "Rendered from Phlex"
1516
end
1617
end

0 commit comments

Comments
 (0)