From a4ca370aab8cef6e03c720f385f1b3183d089f99 Mon Sep 17 00:00:00 2001 From: Brad Gessler Date: Sat, 3 Feb 2024 17:07:35 -0800 Subject: [PATCH] Add version 2, which is currently the same as 1.x --- lib/phlex/rails.rb | 2 +- lib/phlex/rails/sgml/overrides.rb | 5 +- lib/phlex/rails/sgml/overrides/version_1.rb | 2 +- lib/phlex/rails/sgml/overrides/version_2.rb | 96 +++++++++++++++++++++ 4 files changed, 100 insertions(+), 5 deletions(-) create mode 100644 lib/phlex/rails/sgml/overrides/version_2.rb diff --git a/lib/phlex/rails.rb b/lib/phlex/rails.rb index b14d1bfe..18e5eb51 100644 --- a/lib/phlex/rails.rb +++ b/lib/phlex/rails.rb @@ -20,7 +20,7 @@ module Rails class SGML extend Phlex::Rails::SGML::ClassMethods - prepend Phlex::Rails::SGML::Overrides + prepend Phlex::Rails::SGML::Overrides::Version1 end # @api private diff --git a/lib/phlex/rails/sgml/overrides.rb b/lib/phlex/rails/sgml/overrides.rb index cdcad765..dba33247 100644 --- a/lib/phlex/rails/sgml/overrides.rb +++ b/lib/phlex/rails/sgml/overrides.rb @@ -1,12 +1,11 @@ # frozen_string_literal: true +require 'rubygems' # Ensure RubyGems is loaded for Gem::Version + module Phlex module Rails module SGML module Overrides - def self.prepended(klass) - klass.prepend Version1 - end end end end diff --git a/lib/phlex/rails/sgml/overrides/version_1.rb b/lib/phlex/rails/sgml/overrides/version_1.rb index 116daead..c367b205 100644 --- a/lib/phlex/rails/sgml/overrides/version_1.rb +++ b/lib/phlex/rails/sgml/overrides/version_1.rb @@ -16,7 +16,7 @@ def helpers def render(*args, **kwargs, &block) renderable = args[0] - warn "Rendering strings will render the string instead of a Rails template in Phlex Rails 2.0" if renderable.is_a? String + warn "Rendering strings will render the string instead of a Rails partial in Phlex Rails 2.0" if renderable.is_a? String case renderable when Phlex::SGML, Proc diff --git a/lib/phlex/rails/sgml/overrides/version_2.rb b/lib/phlex/rails/sgml/overrides/version_2.rb new file mode 100644 index 00000000..c8e499e7 --- /dev/null +++ b/lib/phlex/rails/sgml/overrides/version_2.rb @@ -0,0 +1,96 @@ +# frozen_string_literal: true + +module Phlex + module Rails + module SGML + module Overrides + module Version2 + def helpers + if defined?(ViewComponent::Base) && @_view_context.is_a?(ViewComponent::Base) + @_view_context.helpers + else + @_view_context + end + end + + def render(*args, **kwargs, &block) + renderable = args[0] + + warn "Rendering strings will render the string instead of a Rails template in Phlex Rails 2.0" if renderable.is_a? String + + case renderable + when Phlex::SGML, Proc + return super + when Class + return super if renderable < Phlex::SGML + when Enumerable + return super unless renderable.is_a?(ActiveRecord::Relation) + else + captured_block = -> { capture(&block) } if block + @_context.target << @_view_context.render(*args, **kwargs, &captured_block) + end + + nil + end + + def render_in(view_context, &block) + if block_given? + call(view_context: view_context) do |*args| + original_length = @_context.target.length + + if args.length == 1 && Phlex::SGML === args[0] && !block.source_location&.[](0)&.end_with?(".rb") + output = view_context.capture( + args[0].unbuffered, &block + ) + else + output = view_context.capture(*args, &block) + end + + unchanged = (original_length == @_context.target.length) + + if unchanged + case output + when ActiveSupport::SafeBuffer + @_context.target << output + end + end + end.html_safe + else + call(view_context: view_context).html_safe + end + end + + def capture + super&.html_safe + end + + # @api private + def __text__(content) + case content + when ActiveSupport::SafeBuffer + @_context.target << content + else + super + end + end + + # @api private + def await(task) + if task.is_a?(ActiveRecord::Relation) + flush unless task.loaded? + + 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) + end + end + end + end + end +end