Motivation / Background
---
While the `ActionView::TestCase` class isn't marked with a `:nodoc:`
comment to indicate that it's internal to Rails, there isn't much
content in the guides that explains how to test view partials.
Libraries like
[view_component](https://github.com/ViewComponent/view_component/) have
[built-in support for
testing](https://viewcomponent.org/guide/testing.html), including
Capybara integration.
While `ActionView::TestCase` already integrates with
`rails-dom-testing`, that integration could be better documented.
Additionally, it wouldn't take much for consuming applications to mimic
the ViewComponent testing experience for their Action View Partials.
Details
---
First, link to the "Testing Rails Applications" page from the
`ActionView::TestCase` class documentation.
Next, add a "Testing View Partials" section to the guides that expands
upon the variety of tooling available to tests that inherit from
`ActionView::TestCase`. In that section, cover topics like:
* the `render` helper method
* the `rendered` helper attribute reader
* calls to `assert_select` with attribute placeholders
* the `document_root_element` helper method
* integration with Ruby's Pattern Matching
* opportunities to integrate with Capybara
Additional Information
---
Additionally, add test coverage that exercise the examples shared in the
new section, including:
* Calls to `assert_select` that utilize attribute placeholders
* Ruby 3.0's Pattern Matching
* Integration with Capybara
By default `simple_format` method returns the text wrapped with `<p>`. But if we explicitly specify
the `wrapper_tag: nil` in the options, it returns the text wrapped with `<></>` tag.
Before:
```ruby
simple_format("Hello World", {}, { wrapper_tag: nil })
# <>Hello World</>
```
After:
```ruby
simple_format("Hello World", {}, { wrapper_tag: nil })
# <p>Hello World</p>
```
Co-authored-by: Junichi Ito <jit@sonicgarden.jp>
* Document the :ignore_date option on time_select
* split documentation comment into two lines
Co-authored-by: Rafael Mendonça França <rafael@franca.dev>
Before this would raise: `NoMethodError: undefined method `to_key' for nil:NilClass`
After it raises `ArumentError: dom_id must be passed a record_or_class as the first parameter, you passed 'nil'`
Rails 7 ships with Turbo enabled by default. Instead of using
`data-method` and `data-confirm`, Turbo now uses `data-turbo-method` and
`data-turbo-confirm` to perform a link visit with the specified HTTP
method and show confirmation dialog respective.
The deprecated legacy options are documented but the new options are
not.
This commit documents the `data-turbo-method` and `data-turbo-confirm`
for the `link_to` method.
The `button_to` documentation has also been updated to reference the
new `link_to` options.
Followup: https://github.com/rails/rails/pull/48645
Some template engines such as `jbuilder` use these Action View primitives
with types other than strings, which breaks a bunch of assumptions.
I wish I could add a test for this, but this is deep in private methods
I don't see a way to cover this.
Using `initializer` in the railtie ran the block before application
initializers. Instead, use `after_initialize` to ensure that
application initializers take effect properly.
Ref: https://github.com/rails/rails/issues/48611
Individual fragments have been cached as bare string since forever,
but somehow fragment cached via collection caching were stored
as `ActionView::OutputBuffer` instances.
This is both bad for performance, but also can cause issues on
Rails upgrades if the internal representation of `AV::OutputBuffer`
changes.
Closes https://github.com/rails/rails/issues/45483
Closes https://github.com/rails/rails/pull/45523
To quote #45483:
> `field_name` is adding an extra index parameter, the extra `[0]` in
> `parent[children_attributes][0][0][grandchildren_attributes][]`
To resolve that issue, this commit reads its default `field_id` and
`field_name` method's `index:` option directly from the `@options`.
Prior to this commit, that value was read from the `@index` instance
variable.
The FormOptionsHelper is only needed in classes where a <select> tag is rendered. It was previously included in the Tags::Base class which unnecessarily included it every child Tag class.
3 methods that originally lived in the Tags::Base class are only used by tags that output a <select> tag. These were extracted into a SelectRenderer module which is now only added to the tags that need it.
Fix: https://github.com/rails/rails/issues/48402
Some HTTP client and proxies have a 4kiB header limit, but more importantly
including preload links has diminishing returns so it's best to not go overboard
1kB worth of preload links should be plenty.
I noticed that during our application's boot we were spending more time
than desirable matching against this BLOCK_EXPR regex. This was slower
than necessary because even though this regex engine only cares about
the end of the string, Ruby's regex engine only works forward, and this
regex would match whitespace anywhere inside the string using two
ambiguous adjacent whitespace repetitions.
We don't expect untrusted code here (this is Ruby in our ERB which we
are going to execute), but with realistic strings this still causes
unnecessary backtracking.
This commit fixes the problem by removing the initial "\s*" (since this
is optional we don't need to capture it, we can just find the match
later if it exists) and replacing the following "\s+" with just "\s"
(similarly we don't care how many whitespace appear in a row, we can
just match later if there is more than one).
Testing against all the ERB expressions in our app this is 10x faster:
from 1.5 seconds to 0.1 seconds.
* new config value: action_view.sanitizer_vendor
* SanitizerHelper defaults to Rails::HTML4::Sanitizer
* 7.1 config defaults to Rails::HTML5::Sanitizer if it's supported
Text tokens may be conditionally appended to the output buffer. For
example if we have a template like this:
```erb
<h1>Oh no!</h1>
This template has a runtime error
<b>
<% if true %>
<%= method_that_does_not_exist %>
<% end %>
</b>
Yikes!
```
In the above case, a string literal (`" "`) will be conditionally
appended to the output buffer with some code like below:
```ruby
@output_buffer.safe_append=' '.freeze
```
This commit teaches text tokens (string literals) to scan forward in the
compiled template until it finds the literal, thereby skipping the code
generated for appending to the output buffer.
Related to:
* Bug: https://github.com/rails/rails/issues/48173
* PR: https://github.com/rails/rails/pull/48184
According to the HTML spec
found at https://html.spec.whatwg.org/multipage/embedded-content.html\#the-source-element,
> If the type attribute is not specified, the user agent will not select a different
> source element if it finds that it does not support the image format after fetching it.
It goes on to mention:
> When a source element has a following sibling source element or img
> element with a srcset attribute specified, it must have at least one of the following:
Where it lists a 'media' and 'type' attribute.
Follow-up to #40691.
The key argument of `I18n.translate` can be omitted, but this fact may
not be widely known. Therefore, to prevent confusion, specify an
explicit `nil` key when passing default values through `I18n.translate`
for interpolation.
This does not change any behavior, so no tests have been changed.
Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
Fix: https://github.com/rails/rails/issues/48173
If for some reason the column identification fails, we should
fallback to the old error rendering with only line information.
Assuming the overwhelming majority of the time `details[:formats]`
is all valid, we can first check the cache with the unmofidied `details`
and then only if we miss we filter the valid formats.
Fix: https://github.com/rails/rails/issues/48156
The assumption here is that in the overwhelming majority of
cases, all formats are valid.
So we first check if any of the formats is invalid before duping
the details hash and filtering them.
Additonally, by exposing a (non-public) `valid_symbols?` method, we
can check symbols are valid without resporting to `Array#%` which
would needlessly duplicate the `formats` array.