Commit Graph

2877 Commits

Author SHA1 Message Date
Sean Doyle
8a0bc4aa90 Support calls to #field_name with nil object_name
It's possible for `ActionView::Helpers::FormTagHelper#field_name` calls
made by instances constructed through `fields` and `fields_for` helpers
to have an `object_name` argument that's `nil`. For example, the
following will raise an `undefined method `empty?' for nil:NilClass`
exception:

```erb
<%= fields do |f| %>
  <%= f.field_name :body %>
<% end %>
```

To guard against those calls, replace the method's call to
`String#empty?` with `Object#blank?`, since `NilClass#empty?` is not
defined.
2022-06-15 18:51:20 +01:00
eileencodes
935120fdfd
Fix did you mean tests for ruby-trunk (3.2)
In
f075be3dcb
did_you_mean and error_highlight now use `detailed_message` over
`message` to display errors.

For cases where we are testing `message`, in 3.2 and above we need to
test against `detailed_message` instead.

As far as I can tell in a Rails console when these errors are raised the
`detailed_message` is used so we shouldn't need to make other changes to
Rails. The only case where this isn't true is in the Railties changes -
we are explicitly formatting the did you mean message so we need to be
sure to call `detailed_message` here.

This fixes most of the failing tests for ruby-trunk.
2022-06-10 13:06:42 -04:00
Deepak Mahakale
b2ed0b768d
Fix typo in documentation examples (#45272) 2022-06-06 06:01:26 -04:00
Mike Dalessio
5f8f6764d8
Strings returned from strip_tags are correctly tagged html_safe?
Because these strings contain no HTML elements and the basic entities
are escaped, they are safe to be included as-is as PCDATA in HTML
content. Tagging them as html-safe avoids double-escaping entities
when being concatenated to a SafeBuffer during rendering.

Fixes https://github.com/rails/rails-html-sanitizer/issues/124
2022-05-31 10:02:07 -04:00
John Hawthorn
a5b2b6155c
Merge pull request #45169 from oneiros/allow_all_available_locales_for_template_lookup
Allow all available locales for template lookups
2022-05-30 11:49:40 -07:00
David Roetzel
12c12899df Allow all available locales for template lookups.
Following the discussion here:
https://github.com/rails/rails/pull/44174/files#r785160819

Background: The `i18n` gem is relatively lax when it comes
to naming locales. It does not enforce any standard. Thus
it is possible to have e.g. per tenant locales (think
`en_tenant1`, `en_tenant2` etc.). This also worked for
translated templates up until rails 6.1.

Rails 7 changed the template lookup and enforced a naming
scheme for locales. This poses a problem for legacy apps
that use non-standard locale names.

This commit changes the way locale names are detected in
template file names. In addition to the previously used
regexp it also allows all known locales from
`I18n.available_locales`.

This makes it backwards compatible to rails 7.0
behavior while also allowing non-standard locale names.
Thanks to jvillarejo for the great idea.

Also introduce the usage of `Regexp.union`, a wonderful
suggestion by casperisfine.
2022-05-30 09:45:28 +02:00
Gannon McGibbon
01f58d62c2
Merge pull request #45180 from gmcgibbon/deprecate_behaviour_constant
Deprecate behaviour constant
2022-05-27 09:06:54 -04:00
Gannon McGibbon
3dad2919d5 Rename behaviour to behavior in documentation 2022-05-26 17:14:18 -04:00
Lachlan Sylvester
3e2158442b remove rendered_format from LookupContext 2022-05-26 18:04:24 +10:00
Sean Doyle
980de46f54 Move convert_to_model call from form_for to form_with
Ensure models passed to `form_with` attempt to call `to_model`.

Now that `form_for` is implemented in terms of `form_with`, this commit
also removes the `convert_to_model` call from the `form_for` implementation.

To exercise this behavior, change existing `form_with` test coverage.

Prior to this change, a call to `form_with` made with a `model:` argument
that responds to `to_model` would not incorporate the instance's persistence
state into the form's HTTP verb. After this change, the persistence state
inferred from the `model:` argument's `to_model` call is incorporated into
the `<form>` element's `[method]` attribute.

This is a separate follow-up change proposed in [rails/rails#44328][].
The original change to restore old behavior _deliberately_ excluded
applying the same logic to `form_with`, since it would be a breaking
change from how `form_with` behaved previously.

This commit proposed making that breaking change.

[rails/rails#44328]: https://github.com/rails/rails/pull/44328#discussion_r808475585
2022-05-25 09:48:58 -04:00
Shouichi Kamiya
aaa9bcc62e Document that url_for can take classes
Co-authored-by: oljfte <oljfte@gmail.com>
2022-05-13 10:09:17 +09:00
Jean Boussier
dd0b302378
Merge pull request #45029 from jonathanhefner/fix-extract_dimensions-regexp
Escape literal dot in regular expression
2022-05-09 09:55:00 +02:00
Jonathan Hefner
fe24f5880d Escape literal dot in regular expression
Follow-up to #44669.

This ensures that e.g. sizes with commas are not erroneously matched.
2022-05-05 14:08:43 -05:00
eileencodes
944bcb54f6
Fix tag helper regression
Vue.js, alpinejs, and potentially other JS libraries support tags
starting with `@` symbols. This was broken by the recent security release in
649516ce0f

I've only added `@` to the list even though there are potentially other
safe characters. We can add more if necessary (and if safe).

Fixes:
* #45014
* #44972
2022-05-05 10:42:41 -04:00
Alvaro Martin Fraguas
649516ce0f
Fix and add protections for XSS in names.
Add the method ERB::Util.xml_name_escape to escape dangerous characters
in names of tags and names of attributes, following the specification of
XML.

Use that method in the tag helpers of ActionView::Helpers. Rename the option
:escape_attributes to :escape, to simplify by applying the option to the whole
tag.
2022-04-26 12:34:42 -07:00
Xavier Noria
2953ae5c8a Define config.enable_reloading to be !config.cache_classes
Every time I write `config.cache_classes` I have to pause for a moment to make
sure I get it right. It makes you think.

On the other hand, if you read `config.enable_reloading = true`, does the
application reload? You do not need to spend 1 cycle of brain CPU to nod.
2022-04-14 18:11:36 +02:00
Jonathan Hefner
255accb0a5 Fix button_to UJS examples formatting [ci-skip]
Because the indented code block follows an indented list item, RDoc
interprets the examples as a continuation of the list item prose,
instead of code.  To distinguish the two, this commit moves the examples
to their own subsection with an intervening subheading.

Additionally, this commit applies a few other formatting tweaks.
2022-04-11 15:35:58 -05:00
Jonathan Hefner
7ad1bc8845 Fix link_to UJS examples formatting [ci-skip]
Because the indented code block follows an indented list item, RDoc
interprets the examples as a continuation of the list item prose,
instead of code.  To distinguish the two, this commit moves the examples
to their own subsection with an intervening subheading.

Additionally, this commit applies a few other formatting tweaks.
2022-04-11 15:35:24 -05:00
Jonathan Hefner
eea6f4dc81
Merge pull request #44784 from ghiculescu/data-remote-no-turbo
Document that `data-remote` and `data-method` are deprecated [ci-skip]
2022-04-11 15:27:35 -05:00
Alex Ghiculescu
be746b2623 Document that data-remote and data-method are deprecated
Follow up to https://github.com/rails/rails/pull/43112 and https://github.com/rails/rails/pull/44100

- `data-remote` is deprecated on links and buttons. Turbo doesn't need it since that is the default behaviour. You use `data-turbo=false` on elements that opt out of that, but I don't think that's in scope for Rails.
- `data-method` is deprecated on links. Turbo expects [data-turbo-method](https://turbo.hotwired.dev/handbook/drive#performing-visits-with-a-different-method).

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

Co-authored-by: Hartley McGuire <skipkayhil@gmail.com>

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

Co-authored-by: Hartley McGuire <skipkayhil@gmail.com>
2022-04-11 13:03:14 -05:00
Ryuta Kamizono
714fd07fd9 All intermediate delegation methods should preserve kwargs flag
Since 0456826180,
`foo(*caller_args)` method call delegation no longer preserve kwargs
flag.

Fixes #44846.
2022-04-06 15:32:54 +09:00
John Bampton
ad91029117 Change rss to RSS in ImageLinkToTag 2022-04-01 12:05:28 +10:00
John Bampton
c38d585ef0 Fix word casing of XHTML and XML Builder
Co-authored-by: Petrik de Heus <petrik@deheus.net>
2022-03-30 04:21:59 +10:00
Rafael Mendonça França
5c1bd20f0d
Merge pull request #44693 from ghousemohamed/fix-docs-related-gem-versions
Fix `#version` method docs and some typos [ci-skip]
2022-03-15 16:28:07 -04:00
Ghouse Mohamed
6ee6cb554b Fix #version docs and some typos 2022-03-16 01:48:37 +05:30
Aaron Patterson
8961b8ece7
Merge pull request #44669 from kaiquekandykoga/extract-float
Extract float
2022-03-14 15:16:06 -07:00
Kaíque Kandy Koga
16e6d77200 Extract float numbers when using size option
for image_tag and video_tag

Delete space

format_numbers number_formats

Add tests

Change tests

Use regex in extract_dimensations
2022-03-14 14:11:07 -03:00
Aaron Patterson
d28efa55fa
Merge pull request #43975 from codergeek121/bugfix-for-time-select-prompt
Fix a bug with time_select and prompts
2022-03-14 09:44:42 -07:00
Matheus Richard
414394206a Extend audio_tag and video_tag to accept Active Storage attachments.
Now it's possible to write

    audio_tag(user.audio_file)
    video_tag(user.video_file)

Instead of

    audio_tag(polymorphic_path(user.audio_file))
    video_tag(polymorphic_path(user.video_file))

image_tag already supported that, so this follows the same pattern.
2022-03-14 02:05:37 -03:00
Jean Boussier
13dd6f93c8
Merge pull request #44585 from davekaro/remove_circle_void_element
Remove circle from HTML_VOID_ELEMENTS set.
2022-03-02 20:26:45 +01:00
Jean Boussier
2fd34270eb Eager load controllers view_context_class
These classes are relatively small, however they include lots of
modules as helpers. And if any of the included module hold constants
including it cause the global constant cache to be invalidated
which is really bad for performance.

So when eager loading is enabled we create all the possible classes
as part of the application boot.
2022-03-02 08:47:54 +01:00
Dave Kroondyk
8d51706c20 Remove circle from HTML_VOID_ELEMENTS set.
`<circle>` is in the SVG_SELF_CLOSING_ELEMENTS list and is not a void element according to
'https://html.spec.whatwg.org/multipage/syntax.html#void-elements'.
2022-03-01 09:08:23 -05:00
Dave Kroondyk
3814826885 Ensure SVG elements are closed.
Changes introduced in https://github.com/rails/rails/pull/43232 break SVGs
that have mulitple of the same descendant tag because they weren't being
closed. SVG elements must be closed accodoring to spec
https://html.spec.whatwg.org/multipage/syntax.html#elements-2.

Fixes https://github.com/rails/rails/issues/44563
2022-02-28 17:52:02 -05:00
Rafael Mendonça França
201aecb260
Merge pull request #44555 from koic/enable_style_map_to_hash_cop
Enable `Style/MapToHash` cop
2022-02-25 15:23:50 -05:00
Rafael Mendonça França
b64cbc0b27
Merge pull request #44529 from Shopify/actionview-fix-flaky-tests
Fix flaky Action View tests
2022-02-25 14:47:49 -05:00
Koichi ITO
819871cc4e Enable Style/MapToHash cop
Ruby 2.6 added block argument processing to `Enumerable#to_h`.
https://bugs.ruby-lang.org/issues/15143

Rails 7 requires Ruby 2.7.0 or higher, so the new feature can use it.
`Style/MapToHash` cop will detect it. And this cop in the `Style` department,
but this seems to improve performance as follows:

```ruby
# map_to_hash.rb
require 'benchmark/ips'

ARRAY = (1..100).to_a
HASH = {foo: 1, bar: 2}

Benchmark.ips do |x|
  x.report('array.map.to_h') { ARRAY.map { |v| [v, v * 2] }.to_h }
  x.report('array.to_h')     { ARRAY.to_h { |v| [v, v * 2] } }

  x.compare!
end

Benchmark.ips do |x|
  x.report('hash.map.to_h') { HASH.map { |k, v| [k.to_s, v * 2] }.to_h }
  x.report('hash.to_h')     { HASH.to_h { |k, v| [k.to_s, v * 2] } }

  x.compare!
end
```

```console
% ruby map_to_hash.rb
Warming up --------------------------------------
      array.map.to_h     9.063k i/100ms
          array.to_h     9.609k i/100ms
Calculating -------------------------------------
      array.map.to_h     89.063k (± 3.9%) i/s -    453.150k in  5.096572s
          array.to_h     96.449k (± 1.7%) i/s -    490.059k in  5.082529s

Comparison:
          array.to_h:    96448.7 i/s
      array.map.to_h:    89063.4 i/s - 1.08x  (± 0.00) slower

Warming up --------------------------------------
       hash.map.to_h   106.284k i/100ms
           hash.to_h   149.354k i/100ms
Calculating -------------------------------------
       hash.map.to_h      1.102M (± 2.2%) i/s -      5.527M in   5.019657s
           hash.to_h      1.490M (± 0.9%) i/s -      7.468M in   5.013264s

Comparison:
           hash.to_h:  1489707.0 i/s
       hash.map.to_h:  1101561.5 i/s - 1.35x  (± 0.00) slower
```

`Style/MapToHash` cop ... https://docs.rubocop.org/rubocop/1.25/cops_style.html#stylemaptohash
2022-02-26 04:31:03 +09:00
Clayton Smith
6e56e18523 Fix flaky Action View tests 2022-02-23 09:39:57 -05:00
Jonathan Hefner
a199aaedb8 Cross-link API docs [ci-skip]
RDoc will automatically format and link API references as long as they
are not already marked up as inline code.

This commit removes markup from various API references so that those
references will link to the relevant API docs.
2022-02-21 11:45:25 -06:00
Jonathan Hefner
9dbf7a58a2 Fix formatting of parameters doc [ci-skip] 2022-02-21 11:11:11 -06:00
Jonathan Hefner
a801aa7cde Mark up inline code [ci-skip] 2022-02-21 11:11:11 -06:00
Jonathan Hefner
e37adfed4e Add Oxford commas [ci-skip] 2022-02-21 11:11:11 -06:00
Jonathan Hefner
07bee949c4 Replace backticks with RDoc markup [ci-skip]
RDoc does not support backticks the way that Markdown does.  Instead,
inline code must be wrapped with `+` or `<tt>`.
2022-02-21 11:11:11 -06:00
Jonathan Hefner
0d3effc97e Replace "overwrite" with "override" [ci-skip]
"Overwrite" means "destructively replace", and is more suitable when,
for example, talking about writing data to a location.

"Override" means "supersede", and is more suitable when, for example,
talking about redifining methods in a subclass.
2022-02-21 11:11:11 -06:00
Jonathan Hefner
5fdbd217d1 Fix typos [ci-skip] 2022-02-21 11:11:11 -06:00
Jean Boussier
e26372b713 Implicitly assert no exception is raised in assert_queries & al
Fix: https://github.com/rails/rails/pull/44397
Ref: https://github.com/rails/rails/pull/37313
Ref: https://github.com/rails/rails/pull/42459

This avoid mistakes such as:

```ruby
assert_raise Something do
  assert_queries(1) do
    raise Something
  end
end
```

Co-Authored-By: Alex Coomans <alexc@squareup.com>
2022-02-19 09:11:14 +01:00
Sean Doyle
9dc083f95d form_for: Attempt to call to_model on record
Related to https://github.com/rails/rails/issues/44326

---

The changes introduced in [rails/rails#43421][] did not account for
attempting to coerce the record with [#to_model][]. This commit resolves
that issue and adds test coverage to guard against future regressions.

Additionally, this adds tests to exercise `form_with` to ensure that it
_does not_ call `#to_model` on its `model:` argument, since that was the
behavior in [6.1.4.4][]

[rails/rails#43421]: https://github.com/rails/rails/pull/43421
[#to_model]: https://edgeapi.rubyonrails.org/classes/ActiveModel/Conversion.html#method-i-to_model
[6.1.4.4]: https://github.com/rails/rails/issues/44326#issuecomment-1029344241
2022-02-17 16:01:57 -05:00
John Hawthorn
3e2f9a66db
Merge pull request #43390 from jhawthorn/remove_notification_event_children
Remove child event tracking from ActiveSupport::Subscriber
2022-02-17 08:45:48 -08:00
John Hawthorn
139ef8a0bc
Merge pull request #44174 from jguecaimburu/add_lowdash_support_to_path_parser_locale_regex
Add support to locales with lowdash in Resolver::PathParser
2022-02-17 08:27:34 -08:00
John Hawthorn
9c58a54702 Split actionview log subscriber Start events
Previously one subscriber was used for both the "Rendering" (before) and
"Rendered" (after) events. With the previous change to AS::Subscriber
these need to be split.

Co-authored-by: Adam Hess <HParker@github.com>
2022-02-17 08:20:01 -08:00
Rafael Mendonça França
b961af3345
Merge pull request #44093 from motine/doc-hint-for-fields_for
add hint to ActionView's fields_for
2022-02-09 20:17:01 -05:00