-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Delegate properly to custom Resolver instances #1568
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
Changes from 1 commit
f705bda
158af81
790b2cd
9ab092c
f8dbe7c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,18 +39,50 @@ def render_views? | |
self.class.render_views? || !controller.class.respond_to?(:view_paths) | ||
end | ||
|
||
# Delegates find_all to the submitted path set and then returns templates | ||
# with modified source | ||
# | ||
# @private | ||
class EmptyTemplateResolver < ::ActionView::FileSystemResolver | ||
class EmptyTemplateResolverFactory | ||
def initialize(path) | ||
@path = path | ||
end | ||
|
||
def initialize(path, pattern=nil) | ||
unless path.is_a?(::ActionView::Resolver) | ||
super | ||
def resolver | ||
if @path.is_a?(::ActionView::Resolver) | ||
EmptyTemplateResolverDecorator.new(@path) | ||
else | ||
EmptyTemplateFileSystemResolver.new(@path) | ||
end | ||
end | ||
end | ||
|
||
class EmptyTemplateResolverDecorator | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
def initialize(resolver) | ||
@resolver = resolver | ||
end | ||
|
||
def method_missing(name, *args, &block) | ||
@resolver.send(name, *args, &block) | ||
end | ||
|
||
private | ||
|
||
def find_templates(*args) | ||
templates = @resolver.find_templates(*args) | ||
templates.map do |template| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This map could probably now be delegated to outside as it does the same as below. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, but I don’t really know where to move it. |
||
::ActionView::Template.new( | ||
"", | ||
template.identifier, | ||
EmptyTemplateHandler, | ||
:virtual_path => template.virtual_path, | ||
:format => template.formats | ||
) | ||
end | ||
end | ||
end | ||
|
||
# Delegates find_all to the submitted path set and then returns templates | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be |
||
# with modified source | ||
# | ||
# @private | ||
class EmptyTemplateFileSystemResolver < ::ActionView::FileSystemResolver | ||
private | ||
|
||
def find_templates(*args) | ||
|
@@ -88,13 +120,13 @@ def append_view_path(new_path) | |
private | ||
|
||
def _path_decorator(*paths) | ||
paths.map { |path| EmptyTemplateResolver.new(path) } | ||
paths.map { |path| EmptyTemplateResolverFactory.new(path).resolver } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above about allocations. |
||
end | ||
end | ||
|
||
# @private | ||
RESOLVER_CACHE = Hash.new do |hash, path| | ||
hash[path] = EmptyTemplateResolver.new(path) | ||
hash[path] = EmptyTemplateResolverFactory.new(path).resolver | ||
end | ||
|
||
included do | ||
|
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.
When we build thinks like this we normally make them "instance less" to avoid unnecessary allocations, please refactor to a static instance, or better yet a module method. Can I also suggest a better name is desired? Something like
DecoratePath
orBuildEmptyTemplateResolver
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.
Agree on both the allocation and naming concerns. How does
EmptyTemplateResolver.build
sound?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.
Actually, we could move
EmptyTemplateFileSystemResolver
andEmptyTemplateResolverDecorator
into thisEmptyTemplateResolver
module (class), since they’re already supposed to be private and are only used by this factory class.