Commit Graph

2203 Commits

Author SHA1 Message Date
Sean Doyle
a9148b45f3 Add "Testing View Partials" section to the Testing Guides
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
2023-09-13 09:44:44 -04:00
Rafael Mendonça França
699dfdb426
Preparing for 7.1.0.beta1 release 2023-09-13 00:36:01 +00:00
Akhil G Krishnan
1677e31240
Fix: simple_format with blank wrapper_tag option returns plain html tag.
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>
2023-09-05 10:09:40 +00:00
Gannon McGibbon
7ea19698b0 Fix form_for id generation for new CPK models
Adds tests for form_for use with composite primary key models. Fixes bug
related to new CPK model dom id generation.
2023-08-28 15:56:25 -05:00
Oscar Romero
9db860f1b4
Document the :ignore_date option on time_select (#49017)
* 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>
2023-08-23 15:41:53 -04:00
Jean Boussier
40d761c3e1
Merge pull request #48987 from Austio/better-dom-id-error-on-nil
Adds a specific ArgumentError when passing nil to dom_id.
2023-08-22 17:19:45 +02:00
Austin Story
6245c0048a Adds a specific ArgumentError when passing nil to dom_id. Which can happen if you do something like pass a non-existent ivar dom_id(@something_non_existant)
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'`
2023-08-22 17:16:25 +02:00
julianfssen
6a798d079a Add docs for data-turbo-method and data-turbo-confirm for link_to
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.
2023-08-16 19:09:17 +08:00
Jean Boussier
770060d93a Handle non-string partial body in ActionView::CollectionCaching
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.
2023-08-14 11:20:43 +02:00
Mike Dalessio
8a57ba8c0e
Update Action View and Dispatch to use Rails::Dom::Testing helpers
Use the helpers introduced in rails-dom-testing 2.2.0 instead of
managing the HTML parsers as was done in #48523.

See also related #47144 / ad79ed0e
2023-08-03 11:17:38 -04:00
Mike Dalessio
d316d690b9
Fix Action View sanitizer initialization
Using `initializer` in the railtie ran the block before application
initializers. Instead, use `after_initialize` to ensure that
application initializers take effect properly.
2023-07-17 11:20:40 -04:00
Mike Munroe
1304d39053 Remove RDoc auto-link from Template text 2023-07-14 10:18:31 -04:00
Guillermo Iguaran
a5fc471b3f
Merge pull request #48667 from igor-drozdov/fix-ruby-keywords-for-action-view-test-case
Add ruby2_keywords to ActionView::TestCase#method_missing
2023-07-06 14:24:49 -07:00
Guillermo Iguaran
d540abd31b
Add parentheses to ruby2_keywords method call
For consistency with all the other usages of the method in the code base.
2023-07-05 23:35:23 -07:00
Igor Drozdov
79cdc0c8f2
Add ruby2_keywords to ActionView::TestCase#method_missing
For example RSpec predicate methods rely on method_missing.
Without ruby2_keywords, matchers like be_something(kwarg: 1)
don't work
2023-07-05 19:21:12 +02:00
Jean Boussier
76deaf6362
Merge pull request #48645 from Shopify/action-view-bare-cache-fragments
Fix Action View collection caching to store fragments as bare strings
2023-07-05 12:14:23 +02:00
Jean Boussier
6842e766a1 Fix Action View collection caching to store fragments as bare strings
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.
2023-07-04 16:37:22 +02:00
Ricardo Trindade
adf80fe807
Update streaming_template_renderer.rb
Remove extra `layout_name` check. At that point in the time layout_time will be defined so there's no need to check for it once again.
2023-07-04 15:21:51 +02:00
Akira Matsuda
bceac79565
Merge branch 'main' into action-view-nested-field-name-calls 2023-07-03 06:15:43 +09:00
Guillermo Iguaran
8a158ed5e3
Merge pull request #48553 from ghiculescu/preload_links_header-invocation
Allow opting in/out of `Link preload` headers when calling `stylesheet_link_tag` or `javascript_include_tag`
2023-06-30 13:40:03 -07:00
Sean Doyle
5cb8678dd6 Don't double-encode nested field_id and field_name index
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.
2023-06-30 09:54:23 -04:00
Rafael Mendonça França
ba4169668b
Don't require an autoloaded constant 2023-06-26 04:18:47 +00:00
Rafael Mendonça França
847a27a3cb
Don't eager load SelectRenderer
This module is included in classes that are already eager loaded, so
we don't need to eager load it.
2023-06-26 04:18:10 +00:00
Rafael Mendonça França
51ec30a88d
Merge pull request #48574 from pinzonjulian/simplify_action_view_bases_helper_logic
Extract Select rendering methods into SelectRenderer module and simplify Action View base helper logic
2023-06-26 00:17:03 -04:00
pinzonjulian
77f3fc118a Move Form Options helper into select rendering classes
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.
2023-06-26 09:39:37 +10:00
pinzonjulian
f19f426c6a Simplify Tags::Base private method logic 2023-06-25 22:44:27 +10:00
pinzonjulian
9e85c2cd05 Extract Select rendering methods into SelectRenderer module
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.
2023-06-25 22:44:27 +10:00
zzak
dd89f600f7
🔗 Remove RDoc auto-link from Rails module everywhere 2023-06-23 10:49:30 +09:00
Alex
5b116c5216 Allow opting in/out of Link preload headers when calling stylesheet_link_tag or javascript_include_tag
Fixes https://github.com/rails/rails/issues/48517

```ruby
javascript_include_tag("http://example.com/all.js", preload_links_header: false)

stylesheet_link_tag("http://example.com/all.js", preload_links_header: true)
```
2023-06-22 12:15:22 +10:00
Mike Dalessio
2362f57cd8
Update Action View and Action Dispatch to use HTML5 when available 2023-06-19 16:34:33 -04:00
John Hawthorn
55c3066da3
Merge pull request #48414 from jhawthorn/faster_block_expr
Speed up ERB expression BLOCK_EXPR regex
2023-06-07 09:20:20 -07:00
Jean Boussier
facc260fbb Stop appending preload links once the head is already 1kB
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.
2023-06-07 11:18:45 +02:00
John Hawthorn
d8e11e9100 Speed up ERB expression BLOCK_EXPR regex
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.
2023-06-06 16:26:40 -07:00
Jean Boussier
a2f685c7c0 Cleanly fallback when failing to tokenize ERB templates
Fix: https://github.com/rails/rails/issues/48319
Followup: https://github.com/rails/rails/pull/48184
2023-06-01 11:35:31 +02:00
Andriichuk
ef7449c9fe Handle sanitize_options in simple_format helper 2023-05-31 21:11:40 +01:00
Mike Dalessio
ce43ac6088
Add support for HTML5 sanitizers
* 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
2023-05-28 14:00:35 -04:00
Mike Dalessio
18dd733c01
Add sanitizer_vendor accessor to ActionView SanitizeHelper 2023-05-28 13:54:32 -04:00
zzak
073269c9b9
Merge pull request #48288 from zzak/rdoc-fixed-width-namespaces
Use short-form fixed-width RDoc form for namespaces
2023-05-25 07:14:47 +09:00
zzak
e3c73fd183
Replace all occurrences of '<tt>(\w+::\w+)</tt>' with '+$1+'
E.g.:

* <tt>ActiveRecord::Base</tt> -> +ActiveRecord::Base+

Co-authored-by: Hartley McGuire <skipkayhil@gmail.com>
Co-authored-by: Petrik de Heus <petrik@deheus.net>
2023-05-25 06:52:32 +09:00
Aaron Patterson
5e354637c0
Scan forward until text tokens match
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
2023-05-24 13:01:13 -07:00
Jonathan Hefner
5efba526a4 Avoid unnecessary allocations in picture_tag
This avoids an Array allocation for `source_tags` and a Hash allocation
for `picture_options`.
2023-05-21 16:03:28 -05:00
Emmanuel Hayford
d42e426e53 Add 'type' attribute to 'source' element
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.
2023-05-20 18:30:14 -07:00
Aaron Patterson
e89664bc3a
Merge pull request #48184 from Shopify/action-view-error-handling
Properly fallback when ERB column can't be computed
2023-05-19 04:45:43 +09:00
Jonathan Hefner
1173d2ce04
Merge pull request #48220 from artemas/actionview_default_translation
Adds missing key argument for I18n.translate
2023-05-16 18:03:00 -05:00
Artem Pisarev
58c8a9328c Specify nil key when passing defaults through I18n
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>
2023-05-16 17:37:27 -05:00
sampatbadhe
77414bfac3 add missing closing bracket to picture_tag example 2023-05-13 13:50:21 +05:30
Guillermo Iguaran
a3adc6ce18
Merge pull request #48100 from jpbalarini/add-picture-tag-helper
Add a picture_tag helper
2023-05-11 19:27:35 -07:00
Jean Boussier
096c785c39 Properly fallback when ERB column can't be computed
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.
2023-05-10 10:42:11 +09:00
Jean Boussier
4c6bc6c908 Optimize ActionView::LookupContext::DetailsKey
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.
2023-05-08 10:49:33 +09:00
Jean Boussier
583afa1404 Optimize mime types validation in ActionView::LookupContext
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.
2023-05-08 10:34:55 +09:00