Commit Graph

3005 Commits

Author SHA1 Message Date
zzak f7c0b38dd4 Several fixes for AV::Helpers::CaptureHelper 2023-01-29 09:54:11 +09:00
eileencodes 350311dbe4
Remove deprecation from template
This PR removes the deprecation added in #47005 because it is
non-trivial for HAML and Slim to implement error highlighting for
templates. If a template engine doesn't support `translate_location`
then we can fallback to `spot` and the error will be displayed but we
won't be able to automatically underline the column.
2023-01-24 12:07:49 -05:00
Rafael Mendonça França 5c835bd669
Make explicit this config only affects controllers are views 2023-01-19 20:21:29 +00:00
Rafael Mendonça França 6d4f9a8fbd
Merge pull request #46589 from mattpolito/form_with_form_builder_id
FormBuilder#id finds id set by `form_for` & `form_with`
2023-01-18 15:49:01 -05:00
Jean Boussier aa7d78d9b1 Improve Rails' Shape friendliness (second pass)
Followup: https://github.com/rails/rails/pull/47023

```
Shape Edges Report
-----------------------------------
snip...
       238  @errors
snip...
       219  @options
snip...
       129  @_request
       128  @type
       125  @virtual_path
       124  @_assigns
       123  @_config
       123  @_controller
       123  @output_buffer
       123  @view_flow
       122  @_default_form_builder
snip...
        89  @_already_called
        75  @validation_context
snip...
        65  @_new_record_before_last_commit
snip...
        58  @_url_options
snip...
```
2023-01-17 13:55:49 +01:00
Jean Boussier fc950324bd Improve Rails' Shape friendliness
Ruby 3.2 significantly changed how instance variables are store.
It now use shapes, and in short, it's important for performance
to define instance variables in a consistent order to limit the
amount of shapes.

Otherwise, the number of shapes will increase past a point where
MRI won't be able to cache instance variable access. The impact
is even more important when YJIT is enabled.

This PR is data driven. I dump the list of Shapes from Shopify's
monolith production environment, and Rails is very present among
the top offenders:

```
Shape Edges Report
-----------------------------------
       770  @default_graphql_name
       697  @own_fields
       661  @to_non_null_type
       555  @own_interface_type_memberships
       472  @description
       389  @errors
       348  @oseid
       316  @_view_runtime
       310  @_db_runtime
       292  @visibility
       286  @shop
       271  @attribute_method_patterns_cache
       264  @namespace_for_serializer
       254  @locking_column
       254  @primary_key
       253  @validation_context
       244  @quoted_primary_key
       238  @access_controls
       234  @_trigger_destroy_callback
       226  @_trigger_update_callback
       224  @finder_needs_type_condition
       215  @_committed_already_called
       214  @api_type
       203  @mutations_before_last_save
       202  @access_controls_overrides
       201  @options
       198  @mutations_from_database
       190  @_already_called
       183  @name
       179  @_request
       176  @own_arguments
       175  @_assigns
       175  @virtual_path
       174  @context
       173  @_controller
       173  @output_buffer
       173  @view_flow
       172  @_default_form_builder
       169  @cache
       159  @_touch_record
       151  @attribute_names
       151  @default_attributes
       150  @columns_hash
       149  @attribute_types
       148  @columns
       147  @marked_for_same_origin_verification
       146  @schema_loaded
       143  @_config
       143  @type
       141  @column_names
```

All the changes are of similar nature, the goal is to preset the instance
variable to nil when objects are allocated, or when classes are created.

For classes I leverage the `inherited` hook. If the patern becomes common enough
it might make sense to add a helper for this in `ActiveSupport::Concern`.
2023-01-16 12:31:37 +01:00
eileencodes 45430ae99d
Add warning for missing translate_location
If other libraries like `haml` don't implement `translate_location` then
they won't get the error highlighting behavior that was implemented to
underline errors coming from templates.

This deprecation is to notify templating engines that this change is
necessary for them to get this highlighting.
2023-01-13 16:47:22 -05:00
eileencodes d9dd1c59d0
Implement spot and don't use `keep_script_lines` in Ruby 3.2
We want to use error highlight with eval'd code, specifically ERB
templates.

Previously we could only get the information we needed by setting
`keep_script_lines` to true. In Ruby 3.2 and error_highlight we added
the ability to get this information without setting `keep_script_lines`.

This change implements that new behavior for Rails.

