Commit Graph

3248 Commits

Author SHA1 Message Date
Jonathan Hefner 1743d7ad38 Point AV::Helpers::NumberHelper method docs to AS [ci-skip]
`ActionView::Helpers::NumberHelper` methods are thin wrappers over
`ActiveSupport::NumberHelper` methods, which are well documented.  Thus
this commit points `ActionView::Helpers::NumberHelper` method docs to
`ActiveSupport::NumberHelper` methods instead of duplicating those docs.
2024-01-06 18:07:26 -06:00
Sean Doyle 4117583e4b Document rendering `:renderable` and `#render_in`
Provide examples for rendering objects that respond to `render_in`. Also
highlight that the object can also define a `#format` method to control
how the rendered String should be treated.

Add test coverage for both Action View's and Action Pack's support for
`render` with `:renderable` options.
2024-01-06 17:57:02 -05:00
Ricardo Díaz de154095ed Replace usage of `Array#?` with `Array#intersect?` for efficiency
`Array#intersect?` was introduced in Ruby 3.1.0 and it's more efficient and
useful when the result of the intersection is not needed as the
following benchmarks show:

```
require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem "rails", path: "./"
  # If you want to test against edge Rails replace the previous line with this:
  # gem "rails", github: "rails/rails", branch: "main"
  gem "benchmark-ips"
end

require "active_support"

SCENARIOS = [
  [(1..100).to_a, (90..200).to_a],    # Case 1
  [("a".."m").to_a, ("j".."z").to_a], # Case 2
  [(1..100).to_a, (101..200).to_a],   # Case 3
]

SCENARIOS.each_with_index do |values, n|
  puts
  puts " Case #{n + 1} ".center(80, "=")
  puts
  Benchmark.ips do |x|
    x.report("Array#?") { !(values[0] & values[1]).empty? }
    x.report("Array#intersect?")      { values[0].intersect?(values[1]) }
    x.compare!
  end
end
```

Results:

```
==================================== Case 1 ====================================

ruby 3.2.2 (2023-03-30 revision e51014f9c0) [arm64-darwin21]
Warming up --------------------------------------
             Array#?    34.221k i/100ms
    Array#intersect?    62.035k i/100ms
Calculating -------------------------------------
             Array#?    343.119k (± 1.1%) i/s -      1.745M in   5.087078s
    Array#intersect?    615.394k (± 1.1%) i/s -      3.102M in   5.040838s

Comparison:
    Array#intersect?:   615393.7 i/s
             Array#?:   343119.4 i/s - 1.79x  slower

==================================== Case 2 ====================================

ruby 3.2.2 (2023-03-30 revision e51014f9c0) [arm64-darwin21]
Warming up --------------------------------------
             Array#?   103.256k i/100ms
    Array#intersect?   185.104k i/100ms
Calculating -------------------------------------
             Array#?      1.039M (± 1.3%) i/s -      5.266M in   5.066847s
    Array#intersect?      1.873M (± 1.6%) i/s -      9.440M in   5.041740s

Comparison:
    Array#intersect?:  1872932.7 i/s
             Array#?:  1039482.4 i/s - 1.80x  slower

==================================== Case 3 ====================================

ruby 3.2.2 (2023-03-30 revision e51014f9c0) [arm64-darwin21]
Warming up --------------------------------------
             Array#?    37.070k i/100ms
    Array#intersect?    41.438k i/100ms
Calculating -------------------------------------
             Array#?    370.902k (± 0.8%) i/s -      1.891M in   5.097584s
    Array#intersect?    409.902k (± 1.0%) i/s -      2.072M in   5.055185s

Comparison:
    Array#intersect?:   409901.8 i/s
             Array#?:   370902.3 i/s - 1.11x  slower
```
2024-01-05 15:15:30 -05:00
Rafael Mendonça França 8c4af05a38
Merge pull request #50591 from akhilgkrishnan/add-nonce-stylesheet-link-tag
Add the nonce: true option for stylesheet_link_tag helper
2024-01-05 10:29:13 -05:00
Rafael Mendonça França e7cf982700
Merge pull request #50588 from sato11/remove-actionview-renderer-render-template
Remove `ActionView::Renderer#render_template`
2024-01-05 10:06:20 -05:00
Jean Boussier 27140247c2 Cleanup `defined?` usage
Now that we dropped support for Ruby 2.7, we no longer
need to check if variables are defined before accessing them
to avoid the undefined variable warning.
2024-01-05 15:05:35 +01:00
Jean Boussier ef65e5fb32 Cleanup usage of ruby2_keywords
Now that we no longer support Ruby 2.7, many `ruby2_keyword` calls
can be eliminated.

The ones that are left could be eliminated but would end up substantially
slower or more compliacated so I left them for now.
2024-01-05 14:40:18 +01:00
Akhil G Krishnan 7fa6d15890 Add the nonce: true option for stylesheet_link_tag helper
This provides a shortcut for setting a Content Security Policy nonce on
a stylesheet_link_tag.

Co-authored-by: AJ Esler <ajesler@users.noreply.github.com>
2024-01-05 13:02:51 +05:30
Junichi Sato 2af3d84987
Remove `ActionView::Renderer#render_template`
This seems to be making a pair with `#render_partial` but in reality
it's no longer refered to from anywhere. Since it is marked with
nodoc, I propose to get rid of it.

