From d99691518323a63afba611aca6ecc7464327af85 Mon Sep 17 00:00:00 2001 From: Joel Drapper Date: Tue, 26 Nov 2024 13:19:51 +0000 Subject: [PATCH] Use renderable partial proxy for rendering partials --- lib/phlex/rails.rb | 3 +- lib/phlex/rails/partial.rb | 15 ++++++++ lib/phlex/rails/sgml.rb | 34 +++++++++---------- .../app/views/layouts/application_layout.rb | 2 +- .../app/views/rendering/partial_from_phlex.rb | 2 +- test/phlex/test_helpers_test.rb | 2 +- 6 files changed, 36 insertions(+), 22 deletions(-) create mode 100644 lib/phlex/rails/partial.rb diff --git a/lib/phlex/rails.rb b/lib/phlex/rails.rb index afa5a2f..a800302 100644 --- a/lib/phlex/rails.rb +++ b/lib/phlex/rails.rb @@ -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" diff --git a/lib/phlex/rails/partial.rb b/lib/phlex/rails/partial.rb new file mode 100644 index 0000000..5c45c2a --- /dev/null +++ b/lib/phlex/rails/partial.rb @@ -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 diff --git a/lib/phlex/rails/sgml.rb b/lib/phlex/rails/sgml.rb index 3753a3d..71f6f5a 100644 --- a/lib/phlex/rails/sgml.rb +++ b/lib/phlex/rails/sgml.rb @@ -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 @@ -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] diff --git a/test/dummy/app/views/layouts/application_layout.rb b/test/dummy/app/views/layouts/application_layout.rb index 496de5b..ae7df26 100644 --- a/test/dummy/app/views/layouts/application_layout.rb +++ b/test/dummy/app/views/layouts/application_layout.rb @@ -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 diff --git a/test/dummy/app/views/rendering/partial_from_phlex.rb b/test/dummy/app/views/rendering/partial_from_phlex.rb index c3dff24..38c9f4e 100644 --- a/test/dummy/app/views/rendering/partial_from_phlex.rb +++ b/test/dummy/app/views/rendering/partial_from_phlex.rb @@ -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 diff --git a/test/phlex/test_helpers_test.rb b/test/phlex/test_helpers_test.rb index c5dc716..c69cee4 100644 --- a/test/phlex/test_helpers_test.rb +++ b/test/phlex/test_helpers_test.rb @@ -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