Skip to content

Commit

Permalink
Support selective rendering (#154)
Browse files Browse the repository at this point in the history
* Support selective rendering

* Use X-Fragment header

* Make rails helpers respect selective rendering

* Support multipel fragments

* Support selective rendering for turbo frames

* Update overrides.rb

* Fixes

---------

Co-authored-by: Will Cosgrove <[email protected]>
  • Loading branch information
joeldrapper and willcosgrove authored Mar 16, 2024
1 parent d2dbd2a commit d0dad36
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
8 changes: 8 additions & 0 deletions lib/phlex/rails/helper_macros.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ def define_output_helper(method_name)
# frozen_string_literal: true
def #{method_name}(...)
context = @_context
return if context.fragments && !context.in_target_fragment
output = helpers.#{method_name}(...)
case output
Expand All @@ -24,6 +27,9 @@ def define_output_helper_with_capture_block(method_name)
# frozen_string_literal: true
def #{method_name}(*args, **kwargs, &block)
context = @_context
return if context.fragments && !context.in_target_fragment
output = if block
helpers.#{method_name}(*args, **kwargs) { capture(&block) }
else
Expand Down Expand Up @@ -69,6 +75,8 @@ def define_builder_yielding_helper(method_name, builder)
# frozen_string_literal: true
def #{method_name}(*args, **kwargs)
context = @_context
return if context.fragments && !context.in_target_fragment
output = if block_given?
helpers.#{method_name}(*args, **kwargs) { |form|
capture do
Expand Down
10 changes: 8 additions & 2 deletions lib/phlex/rails/sgml/overrides.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@ def render(*args, **kwargs, &block)
end

def render_in(view_context, &block)
fragments = if (fragment_header = view_context.request.headers["X-Fragment"])
fragment_header.split(" ")
elsif (turbo_frame = view_context.request.headers["Turbo-Frame"])
[turbo_frame]
end

if block_given?
call(view_context: view_context) do |*args|
call(view_context: view_context, fragments: fragments) do |*args|
original_length = @_context.target.length

if args.length == 1 && Phlex::SGML === args[0] && !block.source_location&.[](0)&.end_with?(".rb")
Expand All @@ -53,7 +59,7 @@ def render_in(view_context, &block)
end
end.html_safe
else
call(view_context: view_context).html_safe
call(view_context: view_context, fragments: fragments).html_safe
end
end

Expand Down
2 changes: 1 addition & 1 deletion phlex-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Gem::Specification.new do |spec|
end
spec.require_paths = ["lib"]

spec.add_dependency "phlex", "~> 1.9"
spec.add_dependency "phlex", "~> 1.10"
spec.add_dependency "railties", ">= 6.1", "< 8"
spec.add_dependency "zeitwerk", "~> 2.6"

Expand Down

0 comments on commit d0dad36

Please sign in to comment.