Sha256: 25db6d7b31fd89836f1a3e70e4282f7a51e9724ece4728eaf13e0cd793f6c761

Contents?: true

Size: 1.68 KB

Versions: 3

Compression:

Stored size: 1.68 KB

Contents

require 'action_view/testing/resolvers'

module RSpec
  module Rails
    module ViewRendering
      extend ActiveSupport::Concern

      module ClassMethods
        def metadata_for_rspec_rails
          metadata[:rspec_rails] ||= {}
        end

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

        def render_views?
          !!metadata_for_rspec_rails[:render_views]
        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 source generated by RSpec.",
              template.identifier,
              template.handler,
              {
                :virtual_path => template.virtual_path,
                :format => template.formats
              }
            )
          end
        end
      end

      included do
        before do
          unless self.class.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 self.class.render_views?
            @controller.class.view_paths = @_path_set_delegator_resolver.path_set
          end
        end
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rspec-rails-2.0.0.beta.16 lib/rspec/rails/view_rendering.rb
rspec-rails-2.0.0.beta.15 lib/rspec/rails/view_rendering.rb
rspec-rails-2.0.0.beta.14.2 lib/rspec/rails/view_rendering.rb