-
Notifications
You must be signed in to change notification settings - Fork 440
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
Speed up collection rendering and add support for multifetch collection handling #501
Merged
Merged
Changes from all commits
Commits
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 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,108 @@ | ||
require 'delegate' | ||
require 'active_support/concern' | ||
|
||
begin | ||
require 'action_view/renderer/collection_renderer' | ||
rescue LoadError | ||
require 'action_view/renderer/partial_renderer' | ||
end | ||
|
||
class Jbuilder | ||
module CollectionRenderable # :nodoc: | ||
extend ActiveSupport::Concern | ||
|
||
class_methods do | ||
def supported? | ||
superclass.private_method_defined?(:build_rendered_template) && self.superclass.private_method_defined?(:build_rendered_collection) | ||
end | ||
end | ||
|
||
private | ||
|
||
def build_rendered_template(content, template, layout = nil) | ||
super(content || json.attributes!, template) | ||
end | ||
|
||
def build_rendered_collection(templates, _spacer) | ||
json.merge!(templates.map(&:body)) | ||
end | ||
|
||
def json | ||
@options[:locals].fetch(:json) | ||
end | ||
|
||
class ScopedIterator < ::SimpleDelegator # :nodoc: | ||
include Enumerable | ||
|
||
def initialize(obj, scope) | ||
super(obj) | ||
@scope = scope | ||
end | ||
|
||
# Rails 6.0 support: | ||
def each | ||
return enum_for(:each) unless block_given? | ||
|
||
__getobj__.each do |object| | ||
@scope.call { yield(object) } | ||
end | ||
end | ||
|
||
# Rails 6.1 support: | ||
def each_with_info | ||
return enum_for(:each_with_info) unless block_given? | ||
|
||
__getobj__.each_with_info do |object, info| | ||
@scope.call { yield(object, info) } | ||
end | ||
end | ||
end | ||
|
||
private_constant :ScopedIterator | ||
end | ||
|
||
if defined?(::ActionView::CollectionRenderer) | ||
# Rails 6.1 support: | ||
class CollectionRenderer < ::ActionView::CollectionRenderer # :nodoc: | ||
include CollectionRenderable | ||
|
||
def initialize(lookup_context, options, &scope) | ||
super(lookup_context, options) | ||
@scope = scope | ||
end | ||
|
||
private | ||
def collection_with_template(view, template, layout, collection) | ||
super(view, template, layout, ScopedIterator.new(collection, @scope)) | ||
end | ||
end | ||
else | ||
# Rails 6.0 support: | ||
class CollectionRenderer < ::ActionView::PartialRenderer # :nodoc: | ||
include CollectionRenderable | ||
|
||
def initialize(lookup_context, options, &scope) | ||
super(lookup_context) | ||
@options = options | ||
@scope = scope | ||
end | ||
|
||
def render_collection_with_partial(collection, partial, context, block) | ||
render(context, @options.merge(collection: collection, partial: partial), block) | ||
yuki24 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
private | ||
def collection_without_template(view) | ||
@collection = ScopedIterator.new(@collection, @scope) | ||
|
||
super(view) | ||
end | ||
|
||
def collection_with_template(view, template) | ||
@collection = ScopedIterator.new(@collection, @scope) | ||
|
||
super(view, template) | ||
end | ||
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not believe we have to mention it any more as Jbuilder will have it built-in once this PR is merged. So I just replaced it with how
cached: true
could be used on collection rendering.