I removed the script line changes to support this in 3.1 because it is
not in any released version.

Ruby change: https://github.com/ruby/ruby/pull/6593
Erorr highlight change: https://github.com/ruby/error_highlight/pull/26

Co-authored-by: Aaron Patterson <tenderlove@ruby-lang.org>
2023-01-13 16:20:09 -05:00
Hartley McGuire 978993e880
Enable Style/EvalWithLocation
Ref: ad39d6b

Ensure that all evals include file and line number to identify their
source.

Two of the evals reported by this cop were unneccesary and replaced with
non-eval alternatives: xml is set as a local variable in
Template::Handlers::Builder#call, and instance_eval isn't needed to get
the current binding.

There are additionally 27 offenses in test directories, but since it
seems less important to fix those they are currently ignored.
2023-01-11 18:46:09 -05:00
Jean Boussier ad39d6b2f9 Fix anonymous evals
This makes it very annoying to locate the source of methods
and such when profiling etc.
2023-01-11 11:22:42 +01:00
Akira Matsuda 4c23742a13
`delegate to: :class` has to be defined after the target method
in order to derive the arity from the target class method.
2023-01-11 04:32:08 +09:00
Jonathan Hefner cfe300ceb1 Format inline code [ci-skip] 2023-01-08 15:47:20 -06:00
Jonathan Hefner 3eadf057db Fix typos in API docs [ci-skip] 2023-01-08 15:47:20 -06:00
Lachlan Sylvester 143472b944 restore coment from the old index.html about overwriting the link selector 2023-01-04 00:46:36 +11:00
Lachlan Sylvester b39f06b226 move attach-bindings out of test as it doesn't contain test cases 2023-01-04 00:36:57 +11:00
Lachlan Sylvester 28b7a84b69 add comments to explain the test rollup and karma config files 2023-01-04 00:08:47 +11:00
Lachlan Sylvester fbb5f5b4b6 Formatting
Co-authored-by: Michael Adams <github@michaeladams.org>
2022-12-31 23:03:09 +11:00
Lachlan Sylvester b1bc3a0b20 add explaination of extracting from the ENCODED env var 2022-12-31 23:03:09 +11:00
Lachlan Sylvester 2f42c5810a Use explicit argument lists when spawning the server and tests.
Co-authored-by: Samuel Williams <samuel.williams@oriontransfer.co.nz>
2022-12-31 23:03:09 +11:00
Lachlan Sylvester d9e79ce7f4 use Karma as the test runner for the UJS tests 2022-12-31 23:03:09 +11:00
Matt Polito af8d47bf35 Clarify form builder test scenarios
Co-authored-by: Matt Polito <mattpolito@users.noreply.github.com>
Co-authored-by: Sean Doyle <seanpdoyle@users.noreply.github.com>
2022-12-23 11:58:35 -05:00
Akira Matsuda 974de71036
Merge pull request #46763 from jdufresne/trimws
Trim trailing whitespace from *.md files
2022-12-18 12:22:50 +09:00
Akira Matsuda 45dd422901
Let `initialize` not create an Array as its return value 2022-12-18 12:11:34 +09:00
Jon Dufresne aa81f67a9e Trim trailing whitespace from *.md files
Fixes the error:

    Run bin/check-changelogs ./rails
    ............F

    Offenses:

    ./rails/railties/CHANGELOG.md:4 Trailing whitespace detected.
2022-12-17 15:27:51 -08:00
Akira Matsuda 6576eec6a8
Create an Array for the error message only when invalid value exists 2022-12-17 17:13:17 +09:00
Akira Matsuda d8c05043c3
Avoid creating new Array object per each comparison
Benchmark.ips do |x|
  arr = [:js]

  x.report('==') { arr == [:js] }
  x.report('length') { (arr.length == 0) && (arr[0] == :js) }
  x.report('one?') { arr.one? && (arr[0] == :js) }

  x.compare!
end

Warming up --------------------------------------
                  ==   731.426k i/100ms
              length     1.967M i/100ms
                one?     1.440M i/100ms
Calculating -------------------------------------
                  ==      7.291M (± 0.8%) i/s -     36.571M in   5.016130s
              length     19.633M (± 0.8%) i/s -     98.356M in   5.010104s
                one?     14.387M (± 0.8%) i/s -     71.998M in   5.004721s

Comparison:
              length: 19632843.5 i/s
                one?: 14386784.0 i/s - 1.36x  (± 0.00) slower
                  ==:  7291274.5 i/s - 2.69x  (± 0.00) slower
