Commit Graph

70582 Commits

Author SHA1 Message Date
bogdanvlviv 0925d7340e
Clarify docs of `ActiveJob::TestHelper` [ci skip]
I found a few sentences that should be updated as well.
See https://github.com/rails/rails/pull/33571#discussion_r209435886

Follow up #33571
2018-10-16 00:26:18 +03:00
schneems f45267bc42 ActiveRecord#respond_to? No longer allocates strings
This is an alternative to https://github.com/rails/rails/pull/34195

The active record `respond_to?` method needs to do two things if `super` does not say that the method exists. It has to see if the "name" being passed in represents a column in the table. If it does then it needs to pass it to `has_attribute?` to see if the key exists in the current object. The reason why this is slow is that `has_attribute?` needs a string and most (almost all) objects passed in are symbols.

The only time we need to allocate a string in this method is if the column does exist in the database, and since these are a limited number of strings (since column names are a finite set) then we can pre-generate all of them and use the same string. 

We generate a list hash of column names and convert them to symbols, and store the value as the string name. This allows us to both check if the "name" exists as a column, but also provides us with a string object we can use for the `has_attribute?` call. 

I then ran the test suite and found there was only one case where we're intentionally passing in a string and changed it to a symbol. (However there are tests where we are using a string key, but they don't ship with rails).

As re-written this method should never allocate unless the user passes in a string key, which is fairly uncommon with `respond_to?`.

This also eliminates the need to special case every common item that might come through the method via the `case` that was originally added in f80aa59946 (by me) and then with an attempt to extend in https://github.com/rails/rails/pull/34195.

As a bonus this reduces 6,300 comparisons (in the CodeTriage app homepage) to 450 as we also no longer need to loop through the column array to check for an `include?`.
2018-10-15 15:31:32 -05:00
Francesco Rodríguez 134dab46e4 Deprecate ActiveSupport::Multibyte::Chars.consumes?
In favor of String#is_utf8?.

I think this method was made for internal use only, and its usage was removed here: https://github.com/rails/rails/pull/8261/files#diff-ce956ebe93786930e40f18db1da5fd46L39.
2018-10-15 13:11:59 -07:00
Ryuta Kamizono 25d1fefe87
Merge pull request #34214 from brasscapon/rails_five
Update snippet to rails 5 syntax

