resolvers.rb 1.6 KB
Newer Older
1
require "action_view/template/resolver"
2

Y
Yehuda Katz and Carl Lerche 已提交
3
module ActionView #:nodoc:
4 5 6 7
  # Use FixtureResolver in your tests to simulate the presence of files on the
  # file system. This is used internally by Rails' own test suite, and is
  # useful for testing extensions that have no way of knowing what the file
  # system will look like at runtime.
8
  class FixtureResolver < PathResolver
J
José Valim 已提交
9 10
    attr_reader :hash

11
    def initialize(hash = {}, pattern = nil)
12
      super(pattern)
Y
Yehuda Katz + Carl Lerche 已提交
13 14
      @hash = hash
    end
15

A
artemave 已提交
16
    def to_s
17
      @hash.keys.join(", ")
A
artemave 已提交
18 19
    end

K
kenta-s 已提交
20 21 22
    private

      def query(path, exts, _, _)
23
        query = "".dup
K
kenta-s 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
        EXTENSIONS.each_key do |ext|
          query << "(" << exts[ext].map { |e| e && Regexp.escape(".#{e}") }.join("|") << "|)"
        end
        query = /^(#{Regexp.escape(path)})#{query}$/

        templates = []
        @hash.each do |_path, array|
          source, updated_at = array
          next unless query.match?(_path)
          handler, format, variant = extract_handler_and_format_and_variant(_path)
          templates << Template.new(source, _path, handler,
            virtual_path: path.virtual,
            format: format,
            variant: variant,
            updated_at: updated_at
          )
        end

        templates.sort_by { |t| -t.identifier.match(/^#{query}$/).captures.reject(&:blank?).size }
Y
Yehuda Katz + Carl Lerche 已提交
43
      end
Y
Yehuda Katz and Carl Lerche 已提交
44
  end
D
David Chelimsky 已提交
45

46
  class NullResolver < PathResolver
K
kenta-s 已提交
47 48
    def query(path, exts, _, _)
      handler, format, variant = extract_handler_and_format_and_variant(path)
49
      [ActionView::Template.new("Template generated by Null Resolver", path.virtual, handler, virtual_path: path.virtual, format: format, variant: variant)]
D
David Chelimsky 已提交
50 51
    end
  end
52
end