-
Notifications
You must be signed in to change notification settings - Fork 461
Add optional compatibility module for capture/form/turbo compatibility fixes #1650
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
f45c0c8
Add optional capture compatibility module for forms
BlakeWilliams a24585a
Update patch
BlakeWilliams 27ea033
Namespace
BlakeWilliams d86de8a
Derisk the patch
BlakeWilliams 39dcff4
Use test matrix, add initializers
BlakeWilliams d098fdf
Temporary include matrix
BlakeWilliams 37ed622
Run standard
BlakeWilliams bee1c22
Add basic changelog entry
BlakeWilliams 0ee2df7
Add basic module doc
BlakeWilliams 27e4d48
Merge branch 'main' into bmw/compat-module
BlakeWilliams eb66262
Add partial test
BlakeWilliams 54d3f0a
Use safer check
BlakeWilliams 66ec2ea
Feedback, use string for matrix for better build names
BlakeWilliams c22c652
Update docs/CHANGELOG.md
BlakeWilliams 28e2476
Add config value to config defaults
BlakeWilliams 70ab28f
Document config value and test include logic
BlakeWilliams c8b13f6
Add fix for C binding problem
BlakeWilliams cce596a
Update lib/view_component/config.rb
BlakeWilliams File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,42 @@ | ||
# frozen_string_literal: true | ||
|
||
module ViewComponent | ||
# CaptureCompatibility is a module that patches #capture to fix issues | ||
# related to ViewComponent and functionality that relies on `capture` | ||
# like forms, capture itself, turbo frames, etc. | ||
# | ||
# This underlying incompatibility with ViewComponent and capture is | ||
# that several features like forms keep a reference to the primary | ||
# `ActionView::Base` instance which has its own @output_buffer. When | ||
# `#capture` is called on the original `ActionView::Base` instance while | ||
# evaluating a block from a ViewComponent the @output_buffer is overridden | ||
# in the ActionView::Base instance, and *not* the component. This results | ||
# in a double render due to `#capture` implementation details. | ||
# | ||
# To resolve the issue, we override `#capture` so that we can delegate | ||
# the `capture` logic to the ViewComponent that created the block. | ||
module CaptureCompatibility | ||
def self.included(base) | ||
base.class_eval do | ||
alias_method :original_capture, :capture | ||
end | ||
|
||
base.prepend(InstanceMethods) | ||
end | ||
|
||
module InstanceMethods | ||
def capture(*args, &block) | ||
# Handle blocks that originate from C code and raise, such as `&:method` | ||
return original_capture(*args, &block) if block.source_location.nil? | ||
|
||
block_context = block.binding.receiver | ||
|
||
if block_context != self && block_context.class < ActionView::Base | ||
block_context.original_capture(*args, &block) | ||
else | ||
original_capture(*args, &block) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or 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 hidden or 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 hidden or 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 @@ | ||
<%= form_for Post.new, url: "/" do |form| %> | ||
<%= render "forms/label", form: form %> | ||
<% end %> |
This file contains hidden or 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 FormPartialComponent < ViewComponent::Base | ||
end |
This file contains hidden or 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 @@ | ||
<%= form.label :published do %> | ||
<%= form.check_box :published %> | ||
<% end %> |
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.