As far as I can tell from my non-comprehensive research,
this method was introduced in the commit b735761,
became practically private in f984907 and unused in 1bc0a59.
2024-01-05 10:50:50 +09:00
Rafael Mendonça França 64c93600bd
Merge pull request #50584 from seanpdoyle/action-view-rendered-html-parser
Parse `ActionView::TestCase#rendered` as DocumentFragment
2024-01-04 17:38:53 -05:00
Petrik de Heus 0689b933c4
Merge pull request #50464 from vlado/round_mode_missing_mode
Document `round_mode` option in action view's number helper [ci skip]
2024-01-04 21:33:31 +01:00
Sean Doyle bec80373cd Parse `ActionView::TestCase#rendered` as DocumentFragment
To integrate with [rails-dom-testing][] and its selector assertions,
`ActionView::TestCase` [defines a `#document_root_element`
method][document_root_element] that parses the HTML into a fully valid
HTML document and returns the "root".

In the case of most Action View partials rendered with `render partial:
"..."`, the resulting document would be invalid, so its constituent
parts (its `<html>`, `<head>`, and `<body>` elements) are synthesized in
during the parsing process. This results in a document whose _contents_
are equivalent to the original HTML string, but whose structure is not.

To share a concrete example:

  ```ruby
  irb(main):002:0> rendered = "<h1>Hello world</h1><h2>Goodbye world</h2>"
  => "<h1>Hello world</h1><h2>Goodbye world</h2>"
  irb(main):003:0> root = Rails::Dom::Testing.html_document.parse(rendered).root
  =>
  #(Element:0x57080 {
  ...
  irb(main):004:0> rendered.to_s
  => "<h1>Hello world</h1><h2>Goodbye world</h2>"
  irb(main):005:0> root.to_s
  => "<html><head></head><body><h1>Hello world</h1><h2>Goodbye world</h2></body></html>"
  irb(main):006:0> rendered.to_s == root.to_s
  => false
  ```

Prior to this commit, the parsed HTML content returned from calling
`rendered.html` relied on the same mechanisms as
`#document_root_element`, and parsed the HTML fragment into a full
document, with a synthesized `<html>` element as its root. The
`rendered.html` value should reflect the content that was **rendered**
by the partial, and should not behave the same as
`#document_root_element`.

When the parsing class is changed from [Nokogiri::XML::Document][] to
[Nokogiri::XML::DocumentFragment][], the returned value reflects the
same **exact** content as what was rendered.

To elaborate on the previous example:

  ```ruby
  irb(main):007:0> fragment = Rails::Dom::Testing.html_document_fragment.parse(rendered)
  =>
  #(DocumentFragment:0x62ee4 {
  ...
  irb(main):008:0> fragment.to_s
  => "<h1>Hello world</h1><h2>Goodbye world</h2>"
  irb(main):009:0> rendered.to_s == fragment.to_s
  => true
  ```

This commit changes the default `rendered.html` behavior to rely on
`Nokogiri::XML::DocumentFragment` instead of `Nokogiri::XML::Document`.

[Nokogiri::XML::Document]: https://nokogiri.org/rdoc/Nokogiri/XML/Document.html
[Nokogiri::XML::DocumentFragment]: https://nokogiri.org/rdoc/Nokogiri/XML/DocumentFragment.html
[document_root_element]: https://github.com/rails/rails-dom-testing/blob/v2.2.0/lib/rails/dom/testing/assertions/selector_assertions.rb#L75
2024-01-04 14:49:51 -05:00
Sean Doyle cebbd1cf0a Rename `ActionView::TestCase::Behavior::{Content,RenderedViewContent}`
Closes #49818

Renames `ActionView::TestCase::Behavior::Content` to
`RenderedViewContent`, with the goal of making it more of an internal
implementation detail that's unlikely to collide with an
application-side `::Content` class.

The `RenderedView`-prefix mirrors the module's `RenderedViewsCollection`
class. Since the intention is to treat it as a private implementation
detail, `RenderedViewContent` is marked with `:nodoc:`.

Along with the rename, this commit also modifies the class inheritance,
replacing the `SimpleDelegator` superclass with `String`. [String.new][]
accepts a `String` positional argument in the same way as
`SimpleDelegator.new` accepts a delegate object positional argument.
Sharing the `String` superclass also makes it a good candidate for being
passed to [Capybara.string][] (and [Capybara::Node::Simple.new][]) like
the documentation suggests.

[Capybara.string]: https://github.com/teamcapybara/capybara/blob/3.39.2/lib/capybara.rb#L212-L242
[Capybara::Node::Simple.new]: https://github.com/teamcapybara/capybara/blob/3.39.2/lib/capybara/node/simple.rb#L23
[String.new]: https://ruby-doc.org/core/String.html#method-c-new
2024-01-04 14:35:28 -05:00
Rafael Mendonça França 4544b0d15b
Merge pull request #49943 from cjilbert504/handle-nil-form-model-object
Handle nil form_with model argument
2024-01-03 22:02:41 -05:00
Junichi Sato 994aeacddf
`#render_template_to_object` and `#render_partial_to_object` are private
Apparently it had not been clear what visibility they should assume
when they were initially created at https://github.com/rails/rails/pull/35265,
then a big refactoring took place at https://github.com/rails/rails/pull/38594
after which they remain intact.

