Commit Graph

77502 Commits

Author SHA1 Message Date
Eileen M. Uchitelle
9a36b50b84
Merge branch 'master' into combine-structure-and-schema-tasks 2020-08-17 08:47:51 -04:00
Eileen M. Uchitelle
6508a5f25d
Merge pull request #39886 from lanzhiheng/append-some-description-about-advance-constraints-in-block-form
[ci skip] Append some description about advance constraints
2020-08-17 08:45:16 -04:00
Eileen M. Uchitelle
443c8470d1
Merge pull request #39939 from Shopify/link-preload-headers
Automatically set Link header for each stylesheet and script
2020-08-17 08:37:22 -04:00
Ryuta Kamizono
9078abc0b8
Merge pull request #39999 from kamipo/fix_eager_loading_with_explicit_non_select
Fix eager loading that non-select columns will be loaded
2020-08-17 14:34:32 +09:00
Ryuta Kamizono
4aa6559beb
Merge pull request #40056 from kamipo/fix_preloader_associate_by_default
Fix preloader to associate preloaded records by default
2020-08-17 12:35:34 +09:00
Ryuta Kamizono
4639318215 Fix eager loading that non-select columns will be loaded
Related to #35210.

We sometimes use `select` to limit unused columns for performance.

For example, `GET /posts/1` (post detail) usually use (almost) all
columns, but `GET /posts` (post list) does not always use all columns
(e.g. use `id` and `title` for the list view, but `body` is not used).

If an association is eager loaded, the limited `select` doesn't works as
expected, eager loading will load all columns on the model, plus also
load the `select` columns additionally. It works differently with
natural load and preload. It means that changing natural load or preload
to eager load (or vice versa) is unsafe.

This fixes eager loading that always load all columns (plus extra
`select` columns), to respect the `select` columns like as others.

```ruby
post = Post.select("UPPER(title) AS title").first
post.title # => "WELCOME TO THE WEBLOG"
post.body  # => ActiveModel::MissingAttributeError

# Rails 6.0 (ignore the `select` values)
post = Post.select("UPPER(title) AS title").eager_load(:comments).first
post.title # => "Welcome to the weblog"
post.body  # => "Such a lovely day"

# Rails 6.1 (respect the `select` values)
post = Post.select("UPPER(title) AS title").eager_load(:comments).first
post.title # => "WELCOME TO THE WEBLOG"
post.body  # => ActiveModel::MissingAttributeError
```
2020-08-17 12:05:43 +09:00
Ryuta Kamizono
f80179db90 Fix preloader to associate preloaded records by default
Someone had relied on the behavior that preloading with a given scope,
but the behavior has lost in #35496 to fix the minor bug that unloading
through association.

Basically we don't guarantee the internal behavior, but the bugfix can
be achieved without any breaking change, so I've restored the lost
functionality.

Fixes #36638.
Fixes #37720.
2020-08-16 07:52:13 +09:00
Daniel Colson
d72ade10a6
Merge pull request #40035 from KapilSachdev/fix/rails_book_link
fix(guides): Update AWDwR book link in Releasing Rails guide [ci skip]
2020-08-15 18:46:26 -04:00
Eugene Kenny
2960709e8f
Merge pull request #39819 from p8/improved-inspect-for-view-context
Use superclass name in inspect of anonymous ActionView::Base subclass
2020-08-15 17:06:54 +01:00
Petrik
a7ed92bd1d Use superclass name in inspect of ActionView::Base.with_empty_template_cache
When rendering views an anonymous subclass is created by calling
ActionView::Base.with_empty_template_cache.
This causes inspect to return an unhelpful description when calling
inspect:
`#<#<Class:0x012345012345>:<0x012345012345>`.

This can be confusing when exceptions are raised because it's hard to
figure out where to look. For example calling an undefined method in a
template would raise the following exception:

    undefined method `undefined' for #<#<Class:0x012345012345>:<0x012345012345>

Instead we can return the non-anonymous superclass name.

    undefined method `undefined' for #<ActionView::Base:0x01234502345>