[ci skip]
2018-10-15 21:55:12 +09:00
Bogdan 6fe45f378a Fix `ActionController::Parameters#each_value` and add changelog entry to this method (#34210)
* Fix `ActionController::Parameters#each_value`

`each_value` should yield with "value" of the params instead of "value" as an array.
Related to #33979

* Add changelog entry about `ActionController::Parameters#each_value`.

Follow up #33979
2018-10-15 21:28:15 +09:00
Andrew White 777cc1da7d
Merge pull request #34135 from Edouard-chin/ec-rounded-duration
Fix issue where duration where always rounded up to a second:
2018-10-15 09:53:13 +01:00
Adam Demirel 4c15ed7753 Update snippet to rails 5 syntax 2018-10-15 15:14:48 +11:00
Ryuta Kamizono ecd46231ff Ensure to test that `project.developers` is ordered by `developers.name desc`
`developers.name desc` was added at d59f3a7, but any test case isn't
failed even if the `developers.name desc` is removed since all tested
developers are consistently ordered on both `name` and `id`.

I changed one developers creation ordering to ensure to test that
`project.developers` is ordered by `developers.name desc`.
2018-10-15 12:23:19 +09:00
Yuji Yaginuma bc2a77ab27
Merge pull request #34209 from y-yagi/fix_tests_on_mail_271
Fix tests on Mail 2.7.1
2018-10-15 05:40:48 +09:00
Rasesh Patel ecb2850a04 Show object ids in scaffold pages when displaying referenced objects
Resolve Issue#29200

When scaffolding a model that references another model the
generated show and index html pages display the object directly
on the page. Basically, it just shows a memory address. That is
not very helpful. In this commit we show the object's id rather
than the memory address.

This updates the scaffold templates and the json builder files.
2018-10-14 15:54:53 -04:00
yuuji.yaginuma d2d6f6b7db Fix tests on Mail 2.7.1
Up to `2.7.0`, encoding was chosen using `Mail::Encodings::TransferEncoding.negotiate`,
and base64 encoding was used.
In `2.7.1`, when `transfer_encoding` is not specified, the encoding
of the message is respected.
Related to: dead487e02

However, what chosen for transfer encoding is not essential in these tests.
To test more accurately, confirm that the decoded body instead.
2018-10-14 14:44:47 +09:00
yuuji.yaginuma 4a51cbba58 Bump mail to `2.7.1` 2018-10-14 08:33:40 +09:00
Edouard CHIN c85e3f65f3 Fix issue where duration where always rounded up to a second:
- Adding a Float as a duration to a datetime would result in the Float
  being rounded. Doing something like would have no effect because the
  0.45 seconds would be rounded to 0 second.

  ```ruby
    time = DateTime.parse("2018-1-1")
    time += 0.45.seconds
  ```

  This behavior was intentionally added a very long time ago, the
  reason was because Ruby 1.8 was using `Integer#gcd` in the
  constructor of Rational which didn't accept a float value.

  That's no longer the case and doing `Rational(0.45, 86400)` would
  now perfectly work fine.

- Fixes #34008
2018-10-12 13:27:13 -04:00
Dylan Thacker-Smith 99c87ad247 Improve model attribute accessor method names for backtraces
Ruby uses the original method name, so will show the __temp__ method
name in the backtrace. However, in the common case the method name
is compatible with the `def` keyword, so we can avoid the __temp__
method name in that case to improve the name shown in backtraces
or TracePoint#method_id.
2018-10-12 09:50:10 -07:00
Alan Wu 3ebc229034 Include deserialized arguments in jobs returned by AJ test helpers
`assert_enqueued_with` and `assert_performed_with` return a instantiated
instance of the matching job for further assertion (#21010).

Before this commit the `arguments` method on the returned instance
returns a serialized version of the arguments.
2018-10-12 12:15:16 -04:00
Francesco Rodríguez ee95bed3e6 Deprecate Unicode#normalize and Chars#normalize (#34202) 2018-10-12 08:40:29 -07:00
Jeremy Daer 619b2ae6a4
Merge pull request #34123 from frodsan/deprecate_unicode_string_wrappers
Deprecate Unicode#downcase/upcase/swapcase.
2018-10-12 07:36:15 -07:00
yuuji.yaginuma 29b1984a05 Runs the generator before assertions 2018-10-12 18:40:09 +09:00
Francesco Rodríguez 16e3b65674 Use native String#capitalize 2018-10-12 07:47:23 +02:00
Francesco Rodríguez e52b223487 Deprecate Unicode#downcase/upcase/swapcase.
Use String methods directly instead.
2018-10-12 07:42:51 +02:00
Eileen Uchitelle 8df7ed3b88 Test that nested structs to_json works as expected
Check that options passed to the to_json are passed to all objects that
respond to as_json.
2018-10-11 15:28:52 -04:00
Rafael França 92a796bf6d
Merge pull request #34185 from gmcgibbon/test_retry
Add test retries for railties
2018-10-11 13:33:37 -04:00
Gannon McGibbon d888bec6da Include test gems in CI 2018-10-11 10:43:30 -04:00
Gannon McGibbon 2e53c2b130 Add test retries for railties 2018-10-11 10:43:29 -04:00
Kasper Timm Hansen 81bbcea6cd
Merge pull request #34189 from orhantoy/fix-link-to-concurrent-ruby-docs
[ci skip] Fix link to Concurrent::ThreadPoolExecutor docs
2018-10-11 13:44:18 +02:00
Orhan Toy 6865f73b91 [ci skip] Fix link to Concurrent::ThreadPoolExecutor docs 2018-10-11 13:29:34 +02:00
Ryuta Kamizono 6a278c6e9a
Merge pull request #34188 from yskkin/remove_comment
Remove invalid magic comment [ci skip]
2018-10-11 17:10:53 +09:00
Yoshiyuki Kinjo e6a0c34be4 Remove invalid magic comment [ci skip] 2018-10-11 14:06:32 +09:00
Eileen M. Uchitelle fb8bee4a47
Merge pull request #34052 from eileencodes/connection-switching
Part 4: Multi db improvements, Basic API for connection switching
2018-10-10 13:58:17 -04:00
Eileen Uchitelle 31021a8c85 Basic API for connection switching
This PR adds the ability to 1) connect to multiple databases in a model,
and 2) switch between those connections using a block.

