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

Fix issue with ViewComponent yields #199

Merged
merged 2 commits into from
Aug 12, 2024
Merged
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: 1 addition & 1 deletion lib/phlex/rails/buffered.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def respond_to_missing?(...)

def method_missing(*args, **kwargs, &block)
output = if block
@object.public_send(*args, **kwargs) { @view.capture(&block) }
@object.public_send(*args, **kwargs) { |*a| @view.capture(*a, &block) }
else
@object.public_send(*args, **kwargs)
end
Expand Down
10 changes: 8 additions & 2 deletions lib/phlex/rails/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ def render(*args, **kwargs, &block)
return super unless renderable.is_a?(ActiveRecord::Relation)
else
if block
@_context.target << @_view_context.render(*args, **kwargs) { capture(&block) }
@_context.target << @_view_context.render(*args, **kwargs) do |*yielded_args|
if yielded_args.length == 1 && defined?(ViewComponent::Base) && ViewComponent::Base === yielded_args[0]
capture(Phlex::Rails::Buffered.new(yielded_args[0], view: self), &block)
else
capture(*yielded_args, &block)
end
end
else
@_context.target << @_view_context.render(*args, **kwargs)
end
Expand Down Expand Up @@ -70,7 +76,7 @@ def render_in(view_context, &block)
end
end

def capture
def capture(...)
super&.html_safe
end

Expand Down
2 changes: 2 additions & 0 deletions test/dummy/app/views/rendering/vc_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

<%= slot %>
6 changes: 0 additions & 6 deletions test/dummy/app/views/rendering/vc_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,5 @@
module Rendering
class VcComponent < ViewComponent::Base
renders_one :slot

def call
if slot?
slot
end
end
end
end
8 changes: 6 additions & 2 deletions test/dummy/app/views/rendering/view_component_from_phlex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
module Rendering
class ViewComponentFromPhlex < ApplicationView
def view_template
h1 { "Before" }

render VcComponent.new do |component|
component.slot do
h1 { "Rendered from Phlex" }
component.with_slot do
h1(id: "phlex") { "Rendered from Phlex" }
end
end

h1 { "After" }
end
end
end
1 change: 1 addition & 0 deletions test/phlex/render_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ class RenderTest < ActionDispatch::IntegrationTest
test "rendering view_component component from Phlex view" do
get "/rendering/view_component_from_phlex"
assert_response :success
assert_select "#phlex", "Rendered from Phlex"
end
end
Loading