From 30eab4e33eb139d4aa3dd0cb003021eac8d6cb09 Mon Sep 17 00:00:00 2001 From: Will Cosgrove Date: Fri, 8 Nov 2024 23:23:37 -0600 Subject: [PATCH 1/2] Access `view_context` from Phlex::Context instead of SGML ivar Goes with phlex-ruby/phlex#821 --- lib/phlex/rails/sgml.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/phlex/rails/sgml.rb b/lib/phlex/rails/sgml.rb index d0a1e87..44f528d 100644 --- a/lib/phlex/rails/sgml.rb +++ b/lib/phlex/rails/sgml.rb @@ -11,10 +11,12 @@ def render_in(...) module Overrides def helpers - if defined?(ViewComponent::Base) && ViewComponent::Base === @_view_context - @_view_context.helpers + view_context = @_context.view_context + + if defined?(ViewComponent::Base) && ViewComponent::Base === view_context + view_context.helpers else - @_view_context + view_context end end @@ -33,7 +35,7 @@ def render(*args, **kwargs, &block) if partial # this is a hack to get around https://github.com/rails/rails/issues/51015 return raw( - @_view_context.render(partial, **kwargs) do |*yielded_args| + @_context.view_context.render(partial, **kwargs) do |*yielded_args| capture(*yielded_args, &block) end, ) @@ -41,7 +43,7 @@ def render(*args, **kwargs, &block) end output = if block - @_view_context.render(*args, **kwargs) do |*yielded_args| + @_context.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 @@ -49,7 +51,7 @@ def render(*args, **kwargs, &block) end end else - @_view_context.render(*args, **kwargs) + @_context.view_context.render(*args, **kwargs) end raw(output) From b7f6626b77b20b09f1743971f72af988879ce2ec Mon Sep 17 00:00:00 2001 From: Joel Drapper Date: Tue, 26 Nov 2024 11:12:22 +0000 Subject: [PATCH 2/2] Update sgml.rb --- lib/phlex/rails/sgml.rb | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/phlex/rails/sgml.rb b/lib/phlex/rails/sgml.rb index 4c401ad..48b27eb 100644 --- a/lib/phlex/rails/sgml.rb +++ b/lib/phlex/rails/sgml.rb @@ -13,8 +13,9 @@ module Overrides class HelpersCalledBeforeRenderError < StandardError; end def helpers - view_context = @_context.view_context - raise HelpersCalledBeforeRenderError.new("Do not use rails helpers until after the view has been rendered.") unless view_context + 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 + end if defined?(ViewComponent::Base) && ViewComponent::Base === view_context view_context.helpers @@ -36,13 +37,13 @@ def render(*args, **kwargs, &block) 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, - ) - end + 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, + ) + end end output = if block