Skip to content

Commit

Permalink
Merge pull request #246 from phlex-ruby/partial-proxy
Browse files Browse the repository at this point in the history
Use renderable partial proxy for rendering partials
  • Loading branch information
joeldrapper authored Nov 26, 2024
2 parents 6088796 + d996915 commit cd16a6d
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 22 deletions.
3 changes: 2 additions & 1 deletion lib/phlex/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ module Rails
autoload :BufferedRadioButtonBuilder, "phlex/rails/buffered_radio_button_builder"
autoload :CSV, "phlex/rails/csv"
autoload :FragmentFinder, "phlex/rails/fragment_finder"
autoload :HTML, "phlex/rails/html"
autoload :HelperFinder, "phlex/rails/helper_finder"
autoload :HelperMacros, "phlex/rails/helper_macros"
autoload :Helpers, "phlex/rails/helpers"
autoload :HTML, "phlex/rails/html"
autoload :Layout, "phlex/rails/layout"
autoload :Partial, "phlex/rails/partial"
autoload :SGML, "phlex/rails/sgml"
autoload :Streaming, "phlex/rails/streaming"
autoload :Unbuffered, "phlex/rails/unbuffered"
Expand Down
15 changes: 15 additions & 0 deletions lib/phlex/rails/partial.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

class Phlex::Rails::Partial
def initialize(path, *args, **kwargs, &block)
@path = path
@args = args
@kwargs = kwargs
@block = block
end

def render_in(view_context, &block)
block ||= @block
view_context.render(@path, *@args, **@kwargs, &block)
end
end
34 changes: 16 additions & 18 deletions lib/phlex/rails/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ def render_in(...)
module Overrides
class HelpersCalledBeforeRenderError < StandardError; end

def partial(*, **, &block)
if block
Phlex::Rails::Partial.new(*, **) { capture(&block) }
else
Phlex::Rails::Partial.new(*, **)
end
end

def helpers
unless @_context && (view_context = @_context.view_context)
raise HelpersCalledBeforeRenderError.new("Do not use rails helpers until after the view has been rendered.") unless view_context
Expand All @@ -28,26 +36,16 @@ def render(*args, **kwargs, &block)
renderable = args[0]

case renderable
when Phlex::SGML, Proc, Method, String
return super
when Class
return super if renderable < Phlex::SGML
when Enumerable
return super unless ActiveRecord::Relation === renderable
when nil
partial = kwargs.delete(:partial)

if partial # this is a hack to get around https://github.com/rails/rails/issues/51015
return raw(
@_context.view_context.render(partial, **kwargs) do |*yielded_args|
capture(*yielded_args, &block)
end
)
else
return super
end
when Phlex::SGML, Proc, Method, String
return super
when Class
return super if renderable < Phlex::SGML
when Enumerable
return super unless ActiveRecord::Relation === renderable
end

return super if args.length == 0 && kwargs.length == 0

output = if block
@_context.view_context.render(*args, **kwargs) do |*yielded_args|
if yielded_args.length == 1 && defined?(ViewComponent::Base) && ViewComponent::Base === yielded_args[0]
Expand Down
2 changes: 1 addition & 1 deletion test/dummy/app/views/layouts/application_layout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def view_template(&block)

body(class: "bg-blue") do
main(&block)
render partial: "rendering/partial"
render partial("rendering/partial")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/dummy/app/views/rendering/partial_from_phlex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Rendering
class PartialFromPhlex < ApplicationView
def view_template
render partial: "partial" do
render partial("partial") do
h1(id: "phlex") { "Partial from Phlex" }
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/phlex/test_helpers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class TestHelpersTest < ActiveSupport::TestCase

class PartialFromPhlex < ApplicationView
def view_template
render partial: "rendering/partial" do
render partial("rendering/partial") do
h1(id: "phlex") { "Partial from Phlex" }
end
end
Expand Down

0 comments on commit cd16a6d

Please sign in to comment.