|
| 1 | +# This is mostly taken from rspec. |
| 2 | +# Specifically: https://raw.github.com/rspec/rspec-rails/52493649d70754377b5e41be1c385a742f268bd7/lib/rspec/rails/view_rendering.rb |
| 3 | +# Helpers for optionally rendering views in controller specs. |
| 4 | +module ViewRendering |
| 5 | + extend ActiveSupport::Concern |
| 6 | + |
| 7 | + attr_accessor :controller |
| 8 | + |
| 9 | + def render_views? |
| 10 | + self.class.view_tests.include?(method_name) |
| 11 | + end |
| 12 | + |
| 13 | + module ClassMethods |
| 14 | + def view_test(name, &block) |
| 15 | + test(name, &block) |
| 16 | + add_view_test("test_#{name.gsub(/\s+/, '_')}") |
| 17 | + end |
| 18 | + |
| 19 | + def view_tests |
| 20 | + @view_tests ||= [] |
| 21 | + end |
| 22 | + |
| 23 | + def add_view_test(test_name) |
| 24 | + view_tests << test_name |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + class EmptyTemplateResolver |
| 29 | + def self.build(path) |
| 30 | + if path.is_a?(::ActionView::Resolver) |
| 31 | + ResolverDecorator.new(path) |
| 32 | + else |
| 33 | + FileSystemResolver.new(path) |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + def self.nullify_template_rendering(templates) |
| 38 | + templates.map do |template| |
| 39 | + ::ActionView::Template.new( |
| 40 | + "", |
| 41 | + template.identifier, |
| 42 | + EmptyTemplateHandler, |
| 43 | + virtual_path: template.virtual_path, |
| 44 | + format: template.format, |
| 45 | + locals: [], |
| 46 | + ) |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + # Delegates all methods to the submitted resolver and for all methods |
| 51 | + # that return a collection of `ActionView::Template` instances, return |
| 52 | + # templates with modified source |
| 53 | + class ResolverDecorator < ::ActionView::Resolver |
| 54 | + (::ActionView::Resolver.instance_methods - Object.instance_methods).each do |method| |
| 55 | + undef_method method |
| 56 | + end |
| 57 | + |
| 58 | + (::ActionView::Resolver.methods - Object.methods).each do |method| |
| 59 | + singleton_class.undef_method method |
| 60 | + end |
| 61 | + |
| 62 | + # rubocop:disable Lint/MissingSuper |
| 63 | + def initialize(resolver) |
| 64 | + @resolver = resolver |
| 65 | + end |
| 66 | + # rubocop:enable Lint/MissingSuper |
| 67 | + |
| 68 | + # rubocop:disable Style/MissingRespondToMissing |
| 69 | + def method_missing(name, *args, &block) |
| 70 | + result = @resolver.send(name, *args, &block) |
| 71 | + nullify_templates(result) |
| 72 | + end |
| 73 | + # rubocop:enable Style/MissingRespondToMissing |
| 74 | + |
| 75 | + private |
| 76 | + |
| 77 | + def nullify_templates(collection) |
| 78 | + return collection unless collection.is_a?(Enumerable) |
| 79 | + return collection unless collection.all? { |element| element.is_a?(::ActionView::Template) } |
| 80 | + |
| 81 | + EmptyTemplateResolver.nullify_template_rendering(collection) |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + # Delegates find_templates to the submitted path set and then returns |
| 86 | + # templates with modified source |
| 87 | + class FileSystemResolver < ::ActionView::FileSystemResolver |
| 88 | + private |
| 89 | + |
| 90 | + def find_templates(*args) |
| 91 | + templates = super |
| 92 | + EmptyTemplateResolver.nullify_template_rendering(templates) |
| 93 | + end |
| 94 | + end |
| 95 | + end |
| 96 | + |
| 97 | + class EmptyTemplateHandler |
| 98 | + def self.call(_template, _source = nil) |
| 99 | + ::Rails.logger.info(" Template rendering was prevented by rspec-rails. Use `render_views` to verify rendered view contents if necessary.") |
| 100 | + |
| 101 | + %("") |
| 102 | + end |
| 103 | + end |
| 104 | + |
| 105 | + # Used to null out view rendering in controller specs. |
| 106 | + module EmptyTemplates |
| 107 | + def prepend_view_path(new_path) |
| 108 | + super(_path_decorator(*new_path)) |
| 109 | + end |
| 110 | + |
| 111 | + def append_view_path(new_path) |
| 112 | + super(_path_decorator(*new_path)) |
| 113 | + end |
| 114 | + |
| 115 | + private |
| 116 | + |
| 117 | + def _path_decorator(*paths) |
| 118 | + paths.map { |path| EmptyTemplateResolver.build(path) } |
| 119 | + end |
| 120 | + end |
| 121 | + |
| 122 | + RESOLVER_CACHE = Hash.new do |hash, path| |
| 123 | + hash[path] = EmptyTemplateResolver.build(path) |
| 124 | + end |
| 125 | + |
| 126 | + included do |
| 127 | + setup do |
| 128 | + unless render_views? |
| 129 | + @_original_path_set = controller.class.view_paths |
| 130 | + path_set = @_original_path_set.map { |resolver| RESOLVER_CACHE[resolver] } |
| 131 | + |
| 132 | + controller.class.view_paths = path_set |
| 133 | + controller.extend(EmptyTemplates) |
| 134 | + end |
| 135 | + end |
| 136 | + |
| 137 | + teardown do |
| 138 | + controller.class.view_paths = @_original_path_set unless render_views? |
| 139 | + end |
| 140 | + end |
| 141 | +end |
0 commit comments