Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require long-form partial rendering #223

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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://github.com/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
Loading