Skip to content

Commit

Permalink
Merge pull request #199 from phlex-ruby/fix-view-component-yield-issue
Browse files Browse the repository at this point in the history
Fix issue with ViewComponent yields
  • Loading branch information
joeldrapper authored Aug 12, 2024
2 parents f157fbf + db0fd20 commit d941453
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 11 deletions.
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

0 comments on commit d941453

Please sign in to comment.