Commit Graph

2668 Commits

Author SHA1 Message Date
John Hawthorn
f8f9a085cc Encapsulate "details" into TemplateDetails
When dealing with the "details" for a template: locale, format,
variant, and handler, previously we would store these in an ad-hoc way
every place we did. Often as a hash or as separate instance variables on
a class.

This PR attempts to simplify this by encapsulating known details on a
template in a new ActionView::TemplateDetails class, and requested
details in ActionView::TemplateDetails::Requested.

This allowed extracting and simplifying filtering and sorting logic from
the Resolver class as well as extracting default format logic from
UnboundTemplate.

As well as reducing complexity, in the future this should make it
possible to provide suggestions on missing template errors due to
mismatched details, and might allow improved performance.

At least for now these new classes are private (:nodoc)

Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
2021-05-11 18:48:24 -07:00
Ryuta Kamizono
f55010a179 select! and reject! doesn't return self unless any elements are matched
https://docs.ruby-lang.org/en/3.0.0/Array.html#method-i-select-21
https://docs.ruby-lang.org/en/3.0.0/Array.html#method-i-reject-21
2021-05-12 06:49:49 +09:00
Ryuta Kamizono
b77ae45d29 Add missing slash to test_parse_root_partial_with_slash 2021-05-12 06:24:08 +09:00
Ryuta Kamizono
6c864fa725 Remove duplicated test_parse_partial
https://buildkite.com/rails/rails/builds/77358#27d13623-5ea6-4db8-a16c-2a2ceb6a0caa/977-979
2021-05-12 06:17:43 +09:00
John Hawthorn
88b18d7f7e
Merge pull request #42195 from jhawthorn/template_path
Extract ActionView::Resolver::Path into ActionView::TemplatePath
2021-05-11 13:14:01 -07:00
John Hawthorn
f441e433d4 Appease rubocop
Co-authored-by: Ryuta Kamizono <kamipo@gmail.com>
2021-05-11 10:15:24 -07:00
John Hawthorn
8d01462c85
Merge pull request #42194 from jhawthorn/optimize_normalize_name
Optimize normalize_name
2021-05-11 10:11:40 -07:00
Jean Boussier
a939c53990 Avoid sending preload links for data: URLs 2021-05-11 09:29:40 +02:00
John Hawthorn
ddb1ec2647 Improve docs for TemplatePath 2021-05-10 22:26:14 -07:00
John Hawthorn
54f8d0b269 Use TemplatePath in Digestor 2021-05-10 22:26:14 -07:00
John Hawthorn
ef2cb32090 Return TemplatePaths from all_template_paths 2021-05-10 22:26:13 -07:00
John Hawthorn
b6b1b6b27f Implement TemplatePath.parse 2021-05-10 22:26:13 -07:00
John Hawthorn
9ec086311a Extract ActionView::TemplatePath 2021-05-10 22:26:13 -07:00
John Hawthorn
5955a0a3f3 Optimize normalize_name
This is called on every render or template lookup.

    lookup_context = ActionController::Base.new.lookup_context
    normalize = lookup_context.method(:normalize_name)

    Benchmark.ips do |x|
      x.report "already normalized" do
        normalize.call("show", ["users"])
      end

      x.report "without prefixes" do
        normalize.call("users/show", [])
      end

      x.report "with prefixes in both arguments" do
        normalize.call("extra/show", ["users", "application"])
      end
    end

Before:

      already normalized      2.322M (± 0.5%) i/s -     11.615M in   5.003512s
        without prefixes      1.613M (± 0.7%) i/s -      8.076M in   5.007165s
    with prefixes in both arguments
                              1.020M (± 0.7%) i/s -      5.154M in   5.054101s

After:

      already normalized      3.740M (± 0.3%) i/s -     18.715M in   5.003477s
        without prefixes      2.197M (± 0.6%) i/s -     11.089M in   5.047800s
    with prefixes in both arguments
                              1.244M (± 0.9%) i/s -      6.246M in   5.020968s
