Find the layout earlier

Remove duplication in finding a layout.  Remove `@as` ivar.
This commit is contained in:
Aaron Patterson 2020-02-25 14:12:38 -08:00
parent 67b1a7942e
commit c52fa675b8
No known key found for this signature in database
GPG Key ID: 953170BCB4FFAFC6
2 changed files with 24 additions and 28 deletions

@ -291,20 +291,12 @@ def initialize(*)
@context_prefix = @lookup_context.prefixes.first @context_prefix = @lookup_context.prefixes.first
end end
def template_keys
if @has_object || @collection
@locals.keys + retrieve_variable(@path, @as)
else
@locals.keys
end
end
def render(context, options, block) def render(context, options, block)
@as = as_variable(options) as = as_variable(options)
setup(context, options, @as) setup(context, options, as)
if @path if @path
template = find_template(@path, template_keys) template = find_template(@path, template_keys(@path, as))
else else
if options[:cached] if options[:cached]
raise NotImplementedError, "render caching requires a template. Please specify a partial when rendering" raise NotImplementedError, "render caching requires a template. Please specify a partial when rendering"
@ -312,15 +304,27 @@ def render(context, options, block)
template = nil template = nil
end end
if !block && (layout = @options[:layout])
layout = find_template(layout.to_s, template_keys(@path, as))
end
if @collection if @collection
render_collection(context, template) render_collection(context, template, layout)
else else
render_partial(context, template, block) render_partial(context, template, layout, block)
end end
end end
private private
def render_collection(view, template) def template_keys(path, as)
if @has_object || @collection
@locals.keys + retrieve_variable(path, as)
else
@locals.keys
end
end
def render_collection(view, template, layout)
identifier = (template && template.identifier) || @path identifier = (template && template.identifier) || @path
instrument(:collection, identifier: identifier, count: @collection.size) do |payload| instrument(:collection, identifier: identifier, count: @collection.size) do |payload|
return RenderedCollection.empty(@lookup_context.formats.first) if @collection.blank? return RenderedCollection.empty(@lookup_context.formats.first) if @collection.blank?
@ -334,25 +338,21 @@ def render_collection(view, template)
collection_body = if template collection_body = if template
cache_collection_render(payload, view, template, @collection) do |collection| cache_collection_render(payload, view, template, @collection) do |collection|
collection_with_template(view, template, collection) collection_with_template(view, template, layout, collection)
end end
else else
collection_with_template(view, nil, @collection) collection_with_template(view, nil, layout, @collection)
end end
build_rendered_collection(collection_body, spacer) build_rendered_collection(collection_body, spacer)
end end
end end
def render_partial(view, template, block) def render_partial(view, template, layout, block)
instrument(:partial, identifier: template.identifier) do |payload| instrument(:partial, identifier: template.identifier) do |payload|
locals = @locals locals = @locals
as = template.variable as = template.variable
object = @object object = @object
if !block && (layout = @options[:layout])
layout = find_template(layout.to_s, template_keys)
end
locals[as] = object if @has_object locals[as] = object if @has_object
content = template.render(view, locals) do |*name| content = template.render(view, locals) do |*name|
@ -436,13 +436,9 @@ def find_template(path, locals)
@lookup_context.find_template(path, prefixes, true, locals, @details) @lookup_context.find_template(path, prefixes, true, locals, @details)
end end
def collection_with_template(view, template, collection) def collection_with_template(view, template, layout, collection)
locals, collection_data = @locals, @collection_data locals, collection_data = @locals, @collection_data
cache = {} cache = template || {}
if layout = @options[:layout]
layout = find_template(layout, template_keys)
end
partial_iteration = PartialIteration.new(collection.size) partial_iteration = PartialIteration.new(collection.size)

@ -22,7 +22,7 @@ def relation_from_options(partial, collection)
end end
end end
def collection_with_template(_, _, collection) def collection_with_template(_, _, _, collection)
@relation.preload_associations(collection) if @relation @relation.preload_associations(collection) if @relation
super super
end end