From e7c3eb11e05236c22475ff391303bcfb80cf4fbe Mon Sep 17 00:00:00 2001 From: Iago Pimenta Date: Thu, 24 Oct 2019 21:51:49 +0100 Subject: [PATCH] Use default order of PathResolver::EXTENSIONS for sort templates --- actionview/CHANGELOG.md | 5 +++++ .../lib/action_view/template/resolver.rb | 2 +- .../test/template/resolver_shared_tests.rb | 20 +++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/actionview/CHANGELOG.md b/actionview/CHANGELOG.md index 88475325ff..1673a87a59 100644 --- a/actionview/CHANGELOG.md +++ b/actionview/CHANGELOG.md @@ -1,3 +1,8 @@ +* `OptimizedFileSystemResolver` prefers template details in order of locale, + formats, variants, handlers. + + *Iago Pimenta* + * Added `class_names` helper to create a CSS class value with conditional classes. *Joel Hawksley*, *Aaron Patterson* diff --git a/actionview/lib/action_view/template/resolver.rb b/actionview/lib/action_view/template/resolver.rb index 2392cc2de9..2a5aaba883 100644 --- a/actionview/lib/action_view/template/resolver.rb +++ b/actionview/lib/action_view/template/resolver.rb @@ -335,7 +335,7 @@ def find_template_paths_from_details(path, details) # We can use the matches found by the regex and sort by their index in # details. match = filename.match(regex) - EXTENSIONS.keys.reverse.map do |ext| + EXTENSIONS.keys.map do |ext| if ext == :variants && details[ext] == :any match[ext].nil? ? 0 : 1 elsif match[ext].nil? diff --git a/actionview/test/template/resolver_shared_tests.rb b/actionview/test/template/resolver_shared_tests.rb index 004361cdb8..470628af11 100644 --- a/actionview/test/template/resolver_shared_tests.rb +++ b/actionview/test/template/resolver_shared_tests.rb @@ -146,6 +146,26 @@ def test_templates_with_optional_locale_shares_common_object assert_same en[0], fr[1] end + def test_templates_sort_by_formats_json_first + with_file "test/hello_world.html.erb", "Hello HTML!" + with_file "test/hello_world.json.jbuilder", "Hello JSON!" + + templates = resolver.find_all("hello_world", "test", false, locale: [], formats: [:json, :html], variants: :any, handlers: [:erb, :jbuilder]) + + assert_equal 2, templates.size + assert_equal "Hello JSON!", templates[0].source + end + + def test_templates_sort_by_formats_html_first + with_file "test/hello_world.html.erb", "Hello HTML!" + with_file "test/hello_world.json.jbuilder", "Hello JSON!" + + templates = resolver.find_all("hello_world", "test", false, locale: [], formats: [:html, :json], variants: :any, handlers: [:erb, :jbuilder]) + + assert_equal 2, templates.size + assert_equal "Hello HTML!", templates[0].source + end + def test_virtual_path_is_preserved_with_dot with_file "test/hello_world.html.erb", "Hello html!" -- GitLab