Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactor controller/view_paths extensions for view isolation

- handle both Strings and PathSets passed to append_view_path or
  prepend_view_path
  • Loading branch information...
commit 7cd0f5e8e16d59344ab270d8538a71cd917a4327 1 parent 39961a0
@dchelimsky dchelimsky authored
Showing with 24 additions and 26 deletions.
  1. +24 −26 lib/rspec/rails/view_rendering.rb
View
50 lib/rspec/rails/view_rendering.rb
@@ -49,15 +49,15 @@ def render_views?
# Delegates find_all to the submitted path set and then returns templates
# with modified source
- class PathSetDelegatorResolver < ::ActionView::Resolver
- attr_reader :path_set
+ class EmptyTemplatePathSetDecorator < ::ActionView::Resolver
+ attr_reader :original_path_set
- def initialize(path_set)
- @path_set = path_set
+ def initialize(original_path_set)
+ @original_path_set = original_path_set
end
def find_all(*args)
- path_set.find_all(*args).collect do |template|
+ original_path_set.find_all(*args).collect do |template|
::ActionView::Template.new(
"",
template.identifier,
@@ -71,35 +71,33 @@ def find_all(*args)
end
end
+ module EmptyTemplates
+ def prepend_view_path(new_path)
+ lookup_context.view_paths.unshift(*_path_decorator(new_path))
+ end
+
+ def append_view_path(new_path)
+ lookup_context.view_paths.push(*_path_decorator(new_path))
+ end
+
+ private
+ def _path_decorator(path)
+ EmptyTemplatePathSetDecorator.new(::ActionView::Base::process_view_paths(path))
@dchelimsky Owner

This ^^ supports Strings and PathSets (allowing for non-file-system paths).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ end
+ end
+
included do
before do
unless render_views?
- @_path_set_delegator_resolver = PathSetDelegatorResolver.new(controller.class.view_paths)
- controller.class.view_paths = ::ActionView::PathSet.new.push(@_path_set_delegator_resolver)
- controller.instance_eval do
- alias orig_prepend_view_path prepend_view_path
- alias orig_append_view_path append_view_path
-
- def prepend_view_path(new_path)
- _new_path_delegator = PathSetDelegatorResolver.new(::ActionView::FileSystemResolver.new(new_path))
- lookup_context.view_paths.unshift(*_new_path_delegator)
- end
-
- def append_view_path(new_path)
- _new_path_delegator = PathSetDelegatorResolver.new(::ActionView::FileSystemResolver.new(new_path))
- lookup_context.view_paths.push(*_new_path_delegator)
- end
- end
+ @_empty_view_path_set_delegator = EmptyTemplatePathSetDecorator.new(controller.class.view_paths)
+ controller.class.view_paths = ::ActionView::PathSet.new.push(@_empty_view_path_set_delegator)
+ controller.extend(EmptyTemplates)
end
end
after do
unless render_views?
- controller.class.view_paths = @_path_set_delegator_resolver.path_set
- controller.instance_eval do
- alias prepend_view_path orig_prepend_view_path
- alias append_view_path orig_append_view_path
- end
+ controller.class.view_paths = @_empty_view_path_set_delegator.original_path_set
end
end
end
@dchelimsky

This ^^ supports Strings and PathSets (allowing for non-file-system paths).

Please sign in to comment.
Something went wrong with that request. Please try again.