-
Notifications
You must be signed in to change notification settings - Fork 444
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure content is rendered correctly for forwarded slots (#2016)
* WIP * Add benchmark * Add benchmark * Clean up benchmark; remove old slot content impl * Fix bad merge * Prevent warnings * Add CHANGELOG entry * Update docs/CHANGELOG.md Co-authored-by: Hans Lemuet <[email protected]> --------- Co-authored-by: Hans Lemuet <[email protected]>
- Loading branch information
Showing
9 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<%= render(Performance::SlotsComponent.new(name: "Fox Mulder")) do |component| %> | ||
<% component.with_header(classes: "foo") { "Header" } %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# frozen_string_literal: true | ||
|
||
class Performance::SlotsWrapperComponent < ViewComponent::Base | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
|
||
# Run `bundle exec rake benchmark` to execute benchmark. | ||
# This is very much a work-in-progress. Please feel free to make/suggest improvements! | ||
|
||
require "benchmark/ips" | ||
|
||
# Configure Rails Environment | ||
ENV["RAILS_ENV"] = "production" | ||
require File.expand_path("../test/sandbox/config/environment.rb", __dir__) | ||
|
||
module Performance | ||
require_relative "components/slots_component" | ||
require_relative "components/slots_wrapper_component" | ||
end | ||
|
||
class BenchmarksController < ActionController::Base | ||
end | ||
|
||
BenchmarksController.view_paths = [File.expand_path("./views", __dir__)] | ||
controller_view = BenchmarksController.new.view_context | ||
|
||
Benchmark.ips do |x| | ||
x.time = 10 | ||
x.warmup = 2 | ||
|
||
x.report("slots") do | ||
controller_view.render(Performance::SlotsWrapperComponent.new(name: "Fox Mulder")) | ||
end | ||
|
||
x.compare! | ||
end |
5 changes: 5 additions & 0 deletions
5
test/sandbox/app/components/forwarding_slot_wrapper_component.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<%= render(ForwardingSlotComponent.new) do |panel| %> | ||
<% panel.with_target_content do |header| %> | ||
Target content | ||
<% end %> | ||
<% end %> |
38 changes: 38 additions & 0 deletions
38
test/sandbox/app/components/forwarding_slot_wrapper_component.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
class ForwardingSlotWrapperComponent < ViewComponent::Base | ||
end | ||
|
||
unless defined?(ForwardingSlotComponent) | ||
class ForwardingSlotComponent < ViewComponent::Base | ||
def initialize | ||
@target = TargetSlotComponent.new | ||
end | ||
|
||
def with_target_content(&block) | ||
@target.with_target_content(&block) | ||
end | ||
|
||
def before_render | ||
content | ||
end | ||
|
||
def call | ||
render(@target) | ||
end | ||
end | ||
|
||
class TargetSlotComponent < ViewComponent::Base | ||
renders_one :target_content, "TargetContentComponent" | ||
|
||
def call | ||
target_content | ||
end | ||
end | ||
|
||
class TargetContentComponent < ViewComponent::Base | ||
def call | ||
content | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters