Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Resolver::PathParser #39361

Merged
merged 6 commits into from
May 27, 2020
Merged

Introduce Resolver::PathParser #39361

merged 6 commits into from
May 27, 2020

Conversation

jhawthorn
Copy link
Member

@jhawthorn jhawthorn commented May 20, 2020

The template resolver is supposed to determine the handler, format, and variant from the path in extract_handler_and_format_and_variant.

Previously this behaviour was close but didn't exactly match the behaviour of finding templates, and in some cases (particularly with handlers or formats missing) would return incorrect results. I've added previously failing test for where the variant was misdetected.

This commit introduces Resolver::PathParser, a class which should be able to accurately from any path inside a view directory be able to tell us exactly the prefix, partial, variant, locale, format, variant, and handler of that template.

This works by building a building a regexp from the known handlers and file types. This requires that any resolvers have their cache cleared when new handlers or types are registered (this was already somewhat the requirement, since resolver lookups are cached, but this makes it necessary in more situations).


Keen readers will note that there's now two different regexes being made in this file 😓. One for finding files and this new one. In a follow up PR #39362 I intend to replace the find logic to instead use this regex, which will require some extra filtering. This will be possible because we have deprecated .s from being part of the action name, making the template names unambiguously refer to these properties.

This could also allow us in the future to offer did_you_mean style suggestions on missing templates (I really love these changes we've been seeing), or even parsing the entire app/views directory at boot (or on the first render call) and warning the user about any template's extensions we don't understand.

Thanks for reading ❤️

We don't have jbuilder installed (this would work if it was).
The template resolver is supposed to determine the handler, format, and
variant from the path in extract_handler_and_format_and_variant.

This commit adds tests for how variants are parsed from the filenames
found in the resolver. It includes two skipped tests which currently fail.
The template resolver is supposed to determine the handler, format, and
variant from the path in extract_handler_and_format_and_variant.

Previously this behaviour was close but didn't exactly match the
behaviour of finding templates, and in some cases (particularly with
handlers or formats missing) would return incorrect results.

This commit introduces Resolver::PathParser, a class which should be
able to accurately from any path inside a view directory be able to tell
us exactly the prefix, partial, variant, locale, format, variant, and
handler of that template.

This works by building a building a regexp from the known handlers and
file types. This requires that any resolvers have their cache cleared
when new handlers or types are registered (this was already somewhat the
requirement, since resolver lookups are cached, but this makes it
necessary in more situations).
@rails-bot rails-bot bot added the actionview label May 20, 2020
@p8
Copy link
Member

p8 commented May 20, 2020

😄 I was trying to add DidYouMean for TemplateMissing errors but had a hard time finding suggestions.

Copy link
Contributor

@kaspth kaspth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️💪

details_cache_key already references Template::Types.symbols and view
resolvers cache based on default_formats and other values. This
previously wasn't an issue because no views had been looked up before
this was set. Now that we are building a regex from the values of
Template::Types.symbols we need to clear cache after changing this
setting.
@jhawthorn jhawthorn merged commit db543ba into rails:master May 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants