Skip to content

Commit

Permalink
Merge pull request #223 from phlex-ruby/explicit-partial-rendering
Browse files Browse the repository at this point in the history
Require long-form partial rendering
  • Loading branch information
joeldrapper authored Sep 6, 2024
2 parents f0dd6d0 + 7458c4d commit 4ed04e8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 35 deletions.
56 changes: 22 additions & 34 deletions lib/phlex/rails/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,38 @@ def helpers
end
end

def render(*args, **, &block)
def render(*args, **kwargs, &block)
renderable = args[0]

case renderable
when Phlex::SGML, Proc, Method
when Phlex::SGML, Proc, Method, String
return super
when Class
return super if renderable < Phlex::SGML
when Enumerable
return super unless ActiveRecord::Relation === renderable
else
if block
@_context.target << @_view_context.render(*args, **) 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
when nil
partial = kwargs.delete(:partial)

if partial # this is a hack to get around https:/rails/rails/issues/51015
@_context.target << @_view_context.render(partial, **kwargs) do |*yielded_args|
capture(*yielded_args, &block)
end

return nil
end
end

if 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
else
@_context.target << @_view_context.render(*args, **)
end
else
@_context.target << @_view_context.render(*args, **kwargs)
end

nil
Expand Down Expand Up @@ -81,28 +91,6 @@ def capture(...)
super&.html_safe
end

# @api private
def __text__(content)
case content
when ActiveSupport::SafeBuffer
@_context.target << content
else
super
end
end

# TODO: Re-introduce this when we can figure out how to test it
# def await(task)
# case task
# when ActiveRecord::Relation
# future = task.instance_variable_get(:@future_result)
# flush if future && future.pending?
# task
# else
# super
# end
# end

# Trick ViewComponent into thinking we're a ViewComponent to fix rendering output
# @api private
def set_original_view_context(view_context)
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" do
render partial: "partial" do
h1(id: "phlex") { "Partial from Phlex" }
end
end
Expand Down

0 comments on commit 4ed04e8

Please sign in to comment.