Skip to content

Commit

Permalink
Fix capture block issue (#193)
Browse files Browse the repository at this point in the history
Co-authored-by: Will Cosgrove <[email protected]>
  • Loading branch information
joeldrapper and willcosgrove authored Apr 5, 2024
1 parent f180318 commit 09ed58f
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }

gemspec

gem "phlex", github: "phlex-ruby/phlex"
gem "phlex", github: "phlex-ruby/phlex", branch: "1.10"
gem "rubocop"
gem "solargraph"
gem "view_component"
Expand Down
7 changes: 5 additions & 2 deletions lib/phlex/rails/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ def render(*args, **kwargs, &block)
when Enumerable
return super unless renderable.is_a?(ActiveRecord::Relation)
else
captured_block = -> { capture(&block) } if block
@_context.target << @_view_context.render(*args, **kwargs, &captured_block)
if block
@_context.target << @_view_context.render(*args, **kwargs) { capture(&block) }
else
@_context.target << @_view_context.render(*args, **kwargs)
end
end

nil
Expand Down
11 changes: 11 additions & 0 deletions test/dummy/app/controllers/rendering_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class RenderingController < ApplicationController
def partial_from_phlex
render Rendering::PartialFromPhlex.new
end

def view_component_from_phlex
render Rendering::ViewComponentFromPhlex.new
end
end
2 changes: 1 addition & 1 deletion test/dummy/app/views/posts/index_view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
class Posts::IndexView < ApplicationView
def view_template
h1 { "Posts::Index" }
p { "Find me in app/views/posts/index_view.rb" }
aside { "Find me in app/views/posts/index_view.rb" }
end
end
3 changes: 3 additions & 0 deletions test/dummy/app/views/rendering/_partial.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div id="erb">
<%= yield %>
</div>
11 changes: 11 additions & 0 deletions test/dummy/app/views/rendering/partial_from_phlex.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module Rendering
class PartialFromPhlex < ApplicationView
def view_template
render "partial" do
h1(id: "phlex") { "Partial from Phlex" }
end
end
end
end
13 changes: 13 additions & 0 deletions test/dummy/app/views/rendering/vc_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Rendering
class VcComponent < ViewComponent::Base
renders_one :slot

def call
if slot?
slot
end
end
end
end
13 changes: 13 additions & 0 deletions test/dummy/app/views/rendering/view_component_from_phlex.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Rendering
class ViewComponentFromPhlex < ApplicationView
def view_template
render VcComponent.new do |component|
component.slot do
h1 { "Rendered from Phlex" }
end
end
end
end
end
3 changes: 3 additions & 0 deletions test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@
get "/helpers/form_with", to: "helpers#form_with"
get "/helpers/tag", to: "helpers#tag"
get "/helpers/missing_helper", to: "helpers#missing_helper"

get "/rendering/partial_from_phlex", to: "rendering#partial_from_phlex"
get "/rendering/view_component_from_phlex", to: "rendering#view_component_from_phlex"
end
16 changes: 16 additions & 0 deletions test/phlex/render_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require "test_helper"

class RenderTest < ActionDispatch::IntegrationTest
test "rendering partial from Phlex view" do
get "/rendering/partial_from_phlex"
assert_response :success
assert_select "#erb>h1#phlex", "Partial from Phlex"
end

test "rendering view_component component from Phlex view" do
get "/rendering/view_component_from_phlex"
assert_response :success
end
end

0 comments on commit 09ed58f

Please sign in to comment.