To connect a model to a set of databases for writing and reading use
the following API. This API supercedes `establish_connection`. The
`writing` and `reading` keys represent handler / role names and
`animals` and `animals_replica` represents the database key to look up
the configuration hash from.

```
class AnimalsBase < ApplicationRecord
  connects_to database: { writing: :animals, reading: :animals_replica }
end
```

Inside the application - outside the model declaration - we can switch
connections with a block call to `connected_to`.

If we want to connect to a db that isn't default (ie readonly_slow) we
can connect like this:

Outside the model we may want to connect to a new database (one that is
not in the default writing/reading set) - for example a slow replica for
making slow queries. To do this we have the `connected_to` method that
takes a `database` hash that matches the signature of `connects_to`. The
`connected_to` method also takes a block.

```
AcitveRecord::Base.connected_to(database: { slow_readonly: :primary_replica_slow }) do
  ModelInPrimary.do_something_thats_slow
end
```

For models that are already loaded and connections that are already
connected, `connected_to` doesn't need to pass in a `database` because
you may want to run queries against multiple databases using a specific
role/handler.

In this case `connected_to` can take a `role` and use that to swap on
the connection passed. This simplies queries - and matches how we do it
in GitHub. Once you're connected to the database you don't need to
re-connect, we assume the connection is in the pool and simply pass the
handler we'd like to swap on.

```
ActiveRecord::Base.connected_to(role: :reading) do
  Dog.read_something_from_dog
  ModelInPrimary.do_something_from_model_in_primary
end
```
2018-10-10 12:13:46 -04:00
Eileen M. Uchitelle 58999af99f
Merge pull request #34110 from albertoalmagro/enum-raises-on-invalid-definition-values
Enum raises on invalid definition values
2018-10-10 11:42:41 -04:00
Eileen M. Uchitelle 9413ef56e5
Merge pull request #34137 from gmcgibbon/db_migrate_status_multi_db
Add multi-db support to rails db:migrate:status
2018-10-10 11:41:42 -04:00
Eileen M. Uchitelle 8a1cc51ed0
Merge pull request #34109 from bogdanvlviv/follow-up-34064
Follow up #34064
2018-10-10 09:42:03 -04:00
Eileen M. Uchitelle 8ecb75e405
Merge pull request #34136 from rails/add-allocations-to-template-renderer
Add allocations to template renderer subscription
2018-10-10 09:31:31 -04:00
Eileen Uchitelle e8c1be4ae7 Add allocations to template renderer subscription
This PR adds the allocations to the instrumentation for template and
partial rendering.

Before:

```
  Rendering posts/new.html.erb within layouts/application
  Rendered posts/_form.html.erb (9.7ms)
  Rendered posts/new.html.erb within layouts/application (10.9ms)
Completed 200 OK in 902ms (Views: 890.8ms | ActiveRecord: 0.8ms)
```

After:

```
  Rendering posts/new.html.erb within layouts/application
  Rendered posts/_form.html.erb (Duration: 7.1ms | Allocations: 6004)
  Rendered posts/new.html.erb within layouts/application (Duration: 8.3ms | Allocations: 6654)
Completed 200 OK in 858ms (Views: 848.4ms | ActiveRecord: 0.4ms | Allocations: 1539564)
```
2018-10-10 08:07:12 -04:00
Alberto Almagro df11558942 Privatize ENUM_CONFLICT_MESSAGE constant 2018-10-10 10:22:10 +02:00
Alberto Almagro 9b9640112e Raise on invalid definition values
When defining a Hash enum it can be easy to use [] instead of {}. This
commit checks that only valid definition values are provided, those can
be a Hash, an array of Symbols or an array of Strings. Otherwise it
raises an ArgumentError.

Fixes #33961
2018-10-10 10:22:07 +02:00
Ryuta Kamizono a52c6989a0
Merge pull request #34122 from kamipo/generate_relation_methods
Generate delegation methods to named scope in the definition time
2018-10-10 16:59:21 +09:00
Ryuta Kamizono 6c69a96048
Merge pull request #34094 from christophemaximin/fix-activerecord-clearing-of-query-cache
Fix inconsistent behavior by clearing QueryCache when reloading associations
2018-10-10 16:51:14 +09:00
Ryuta Kamizono 301409a98f Call `load_schema` before `assert_no_queries`
Follow up 45be690f8e.

`predicate_builder.build` in `where` requires `load_schema` for
`type_for_attribute`.
2018-10-10 09:50:22 +09:00
Christophe Maximin 961776832d Clear QueryCache when reloading associations 2018-10-10 00:17:05 +01:00
Ryuta Kamizono e0d147fa90 Fix odd indentation 2018-10-10 08:03:06 +09:00
Ryuta Kamizono f569955e06 Refactor Arel visitor to use `collect_nodes_for` as much as possible 2018-10-10 06:48:20 +09:00
Gannon McGibbon 287c0de8a1 Add multi-db support to rails db:migrate:status 2018-10-09 16:40:06 -04:00
Ryuta Kamizono 92ccb7c75f Improve DELETE with JOIN handling to avoid subqueries if possible
Before:

```
  Pet Destroy (0.8ms)  DELETE FROM `pets` WHERE `pets`.`pet_id` IN (SELECT `pet_id` FROM (SELECT DISTINCT `pets`.`pet_id` FROM `pets` LEFT OUTER JOIN `toys` ON `toys`.`pet_id` = `pets`.`pet_id` WHERE `toys`.`name` = ?) AS __active_record_temp)  [["name", "Bone"]]
```

After:

```
  Pet Destroy (1.0ms)  DELETE `pets` FROM `pets` LEFT OUTER JOIN `toys` ON `toys`.`pet_id` = `pets`.`pet_id` WHERE `toys`.`name` = ?  [["name", "Bone"]]
```
2018-10-10 05:31:20 +09:00
Eileen M. Uchitelle 3101a4136b
Merge pull request #34071 from y-yagi/skip_webpacker_install
Add `skip-webpack-install` option
2018-10-09 15:52:14 -04:00
Gannon McGibbon 168a9728dd
Merge pull request #34075 from hakusaro/guides-reference-leases
Change contributing guide to suggest using safer force push
2018-10-09 14:38:39 -04:00
Eileen M. Uchitelle 1ddfee9d11
Merge pull request #34081 from gmcgibbon/db_migrate_status_move
Move db:migrate:status to DatabaseTasks method
2018-10-09 14:34:26 -04:00
Gannon McGibbon 88813a98b9
Merge pull request #34105 from zvkemp/correct-asn-docs
clarify role of unique_id in ActiveSupport::Notifications [ci skip]
2018-10-09 13:58:39 -04:00
Ryuta Kamizono 82ac2114a1
Merge pull request #34117 from aergonaut/docs/ActiveRecord--Persistence-belongs_to_touch_method
Add docs to ActiveRecord::Persistence#belongs_to_touch_method

[ci skip]
2018-10-10 02:38:56 +09:00