2021-05-10 16:13:37 -07:00
Étienne Barrié
f55f923a5f Restore implicit to_s for content_for and provide 2021-05-07 12:14:39 -04:00
Aaron Patterson
908d7bc1bf
Merge branch 'main-sec'
* main-sec:
  Prevent slow regex when parsing host authorization header
  Escape allow list hosts correctly
  Prevent string polymorphic route arguments
  Prevent catastrophic backtracking during mime parsing
2021-05-05 09:03:28 -07:00
John Bampton
90b8ad5256 Fix spelling in CoffeeScript and JavaScript files [ci skip] 2021-05-05 11:08:33 +10:00
Zachary Scott
869532ccea
Merge pull request #41276 from RobinDaugherty/form_with-local-doc
Improve documentation of FormHelper#form_with `local` option [ci skip]
2021-05-05 07:16:32 +09:00
Gannon McGibbon
c4c21a9f8d
Prevent string polymorphic route arguments
url_for supports building polymorphic URLs via an array
of arguments (usually symbols and records). If an array is passed,
strings can result in unwanted route helper calls.

CVE-2021-22885
2021-05-04 13:56:37 -07:00
John Bampton
6e85b6b86e Add spell checking with codespell as a GitHub Action
`codespell` works with a small custom dictionary and seems to find perhaps more spelling mistakes than `misspell` which really only fixes commonly misspelled English words.

Not all spell checkers can check all file types and most spell checkers can't find all the errors.

https://github.com/codespell-project/codespell
https://pypi.org/project/codespell/
2021-05-04 14:46:21 +10:00
John Bampton
6d649531b9 Fix case of GitHub, JavaScript, TypeScript [ci skip] 2021-05-04 02:02:42 +10:00
Jean Boussier
147f207a57 Deprecate implicitly coercing objects to string in ActiveSupport::SafeBuffer 2021-05-03 14:16:03 +02:00
Jean Boussier
08e3e663ca
Merge pull request #42056 from Shopify/split-link-headers
Make sure not to generate Link headers longer than 8kiB
2021-05-01 17:43:58 +02:00
Jonathan Hefner
dd884eb425 Pass default values for translate through I18n
A default value can be a string that needs interpolation, Hash that
needs resolution via the `:count` option, or a Proc that needs
evaluation.  Therefore, pass default values through `I18n.translate` to
handle these cases.

Fixes #26032.
Fixes #41277.
Fixes #41380.
2021-04-30 09:25:41 -05:00
Abhay Nikam
2005afbe04 Adds option extname to stylesheet_link_tag to exclude automatically appended .css extension.
Fixes: #41918

This PR adds the option `extname` to `stylesheet_link_tag`.
Previously, `stylesheet_link_tag` automatically added `.css`.
2021-04-29 11:20:14 +05:30
John Hawthorn
e2781c2d93 Use filter_map for template_glob 2021-04-28 16:05:32 -07:00
John Hawthorn
7faa50cb32 Extract and optimize virtual path building
Before:  build      1.759M (± 0.4%) i/s -      8.855M in   5.034065s
After:   build      2.700M (± 0.8%) i/s -     13.584M in   5.031691s
2021-04-28 15:14:49 -07:00
Jean Boussier
2cb25ab35d Make sure not to generate Link headers longer than 8kiB 2021-04-27 09:41:46 +02:00
Hartley McGuire
a3e5e8d909 use ruby 2.7's filter_map instead of select + map
Related: c3d7794, bbbc861
2021-04-26 17:55:33 -04:00
Ryuta Kamizono
bbbc861f71 Enable Performance/MapCompact cop
Follow up to #42053.
2021-04-23 16:33:02 +09:00
Matheus Richard
c3d7794f16 Replace map + compact with filter_map 2021-04-22 22:08:34 -03:00
Ricardo Díaz
3af1551588 Remove unused constant FileSystemResolver::EXTENSIONS
Following up #42004
2021-04-21 12:39:41 -05:00
John Hawthorn
9e0c42b0bd Perform details matching on UnboundTemplates
In old versions of Rails, we would rely entirely on what was returned by
Dir.glob to determine the match and sorting of our templates.

