Skip to content

Commit cd16a6d

Browse files
authored
Merge pull request #246 from phlex-ruby/partial-proxy
Use renderable partial proxy for rendering partials
2 parents 6088796 + d996915 commit cd16a6d

File tree

6 files changed

+36
-22
lines changed

6 files changed

+36
-22
lines changed

lib/phlex/rails.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ module Rails
1212
autoload :BufferedRadioButtonBuilder, "phlex/rails/buffered_radio_button_builder"
1313
autoload :CSV, "phlex/rails/csv"
1414
autoload :FragmentFinder, "phlex/rails/fragment_finder"
15+
autoload :HTML, "phlex/rails/html"
1516
autoload :HelperFinder, "phlex/rails/helper_finder"
1617
autoload :HelperMacros, "phlex/rails/helper_macros"
1718
autoload :Helpers, "phlex/rails/helpers"
18-
autoload :HTML, "phlex/rails/html"
1919
autoload :Layout, "phlex/rails/layout"
20+
autoload :Partial, "phlex/rails/partial"
2021
autoload :SGML, "phlex/rails/sgml"
2122
autoload :Streaming, "phlex/rails/streaming"
2223
autoload :Unbuffered, "phlex/rails/unbuffered"

lib/phlex/rails/partial.rb

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
class Phlex::Rails::Partial
4+
def initialize(path, *args, **kwargs, &block)
5+
@path = path
6+
@args = args
7+
@kwargs = kwargs
8+
@block = block
9+
end
10+
11+
def render_in(view_context, &block)
12+
block ||= @block
13+
view_context.render(@path, *@args, **@kwargs, &block)
14+
end
15+
end

lib/phlex/rails/sgml.rb

+16-18
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ def render_in(...)
1212
module Overrides
1313
class HelpersCalledBeforeRenderError < StandardError; end
1414

15+
def partial(*, **, &block)
16+
if block
17+
Phlex::Rails::Partial.new(*, **) { capture(&block) }
18+
else
19+
Phlex::Rails::Partial.new(*, **)
20+
end
21+
end
22+
1523
def helpers
1624
unless @_context && (view_context = @_context.view_context)
1725
raise HelpersCalledBeforeRenderError.new("Do not use rails helpers until after the view has been rendered.") unless view_context
@@ -28,26 +36,16 @@ def render(*args, **kwargs, &block)
2836
renderable = args[0]
2937

3038
case renderable
31-
when Phlex::SGML, Proc, Method, String
32-
return super
33-
when Class
34-
return super if renderable < Phlex::SGML
35-
when Enumerable
36-
return super unless ActiveRecord::Relation === renderable
37-
when nil
38-
partial = kwargs.delete(:partial)
39-
40-
if partial # this is a hack to get around https://github.com/rails/rails/issues/51015
41-
return raw(
42-
@_context.view_context.render(partial, **kwargs) do |*yielded_args|
43-
capture(*yielded_args, &block)
44-
end
45-
)
46-
else
47-
return super
48-
end
39+
when Phlex::SGML, Proc, Method, String
40+
return super
41+
when Class
42+
return super if renderable < Phlex::SGML
43+
when Enumerable
44+
return super unless ActiveRecord::Relation === renderable
4945
end
5046

47+
return super if args.length == 0 && kwargs.length == 0
48+
5149
output = if block
5250
@_context.view_context.render(*args, **kwargs) do |*yielded_args|
5351
if yielded_args.length == 1 && defined?(ViewComponent::Base) && ViewComponent::Base === yielded_args[0]

test/dummy/app/views/layouts/application_layout.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def view_template(&block)
2121

2222
body(class: "bg-blue") do
2323
main(&block)
24-
render partial: "rendering/partial"
24+
render partial("rendering/partial")
2525
end
2626
end
2727
end

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module Rendering
44
class PartialFromPhlex < ApplicationView
55
def view_template
6-
render partial: "partial" do
6+
render partial("partial") do
77
h1(id: "phlex") { "Partial from Phlex" }
88
end
99
end

test/phlex/test_helpers_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class TestHelpersTest < ActiveSupport::TestCase
77

88
class PartialFromPhlex < ApplicationView
99
def view_template
10-
render partial: "rendering/partial" do
10+
render partial("rendering/partial") do
1111
h1(id: "phlex") { "Partial from Phlex" }
1212
end
1313
end

0 commit comments

Comments
 (0)