Skip to content

Commit

Permalink
Merge pull request #242 from phlex-ruby/view-context-in-context
Browse files Browse the repository at this point in the history
Access `view_context` from Phlex::Context instead of SGML ivar
  • Loading branch information
joeldrapper authored Nov 26, 2024
2 parents fc148d0 + b7f6626 commit cc3703e
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/phlex/rails/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ module Overrides
class HelpersCalledBeforeRenderError < StandardError; end

def helpers
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
if defined?(ViewComponent::Base) && ViewComponent::Base === view_context
view_context.helpers
else
@_view_context
view_context
end
end

Expand All @@ -37,23 +39,23 @@ 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,
)
end
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
capture(*yielded_args, &block)
end
end
else
@_view_context.render(*args, **kwargs)
@_context.view_context.render(*args, **kwargs)
end

raw(output)
Expand Down

0 comments on commit cc3703e

Please sign in to comment.