So I think they can now become properly private as they are nodoc and
not refered to but from within the same class.
2024-01-04 10:50:08 +09:00
Rafael Mendonça França 8b4e92f4be
Point rubocop to ruby 3.1 2024-01-03 19:02:32 +00:00
Rafael Mendonça França 9d18dc8505
Remove all code to work with Ruby < 3.1 2024-01-03 19:02:31 +00:00
Rafael Mendonça França 664eb0dfc4
Merge pull request #50473 from seanpdoyle/action-text-content-pattern-matching
Delegate `ActionText::Content#deconstruct` to Nokogiri
2024-01-03 12:31:22 -05:00
David Heinemeier Hansson 8397eb24da
Remove rollup and test machinery for rails-ujs (#50535)
This leaves only the final compiled targets in place.
2024-01-02 16:49:36 +01:00
Hartley McGuire 90da071bb6
bundle update rubocop --conservative (#50515)
Also perform two autocorrects with `bundle exec rubocop -A`:

- fixes a new case of [`Style/RedundantReturn`][1]
- fixes a new case of [`Performance/StringInclude`][2]

[1]: 146b1c2e33
[2]: 3158bbb9f6

Co-authored-by: David Heinemeier Hansson <david@basecamp.com>
2024-01-02 12:49:36 +01:00
Jean Boussier 6ba2fdb2fe Bump the required Ruby version to 3.1.0
Until now, Rails only droped compatibility with older
rubies on new majors, but I propose to change this policy
because it causes us to either keep compatibility with long
EOLed rubies or to bump the Rails major more often, and to
drop multiple Ruby versions at once when we bump the major.

In my opinion it's a bad alignments of incentives. And we'd
be much better to just drop support in new minors whenever they
go EOL (so 3 years).

Also Ruby being an upstream dependency, it's not even
a semver violation AFAICT.

Since Rails 7.2 isn't planned before a few months, we
can already drop Ruby 3.0 as it will be EOL in March.
2023-12-31 08:54:03 +01:00
Sean Doyle 2e15010d56 Delegate `ActionText::Content#deconstruct` to Nokogiri
Since `ActionText::Content` wraps an `ActionText::Fragment`, and
`ActionText::Fragment` wraps a `Nokogiri::XML::DocumentFragment`, then
`ActionText::Content` should be able to rely on the newer Ruby pattern
matching introduced by [nokogiri@1.16.0][] (mainly the
[DocumentFragment#deconstruct][] method):

```ruby
content = ActionText::Content.new <<~HTML
  <h1>Hello, world</h1>

  <div>The body</div>
HTML

content => [h1, div]

assert_pattern { h1 => { content: "Hello, world" } }
assert_pattern { div => { content: "The body" } }
```

The implementation change relies on delegating from `Content` to
`Fragment`, and from `Fragment` to `DocumentFragment#elements` (to
deliberately exclude text nodes).

[nokogiri@1.16.0]: https://nokogiri.org/CHANGELOG.html?h=pattern
[DocumentFragment#deconstruct]: https://nokogiri.org/rdoc/Nokogiri/XML/DocumentFragment.html?h=deconstruct#method-i-deconstruct
2023-12-28 09:19:18 -05:00
Vlado Cingel 49a73b5ec1 Document `round_mode` option in action view's number helper
Round mode option was added in 7905bdfd8b
but it was documented in active supports number helper only. Action
views number helper is using active supports helper and accepts
round_mode option so it should be documented there also.

Examples are also unified and it should be more clear that they return
string and not a number.
[ci skip]
2023-12-27 21:31:28 +01:00
fatkodima f48bbff32c Expose `assert_queries_match` and `assert_no_queries_match` assertions 2023-12-21 01:30:16 +02:00
Jean Boussier 3881518c47
Merge pull request #50281 from p8/activerecord/assert-queries
Expose `assert_queries` and `assert_no_queries` assertions
2023-12-12 00:29:45 +01:00
Aaron Patterson 0915a3eed8
Merge pull request #49858 from skipkayhil/hm-dont-assign-internal-variables
Prevent assigning internal ivars to AV::Base
2023-12-11 09:06:05 -08:00
Petrik 8392c54e73 Expose `assert_queries` and `assert_no_queries` assertions
To assert the expected number of queries are made, Rails internally uses
`assert_queries` and `assert_no_queries`. These assertions can be
useful in applications as well.

By extracting these assertions to a module, the assertions can be
included where required.
These assertions are added to `ActiveSupport::TestCase` when
ActiveRecord is defined.

ActiveStorage, ActionView and ActionText are using this module now as
well, instead of duplicating the implementation.
The internal ActiveRecord::TestCase, used for testing ActiveRecord,
implements these assertions as well. However, these are slighlty more
advanced/complex and use the SQLCounter class. To keep things simple,
for now this implementation isn't used.
2023-12-11 12:31:16 +01:00
Jean Boussier be258503ac Module#delegate takes a new private `as` parameter
This is a continuation of https://github.com/rails/rails/pull/46875

The behavior of looking up the class method when `to: :class` is passed
is a bit error prone because it silently degrades.

By passing the expected owner of the delegated method, we can be more
strict, and also generate a delegator in a module rather than having
to do it at inclusion time.

I made this argument private API because we want it in Rails, but
I'm worried it might be a bit too sharp for public API. I can
be convinced otherwise though.
2023-12-08 15:09:59 +01:00
Collin Jilbert 12b093df63 change model arg default to false; raise if model arg is nil 2023-12-08 05:48:12 -06:00
Akhil G Krishnan 204d86b1a2 Improve documentation for highlight text helper
Update actionview/lib/action_view/helpers/text_helper.rb

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>

Update actionview/lib/action_view/helpers/text_helper.rb

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>

Update actionview/lib/action_view/helpers/text_helper.rb

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2023-12-04 01:03:19 +05:30
Sean Doyle 9de8f1a26a Action View Docs: `field_id` and `field_name` examples [ci skip]
Several code samples for `field_id` and `field_name` cite the use of
[text_field_tag][]-style helper. That usage is incorrect. The helper
interface that `field_id` and `field_name` mimic is the
[text_field][]-style helper, without the `_tag` suffix.

[text_field_tag]: https://edgeapi.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-text_field_tag
[text_field]: https://edgeapi.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-text_field
2023-12-02 09:04:12 -05:00
Jean Boussier 90daef6928
Merge pull request #50241 from seanpdoyle/fieldset-tag-alias
Alias `field_set_tag` helper to `fieldset_tag`
2023-12-02 10:23:24 +01:00
Sean Doyle 0803405fdb Batch define `FormBuilder` methods with `CodeGenerator`
Define the `ActionView::Helpers::FormBuilder` methods that wrap the
`@template` instance methods inside an
`ActiveSupport::CodeGenerator.batch` call so that the underlying `class`
extensions aren't invoked more than once.
2023-12-02 10:08:24 +01:00
Sean Doyle c5ae44d659 Alias `field_set_tag` helper to `fieldset_tag`
The [field_set_tag][] renders a `<fieldset>` element. At times, the
desire to render a `fieldset` results in calling `fieldset_tag`, only to
be surprised by a `NoMethodError`.

Originally, the method's name was `fieldset_tag` helper (defined in
[0e6c8e5][] in 2007), but was renamed in [73c7083][] (3 days later).

This commit aliases `field_set_tag` to `fieldset_tag` so that both are
available.

Additionally, defines the method so that it utilizes the [content_tag][]
so that it isn't responsible for manually managing the closing
(`</fieldset>`) of the opening `<fieldset>` tag.

[field_set_tag]: https://edgeapi.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-field_set_tag
[0e6c8e5]: 0e6c8e5f6c
[73c7083]: 73c7083651
[content_tag]: https://edgeapi.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-content_tag
2023-12-02 01:09:42 -05:00
Eileen M. Uchitelle d6197c5efc
Merge pull request #50159 from skipkayhil/hm-deprecate-void-content
Deprecate content for void elements in TagBuilder
2023-12-01 09:34:30 -05:00
Jonathan Hefner 52affa8357 Fix example output for truncate helper [ci-skip]
The previous output was incorrect for the default truncation length, and
it included unescaped quotes.
2023-11-30 11:19:37 -06:00
Akhil G Krishnan c5add29025 Improve documentation for truncate text helper
Update actionview/lib/action_view/helpers/text_helper.rb

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>

Update actionview/lib/action_view/helpers/text_helper.rb

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>

Update actionview/lib/action_view/helpers/text_helper.rb

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>

Update actionview/lib/action_view/helpers/text_helper.rb

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>

Update actionview/lib/action_view/helpers/text_helper.rb

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>

Update actionview/lib/action_view/helpers/text_helper.rb

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2023-11-30 10:51:11 +05:30
Hartley McGuire c1f3d6851e
Remove unused DeprecatedConstantAccessor include
deprecate_constant removed in 23344d4b8c
2023-11-26 12:38:21 -05:00
Akhil G Krishnan 4c9d56a5c4 Improve documentation for excerpt text helper
Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2023-11-25 11:16:28 +05:30
Jonathan Hefner df2961b3d7 Put Ruby code in ERB tags [ci-skip]
This allows the code to be properly syntax-highlighted.
2023-11-24 17:57:17 -06:00
Jonathan Hefner ed694bfe0a Remove unused variable in code example [ci-skip] 2023-11-24 17:52:33 -06:00
Jonathan Hefner 4bfa69a785 Revise TextHelper#concat code example [ci-skip]
This simplifies the code example, and avoids putting `%>` _inside_ an
ERB tag, which confuses the syntax highlighter.
2023-11-24 17:19:47 -06:00
Jonathan Hefner ca5c27ed63 Format inline code [ci-skip] 2023-11-24 16:47:20 -06:00
Jonathan Hefner 1d604a459b Quote example output strings [ci-skip]
This portrays the return values more accurately and, in some cases,
fixes syntax highlighting when using the upcoming version of SDoc
(thanks to its heuristic for detecting Ruby code vs HTML code).
2023-11-24 16:22:57 -06:00
Jonathan Hefner dc53bb9c27 Fix prose indentation [ci-skip]
Prior to this commit, the prose was formatted as a code example.
2023-11-24 16:13:03 -06:00
Hartley McGuire 343897980c
Deprecate content for void elements in TagBuilder
According to the [HTML5 Spec][1]

> Void elements can't have any contents (since there's no end tag, no
> content can be put between the start tag and the end tag).

Up to this point, the only special handling of void elements has been to
use ">" to close them instead of "/>" (which is optional but valid
according to the spec)

> Then, if the element is one of the void elements, ... , then there may
> be a single U+002F SOLIDUS character (/), ... . On void elements, it
> does not mark the start tag as self-closing but instead is unnecessary
> and has no effect of any kind.

This commit deprecates the ability to pass content (either through the
positional "content" parameter or a block) to a void element since it is
not valid according to the Spec. This has the benefit of both
encouraging more correct HTML generation, and also simplifying the
method definition of void elements once the deprecation has been
removed.

This commit additionally tweaks the signature of "void_tag_string"
slightly with two changes. The first change is renaming it to be
"self_closing_tag_string". This is more accurate than "void_tag_string"
because the definition of "void element" is more specific and has more
requirements than "self closing element". For example, tags in the SVG
namespace _can_ be self closing but the "/" at the end of the start tag
is _not_ optional because they are not void elements. The second change
to this method is swapping from a boolean "self_closing" parameter to a
string "tag_suffix" parameter. This enables the void element method
definition to specialize the tag_suffix (to just ">") without either
void elements or self closing elements having to pay the runtime cost of
the self_closing conditional since we know at method definition time
which suffix each type of tag should use.

[1]: https://html.spec.whatwg.org/multipage/syntax.html#void-elements
2023-11-24 11:42:57 -05:00
Hartley McGuire 28c4f6b598
Fix preposition in fields_for doc
Followup to 2a5e9bc85b
2023-11-22 09:07:17 -05:00
Jonathan Hefner 2191f20706 Fix word_wrap with empty string
Follow-up to #45948.

This fixes `word_wrap` to return an empty string instead of `nil` when
given an empty string.

Fixes #50067.
2023-11-15 16:22:02 -06:00
Jean Boussier 427898293d ActionView::Template fix computation of strict locals
Fix: https://github.com/rails/rails/pull/49782

There was a bug in how we computed the list of required keys which
wasn't caught by the test.
2023-11-14 13:14:59 +01:00
Collin Jilbert 2a5e9bc85b fix preposition in ActionView::Helpers::FormHelper comments 2023-11-09 10:35:34 -06:00
Collin Jilbert bf4057d57e remove duplicate test whos name doesn't match the tested method 2023-11-08 14:54:44 -06:00
Garen Torikian 94de154a23 Update documentation in asset_tag_helper.rb
Just a wee bit of visualization changes to make it more explicitly clear what is being passed (a quoted number)

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2023-11-07 16:42:59 -06:00
Jonathan Hefner 4dcd6ba8d3 Update .gitattributes for generated JavaScript [ci-skip]
This adds `linguist-generated` and `linguist-vendored` attributes where
appropriate to suppress the files in diffs and exclude the files from
the project's language stats on GitHub.

See https://github.com/github/linguist for more information.
2023-11-05 15:48:08 -06:00
Jonathan Hefner 859cab5607 Fail on error in JavascriptPackageTest for UJS
Prior to this commit, if `app/javascript/rails-ujs/index.js`
contained a syntax error, `JavascriptPackageTest` would still pass
because `system "yarn build"` would simply return `false` and the
compiled output would not change.  This commit adds `exception: true` to
the `system` call so that an error will be raised if `yarn build` fails.
2023-11-05 15:12:22 -06:00
Jonathan Hefner 916c419dad Fix formatting of AV::TestCase.register_parser doc [ci-skip]
This fixes a few issues with the formatting of the API documentation for
`ActionView::TestCase::Behavior::ClassMethods#register_parser`:

* Use `h4` headings instead of `h3` headings.  SDoc renders method
  signatures as `h3` headings, so subheadings such as "Examples" should
  use `h4`.

* Replace "Arguments" heading with "Parameters".  "Parameters" are
  elements of function's signature, whereas "arguments" are elements of
  a function call.  The API documentation for other methods follows this
  convention.

* Format parameters as term list.

* Fix indentation of pre-registered parser lists to render them as lists
  instead of as code.

* Miscellaneous rewording, reorganization, and additional monospace
  formatting.
2023-11-04 22:55:13 -05:00
Hartley McGuire 24213d6954
Prevent assigning internal ivars to AV::Base
Previously, both the `@rendered_format` and
`@marked_for_same_origin_verification` instance variables would be
assigned to instances of `ActionView::Base`, making them accessible in
view templates. However, these instance variables are really internal to
the controller and result in extra string allocations because the `@`
gets stripped and readded when going through the assignment.

This commit prefixes the variables with an underscore to help indicate
that they are internal, and then adds them to the list of
`_protected_ivars` to prevent assigning them when rendering templates.

Co-authored-by: Jean Boussier <jean.boussier@gmail.com>
Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2023-11-03 20:52:47 -04:00
Jonathan Hefner f15cf4b093 Format inline code [ci-skip] 2023-10-26 17:37:32 -05:00
Earlopain c6dcb11691
Handle negative numbers in NumberToHumanSizeConverter 2023-10-26 18:07:15 +02:00
Jean Boussier 110206a74b Ignore implicit locals if not declared by templates with strict locals
Fix: https://github.com/rails/rails/pull/49780
Fix: https://github.com/rails/rails/issues/49761

`CollectionRenderer` implictly inject `<name>_counter` and `<name>_iteration`
locals, but in the overwhelming majority of cases they aren't used.

So when the rendered template has strict locals we shouldn't require
these to be declared, and if they aren't we should simply not pass them.

Co-Authored-By: HolyWalley <yakau@hey.com>
2023-10-25 17:45:59 +02:00
Eileen M. Uchitelle 4c5c904a21
Merge pull request #49668 from skipkayhil/hm-fix-rails-ujs-start
Fix rails-ujs auto start() in bundled environments
2023-10-24 15:01:24 -04:00
Wojciech Wnętrzak e6a096968b
Fix code example in the field_name method 2023-10-18 15:20:57 +02:00
Hartley McGuire f761780899
Fix rails-ujs auto start() in bundled environments
An issue was recently raised that users were seeing an error when
upgrading `@rails/ujs` to 7.1.0+:

```
Uncaught Error: rails-ujs has already been loaded!
```

Generally this issue appears due to the difference in how rails-ujs is
expected to behave in a pure sprockets environment (js appending) vs a
bundled environment (webpack, esbuild, etc.). The sprockets environment
is supposed to call start() automatically, while the bundled environment
is supposed to require users to import and call start() themselves.

As part of the transition from coffeescript to javascript, a condition
was added that was intended to detect whether the current environment
was a bundler so that it would only call start() when in sprockets.
However, this condition is not working as expected, and I was able to
reproduce the error appearing when using rails-ujs with importmaps and
esbuild. Interestingly, the issue did not appear when using Webpack as a
bundler.

I believe the best fix here is to make the condition very explicit.
Since sprockets users should not be using the esm version of rails-ujs,
we can use the rollup replace plugin to explicitly opt the esm bundle
out of _ever_ calling start() automatically. This works because terser
will run after the replace plugin and remove the whole condition as dead
code (since it sees true == false).

Co-authored-by: Ryunosuke Sato <tricknotes.rs@gmail.com>
2023-10-17 20:47:12 -04:00
Nikita Vasilevsky 19f8ab2e7d
[Tests only] Enable `Minitest/AssertPredicate` rule 2023-10-13 19:26:47 +00:00
Jean Boussier 79a242dc54 Fix `capture` view helper for HAML and Slim
Ref: https://github.com/rails/rails/pull/47194#issuecomment-1760334146

They both give the buffer as return value of the capture block
which confuses the `capture` helper.

Ideally we wouldn't have to check for that, but it's
an acceptable tradeoff for backward compatibility.
2023-10-13 08:27:09 +02:00
Rafael Mendonça França fc8d9ed843
Merge pull request #49553 from tricknotes/update-rails-ujs-build
Updated `@rails/ujs` files to follow 8e3449908c
2023-10-11 22:44:41 +02:00
fatkodima b8829cabec Enable `Style/RedundantDoubleSplatHashBraces` rubocop cop 2023-10-11 14:55:00 +03:00
Ryunosuke Sato 7365897530 [Refactor] Remove unused from tests for `@rails/ujs` 2023-10-11 01:14:33 +09:00
Ryunosuke Sato 9753982ec9 Add test for compiled `@rails/ujs` 2023-10-11 00:39:09 +09:00
Ryunosuke Sato 8b18d44d17 Updated `@rails/ujs` files to follow 8e3449908c.
This change was built by `$ yarn build` in actionview.

This commit includes the following changes into build files.
- 8e3449908c
2023-10-11 00:39:09 +09:00
Akira Matsuda 8104683c65
"NoMethodError: undefined method `pluralize' for an instance of Symbol"
https://buildkite.com/rails/rails/builds/100525#018b10a0-9ed9-4c7b-b6d2-028ed05a0a04
2023-10-09 12:28:14 +09:00
Sean Doyle 67bb1b64b7 Define `TagBuilder` methods for void Elements
The `ActionView::Helpers::TagHelper::TagBuilder` class renders HTML
elements based on the methods invoked on it. For example, `tag.input`
will render an `<input>` element, while `tag.turbo_frame` will render a
`<turbo-frame>` element.

The magic of the class is rooted in its definition of `#method_missing`.

The current implementation bakes-in special treatment of void HTML
elements (for example: `<input>`) and self-closing SVG elements (for
example: `<use />`). Despite its ahead-of-time knowledge of these
element names, calls to corresponding methods `tag.input` and `tag.use`
still rely on `#method_missing`.

This has performance implications.

This commit defines a new `TagBuilder.define_void_element` class method
to dynamically define methods for each known element. Then, the class
invokes the method for each element name in `HTML_VOID_ELEMENTS` and
`SVG_SELF_CLOSING_ELEMENTS`.

Calls to `.define_void_element` and `.define_self_closing_element` makr
`HTML_VOID_ELEMENTS` and `SVG_SELF_CLOSING_ELEMENTS` unnecessary, so
this commit removes those constant definitions.

Additionally, this call removes calls to
`TagHelper.ensure_valid_html5_tag_name` from the `TagBuilder` defined
element methods, since they're known ahead of time to be valid HTML.
Instead, only call `ensure_valid_html5_tag_name` from within the
`method_missing` calls, since those are determined at runtime and are
uncontrolled.

By cutting out the reliance on method missing, calls to `TagBuilder`
methods (like `tag.input`) become comparable to calls to the `tag` view
helper with positional arguments (like `tag(:input)`).

```
❯ ruby bench.rb
Warming up --------------------------------------
                 tag    73.438k i/100ms
         tag_builder    79.910k i/100ms
Calculating -------------------------------------
                 tag    732.467k (± 0.9%) i/s -      3.672M in   5.013504s
         tag_builder    810.981k (± 0.8%) i/s -      4.075M in   5.025632s

Comparison:
         tag_builder:   810981.5 i/s
                 tag:   732467.4 i/s - 1.11x  (± 0.00) slower
```

The results were from the following benchmark rendering void (`<input>`)
elements:

```ruby
 # frozen_string_literal: true

require "bundler/setup"

require "action_view"
require "minitest/autorun"
require "benchmark/ips"

class Foo
  include ActionView::Helpers
end

helpers = Foo.new

Benchmark.ips do |x|
  x.report("tag") { helpers.tag("input", value: "foo") }
  x.report("tag_builder") { helpers.tag.input(value: "foo") }
  x.compare!
end
```

Similar to `tag.input` (a void HTML element), calls to `tag.div` are
become somewhat comparable to `tag("div")`:

```
❯ ruby bench.rb
Warming up --------------------------------------
         content_tag    59.548k i/100ms
         tag_builder    51.215k i/100ms
Calculating -------------------------------------
         content_tag    595.067k (± 0.5%) i/s -      2.977M in   5.003570s
         tag_builder    505.553k (± 2.2%) i/s -      2.561M in   5.067704s

Comparison:
         content_tag:   595067.5 i/s
         tag_builder:   505552.6 i/s - 1.18x  (± 0.00) slower
```

The following benchmarks were collected to compare `tag("turbo-frame")`
(an unknown custom HTML element) and `tag.turbo_frame`:

```
❯ ruby bench.rb
Warming up --------------------------------------
         content_tag    56.152k i/100ms
         tag_builder    49.207k i/100ms
Calculating -------------------------------------
         content_tag    561.134k (± 0.5%) i/s -      2.808M in   5.003567s
         tag_builder    491.178k (± 0.3%) i/s -      2.460M in   5.009140s

Comparison:
         content_tag:   561133.8 i/s
         tag_builder:   491177.7 i/s - 1.14x  (± 0.00) slower
```
2023-10-05 08:51:52 -04:00
Jean Boussier 02e679ba75 Get rid of the `jruby_skip` test helper
The last test calling it actually passes on latest
JRuby.
2023-10-02 13:01:44 +02:00
Rafael Mendonça França 77cdcb3993
Fix CHANGELOG 2023-10-01 21:27:28 +00:00
Kevin Newton d743a2b10f
Use Prism for parsing renders when available
Instead of ripper
2023-09-29 20:57:54 -04:00
Jean Boussier dfd086edf3 Fix RenderCallExtractor to be compatible with Ruby 3.3
Fix: https://github.com/rails/rails/issues/49128
Ref: https://github.com/ruby/ruby/commit/45cd011d73

We need to unwrap some nodes.
2023-09-28 14:35:01 +02:00
Akhil G Krishnan bb626ffca8 HTML tag validation added for tags
Added validation for HTML tag names in the `tag` and `content_tag` helper method. The `tag` and
`content_tag` method now checks that the provided tag name adheres to the HTML specification. If
an invalid HTML tag name is provided, the method raises an `ArgumentError` with an appropriate error
message.

Examples:

```ruby
content_tag("12p") # Starting with a number

content_tag("") # Empty tag name

tag("div/") # Contains a solidus

tag("image file") # Contains a space
```
2023-09-27 18:11:01 +05:30
Rafael Mendonça França fb6c6007d0
Development of Rails 7.2 starts now
🎉
2023-09-27 03:59:11 +00:00
Rafael Mendonça França e5386cb402
Preparing for 7.1.0.rc1 release 2023-09-27 03:08:31 +00:00
Rafael Mendonça França 7893e0a165
Merge pull request #49194 from seanpdoyle/action-view-test-case-encoder
Introduce `ActionView::TestCase.register_parser`
2023-09-26 22:30:25 -04:00
Sean Doyle 7badc42723
Introduce `ActionView::TestCase.register_parser`
Register a callable to decode rendered content for a given MIME type

Each registered decoder will also define a `#rendered.$MIME` helper
method, where `$MIME` corresponds to the value of the `mime` argument.

=== Arguments

`mime` - Symbol the MIME Type name for the rendered content
`callable` - Callable to decode the String. Accepts the String
                    value as its only argument
`block` - Block serves as the decoder when the
                 `callable` is omitted

By default, ActionView::TestCase defines a decoder for:

* :html - returns an instance of Nokogiri::XML::Node
* :json - returns an instance of ActiveSupport::HashWithIndifferentAccess

Each pre-registered decoder also defines a corresponding helper:

* :html - defines `rendered.html`
* :json - defines `rendered.json`

=== Examples

To parse the rendered content into RSS, register a call to `RSS::Parser.parse`:

```ruby
register_decoder :rss, -> rendered { RSS::Parser.parse(rendered) }

test "renders RSS" do
  article = Article.create!(title: "Hello, world")

  render formats: :rss, partial: article

  assert_equal "Hello, world", rendered.rss.items.last.title
end
```

To parse the rendered content into a Capybara::Simple::Node,
re-register an `:html` decoder with a call to
`Capybara.string`:

```ruby
register_decoder :html, -> rendered { Capybara.string(rendered) }

test "renders HTML" do
  article = Article.create!(title: "Hello, world")

  render partial: article

  rendered.html.assert_css "h1", text: "Hello, world"
end
```
2023-09-27 02:03:23 +00:00
Rafael Mendonça França acfa045405
Revert typography change in user facing errors
This change would force a lot of existing applications and libraries
to update their tests.

We included it in the beta to collect feedback from the community and
we had some comments about how negative this change would be.

Developers that care about the typography of their error messages
can easily change it in their applications using the translation
files, so there is no need to inflict pain in the upgrade process
by changing the default in the framework.

Revert "Merge PR #45463"

This reverts commit 9f60cd8dc7, reversing
changes made to 35d574dbfd.
2023-09-26 21:45:03 +00:00
Bhanu Bhakta Sigdel 72b3fe9f09 Update doc for `include_hidden` form form file_field.
Update `include_hidden: false` to `include_hidden: true`

Update the language to be more accurate.

Update actionview/lib/action_view/helpers/form_helper.rb

Co-authored-by: Hartley McGuire <skipkayhil@gmail.com>
2023-09-24 19:44:08 -06:00
Sean Doyle 8b77436dbc Add documentation for `locals:` and `local_assigns` [ci skip]
Expand the documentation for [local_assigns][] to mention potential
integrations with [Ruby 3.1's pattern matching
assignment][pattern-matching].

Expand the Action View Overview guides to describe calls to `render`
with `locals:` options, along with the existence of the `local_assigns`
method. Also outline potential integrations with Ruby 3.1's pattern
matching assignment.

[local_assigns]: https://api.rubyonrails.org/classes/ActionView/Template.html#method-i-local_assigns
[pattern-matching]: https://docs.ruby-lang.org/en/master/syntax/pattern_matching_rdoc.html
2023-09-18 08:31:03 -04:00
Sean Doyle 32faee0e52 Action View: docs use `application/` instead of `shared/`
Change mentions of `app/views/shared` in the guides to be
`app/views/application` instead. View partials rely on the same
[Template Inheritance][] as their template counterparts, so the guides
should encourage end-users to benefit from that inheritance.

> This makes `app/views/application/` a great place for your shared
> partials, which can then be rendered in your ERB as such:
>

```html+erb
<%# app/views/admin/products/index.html.erb %>
<%= render @products || "empty_list" %>

<%# app/views/application/_empty_list.html.erb %>
There are no items in this list <em>yet</em>.
```

To enforce that template resolution, this commit also replaces
references to `shared/` with `application/` in the Rails test suite.

[Template Inheritance]: https://guides.rubyonrails.org/layouts_and_rendering.html#template-inheritance
2023-09-15 12:06:22 -04:00
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
Shouichi Kamiya 51ac8b9f6f Enable Minitest/LiteralAsActualArgument
There are assertions that expected/actual arguments are passed in the
reversed order by mistake. Enabling the LiteralAsActualArgument rule
prevents this mistake from happening.

The existing tests were auto-corrected by rubocop with a bit of
indentation adjustment.

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2023-09-13 10:09:32 +09:00
Rafael Mendonça França 699dfdb426
Preparing for 7.1.0.beta1 release 2023-09-13 00:36:01 +00:00
Jason Meller 274bc97d63
Add Bun support (#49241)
* Add Bun support to `rails new -j` generator

* Add additional generation consideration for Bun

* Use development gems to test the whole workflow

* Remove custom gems from local testing

* Revert lock

* Revert errant custom gem declaration

* Fix linting errors

* Fix remnants of bad merge

* Always use latest bun

* Update actioncable/lib/rails/generators/channel/channel_generator.rb

Co-authored-by: Cadu Ribeiro <mail@cadu.dev>

* Update guides/source/working_with_javascript_in_rails.md

Co-authored-by: Rafael Mendonça França <rafael@franca.dev>

* Only use the latest bun if nothing is specified

* Hardcode known good version

---------

Co-authored-by: Cadu Ribeiro <mail@cadu.dev>
Co-authored-by: Rafael Mendonça França <rafael@franca.dev>
2023-09-12 16:55:27 -04:00
Alex Ghiculescu ff6881d2b2
Remove old `raise_on_missing_translations` behaviour
ref: https://github.com/rails/rails/pull/47105#issuecomment-1400843060

Removes the old `raise_on_missing_translations` accessors, that used to live [here](fee61e3abc/actionpack/lib/abstract_controller/translation.rb (L7)) and [here](5c835bd669/actionview/lib/action_view/helpers/translation_helper.rb (L15)).

Closes https://github.com/rails/rails/pull/45361
2023-09-09 19:59:49 +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 a7cc807cb9 Fix to_param parameter generation for partial composite keys
Adds tests for url_for use with composite primary key models. Fixes bug
related to new CPK model to_param generation.
2023-08-29 14:04:03 -05: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
Rafael Mendonça França ed5af00459
Merge pull request #48998 from Shopify/to_key-supports-composite-primary-key
Support composite identifiers in `to_key`
2023-08-22 13:53:19 -04:00
Nikita Vasilevsky 8a5cf4cf44
Support composite identifiers in `to_key`
This commit adds support for composite identifiers in `to_key`.
Rails 7.1 adds support for composite primary key which means that
composite primary key models' `#id` method returns an `Array` and
`to_key` needs to avoid double-wrapping the value.
2023-08-22 16:13:23 +00: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