From 92af4aba25057850a0d80cf5c1a3d1eb4e2576b9 Mon Sep 17 00:00:00 2001 From: Fumiaki MATSUSHIMA Date: Sat, 29 Jun 2019 23:38:05 +0900 Subject: Support :any variants for FixtureResolver --- actionview/lib/action_view/testing/resolvers.rb | 38 ++++++++++++++++--------- 1 file changed, 25 insertions(+), 13 deletions(-) (limited to 'actionview/lib/action_view') diff --git a/actionview/lib/action_view/testing/resolvers.rb b/actionview/lib/action_view/testing/resolvers.rb index 539bedcdf0..03eac29bb4 100644 --- a/actionview/lib/action_view/testing/resolvers.rb +++ b/actionview/lib/action_view/testing/resolvers.rb @@ -7,10 +7,15 @@ module ActionView #:nodoc: # 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. - class FixtureResolver < PathResolver + class FixtureResolver < OptimizedFileSystemResolver def initialize(hash = {}, pattern = nil) - super(pattern) + super("") + if pattern + ActiveSupport::Deprecation.warn "Specifying a custom path for #{self.class} is deprecated. Implement a custom Resolver subclass instead." + @pattern = pattern + end @hash = hash + @path = "" end def data @@ -23,25 +28,32 @@ module ActionView #:nodoc: private def query(path, exts, _, locals, cache:) - query = +"" - EXTENSIONS.each do |ext, prefix| - query << "(" << exts[ext].map { |e| e && Regexp.escape("#{prefix}#{e}") }.join("|") << "|)" - end - query = /^(#{Regexp.escape(path)})#{query}$/ + regex = build_regex(path, exts) - templates = [] - @hash.each do |_path, source| - next unless query.match?(_path) + @hash.select do |_path, _| + ("/" + _path).match?(regex) + end.map do |_path, source| handler, format, variant = extract_handler_and_format_and_variant(_path) - templates << Template.new(source, _path, handler, + + Template.new(source, _path, handler, virtual_path: path.virtual, format: format, variant: variant, locals: locals ) + end.sort_by do |t| + match = ("/" + t.identifier).match(regex) + EXTENSIONS.keys.reverse.map do |ext| + if ext == :variants && exts[ext] == :any + match[ext].nil? ? 0 : 1 + elsif match[ext].nil? + exts[ext].length + else + found = match[ext].to_sym + exts[ext].index(found) + end + end end - - templates.sort_by { |t| -t.identifier.match(/^#{query}$/).captures.reject(&:blank?).size } end end -- cgit v1.2.3