Commit Graph

89977 Commits

Author SHA1 Message Date
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
Hartley McGuire f0d66d6c47
Merge pull request #50279 from skipkayhil/hm-nfd-2
More improvements to new framework defaults doc [ci-skip]
2023-12-07 17:37:00 -05:00
Aaron Patterson c5a8621c7f
Merge pull request #50129 from MaxLap/fix_preload_concat
Fix: Preloader should no longer overwrites built records
2023-12-07 14:21:26 -08:00
Jonathan Hefner 049c95161d
Merge pull request #50296 from jonathanhefner/follow-up-49908-fix-cached-chrome
Fix system tests with Chrome cached by Selenium
2023-12-07 13:25:53 -06:00
Jonathan Hefner be272a83a0 Fix system tests with Chrome cached by Selenium
Follow-up to #49908.

When Selenium resolves the driver path to a copy of Chrome that it has
downloaded / cached, it mutates the `Selenium::WebDriver::Chrome::Options`
object it receives, and relies on those changes later when the options
are used.  If `Selenium::WebDriver::Chrome::Service.driver_path` is set
but a different options object is used, Selenium will raise "cannot find
Chrome binary".  Therefore, this commit ensures that the options object
passed to `Selenium::WebDriver::DriverFinder.path` is the same options
object used by the driver later.
2023-12-07 12:52:51 -06:00
Jonathan Hefner fb09e7f1aa
Merge pull request #50167 from chaadow/fix_activestorage_table_prefix
[ActiveStorage] Take `ActiveRecord` affixes into account for `ActiveStorage` database models
2023-12-07 11:36:39 -06:00
Jean Boussier 745992e93e
Merge pull request #49640 from mguan2020/branch_new
Added link further explaining ERB in the 'Getting Started' documentation [ci skip]
2023-12-07 08:02:38 +01:00
chaadow 4ca61ffacb Take AR affixes into account for AStorage models
All of ActiveStorage database modeltable nameshave been hard coded.
Therefore, ActiveRecord::Base.(prefix|suffix) were not taken into
consideration. To fix this we remove the hard coded lines. But then we
need to also override an internal method for specifying the prefix
because of a mystical ActiveRecord/ActiveStorage sync issue
(Suffix does not appear to have the issue)

Some tests were refactored to remove hard coded table name references,
making ActiveStorage test suite compatible with ActiveRecord config.
2023-12-07 00:01:16 +01:00
Jonathan Hefner c1489a8ca4
Merge pull request #50247 from chaadow/fix_engine_ar_prefix
Make isolated engines aware of ActiveRecord::Base table name prefix
2023-12-06 16:37:21 -06:00
Chedli Bourguiba 0fe286339a Make engines aware of AR::Base table name prefix 2023-12-06 20:09:18 +01:00
Hartley McGuire d3d327bb43
More improvements to new framework defaults doc
- Explain the purpose of wrap_parameters_by_default and how it replaces
  the wrap_parameters initializer.
- Explain why action_mailer.smtp_timeout exists and why its useful
- Add example to action_view.button_to_generates_button_tag
2023-12-05 19:08:54 -05:00
Hartley McGuire c057edaaad
Merge pull request #50277 from skipkayhil/hm-nfd-open-redirects
Improve doc for raise_on_open_redirects [ci-skip]
2023-12-05 17:38:31 -05:00
Hartley McGuire 61a806e4b1
Merge pull request #50278 from skipkayhil/hm-fix-protocol-adapters-link
Fix link to config.active_record.protocol_adapters [ci-skip]
2023-12-05 17:24:30 -05:00
Hartley McGuire 8f1b3ceb7e
Fix link to config.active_record.protocol_adapters 2023-12-05 17:15:17 -05:00
Hartley McGuire ecbe5f9bfe
Improve doc for raise_on_open_redirects
- provide a definition for an "open redirect"
- clarify what "permitted open redirect" means in this context
2023-12-05 17:10:55 -05:00
Jean Boussier b979afe75d
Merge pull request #48957 from cmaruz/48326
Better handle SyntaxError in Action View
2023-12-05 21:00:06 +01:00
Jean Boussier c4733f3bc4
Merge pull request #50271 from Shopify/bump-irb
Use IRB 1.10.1 to avoid console test issues on CI
2023-12-05 18:14:11 +01:00
Stan Lo 6be6669c95
Use IRB 1.10.1 to avoid console test issues on CI 2023-12-05 16:36:27 +00:00
Jean Boussier c3b396cacb
Merge pull request #50270 from Shopify/lock-irb
Lock irb to < 1.10 for now
2023-12-05 16:59:48 +01:00
Jean Boussier 1aa4bd3823 Lock irb to < 1.10 for now
It breaks various `rails console` related integration tests:

```
Failure:
FullStackConsoleTest#test_sandbox [test/application/console_test.rb:123]:
"> " expected, but got:

(END).
Expected "\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n--More--\r        \r(END)" to include "> ".
```

I tried to figure out a fix, but ran out of time, so in order
to fix CI we can lock IRB for now.
2023-12-05 16:31:01 +01:00
Jean Boussier 98df193aaf
Merge pull request #50268 from Shopify/opt-time-at
Optimize `Time.at_with_coercion`
2023-12-05 16:08:28 +01:00
Jean Boussier 7cfc4ee676 Optimize `Time.at_with_coercion`
By using `ruby2_keyword` style delegation we we can avoid a
few allocations and some extra checks.

```
$ ruby --yjit /tmp/bench-as-time-at.rb
ruby 3.2.2 (2023-03-30 revision e51014f9c0) +YJIT [arm64-darwin22]
=== Complex call ====
Warming up --------------------------------------
        Time.without   320.514k i/100ms
           Time.with    68.433k i/100ms
       Time.opt_with   167.532k i/100ms
Calculating -------------------------------------
        Time.without      3.781M (± 4.8%) i/s -     18.910M in   5.014574s
           Time.with      1.586M (± 3.5%) i/s -      7.938M in   5.010525s
       Time.opt_with      2.003M (± 2.4%) i/s -     10.052M in   5.021309s

Comparison:
        Time.without:  3781330.9 i/s
       Time.opt_with:  2003025.9 i/s - 1.89x  slower
           Time.with:  1586289.9 i/s - 2.38x  slower

Time.without: 2.003 alloc/iter
Time.with: 9.002 alloc/iter
Time.opt_with: 7.002 alloc/iter

=== Simple call ====
Warming up --------------------------------------
        Time.without   749.097k i/100ms
           Time.with   342.855k i/100ms
       Time.opt_with   416.063k i/100ms
Calculating -------------------------------------
        Time.without      9.289M (± 3.4%) i/s -     46.444M in   5.005361s
           Time.with      3.601M (± 2.1%) i/s -     18.171M in   5.048794s
       Time.opt_with      4.373M (± 8.1%) i/s -     22.051M in   5.084967s

Comparison:
        Time.without:  9289271.2 i/s
       Time.opt_with:  4373226.2 i/s - 2.12x  slower
           Time.with:  3600733.6 i/s - 2.58x  slower

Time.without: 1.002 alloc/iter
Time.with: 3.001 alloc/iter
Time.opt_with: 3.002 alloc/iter
```