2022-12-15 11:55:03 +09:00
eileencodes bd7b6decb9
Fix typo in new docs from #46730 2022-12-14 16:17:55 -05:00
eileencodes 171fea857d
Improve docs for strict_locals! [ci-skip]
The docs didn't say much about what strict locals are or why you would
use them. This adds docs on usage and what the functionality does.
2022-12-14 15:13:40 -05:00
eileencodes 334fa122b7
Refactor compiled_source
This is a continuation of #46706 to make sure we don't need to set an
instance variable to `@original_source` for the `compile` method to use.

We can't call `strict_locals!` after encode so we need to set it to a
local variable in `complile`. We changed the `strict_locals!` method to
check `NONE` instead of lazily defining instance variables which let us
simplify `strict_locals?` to return the value of `strict_locals!`. This
simplifies and clarifies the code.

Co-authored-by: Aaron Patterson tenderlove@ruby-lang.org
2022-12-14 14:12:25 -05:00
Alex Ghiculescu 49c2e51808 Allow `f.select` to be called with a single hash containing options and HTML options
I do this a lot:

```erb
<%= select :post, :author, authors, required: true %>
```

It doesn't work; the `required` attribute is ignored! Instead, you need to do this:

```erb
<%= select :post, :author, authors, {}, required: true %>
```

It's hard to remember the right API, and it looks to me like a code smell. It looks even smellier when you end up with this:

```erb
<%= select :post, :author, authors, { include_blank: "Choose an option" }, { required: true } %>
```

Where this would be nicer, but again, the `required` attribute is ignored:

```erb
<%= select :post, :author, authors, include_blank: "Choose an option", required: true %>
```

This PR implements a special handling for `required`, `multiple`, and `size` HTML attributes so that these now do the same thing:

```erb
<%= select :post, :author, authors, include_blank: "Choose an option", required: true %>
<%= select :post, :author, authors, { include_blank: "Choose an option" }, { required: true } %>
```

ps. as proof I'm not the only person who makes this mistake, one of the tests in the Rails test suite was wrong! The test added in https://github.com/rails/rails/pull/40522 puts the `multiple` attribute in the wrong place and has the wrong assertion as as result. This PR includes a fix for the test.
2022-12-13 11:44:34 -06:00
eileencodes 5026abaeb1
Refactor compiled source code into method
Moves the part of `compile!` that compiles the template source into it's
own method. We need this for future work in improving exceptions for ERB
templates to pass to ErrorHighlight.

Co-authored-by: Aaron Patterson <tenderlove@ruby-lang.org>
2022-12-12 15:31:19 -05:00
Andrey Samsonov c20b629f52 Fix inconsistent behavior in form helper date/time tags with options; issue #46677
This change makes date/time options (value, min, max) in `time_field`, `date_field`, `datetime_field`, `week_field`, `month_field` form helpers behave in a unified way.
2022-12-11 17:19:18 +01:00
Matt Polito 978a3eb4b0 FormBuilder#id finds id set by `form_for` & `form_with` 2022-12-08 15:44:48 -05:00
Hartley McGuire a07f2ace03
Fix lots of code highlighting issues
The most common is replacing back-ticks with either pluses or tt tags.
There were also a few instances of code blocks not being indented.
2022-11-29 00:51:02 -05:00
Étienne Barrié 3d6a7b2faa Initialize deprecators before configuring them
Since engine initializers run later in the process, we need to run this
initializer earlier than the default.

This ensures they're all registered before the environments are loaded.
2022-11-28 10:47:26 +01:00
Jonathan Hefner 1e68400930 Prefer deprecate_constant over constant proxy
`deprecate_constant` will warn whenever the constant is referenced
instead of when the constant is a method receiver, which increases the
likelihood that the warning will be seen.  Additionally,
`DeprecatedConstantProxy` prevents using the constant as a superclass,
such as in `class MyClass < SomeDeprecatedConstant`.
2022-11-25 11:25:03 -06:00
Rafael Mendonça França db5a6ef624
Merge PR #43019 2022-11-23 19:14:33 +00:00
Mathieu Le Tiec bab93a0570 Add FormBuilder#fields tests 2022-11-22 20:59:43 +01:00
Mathieu Le Tiec a625e20825 Allow hash-like record_object in FormBuilder#fields_for 2022-11-22 20:03:43 +01:00
Rafael Mendonça França a8efccc32e
Merge pull request #46531 from p8/guides/fix-capitalization-of-AJAX
Rename AJAX to Ajax for consistency [ci-skip]
2022-11-21 17:24:09 -05:00
Maciej Mensfeld 9a77b0fe28
Make sure that concurrent map usage is thread-safe
Behavior upon missing prefix partial name may cause a key to overwrite when executed in multiple threads at the same time.