The anonymous class is created in ActionView::Base.with_empty_template_cache.
See f9bea6304dfba902b1937b3bc29b1ebc2f67e55b
This seems to be done for performance reasons only, without expecting a
change to calling `inspect`.
2020-08-15 11:04:28 +02:00
Jonathan Hefner
8a578c4685
Merge pull request #39905 from mehagar/testing_docs
Remove double negative in testing docs [ci-skip]
2020-08-14 18:48:16 -05:00
Michael Hagar
a3e1590e96 epsilon invert
Update guides/source/testing.md

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2020-08-14 17:06:21 -05:00
Ryuta Kamizono
8162c88186
Merge pull request #40043 from kamipo/fix_eager_loading_duplicated_association_part2
Fix incorrect result when eager loading with duplicated through association with join scope Part 2
2020-08-15 06:54:47 +09:00
Jonathan Hefner
30444ff0ca
Merge pull request #40050 from bentranter/active-storage-docs-fix-digitalocean-spelling
Fix spelling of DigitalOcean in docs [ci-skip]
2020-08-14 13:34:49 -05:00
Ben Tranter
a281d1a712
Fix spelling of DigitalOcean in docs
Fixes the spelling of DigitalOcean in the ActiveStorage guide.
2020-08-14 14:11:47 -04:00
Eileen M. Uchitelle
1f9e308695
Merge pull request #40049 from eileencodes/remove-unnecessary-compact
Remove unnecessary `compact`
2020-08-14 13:47:28 -04:00
eileencodes
269a184c19
Remove unnecessary compact
Looking at the history this is holdover from the pre-pool manager and
changes made by Shopify. The tests pass without it and we've had enough
changes that it doesn't appear necessary anymore.

Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
2020-08-14 13:15:49 -04:00
Eileen M. Uchitelle
a22df78626
Merge pull request #40048 from eileencodes/fix-incorrect-removal-of-current_shard-from-establish_connection
Fix incorrect removal of current_shard in establish_connection
2020-08-14 13:00:14 -04:00
eileencodes
ba2f38e498
Fix incorrect removal of current_shard in establish_connection
If we enter a `connected_to` block and call `establish_connection` like
the test added here we need to ensure that `shard: current_shard` is passed
to the handler, otherwise the connection will be established on
`default` not on `shard_one`.

Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
2020-08-14 11:52:02 -04:00
Eileen M. Uchitelle
c93f3f0226
Merge pull request #40046 from composerinteralia/renaming-man
Rename test models for inclusivity
2020-08-14 11:51:37 -04:00
Daniel Colson
d79519f2ff
Rename single letter variables
After renaming `Man` to `Human` the variable letter `m` in these tests
ends up being pretty confusing. Rather than rename it to `h`, this
commit replaces it with the full word `human`.

Since I was already renaming things, I also went ahead and replaced `f`
with `face`, `i` with `interest`, and `a` with `author`.
2020-08-14 11:37:09 -04:00
Daniel Colson
7f938cacba
Replace test Man with Human
The commit replaces the `Man` model used in tests with a `Human` model. It
also replaces the existing `Human` model with a `SuperHuman` model
inheriting from `Human`.

While this may seem like a cosmetic change, I see it as more of an
inclusivity change. I think it makes sense for a number of reasons:

* Prior to this commit the `Human` model inherited from `Man`. At best
  this makes no sense (it should be the other way around). At worst it
  is offensive and harmful to the community.
* It doesn't seem inclusive to me to have exclusively male-gendered
  examples in the codebase.
* There is no particular reason for these examples to be gendered.
* `man` is hard to grep for, since it also matches `many, manager,
  manual, etc`