```ruby
require 'bundler/inline'

gemfile do
  source 'https://rubygems.org'
  gem 'activesupport', require: 'active_support/all', github: 'rails/rails'
  gem 'benchmark-ips'
end

class Time
  class << self
    def opt_at_with_coercion(time_or_number, *args)
      if args.empty?
        if time_or_number.is_a?(ActiveSupport::TimeWithZone)
          at_without_coercion(time_or_number.to_r).getlocal
        elsif time_or_number.is_a?(DateTime)
          at_without_coercion(time_or_number.to_f).getlocal
        else
          at_without_coercion(time_or_number)
        end
      else
        at_without_coercion(time_or_number, *args)
      end
    end
    ruby2_keywords :opt_at_with_coercion
  end
end

puts RUBY_DESCRIPTION

puts "=== Complex call ===="
Benchmark.ips do |x|
  x.report("Time.without") do
    ::Time.at_without_coercion(223423423, 32423423, :nanosecond, in: "UTC")
  end

  x.report("Time.with") do
    ::Time.at_with_coercion(223423423, 32423423, :nanosecond, in: "UTC")
  end

  x.report("Time.opt_with") do
    ::Time.opt_at_with_coercion(223423423, 32423423, :nanosecond, in: "UTC")
  end

  x.compare!(order: :baseline)
end

def measure_allocs(title, iterations: 1_000)
  before = GC.stat(:total_allocated_objects)
  iterations.times do
    yield
  end
  allocs = GC.stat(:total_allocated_objects) - before
  puts "#{title}: #{allocs.to_f / iterations} alloc/iter"
end

measure_allocs("Time.without") do
  ::Time.at_without_coercion(223423423, 32423423, :nanosecond, in: "UTC")
end

measure_allocs("Time.with") do
  ::Time.at_with_coercion(223423423, 32423423, :nanosecond, in: "UTC")
end

measure_allocs("Time.opt_with") do
  ::Time.opt_at_with_coercion(223423423, 32423423, :nanosecond, in: "UTC")
end

puts "=== Simple call ===="
Benchmark.ips do |x|
  x.report("Time.without") do
    ::Time.at_without_coercion(223423423)
  end

  x.report("Time.with") do
    ::Time.at_with_coercion(223423423)
  end

  x.report("Time.opt_with") do
    ::Time.opt_at_with_coercion(223423423)
  end

  x.compare!(order: :baseline)
end

def measure_allocs(title, iterations: 1_000)
  before = GC.stat(:total_allocated_objects)
  iterations.times do
    yield
  end
  allocs = GC.stat(:total_allocated_objects) - before
  puts "#{title}: #{allocs.to_f / iterations} alloc/iter"
end

measure_allocs("Time.without") do
  ::Time.at_without_coercion(223423423)
end

measure_allocs("Time.with") do
  ::Time.at_with_coercion(223423423)
end

measure_allocs("Time.opt_with") do
  ::Time.opt_at_with_coercion(223423423, 32423423)
end
```
2023-12-05 12:42:40 +01:00
Mario Caropreso df6d2fbf2e Addressed an issue where syntax errors generated inside an eval
method cause an Internal Server Error due to a TypeError.

In order to display the extraced source of a syntax error, we try
to locate the node id for the backtrace location. The call to
RubyVM::AbstractSyntaxTree.node_id_for_backtrace_location is expecting
a Thread::Backtrace::Location object, but we are passing a
SourceMapLocation instead.