ref https://github.com/ruby-concurrency/concurrent-ruby/issues/970
2022-11-20 20:50:06 +01:00
Petrik 93c51f92fd Rename AJAX to Ajax for consistency [ci-skip]
Currently we use both `AJAX` (11 times) and `Ajax` (22 times).
Wikipedia uses `Ajax`: https://en.wikipedia.org/wiki/Ajax_(programming)
Mozilla uses `Ajax`: https://developer.mozilla.org/en-US/docs/Web/Guide/AJAX

As `Ajax` is currently used the most and it's preferred by Wikipedia and
Mozilla, we can change all it's occurences to `Ajax`.
2022-11-20 11:17:44 +01:00
Petrik 482d94c139 Use 2 spaces for identation in ActionView code examples [ci-skip]
For consistency use 2 spaces for indentation (+ 1 default space after `#`)
Some of these were formatted incorrectly in the API docs.
2022-11-16 23:08:07 +01:00
Jonathan Hefner ee8d9497dd Auto-link code references [ci-skip] 2022-11-15 13:57:15 -06:00
Jonathan Hefner 9588f780dc Format inline code [ci-skip] 2022-11-15 13:57:15 -06:00
Jonathan Hefner b5248aca16 Add ActionView.deprecator
This commit adds `ActionView.deprecator` and replaces all usages of
`ActiveSupport::Deprecation.warn` in `actionview/lib` with
`ActionView.deprecator`.  This commit also replaces a call to Ruby's
`Module#deprecate_constant` with Rails' `DeprecatedConstantProxy`, so
that its deprecation behavior can be configured using
`ActionView.deprecator`.

Additionally, this commit adds `ActionView.deprecator` to
`Rails.application.deprecators` so that it can be configured via
settings such as `config.active_support.report_deprecations`.

This commit also removes a few defunct `assert_deprecated` calls that
were not failing because they were nested in `assert_raises`, and the
raised error prevented checking the deprecation.  (One was mistakenly
kept in d52d773946 when converting
`test_render_file_with_errors` to `test_render_template_with_errors`;
the other two were added in dd9991bac5 but
not removed when the deprecation was completed in
85ecf6e4098601222b604f7c1cbdcb4e49a6d1f0.)
2022-10-30 16:01:39 -05:00
Jonathan Hefner 48d4e6e02b Add ActionDispatch.deprecator
This commit adds `ActionDispatch.deprecator` and replaces all usages of
`ActiveSupport::Deprecation.warn` in `actionpack/lib/action_dispatch`
with `ActionDispatch.deprecator`.

Additionally, this commit adds `ActionDispatch.deprecator` to
`Rails.application.deprecators` so that it can be configured via
settings such as `config.active_support.report_deprecations`.
2022-10-27 17:11:02 -05:00
Alex Ghiculescu 4fcac154f4 Support `checked` as a keyword argument in `check_box_tag` and `radio_button_tag`
Currently if you do this:

```ruby
check_box_tag "admin", "1", checked: false
```

It is treated [as truthy](19f9922523/actionview/lib/action_view/helpers/form_tag_helper.rb (L444)), and your checkbox is checked. This can be a bit surprising, particularly because the `FormHelper` version [does support](19f9922523/actionview/lib/action_view/helpers/form_helper.rb (L1285)) a keyword argument.

```ruby
f.check_box "admin", checked: false
```

So this PR updates `check_box_tag` and `radio_button_tag` to support `checked` as a positional or keyword argument, this way you can use the same API in both cases.

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2022-10-21 12:41:29 -05:00
Jean Boussier 05919b0802 Fix Action View compatibility with jbuilder
jbuilder is doing some weird things such as instantiatign RenderedTemplate
with a `Hash` instance as a `body`.

So we can't forcibly cast to string in these places.
2022-10-21 13:15:53 +02:00
Aaron Patterson 2f36f0a2bb
Merge pull request #46171 from rails/refactor-errors
Add column information inside ERB templates
2022-10-11 15:10:40 -07:00