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 12
    def initialize(hash = {}, pattern=nil)
      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 20
    end

  private
21

22
    def query(path, exts, formats, _)
23
      query = ""
Ł
Łukasz Strzałkowski 已提交
24
      EXTENSIONS.each_key do |ext|
25
        query << "(" << exts[ext].map { |e| e && Regexp.escape(".#{e}") }.join("|") << "|)"
Y
Yehuda Katz + Carl Lerche 已提交
26
      end
27
      query = /^(#{Regexp.escape(path)})#{query}$/
Y
Yehuda Katz + Carl Lerche 已提交
28

29
      templates = []
J
José Valim 已提交
30 31
      @hash.each do |_path, array|
        source, updated_at = array
32
        next unless query.match?(_path)
33
        handler, format, variant = extract_handler_and_format_and_variant(_path, formats)
34
        templates << Template.new(source, _path, handler,
35 36 37 38
          virtual_path: path.virtual,
          format: format,
          variant: variant,
          updated_at: updated_at
39
        )
Y
Yehuda Katz + Carl Lerche 已提交
40
      end
G
Guillermo Iguaran 已提交
41

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

46
  class NullResolver < PathResolver
47
    def query(path, exts, formats, _)
48
      handler, format, variant = extract_handler_and_format_and_variant(path, formats)
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