For the most part this is a simple search and replace. The one exception
to that is that I had to add the table name to the model so we could use
"humans" instead of "humen".
2020-08-14 11:37:09 -04:00
Ryuta Kamizono
62de28fbcd Fix incorrect result when eager loading with duplicated through association with join scope Part 2
Follow up of #40000.

In #40000, `eager_load(:general_categorizations, :general_posts)` works,
but `eager_load(:general_posts, :general_categorizations)` doesn't work
yet.

This implements the deduplication for the case of reversed eager loading
order.
2020-08-14 14:34:44 +09:00
Jonathan Hefner
601006c56d
Update service metadata for updated Blobs only
Follow-up to #40013.

Newly created `Blob`s may not be uploaded yet, so do not try to update
their service metadata.
2020-08-13 10:45:46 -04:00
KapilSachdev
5027d2dc1a fix: Update Agile Web Development with Rails book link [ci skip] 2020-08-13 10:36:46 +05:30
Jonathan Hefner
b68eea3795
Merge pull request #39732 from sandip-mane/38855-habtm-optional-docs
Adds documentation for habtm association always being optional: true [ci skip]
2020-08-12 12:59:02 -05:00
Sandip Mane
810f2a65c3 Added docs for habtm association about the declaring model referring to zero or more associations
parent f4471fc728d372861095d8f0a5b19831e316ead7
author Sandip Mane <sandip.mane@bigbinary.com> 1593247595 +0530
committer Sandip Mane <sandip2490@gmail.com> 1597253412 +0530

Adds doc for habtm association for always optional: true

Added docs line under definition of habtm with a text containing habtm refers to zero or more associations

Added docs line under definition of habtm with a text containing it refers to zero or more associations

Updated the sentence to include declaring association for habtm relation
2020-08-12 23:01:00 +05:30
Jonathan Hefner
f4471fc728
Merge pull request #39971 from jonathanhefner/docs-baseline-defaults
Add baseline defaults section [ci skip]
2020-08-12 08:06:06 -05:00
Jonathan Hefner
e6026143bc
Merge pull request #39950 from sohymg/patch-1
[ci skip] Add clarity on protect_from_forgery documentation
2020-08-12 08:01:11 -05:00
Ryuta Kamizono
0672be9bf3
Merge pull request #40027 from yahonda/diag_38697_2
Address `ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true`
2020-08-12 20:07:59 +09:00
Yasuo Honda
2d43d47786 Address ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
This pull request fixes #38697

It is caused by `@controller.singleton_class.include @routes.url_helpers` when `@controller` is nil in `ActionController::TestCase`.

* Without this commit

```ruby
% cd actionview
% PARALLEL_WORKERS=1 bin/test test/actionpack/controller/layout_test.rb test/template/url_helper_test.rb --seed 16702 -n "/^(?:LayoutSetInResponseTest#(?:test_layout_symbol_set_in_controller_returning_nil_falls_back_to_default)|UrlHelperTest#(?:test_url_for_with_array_and_only_path_set_to_false))$/"

Run options: --seed 16702 -n "/^(?:LayoutSetInResponseTest#(?:test_layout_symbol_set_in_controller_returning_nil_falls_back_to_default)|UrlHelperTest#(?:test_url_for_with_array_and_only_path_set_to_false))$/"

.E

Error:
UrlHelperTest#test_url_for_with_array_and_only_path_set_to_false:
ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
    /Users/yahonda/src/github.com/rails/rails/actionpack/lib/action_dispatch/http/url.rb:64:in `full_url_for'
    /Users/yahonda/src/github.com/rails/rails/actionpack/lib/action_dispatch/http/url.rb:54:in `url_for'
    /Users/yahonda/src/github.com/rails/rails/actionpack/lib/action_dispatch/routing/route_set.rb:333:in `block in <class:RouteSet>'
    /Users/yahonda/src/github.com/rails/rails/actionpack/lib/action_dispatch/routing/route_set.rb:838:in `url_for'
    /Users/yahonda/src/github.com/rails/rails/actionpack/lib/action_dispatch/routing/route_set.rb:270:in `call'
    /Users/yahonda/src/github.com/rails/rails/actionpack/lib/action_dispatch/routing/route_set.rb:213:in `call'
    /Users/yahonda/src/github.com/rails/rails/actionpack/lib/action_dispatch/routing/route_set.rb:326:in `block in define_url_helper'
    /Users/yahonda/src/github.com/rails/rails/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb:233:in `polymorphic_method'
    /Users/yahonda/src/github.com/rails/rails/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb:116:in `polymorphic_url'
    /Users/yahonda/src/github.com/rails/rails/actionview/lib/action_view/routing_url_for.rb:104:in `url_for'
    /Users/yahonda/src/github.com/rails/rails/actionview/test/template/url_helper_test.rb:102:in `test_url_for_with_array_and_only_path_set_to_false'