Later we switched to building a regex on each search, which allowed us
to perform a much faster glob, find matching templates with the regex,
and then emulate the sort order based on captures from the regex.

Now we have PathParser, which can parse any template's details
accurately from just its filename (not depending on the query being
made).

This commit moves the matching to done on UnboundTemplates, effectively
using details found by the PathParser for both matching and sorting of
templates, and removing the dynamic regex for queries.

This should be faster at boot/after reloads as we're no longer building
a regex and additionally we only need to parse a template's path for
details one time (we can use the same details for matching/sorting in
future queries with different details).
2021-04-20 14:35:40 -07:00
John Hawthorn
bf9edcb8a3 Determine virtual path from file path
Previously, it was possible for these not to match due to providing
templates with .'s or using fallback templates.

There is now an exact 1:1 between templates on disk and virtual path.
2021-04-20 13:48:15 -07:00
John Hawthorn
2be8d3ebf8 Store details on unbound template
Previously we just stored handler, format, and variant and assigned a
default format if none existed.

Now we want to also store locale, and move the default format behaviour
into unbound template.
2021-04-20 13:48:12 -07:00
John Hawthorn
8944bef657 Add TemplateDetails 2021-04-20 13:47:48 -07:00
John Hawthorn
a7048ac9e2 Perform template glob and checks at same time
Previously we would check that our paths were safe and inside the app
right before building templates. Instead we can do this, and reject
directories at the same time as we perform the glob.

Co-authored-by: Kasper Timm Hansen <kaspth@gmail.com>
2021-04-19 16:40:35 -07:00
John Hawthorn
70470d7eed
Merge pull request #41998 from jhawthorn/template_cache_clear_lock
Don't clear view cache during concurrent requests
2021-04-19 15:46:35 -07:00
Adam Hess
f80038ae7d Improve did_you_mean partial name correction
Before this change did_you_mean shows partial paths like `animals/_partial`,
but adding that to your render call in a view like `<%= render 'animals/_partial' %>`
 will still be missing as rails will search for the template `animals/__partial`.
We can provide the user a easier copy/paste correction if we don't tell them about the underscore.

This also reduces the candidates to only return partials if you are
looking for a partial and only look for non-templates when you are not
looking for a template.
2021-04-19 15:01:00 -07:00
John Hawthorn
9a4c1e205e Don't clear view cache during concurrent requests
This updates ActionView::CacheExpiry to hold a lock while inside the
executor (ie. inside a request) and to only clear caches when that is
done.

This is done using Concurrent::ReadWriteLock. This allows any number
of parallel requests to hold the read lock, but once we detect a change
and begin to acquire the write lock, all future requests will be
blocked.
2021-04-19 09:36:38 -07:00
Zachary Scott
0d91d4aa07
Merge pull request #41984 from jbampton/fix-case-of-css-and-javascript
chore: fix case of CSS and JavaScript
2021-04-16 14:49:18 +09:00
John Hawthorn
33e054edbc Convert Template::Types.type_klass to instance ivar 2021-04-15 18:05:07 -07:00
John Bampton
eef63e05fb chore: fix case of CSS and JavaScript 2021-04-15 21:52:58 +10:00
John Bampton
de3b207aa6 chore: fix grammar and spelling 2021-04-15 16:57:01 +10:00
John Hawthorn
c70bd1345d Remove autoloads for removed classes 2021-04-14 11:27:12 -07:00
John Hawthorn
a2b1ea3e9d Remove FileSystemResolver's PATTERN
This is no longer used
2021-04-14 11:27:11 -07:00
John Hawthorn
a1de5ed3ed Merge FileSystemResolver and PathResolver 2021-04-14 11:27:11 -07:00
John Hawthorn
5789e827d0 Remove overridden methods from PathResolver 2021-04-14 11:27:11 -07:00
John Hawthorn
faac734387 Merge {Optimized,}FileSystemResolver 2021-04-14 11:27:11 -07:00
John Hawthorn
e144f098f1 Reimplement NullResolver on base resolver 2021-04-14 11:27:11 -07:00