Skip to content

Commit f705bda

Browse files
committed
Delegate properly to custom Resolver instances
Introduce `EmptyTemplateResolverFactory` for creating instances of either `EmptyTemplateResolverDecorator` or `EmptyTemplateFileSystemResolver`, depending on whether the given path is an `ActionView::Resolver` instance or not. The new `EmptyTemplateResolverDecorator` class simply delegates all method calls to its given resolver, except for `#find_templates` which it overrides to return templates that render with `EmptyTemplateHandler`.
1 parent da88413 commit f705bda

File tree

1 file changed

+42
-10
lines changed

1 file changed

+42
-10
lines changed

lib/rspec/rails/view_rendering.rb

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,50 @@ def render_views?
3939
self.class.render_views? || !controller.class.respond_to?(:view_paths)
4040
end
4141

42-
# Delegates find_all to the submitted path set and then returns templates
43-
# with modified source
44-
#
45-
# @private
46-
class EmptyTemplateResolver < ::ActionView::FileSystemResolver
42+
class EmptyTemplateResolverFactory
43+
def initialize(path)
44+
@path = path
45+
end
4746

48-
def initialize(path, pattern=nil)
49-
unless path.is_a?(::ActionView::Resolver)
50-
super
47+
def resolver
48+
if @path.is_a?(::ActionView::Resolver)
49+
EmptyTemplateResolverDecorator.new(@path)
50+
else
51+
EmptyTemplateFileSystemResolver.new(@path)
5152
end
5253
end
54+
end
5355

56+
class EmptyTemplateResolverDecorator
57+
def initialize(resolver)
58+
@resolver = resolver
59+
end
60+
61+
def method_missing(name, *args, &block)
62+
@resolver.send(name, *args, &block)
63+
end
64+
65+
private
66+
67+
def find_templates(*args)
68+
templates = @resolver.find_templates(*args)
69+
templates.map do |template|
70+
::ActionView::Template.new(
71+
"",
72+
template.identifier,
73+
EmptyTemplateHandler,
74+
:virtual_path => template.virtual_path,
75+
:format => template.formats
76+
)
77+
end
78+
end
79+
end
80+
81+
# Delegates find_all to the submitted path set and then returns templates
82+
# with modified source
83+
#
84+
# @private
85+
class EmptyTemplateFileSystemResolver < ::ActionView::FileSystemResolver
5486
private
5587

5688
def find_templates(*args)
@@ -88,13 +120,13 @@ def append_view_path(new_path)
88120
private
89121

90122
def _path_decorator(*paths)
91-
paths.map { |path| EmptyTemplateResolver.new(path) }
123+
paths.map { |path| EmptyTemplateResolverFactory.new(path).resolver }
92124
end
93125
end
94126

95127
# @private
96128
RESOLVER_CACHE = Hash.new do |hash, path|
97-
hash[path] = EmptyTemplateResolver.new(path)
129+
hash[path] = EmptyTemplateResolverFactory.new(path).resolver
98130
end
99131

100132
included do

0 commit comments

Comments
 (0)