bin/test test/template/url_helper_test.rb:100

Finished in 0.042275s, 47.3093 runs/s, 47.3093 assertions/s.
2 runs, 2 assertions, 0 failures, 1 errors, 0 skips
%
```
2020-08-12 19:43:02 +09:00
Yu Ming
28c5eca9a1 Updated security.md to include default_protect_from_forgery
Update security.md to note that CSRF security token is included in requests automatically when `config.action_controller.default_protect_from_forgery` is set to `true`, which is the default for newly created Rails applications.
2020-08-12 18:23:40 +08:00
Ryuta Kamizono
e4b5ced331
Merge pull request #40025 from KapilSachdev/fix/uninitialized_instance_variable
fix: warning: instance variable @controller not initialized

Co-authored-by: Samuel Williams <samuel.williams@oriontransfer.co.nz>
2020-08-12 18:17:42 +09:00
Jonathan Hefner
6aa26c30e2
Identify directly-uploaded blobs before saving the associated record
An Active Storage `Blob` must be identified before it can be reliably
validated.  For direct uploads, a `Blob` is identified when it is
attached, rather than when it is created.

Before this commit, the sequence of events when attaching a `Blob` was:

1. Find the `Blob`.
2. Assign the `Blob` to an `Attachment`.
3. Save the owner record.
4. Save the `Attachment`.
5. Identify the `Blob`'s true `content_type` from its file.
6. Save the `Blob`.

This meant that the owner record's validations might not see the
`Blob`'s true `content_type`.

After this commit, the sequence of events will be:

1. Find the `Blob`.
2. Identify the `Blob`'s true `content_type` from its file.
3. Assign the `Blob` to an `Attachment`.
4. Save the owner record.
5. Save the `Attachment`.
6. Save the `Blob`.

Thus the `Blob`'s true `content_type` will be available when running the
owner record's validations.
2020-08-11 18:08:09 -04:00
Jonathan Hefner
b32a614d9a
Merge pull request #40024 from abhaynikam/document-multidb-dbconsole-command
Document dbconsole command for multiple database [ci skip]
2020-08-11 11:38:42 -05:00
Abhay Nikam
d747b53172 Document the --database/--db option for multiple database Rails application [skip ci] 2020-08-11 21:37:37 +05:30
KapilSachdev
dabece17ad fix: warning: instance variable @controller not initialized
Fixes #39937
2020-08-11 20:37:27 +05:30
Gannon McGibbon
23a9e29d9b
Merge pull request #39981 from gmcgibbon/mounted_root_route_assert
Fix `assert_recognizes` on mounted root routes.
2020-08-10 17:20:47 -04:00
Eileen M. Uchitelle
685683fd02
Merge pull request #40017 from eileencodes/fix-missed-handler-method
Fix missed establish_connection
2020-08-10 17:07:58 -04:00
Gannon McGibbon
8d4d0f3701 Fix assert_recognizes on mounted root routes.
Allow `assert_recognizes` routing assertions to work on mounted root routes.
2020-08-10 16:59:52 -04:00
eileencodes
919eb6dca9
Fix missed establish_connection
In `connected_to` one of the deprecated arguments wasn't well tested so
the incorrect methods signature wasn't caught by the tests.

This change updates the caller when `connected_to` uses the database
key.

I've also cleaned up a few arguments that weren't necessary. Since
the handler methods set defaults for the `shard` key, we don't need to
pass that in `establish_connection` when not using the sharding API.
2020-08-10 16:31:04 -04:00
Eileen M. Uchitelle
64915e5b7f
Merge pull request #40014 from eileencodes/move-handler-cleanup-to-helper-method
Add helper method for resetting connection handlers in tests
2020-08-10 10:41:50 -04:00
eileencodes
61ab154316
Add helper method for resetting connection handlers in tests
This change makes a helper method for resetting connection handlers in
the Active Record tests. The change here is relatively small and may
seem unnecessary. The reason we're pushing this change is for upcoming
refactoring to connection management. This change will mean that we can
update one location instead of 9+ files to reset connections. It will
reduce the diff of our refactoring and make reusing this code easier in
the future.

The method name chosen is purposefully `clean_up_connection_handler`
over `clean_up_connection_handlers` because in the future there will
only be one handler.

Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
2020-08-10 10:20:12 -04:00
Eileen M. Uchitelle
f2019462f9
Merge pull request #40006 from eileencodes/rename-pool-key-and-use-kwargs
Rename `pool_key` to `shard` and use kwargs for connection methods
2020-08-10 08:53:16 -04:00
Ryuta Kamizono
f9315d2ead
Merge pull request #40000 from kamipo/fix_eager_loading_duplicated_association
Fix incorrect result when eager loading with duplicated through association with join scope
2020-08-10 15:41:22 +09:00
Corey Smith
3c500511e9
Remove the unnessecary default: 'gen_random_uuid()' (#40012) 2020-08-08 15:11:34 +05:30
Eileen M. Uchitelle
5d3b9f7c87
Merge pull request #40003 from eileencodes/remove-unnecessary-with_temporary_connection_pool-calls
Remove unnecessary with_temporary_connection_pool calls
2020-08-07 14:09:13 -04:00
eileencodes
d3061cdab2
Update connection methods to use kwargs
This change ensures that the connection methods are using kwargs instead
of positional arguments. This change may look unnecessary but we're
working on refactoring connection management to make it more robust and
flexible so the method signatures of the methods changed here will
continue to evolve and change.

This commit does not change any released public APIs. The `shard` and
`owner_name` arguments were added in 6.1 which is not released yet.
Using kwargs will allow these methods to be more flexible and not get
super ugly as we change their underlying behavior. The kwargs let us
support multiple non-positional arguments with default.

Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
2020-08-07 14:06:09 -04:00
eileencodes
85ef219621
Rename pool_key to shard
This change renames the following:

* `current_pool_key` -> `current_shard`
* `default_pool_key` -> `default_shard`
* `pool_key` -> `shard`

Originally we had intended to name the `shard` as `pool_key` because
when we implemented the internal private API we weren't sure how it was
going to be used for sharding and wanted to implement behavior without
promising a public API. Now that we have a public API for sharding it's
better to use the same name everywhere rather than have one name for
private APIs and one name for public APIs. This should make
contributions and tracking down bugs easier in the future.

This PR doesn't require any deprecations because the sharding API is
unreleased and so is all the internal code that was using `pool_key`.

Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
2020-08-07 13:43:18 -04:00
eileencodes
9289232bc9
Remove unnecessary with_temporary_connection_pool calls
While debugging a different problem I'm working on I realized that this
method `with_temporary_connection_pool` isn't necessary in most of the
cases we're using it for.

Anywhere we establish new connections inside the block won't throw away
those new connections. I also removed this from places that can use the
existing connection and don't need a new temporary pool. I'm not sure if
this file was using it in many places because of copy / paste or real
issues that are no longer present.
2020-08-07 13:36:03 -04:00