Commit Graph

90558 Commits

Author SHA1 Message Date
Jean Boussier 1faf89f559
Merge pull request #50284 from seanpdoyle/close-49574
Infer `:inverse_of` for `has_many ..., through:`
2024-01-21 17:21:49 +01:00
Sean Doyle 68013b30d2 Infer `:inverse_of` for `has_many ..., through:`
Closes [#49574][]

Issue #49574 outlines how an Active Record join model accessed through a
`has_many ..., through:` association is unable to infer an appropriate
`:inverse_of` association by pluralizing a predictably pluralizable
class name.

This commit resolves that issue by also checking a model's reflections
for a pluralized inverse name in addition to whatever's provided through
the `:as` option or inferred in the singular.

The issue shares a code sample:

```ruby
ActiveRecord::Schema.define do
  create_table "listings", force: :cascade do |t|
    t.bigint "list_id", null: false
    t.bigint "pin_id", null: false
  end

  create_table "lists", force: :cascade do |t|
  end

  create_table "pins", force: :cascade do |t|
  end
end

class Pin < ActiveRecord::Base
  has_many :listings
end

class List < ActiveRecord::Base
  has_many :listings
  has_many :pins, through: :listings
end

class Listing < ActiveRecord::Base
  belongs_to :list
  belongs_to :pin
end

class BugTest < Minitest::Test
  def test_association_stuff
    list = List.create!
    pin = list.pins.new

    pin.save!

    assert_equal [pin], list.reload.pins
    assert_equal 1, list.reload.pins.count
  end
end
```

Unfortunately, there isn't a one-to-one mapping in the test suite's
`test/model` directory for this type of structure. The most similar
associations were between the `Book`, `Subscription`, and `Subscriber`.
For the sake of ease, this commit wraps the test block in a new
`skipping_validations_for` helper method to ignore validations declared
on the `Subscription` join table model.

[#49574]: https://github.com/rails/rails/issues/49574
2024-01-21 10:25:46 -05:00
Jean Boussier 310bb2abdb
Merge pull request #50800 from Edouard-chin/ec-inverseof-blob
Fix ActiveStorage::Blob inverse association:
2024-01-21 09:17:05 +01:00
Jean Boussier 9f52c45b18
Merge pull request #50308 from fatkodima/refactor-loaded-batches
Small refactoring of batching over loaded relation
2024-01-21 09:13:43 +01:00
Jean Boussier 3064d4f53a
Merge pull request #50825 from Shopify/update-rails-console-prompt
Update rails console prompt
2024-01-21 00:36:25 +01:00
Stan Lo 09b2aecec5
Change Rails console prompt's format
Following the discussion in #50770, the new format will be:
`[dasherized-app-name]([colorized-env])>`

For example, if the app's module name is `MyApp`, the prompt will be:
`my-app(dev)>`, where the `dev` part will be colored.

Update railties/lib/rails/commands/console/console_command.rb

Co-authored-by: Jean Boussier <jean.boussier@gmail.com>

Update changelog
2024-01-20 23:34:33 +00:00
Petrik de Heus f84b428efb
Merge pull request #50822 from kylevoyto/patch-1
Fix the CreateProducts migration file name in the docs [ci-skip]
2024-01-20 18:15:59 +01:00
Kyle Voytovich 458f2c1c45
Fix the CreateProducts migration file name in the docs 2024-01-20 10:12:23 -05:00
Jean Boussier fcab061839
Merge pull request #50814 from Shopify/improve-rails-console
Improve Rails console's IRB autocompletion control
2024-01-20 15:01:14 +01:00
Stan Lo 4d6714fcd3
Remove unnecessary nocolorize flag
Setting `TERM=dumb` is enough to disable colorization in those tests.

Mocked app should also load console helpers

Move console method extension to IRBConsole's constructor

Test IRB autocompletion control without mutating ENV

Control IRB autocompletion without mutating ENV

Correct local variable name
2024-01-20 11:45:52 +00:00
Petrik de Heus 93b83f9b96
Merge pull request #50819 from tanaken0515/patch-1
[ci skip] docs: fix `Entry#entryable_types` to `Entry.entryable_types`
2024-01-20 10:25:14 +01:00
tanaken0515 024d0abdec
fix: `Entry#entryable_types` to `Entry.entryable_types` 2024-01-20 16:26:01 +09:00
Jean Boussier 30cb7a6221
Merge pull request #50796 from Shopify/implement-#50770
Add customized prompt for Rails console
2024-01-19 20:11:51 +01:00
Stan Lo 0c2898a768
Create IRBConsole to encapsulate IRB-specific setup
Add Rails-specific IRB prompt

Update changelog
2024-01-19 19:01:26 +00:00
Edouard Chin 21eceb7db2
Merge pull request #50808 from m-nakamura145/actionpack/modules_for_helpers-example
Add example to modules_for_helpers documentation [ci-skip]
2024-01-19 16:59:37 +01:00
m-nakamura145 930649598f
Add example to modules_for_helpers documentation [ci-skip] 2024-01-20 00:48:59 +09:00
Jean Boussier 2ddbca3e6a
Merge pull request #50809 from Shopify/freeze-cached-strings
Freeze and dedup `quote_column_name` and `quote_table_name` caches
2024-01-19 15:23:55 +01:00
Petrik de Heus 25d03be267
Merge pull request #50810 from m-nakamura145/activesupport/duration-parts-example
Add example to parts documentation [ci-skip]
2024-01-19 15:10:24 +01:00
m-nakamura145 a5af0a9118
Add example to parts documentation [ci-skip] 2024-01-19 22:05:59 +09:00
Jean Boussier 44ef49c2d0
Merge pull request #50804 from joshuay03/consistently-raise-an-error-for-nested-attributes-args
[Fix #50803] Consistently raise an `ArgumentError` when passing an invalid argument to a nested attributes association writer
2024-01-19 13:29:01 +01:00
Jean Boussier 9d4a44d049 Freeze and dedup `quote_column_name` and `quote_table_name` caches
Avoid corrupting the cache by mutating the return value, and also
sligthly reduce memory usage when the quoting format often return
an unmodified string.
2024-01-19 12:42:02 +01:00
Joshua Young 1eab83a0a8 [Fix #50803] Consistently raise an `ArgumentError` when passing an invalid argument to a nested attributes association writer 2024-01-19 20:34:33 +10:00
Edouard CHIN 82d4ad5da3 Fix ActiveStorage::Blob inverse association:
- This is a fix needed to unblock
  https://github.com/rails/rails/pull/50284,
  because Active Storage relies on a Active Record bug.

  The problem is best understood with a small snippet:

  ```
    blob = ActiveStorage::Blob.new

    ActiveStorage::Attachment.new(blob: blob)
    ActiveStorage::Attachment.new(blob: blob)

    # Currently:
    p blob.attachments #=> #<ActiveRecord::Associations::CollectionProxy []>

    # Once the Active Record bug is fixed:
    p blob.attachments #=> #<ActiveRecord::Associations::CollectionProxy [#<ActiveStorage::Attachment id: nil, name: nil, record_type: nil, record_id: nil, blob_id: nil, created_at: nil>, #<ActiveStorage::Attachment id: nil, name: nil, record_type: nil, record_id: nil, blob_id: nil, created_at: nil>]>

    # Trying to save the blob would result in trying to create 2 attachments which
    # fails because of unique constrainsts.
  ```

  ### Code path

  The real code path that does what the snippet above does is located here:

  9c3ffab47c/activestorage/lib/active_storage/attached/many.rb (L52)

  It's basically doing this:

  ```
    user.images.attach "photo1.png"
    # Initialize a Blob record and an Attachment

    user.images.attach "photo2.png"
    # Get the Blob from above, create another Attachment
    # Initialize a new Blob record and an new Attachment

    # rinse and repeat every time `attach` is called
  ```

  Basically each time we call `attach`, we grab the previous blobs that were attached
  (and that already have an Attachment record), and

  ### Solution

  - Explicitly set the `inverse_of`, so that it behaves as if #50284 is shipped
  - Don't build a new attachment for blob already having one.

  ### Tests

  I didn't add tests, the test suite is already well covered, adding the `inverse_of`
  without any changes breaks the test suite already. You can try by running
  for instance the `activestorage/test/models/attached/many_test.rb`.
2024-01-19 04:18:15 +01:00
Jonathan Hefner 9c3ffab47c Fix RateLimitingTest assertion for :by option
Follow-up to #50788.

This test wasn't actually asserting that the `:by` option worked.
2024-01-18 18:43:43 -06:00
Jonathan Hefner c8bfd6870d
Merge pull request #50799 from jonathanhefner/command-fix-task-list-when-no-default-task
Fix `bin/rails -T` when no default task is defined
2024-01-18 18:43:18 -06:00
Eugene Kenny 36cc7c560b Remove unsupported values for config.action_dispatch.show_exceptions [ci skip]
Support for `true` and `false` was removed in
ec2c2666c2.
2024-01-19 00:21:26 +00:00
Jonathan Hefner f21e4299d6 Fix `bin/rails -T` when no default task is defined
When Rake parses an argument string with no tasks, it sets the top-level
task as "default".  Prior to this commit, if no default task was defined
(for example, if an app was generated with `--skip-test` and didn't
define its own default task), `Rails::Command::RakeCommand` would raise
`UnrecognizedCommandError`, preventing Rake from displaying the task
list.

This commit changes `Rails::Command::RakeCommand` to let Rake handle the
"default" task.

Fixes #50700.
2024-01-18 18:10:44 -06:00
Jonathan Hefner a18aa26f0c
Merge pull request #50798 from jonathanhefner/command-handle-unrecognized-bare-option
Print `bin/rails` help on unrecognized bare options
2024-01-18 18:10:23 -06:00
Jonathan Hefner dc85b953aa Print bin/rails help on unrecognized bare options
Prior to this commit, `bin/rails` would pass unrecognized bare options
on to Rake:

  ```console
  $ bin/rails -v
  Rails 7.2.0.alpha

  $ bin/rails -V
  rake, version 13.0.6

  $ bin/rails -s
  Running 0 tests in a single process (parallelization threshold is 50)
  ...

  $ bin/rails -S
  invalid option: -S
  ```

This commit changes `bin/rails` to print its help message when given an
unrecognized bare option:

  ```console
  $ bin/rails -v
  Rails 7.2.0.alpha

  $ bin/rails -V
  Usage:
    bin/rails COMMAND [options]

  You must specify a command. The most common commands are:
  ...

  $ bin/rails -s
  Usage:
    bin/rails COMMAND [options]

  You must specify a command. The most common commands are:
  ...

  $ bin/rails -S
  Usage:
    bin/rails COMMAND [options]

  You must specify a command. The most common commands are:
  ...
  ```

However, for backward compatibility, an exception has been made for the
`-T` / `--tasks` option:

  ```console
  $ bin/rails -T
  # Prints list of Rake tasks...
  ```

Addresses #50712.
2024-01-18 17:43:18 -06:00
Eugene Kenny 78fd6b7816 Remove outdated comment in ExceptionsWrapper#show? [ci skip]
Support for `true` and `false` was removed in
ec2c2666c2.
2024-01-18 21:31:32 +00:00
Jean Boussier 4816684c85
Merge pull request #50737 from skipkayhil/hm-further-optimize-tag-builder
Optimize TagBuilder tag generation
2024-01-18 19:19:27 +01:00
Jean Boussier 0a30d2bb3a
Merge pull request #50794 from Shopify/test-trigger-pk-population-against-more-dbs
[Tests only] Expand trigger populated PK test case to run against more DBs
2024-01-18 19:17:00 +01:00
Nikita Vasilevsky 24570678d6
[Tests only] Expand trigger populated PK test case to run against more DBs 2024-01-18 17:28:12 +00:00
Rafael Mendonça França 57034632f5
Merge pull request #50775 from mockdeep/rf-thread_fix
Fix threading issue with strict locals
2024-01-18 11:57:25 -05:00
Jean Boussier 8e0a702bf3
Merge pull request #50788 from Shopify/cleanup-file-store
`RateLimiting` Cleanups
2024-01-18 17:54:44 +01:00
Eileen M. Uchitelle 33aa383c20
Merge pull request #50783 from Shopify/append-pk-to-returning-columns-if-returning-is-empty
Always request `primary_key` in `RETURNING` if no other columns requested
2024-01-18 11:45:40 -05:00
Jean Boussier 4530a56e0f `RateLimiting` Cleanups
Followup: https://github.com/rails/rails/pull/50781

Some small issues I failed to address in the original PR.
2024-01-18 10:11:43 +01:00
Jean Boussier 8f386d7321
Merge pull request #50778 from fatkodima/fix-caching-async-queries
Fix async queries to work with query cache and other cached async queries
2024-01-17 20:44:38 +01:00
fatkodima 63ad11ab20 Fix async queries to work with query cache and other cached async queries 2024-01-17 21:32:14 +02:00
Nikita Vasilevsky c2c861f98a
Always request `primary_key` in `RETURNING` if no other columns requested
Prior 7.1 Rails always included `primary_key` in `RETURNING` clause on
record creation. This was changed in 7.1 to include more auto-populated
columns if such columns exist. This change lead to situations where
no columns were requested in `RETURNING` clause, even the `primary_key`.

This change brings back the old behavior of always requesting the
`primary_key` in `RETURNING` clause if no other columns are requested.
2024-01-17 18:19:40 +00:00
Jean Boussier 194ce13228 More AC::RateLimiting documentation fixes 2024-01-17 18:01:22 +01:00
Jean Boussier 10a34b7621 Touch up the AC::RateLimiting documentation 2024-01-17 17:59:49 +01:00
Jean Boussier 1205a751c5
Merge pull request #50781 from Shopify/as-cache-limitter
Refactor `ActionController::RateLimiting` to use `AS::Cache`
2024-01-17 17:49:27 +01:00
Jean Boussier d839ddb71a Refactor `ActionController::RateLimiting` to use `AS::Cache`
Given that the limiter implementation provided by Kredis is a simple
increment with a limit, all `ActiveSupport::Cache` already provide that
same capability, with a wide range of backing stores, and not just Redis.

This even allow to use SolidCache has a backend if you so desire.

If we feel particularly fancy, we could also accept a more generic
limiter interface to better allow users to swap the implementation
for better algorithms such as leaky-bucket etc.
2024-01-17 15:40:49 +01:00
Jean Boussier b54a287f9d Ensure all Cache store have consistent TTL behavior on increment
Make sure they all increment the counter but don't update the TTL.
2024-01-17 15:40:16 +01:00
Jean Boussier 25b7f64074
Merge pull request #50733 from yash/yk/fix-mysql-escapes-on-default-generated
Fix single quote escapes on default generated MySQL columns
2024-01-17 13:48:03 +01:00
Yash Kapadia 0702c24e7b Fix single quote escapes on default generated MySQL columns
MySQL 5.7.5+ supports generated columns, which can be used to create a column that is computed from an expression. This commit fixes the escaping of the default value for such expressions if a single quote is included.

See the following for more: https://dev.mysql.com/blog-archive/generated-columns-in-mysql-5-7-5/
2024-01-17 07:12:27 -05:00
Jean Boussier a9562e28c4
Merge pull request #50779 from Shopify/debug-ar-time
Fix `ActiveSupport::Notifications.publish_event` to preserve units
2024-01-17 13:07:32 +01:00
Jean Boussier de779f2bf7 Fix `ActiveSupport::Notifications.publish_event` to preserve units
Ref: https://github.com/rails/rails/pull/43502
Fix: https://github.com/rails/rails/pull/50767
Fix: https://github.com/rails/rails/pull/50493

When republishing a an event into a `start, finish` tuple, we need
to convert the timestamps back into seconds.
2024-01-17 12:53:40 +01:00
Yasuo Honda 2fb05958a6
Merge pull request #50756 from Angy-h/getting-started-update-screenshot
Update the screenshot in the section 8.4 of Getting Started with Rails [ci skip]
2024-01-17 18:11:47 +09:00