aboutsummaryrefslogtreecommitdiffstats
path: root/actionview/lib/action_view/template
Commit message (Collapse)AuthorAgeFilesLines
* Enable `Layout/EmptyLinesAroundAccessModifier` copRyuta Kamizono2019-06-133-6/+0
| | | | | | | | | | | We sometimes say "✂️ newline after `private`" in a code review (e.g. https://github.com/rails/rails/pull/18546#discussion_r23188776, https://github.com/rails/rails/pull/34832#discussion_r244847195). Now `Layout/EmptyLinesAroundAccessModifier` cop have new enforced style `EnforcedStyle: only_before` (https://github.com/rubocop-hq/rubocop/pull/7059). That cop and enforced style will reduce the our code review cost.
* Merge pull request #35959 from jhawthorn/unbound_templatesRafael França2019-04-151-12/+32
|\ | | | | De-duplicate templates, introduce UnboundTemplate
| * Avoid duplication using _find_allJohn Hawthorn2019-04-121-11/+7
| |
| * Support disabling cache for DigestorJohn Hawthorn2019-04-121-20/+35
| | | | | | | | | | | | This adds a bit of complexity, but is necessary for now to avoid holding extra copies of templates which are resolved from ActionView::Digestor after disabling cache on the lookup context.
| * De-dup Templates, introduce UnboundTemplateJohn Hawthorn2019-04-121-10/+19
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Previously it's possible to have multiple copies of the "same" Template. For example, if index.html.erb is found both the :en and :fr locale, it will return a different Template object for each. The same can happen with formats, variants, and handlers. This commit de-duplicates templates, there will now only be one template per file/virtual_path/locals tuple. We need to consider virtual_path because both `render "index"`, and `render "index.html"` can both find the same file but will have different virtual_paths. IMO this is rare and should be deprecated/removed, but it exists now so we need to consider it in order to cache correctly. This commit introduces a new UnboundTemplate class, which represents a template with unknown locals. Template objects can be built from it by using `#with_locals`. Currently, this is just a convenience around caching templates, but I hope it's a helpful concept that could have more utility in the future.
* | Squash `warning: instance variable @filename not initialized`utilum2019-04-121-1/+1
|/
* Remove FileTemplateJohn Hawthorn2019-04-041-1/+3
| | | | This is unnecessary now that we can just provide a file source
* Add ActionView::Template::Sources::FileJohn Hawthorn2019-04-042-0/+30
|
* Fix arity warning for template handlerslocalhostdotdev2019-04-041-3/+3
| | | | | | | | | | | | | | | | | | | | | | | Mainly to help with knowning which template is reponsible for the warning. handler.class # => Class handler.to_s # => Coffee::Rails::TemplateHandler Before: Change: >> Class#call(template) To: >> Class#call(template, source) After: Change: >> Coffee::Rails::TemplateHandler.call(template) To: >> Coffee::Rails::TemplateHandler.call(template, source)
* Merge pull request #35825 from jhawthorn/always_filter_view_pathsEileen M. Uchitelle2019-04-031-14/+15
|\ | | | | Make Resolver#find_all_anywhere equivalent to #find_all
| * Always reject files external to appJohn Hawthorn2019-04-031-14/+15
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Previously, when using `render file:`, it was possible to render files not only at an absolute path or relative to the current directory, but relative to ANY view paths. This was probably done for absolutely maximum compatibility when addressing CVE-2016-0752, but I think is unlikely to be used in practice. Tihs commit removes the ability to `render file:` with a path relative to a non-fallback view path. Make FallbackResolver.new private To ensure nobody is making FallbackResolvers other than "/" and "". Make reject_files_external_... no-op for fallbacks Because there are only two values used for path: "" and "/", and File.join("", "") == File.join("/", "") == "/", this method was only testing that the absolute paths started at "/" (which of course all do). This commit doesn't change any behaviour, but it makes it explicit that the FallbackFileSystemResolver works this way. Remove outside_app_allowed argument Deprecate find_all_anywhere This is now equivalent to find_all Remove outside_app argument Deprecate find_file for find Both LookupContext#find_file and PathSet#find_file are now equivalent to their respective #find methods.
* | [ci skip] Updated the doc after renaming Template::File -> Template::RawFile ↵Abhay Nikam2019-04-021-1/+1
| | | | | | | | in #35826
* | Rename File to RawFileCliff Pruitt2019-04-012-2/+2
|/
* Only clear template caches in dev after changes (#35629)John Hawthorn2019-04-011-0/+2
|
* Merge pull request #35688 from jhawthorn/render_file_rfcAaron Patterson2019-03-302-1/+29
|\ | | | | RFC: Introduce Template::File
| * Introduce Template::File as new render file:John Hawthorn2019-03-272-1/+29
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The previous behaviour of render file: was essentially the same as render template:, except that templates can be specified as an absolute path on the filesystem. This makes sense for historic reasons, but now render file: is almost exclusively used to render raw files (not .erb) like public/404.html. In addition to complicating the code in template/resolver.rb, I think the current behaviour is surprising to developers. This commit deprecates the existing "lookup a template from anywhere" behaviour and replaces it with "render this file exactly as it is on disk". Handlers will no longer be used (it will render the same as if the :raw handler was used), but formats (.html, .xml, etc) will still be detected (and will default to :plain). The existing render file: behaviour was the path through which Rails apps were vulnerable in the recent CVE-2019-5418. Although the vulnerability has been patched in a fully backwards-compatible way, I think it's a strong hint that we should drop the existing previously-vulnerable behaviour if it isn't a benefit to developers.
* | Fix annotated typoPrathamesh Sonpatki2019-03-291-2/+2
| |
* | Merge pull request #35308 from ↵Rafael França2019-03-281-0/+20
|\ \ | |/ |/| | | | | erose/better-error-reporting-for-syntax-errors-in-templates Display a more helpful error message when an ERB template has a Ruby syntax error.
| * Add handling and tests.Eli Rose2019-02-171-0/+20
| |
* | Deprecate custom patterns for PathResolverJohn Hawthorn2019-03-261-39/+11
| | | | | | | | | | | | | | | | | | | | | | | | | | Custom glob patterns tie the implementation (Using Dir.glob) to the API we provide. It also doesn't really work. extract_handler_and_format_and_variant expects the handler, format, and variant to be at the end of the template path, and in the same order as they are in the default pattern. This deprecates specifying a custom path for FileSystemResolver and removes the pattern argument of OptimizedFileSystemResolver#initialize, which does not work with a custom pattern.
* | Remove virtual_path from fallback templatesJohn Hawthorn2019-03-181-8/+16
| |
* | Merge pull request #35623 from jhawthorn/actionview_cacheAaron Patterson2019-03-151-37/+3
|\ \ | | | | | | Make Template::Resolver always cache
| * | Remove updated_at from TemplatesJohn Hawthorn2019-03-151-7/+1
| | |
| * | Make Template::Resolver always cacheJohn Hawthorn2019-03-151-30/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | All actionview caches are already cleared at the start of each request (when Resolver.caching is false) by PerExecutionDigestCacheExpiry, which calls LookupContext::DetailsKey.clear (which clears all caches). Because caches are always cleared per-request in dev, we shouldn't need this extra logic to compare mtimes and conditionally reload templates. This should make templates slightly faster in development (particularly multiple renders of the same template)
* | | Rename `ActionView::Base#run` to `#_run`Seb Jacobs2019-03-151-1/+1
|/ / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | There was a recent change by @tenderlove to Action view which introduced `ActionView::Base#run` [1]. We ran into an issue with our application because one of the core concepts in our domain model is a `Run` which is exposed in most of our views as a helper method, which now conflicts with this new method. Although this is a public method it is not really meant to be part of the public API. In order to discourage public use of this method and to reduce the chances of this method conflicting with helper methods we can prefix this method with an underscore, renaming this method to `_run`. [1] https://github.com/rails/rails/commit/c740ebdaf5
* | Remove query_format argument from resolverJohn Hawthorn2019-02-261-2/+2
| |
* | Create templates with format=nilJohn Hawthorn2019-02-261-2/+2
| |
* | Remove unused method / fix documentationAaron Patterson2019-02-261-6/+1
| |
* | Update actionview/lib/action_view/template/resolver.rbJohn Hawthorn2019-02-261-1/+1
| | | | | | Co-Authored-By: tenderlove <tenderlove@github.com>
* | Don't mutate `virtual_path`, remove `decorate`Aaron Patterson2019-02-251-14/+2
| | | | | | | | | | | | `virtual_path` is calculated in the constructor when the Template object is allocated. We don't actually need to set it in the `decorate` method. That means we can remove the decorate method all together.
* | Pass locals in to the template object on constructionAaron Patterson2019-02-251-8/+11
| | | | | | | | | | | | | | This commit ensures that locals are passed in to the template objects when they are constructed, then removes the `locals=` mutator on the template object. This means we don't need to mutate Template objects with locals in the `decorate` method.
* | Remove potential `variants` mutation in `decorate`Aaron Patterson2019-02-251-1/+0
| | | | | | | | | | | | | | | | | | | | Even if the template is constructed with a `nil` variant, the array it constructs will never be `empty?`: https://github.com/rails/rails/blob/56b030605b4d968077a4ddb96b4ab619e75fb999/actionview/lib/action_view/template.rb#L152 We get an array that is `[nil]`, which is not empty, so this conditional is never true.
* | Templates have one formatAaron Patterson2019-02-252-8/+19
| | | | | | | | | | | | | | Templates only have one format. Before this commit, templates would be constructed with a single element array that contained the format. This commit eliminates the single element array and just implements a `format` method. This saves one array allocation per template.
* | Dereference the format type before template constructionAaron Patterson2019-02-251-1/+1
| | | | | | | | | | The format should always be exactly one symbol. Now we don't need to check whether or not the format is a `Type` in the constructor.
* | Always pass a format to the ActionView::Template constructorAaron Patterson2019-02-251-5/+13
| | | | | | | | | | This means we can eliminate nil checks and remove some mutations from the `decorate` method.
* | Add a finalizer to inline templatesAaron Patterson2019-02-221-0/+22
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This commit adds a finalizer just to inline templates. We can't cache compilation of inline templates because it's possible that people could have render calls that look like this: ```ruby loop do render inline: "#{rand}" end ``` and we would cache every one of these different inline templates. That would cause a memory leak. OTOH, we don't need finalizers on regular templates because we can cache, control, and detect changes to the template source. Fixes: #35372
* | Pass the template format to the digestorAaron Patterson2019-02-151-1/+1
|/ | | | | | | | | | | | | | | This commit passes the template format to the digestor in order to come up with a key. Before this commit, the digestor would depend on the side effect of the template renderer setting the rendered_format on the lookup context. I would like to remove that mutation, so I've changed this to pass the template format in to the digestor. I've introduced a new instance variable that will be alive during a template render. When the template is being rendered, it pushes the current template on to a stack, setting `@current_template` to the template currently being rendered. When the cache helper asks the digestor for a key, it uses the format of the template currently on the stack.
* Turn lookup context in to a stack, push and pop if formats changeAaron Patterson2019-02-111-1/+1
| | | | | | | | | | | This commit keeps a stack of lookup contexts on the ActionView::Base instance. If a format is passed to render, we instantiate a new lookup context and push it on the stack, that way any child calls to "render" will use the same format information as the parent. This also isolates "sibling" calls to render (multiple calls to render in the same template). Fixes #35222 #34138
* Take in to account optional arguments when deprecatingAaron Patterson2019-02-041-1/+1
| | | | Refs: rails/jbuilder#452
* Pass source to template handler and deprecate old style handlerAaron Patterson2019-02-015-11/+37
| | | | | | | | | This commit passes the mutated source to the template handler as a parameter and deprecates the old handlers. Old handlers required that templates contain a reference to mutated source code, but we would like to make template objects "read only". This change lets the template remain "read only" while still giving template handlers access to the source code after mutations.
* Introduce a file type template, deprecate `Template#refresh`Aaron Patterson2019-02-011-2/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Every template that specifies a "virtual path" loses the template source when the template gets compiled: https://github.com/rails/rails/blob/eda0f574f129fcd5ad1fc58b55cb6d1db71ea95c/actionview/lib/action_view/template.rb#L275 The "refresh" method seems to think that the source code for a template can be recovered if there is a virtual path: https://github.com/rails/rails/blob/eda0f574f129fcd5ad1fc58b55cb6d1db71ea95c/actionview/lib/action_view/template.rb#L171-L188 Every call site that allocates a template object *and* provides a "virtual path" reads the template contents from the filesystem: https://github.com/rails/rails/blob/eda0f574f129fcd5ad1fc58b55cb6d1db71ea95c/actionview/lib/action_view/template/resolver.rb#L229-L231 Templates that are inline or literals don't provide a "virtual path": https://github.com/rails/rails/blob/eda0f574f129fcd5ad1fc58b55cb6d1db71ea95c/actionview/lib/action_view/renderer/template_renderer.rb#L34 This commit introduces a `FileTemplate` type that subclasses `Template`. The `FileTemplate` keeps a reference to the filename, and reads the source from the filesystem. This effectively makes the template source immutable. Other classes depended on the source to be mutated while being compiled, so this commit also introduces a temporary way to pass the mutated source to the ERB (or whatever) compiler. See `LegacyTemplate`. I think we should consider it an error to provide a virtual path on a non file type template an non-file templates can't recover their source. Here is an example: https://github.com/rails/rails/blob/eda0f574f129fcd5ad1fc58b55cb6d1db71ea95c/actionview/lib/action_view/testing/resolvers.rb#L53 This provides a "virtual path" so the source code (a string literal) is thrown away after compilation. Clearly we can't recover that string, so I think this should be an error.
* Tighten up the AV::Base constructorAaron Patterson2019-01-291-1/+1
| | | | | | | | | | | | | | | The AV::Base constructor was too complicated, and this commit tightens up the parameters it will take. At runtime, AV::Base is most commonly constructed here: https://github.com/rails/rails/blob/94d54fa4ab641a0ddeb173409cb41cc5becc02a9/actionview/lib/action_view/rendering.rb#L72-L74 This provides an AV::Renderer instance, a hash of assignments, and a controller instance. Since this is the common case for construction, we should remove logic from the constructor that handles other cases. This commit introduces special constructors for those other cases. Interestingly, most code paths that construct AV::Base "strangely" are tests.
* Pull buffer assignment upAaron Patterson2019-01-161-1/+1
| | | | | Since everything goes through a `run` method, we can pull the buffer assignment up.
* Always evaluate views against an ActionView::BaseAaron Patterson2019-01-161-4/+6
| | | | | Methods created by views should always be evaluated against an AV::Base instance. This way we can extract and refactor things in to classes.
* Pull output buffer conditional upAaron Patterson2019-01-161-1/+3
| | | | | | This pulls the "output buffer existence" conditional up. Instead of evaling the same conditional over and over, we can pull it in to "only compiled once" Ruby code.
* Module#{define_method,alias_method,undef_method,remove_method} become public ↵Ryuta Kamizono2018-12-211-2/+2
| | | | | | since Ruby 2.5 https://bugs.ruby-lang.org/issues/14133
* use match?pavel2018-12-121-1/+1
|
* Add `Style/RedundantFreeze` to remove redudant `.freeze`Yasuo Honda2018-09-291-2/+2
| | | | | | | | | | | | | | | | | | | | | Since Rails 6.0 will support Ruby 2.4.1 or higher `# frozen_string_literal: true` magic comment is enough to make string object frozen. This magic comment is enabled by `Style/FrozenStringLiteralComment` cop. * Exclude these files not to auto correct false positive `Regexp#freeze` - 'actionpack/lib/action_dispatch/journey/router/utils.rb' - 'activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb' It has been fixed by https://github.com/rubocop-hq/rubocop/pull/6333 Once the newer version of RuboCop released and available at Code Climate these exclude entries should be removed. * Replace `String#freeze` with `String#-@` manually if explicit frozen string objects are required - 'actionpack/test/controller/test_case_test.rb' - 'activemodel/test/cases/type/string_test.rb' - 'activesupport/lib/active_support/core_ext/string/strip.rb' - 'activesupport/test/core_ext/string_ext_test.rb' - 'railties/test/generators/actions_test.rb'
* Enable `Performance/UnfreezeString` copyuuji.yaginuma2018-09-231-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | In Ruby 2.3 or later, `String#+@` is available and `+@` is faster than `dup`. ```ruby # frozen_string_literal: true require "bundler/inline" gemfile(true) do source "https://rubygems.org" gem "benchmark-ips" end Benchmark.ips do |x| x.report('+@') { +"" } x.report('dup') { "".dup } x.compare! end ``` ``` $ ruby -v benchmark.rb ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux] Warming up -------------------------------------- +@ 282.289k i/100ms dup 187.638k i/100ms Calculating ------------------------------------- +@ 6.775M (± 3.6%) i/s - 33.875M in 5.006253s dup 3.320M (± 2.2%) i/s - 16.700M in 5.032125s Comparison: +@: 6775299.3 i/s dup: 3320400.7 i/s - 2.04x slower ```
* Fix for variants: :any special caseJohn Hawthorn2018-09-121-1/+3
|