This commit does two things:
1) it addresses the issue by making sure that we are always passing
a Thread::Backtrace::Location instead
2) it allows the development view to show the extracted source fragment
2023-12-05 06:40:19 +00:00
John Hawthorn 1c8be9c67a
Merge pull request #50266 from rails/revert-50164-do-not-overwrite-aj-logger
Revert "Do not overwrite AJ logger if it is supplied"
2023-12-04 17:52:57 -08:00
John Hawthorn 1a4474e7ee
Revert "Do not overwrite AJ logger if it is supplied" 2023-12-04 17:32:50 -08:00
Hartley McGuire 1544455776
Merge pull request #50249 from akhilgkrishnan/add-actionview-deprecation
Add actionview deprecation to 7.2 release note [skip ci]
2023-12-04 19:25:44 -05:00
Akhil G Krishnan cc55785f4d Add actionview deprecation to 7.2 release note 2023-12-04 22:41:10 +05:30
Jean Boussier a35ceb429c
Merge pull request #50262 from Techbrunch/patch-1
Use RedCloth GitHub instead of an expired domain
2023-12-04 16:52:58 +01:00
Jean Boussier 439e8be98e
Merge pull request #50217 from fastjames/document_variant_preprocessed_option
Document the `preprocessed` variant option [ci skip]
2023-12-04 16:49:05 +01:00
Jim Kane 87c614a1d4 Document the `preprocessed` variant option 2023-12-04 09:21:01 -06:00
Techbrunch a86e8c875a
Use RedCloth GitHub instead of an expired domain 2023-12-04 14:44:30 +01:00
Ryuta Kamizono 06d9c3cbe6
Merge pull request #50251 from abeidahmed/has_secure_token-call-setter-method
[Fix #49874] `has_secure_token` calls the setter method on initialize
2023-12-04 09:34:29 +09:00
Jonathan Hefner 0ad2050c6e
Merge pull request #50179 from akhilgkrishnan/improve-documentation-for-highlight
Improve documentation for highlight text helper [skip ci]
2023-12-03 15:42:19 -06:00
Jonathan Hefner 41edc5a13a
Merge pull request #50255 from seanpdoyle/action-text-rich-text-area-helper-test
Add test coverage for `rich_text_area` helper
2023-12-03 15:41:42 -06:00
Sean Doyle 16c28d0a09 Add test coverage for `rich_text_area` helper
Follow-up to [#50252][]

Similar to the reliance on a `FormBuilder` in the helper methods
documentation examples, the template test coverage for `#rich_text_area`
relied on invocations through a `FormBuilder` instance.

This commit adds explicit coverage for calling the `#rich_text_area`
helper method directly with both an `object_name` and `method_name`
positional arguments.

[#50252]: https://github.com/rails/rails/pull/50252
2023-12-03 14:53:44 -05: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
Jonathan Hefner da871cc28a
Merge pull request #50252 from seanpdoyle/rich-text-area-docs
Action Text `rich_text_area` code samples [ci-skip]
2023-12-03 11:04:12 -06:00
Sean Doyle 638c9d5722 Action Text `rich_text_area` code samples [ci skip]
The API documentation for the `rich_text_area` Action View helper
demonstrates how to invoke the helper through a `FormBuilder` instance,
instead of through the `ActionView::Base` instance.

This commit removes the `form.` prefix, and includes examples of calling
the method with an `object_name` positional argument.
2023-12-03 11:40:55 -05:00
abeidahmed c8caf6d867 [Fix #49874] `has_secure_token` calls the setter method on initialize
Follow-up to #49146

The original behavior of `has_secure_token` was to use the
`send("#{attribute}=", some_value)` method so that the setter method, if
defined, was called. PR #49146 replaced the `send` method with
`write_attribute` which doesn't call the setter method and breaks
existing applications.
2023-12-03 14:35:19 +04:00
Petrik de Heus 6908a66c50
Merge pull request #50245 from seanpdoyle/field-id-field-name-docs
Action View Docs: `field_id` and `field_name` examples [ci skip]
2023-12-02 18:49:21 +01:00
Jean Boussier c5d519f93a
Merge pull request #50240 from joshuay03/preserve-timezone-in-active-job-time-with-zone-serializer
[Fix #50230] Preserve serialized timezone when deserializing with `ActiveJob::Serializers::TimeWithZoneSerializer`
2023-12-02 16:05:46 +01:00
Joshua Young 18098ccc4c [Fix #50230] Preserve serialized timezone when deserializing with `ActiveJob::Serializers::TimeWithZoneSerializer` 2023-12-03 00:37:09 +10:00
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 67fb4414e5
Merge pull request #50236 from 907th/fix-travel-to
Fix `Time.now`/`DateTime.now`/`Date.today` to return results in a system timezone after `#travel_to`
2023-12-02 10:24:54 +01: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
Jean Boussier 9ec451f086
Merge pull request #50239 from seanpdoyle/form-builder-code-generator-batch
Batch define `FormBuilder` methods with `CodeGenerator`
2023-12-02 10:21:35 +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
Jean Boussier fac9a03afe
Merge pull request #50242 from yysaki/security_md_typo
Update security.md typo about HTTP Strict-Transport-Security [ci-skip]
2023-12-02 10:06:25 +01:00
Aleksei Chernenkov aedb808829 Fix Time.now/DateTime.now/Date.today to return results in a system timezone after #travel_to
There is a bug in the current implementation of #travel_to:
it remembers a timezone of its argument, and all stubbed methods start
returning results in that remembered timezone. However, the expected
behaviour is to return results in a system timezone.

It can lead to bugs in tests like this one:
https://github.com/faker-ruby/faker/issues/2861
2023-12-02 10:05:31 +01:00
Jean Boussier 469c3dbeea
Merge pull request #50233 from rails/lock-json-gem
Lock json gem
2023-12-02 09:52:28 +01:00