Skip to content

Commit

Permalink
Add view_context to Phlex::Context
Browse files Browse the repository at this point in the history
  • Loading branch information
willcosgrove committed Jan 31, 2024
1 parent e886ad8 commit d54436e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
4 changes: 4 additions & 0 deletions lib/phlex/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ class HTML
class Unbuffered
prepend ::Phlex::Rails::UnbufferedOverrides
end

class Context
prepend ::Phlex::Rails::Context::Overrides
end
end
16 changes: 16 additions & 0 deletions lib/phlex/rails/context/overrides.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module Phlex
module Rails
module Context
module Overrides
def initialize(...)
super
@view_context = nil
end

attr_accessor :view_context
end
end
end
end
13 changes: 9 additions & 4 deletions lib/phlex/rails/sgml/overrides.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ module Rails
module SGML
module Overrides
def helpers
if defined?(ViewComponent::Base) && @_view_context.is_a?(ViewComponent::Base)
@_view_context.helpers
if defined?(ViewComponent::Base) && @_context.view_context.is_a?(ViewComponent::Base)
@_context.view_context.helpers
else
@_view_context
@_context.view_context
end
end

Expand All @@ -24,7 +24,7 @@ def render(*args, **kwargs, &block)
return super unless renderable.is_a?(ActiveRecord::Relation)
else
captured_block = -> { capture(&block) } if block
@_context.target << @_view_context.render(*args, **kwargs, &captured_block)
@_context.target << @_context.view_context.render(*args, **kwargs, &captured_block)
end

nil
Expand Down Expand Up @@ -57,6 +57,11 @@ def render_in(view_context, &block)
end
end

def __final_call__(buffer = +"", context: Phlex::Context.new, view_context: nil, **kwargs, &block)
context.view_context ||= view_context
super(buffer, context: context, **kwargs, &block)
end

def capture
super&.html_safe
end
Expand Down

0 comments on commit d54436e

Please sign in to comment.