Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 92 lines (75 sloc) 2.388 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
require 'action_view/testing/resolvers'

RSpec.configure do |config|
  config.add_setting :render_views, :default => false

  # TODO - rspec-core needs a way to define a setting that works like this in
  # one go
  def config.render_views
    settings[:render_views] = true
  end

  def config.render_views?
    settings[:render_views]
  end
end

module RSpec
  module Rails
    module ViewRendering
      extend ActiveSupport::Concern

      attr_accessor :controller

      module ClassMethods
        def metadata_for_rspec_rails
          metadata[:rspec_rails] = metadata[:rspec_rails] ? metadata[:rspec_rails].dup : {}
        end

        # See RSpec::Rails::ControllerExampleGroup
        def render_views(true_or_false=true)
          metadata_for_rspec_rails[:render_views] = true_or_false
        end

        def integrate_views
          RSpec.deprecate("integrate_views","render_views")
          render_views
        end

        def render_views?
          metadata_for_rspec_rails[:render_views] || RSpec.configuration.render_views?
        end
      end

      module InstanceMethods
        def render_views?
          self.class.render_views? || !controller.class.respond_to?(:view_paths)
        end
      end

      # Delegates find_all to the submitted path set and then returns templates
      # with modified source
      class PathSetDelegatorResolver < ::ActionView::Resolver
        attr_reader :path_set

        def initialize(path_set)
          @path_set = path_set
        end

        def find_all(*args)
          path_set.find_all(*args).collect do |template|
            ::ActionView::Template.new(
              "",
              template.identifier,
              template.handler,
              {
                :virtual_path => template.virtual_path,
                :format => template.formats
              }
            )
          end
        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)
          end
        end

        after do
          unless render_views?
            controller.class.view_paths = @_path_set_delegator_resolver.path_set
          end
        end
      end

    end
  end
end
Something went wrong with that request. Please try again.