Skip to content

Commit 642d121

Browse files
committed
WIP
1 parent b9d9d67 commit 642d121

File tree

6 files changed

+35
-11
lines changed

6 files changed

+35
-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

+25-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@
22

33
module Phlex
44
module Rails
5+
class ViewComponentDecorator
6+
def initialize(view_component, context:)
7+
@view_component = view_component
8+
@context = context
9+
end
10+
11+
def method_missing(*args, **kwargs, &block)
12+
if block
13+
@view_component.public_send(*args, **kwargs) do |*a|
14+
@context.capture(*a, &block)
15+
end
16+
else
17+
@view_component.public_send(*args, **kwargs)
18+
end
19+
end
20+
end
21+
522
module SGML
623
module ClassMethods
724
def render_in(...)
@@ -30,7 +47,13 @@ def render(*args, **kwargs, &block)
3047
return super unless renderable.is_a?(ActiveRecord::Relation)
3148
else
3249
if block
33-
@_context.target << @_view_context.render(*args, **kwargs) { capture(&block) }
50+
@_context.target << @_view_context.render(*args, **kwargs) do |*yielded_args|
51+
if yielded_args.length == 1 && defined?(ViewComponent::Base) && ViewComponent::Base === yielded_args[0]
52+
capture(Phlex::Rails::ViewComponentDecorator.new(yielded_args[0], context: self), &block)
53+
else
54+
capture(*yielded_args, &block)
55+
end
56+
end
3457
else
3558
@_context.target << @_view_context.render(*args, **kwargs)
3659
end
@@ -70,7 +93,7 @@ def render_in(view_context, &block)
7093
end
7194
end
7295

73-
def capture
96+
def capture(...)
7497
super&.html_safe
7598
end
7699

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)