activerecord 6.1.0.rc1 → 6.1.2.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activerecord might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +177 -20
- data/README.rdoc +1 -1
- data/lib/active_record/aggregations.rb +4 -4
- data/lib/active_record/association_relation.rb +10 -0
- data/lib/active_record/associations.rb +6 -2
- data/lib/active_record/associations/association.rb +14 -9
- data/lib/active_record/associations/association_scope.rb +7 -5
- data/lib/active_record/associations/belongs_to_association.rb +7 -3
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +7 -2
- data/lib/active_record/associations/builder/association.rb +23 -2
- data/lib/active_record/associations/builder/belongs_to.rb +2 -2
- data/lib/active_record/associations/has_many_association.rb +1 -1
- data/lib/active_record/associations/join_dependency.rb +2 -2
- data/lib/active_record/associations/join_dependency/join_association.rb +8 -7
- data/lib/active_record/attribute_methods.rb +13 -7
- data/lib/active_record/attribute_methods/serialization.rb +8 -2
- data/lib/active_record/attributes.rb +6 -1
- data/lib/active_record/callbacks.rb +121 -1
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +8 -3
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +1 -1
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +4 -0
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +7 -1
- data/lib/active_record/connection_adapters/abstract/transaction.rb +24 -18
- data/lib/active_record/connection_adapters/abstract_adapter.rb +23 -8
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +6 -10
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +1 -2
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +4 -1
- data/lib/active_record/connection_adapters/pool_config.rb +13 -3
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +8 -0
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +2 -2
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +1 -3
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +1 -1
- data/lib/active_record/connection_handling.rb +11 -3
- data/lib/active_record/core.rb +51 -32
- data/lib/active_record/database_configurations/url_config.rb +1 -1
- data/lib/active_record/enum.rb +58 -31
- data/lib/active_record/fixtures.rb +4 -1
- data/lib/active_record/gem_version.rb +2 -2
- data/lib/active_record/insert_all.rb +2 -0
- data/lib/active_record/internal_metadata.rb +2 -4
- data/lib/active_record/locking/optimistic.rb +14 -4
- data/lib/active_record/log_subscriber.rb +3 -2
- data/lib/active_record/migration/compatibility.rb +2 -1
- data/lib/active_record/model_schema.rb +29 -0
- data/lib/active_record/railtie.rb +2 -2
- data/lib/active_record/railties/console_sandbox.rb +2 -4
- data/lib/active_record/railties/databases.rake +29 -7
- data/lib/active_record/reflection.rb +2 -1
- data/lib/active_record/relation/predicate_builder.rb +6 -9
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +3 -4
- data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +9 -5
- data/lib/active_record/relation/query_methods.rb +9 -6
- data/lib/active_record/relation/spawn_methods.rb +2 -2
- data/lib/active_record/schema_migration.rb +2 -4
- data/lib/active_record/signed_id.rb +1 -1
- data/lib/active_record/table_metadata.rb +10 -3
- data/lib/active_record/tasks/database_tasks.rb +1 -0
- data/lib/active_record/transactions.rb +4 -2
- metadata +12 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 61210a9bf3705c22919bfa2c07869d5d6f2d634e9483344e861b30b73e77201a
|
4
|
+
data.tar.gz: 0e74625e0dfc674cbcac3a750d628e3ebb0df0e21b4277d72bc1699c403f6e8e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8482b26acd2e28d94d9eeb53e0bcb899e387318c2ef122c8edc9a9243ee3aec0f0d47f8fc3397aea738df4f9c033eff28e813fc8cb12577a4c195594414275f2
|
7
|
+
data.tar.gz: 255438e998bfe3c7a6364c15795416b8d116b6a2babc27718090d5dcc99cbb2f31d63a5cce5bceaffb6a32608e707fda33ba62e29c44cc1ce573617edaed22c8
|
data/CHANGELOG.md
CHANGED
@@ -1,4 +1,161 @@
|
|
1
|
-
## Rails 6.1.
|
1
|
+
## Rails 6.1.2.1 (February 10, 2021) ##
|
2
|
+
|
3
|
+
* Fix possible DoS vector in PostgreSQL money type
|
4
|
+
|
5
|
+
Carefully crafted input can cause a DoS via the regular expressions used
|
6
|
+
for validating the money format in the PostgreSQL adapter. This patch
|
7
|
+
fixes the regexp.
|
8
|
+
|
9
|
+
Thanks to @dee-see from Hackerone for this patch!
|
10
|
+
|
11
|
+
[CVE-2021-22880]
|
12
|
+
|
13
|
+
*Aaron Patterson*
|
14
|
+
|
15
|
+
|
16
|
+
## Rails 6.1.2 (February 09, 2021) ##
|
17
|
+
|
18
|
+
* Fix timestamp type for sqlite3.
|
19
|
+
|
20
|
+
*Eileen M. Uchitelle*
|
21
|
+
|
22
|
+
* Make destroy async transactional.
|
23
|
+
|
24
|
+
An active record rollback could occur while enqueuing a job. In this
|
25
|
+
case the job would enqueue even though the database deletion
|
26
|
+
rolledback putting things in a funky state.
|
27
|
+
|
28
|
+
Now the jobs are only enqueued until after the db transaction has been committed.
|
29
|
+
|
30
|
+
*Cory Gwin*
|
31
|
+
|
32
|
+
* Fix malformed packet error in MySQL statement for connection configuration.
|
33
|
+
|
34
|
+
*robinroestenburg*
|
35
|
+
|
36
|
+
* Connection specification now passes the "url" key as a configuration for the
|
37
|
+
adapter if the "url" protocol is "jdbc", "http", or "https". Previously only
|
38
|
+
urls with the "jdbc" prefix were passed to the Active Record Adapter, others
|
39
|
+
are assumed to be adapter specification urls.
|
40
|
+
|
41
|
+
Fixes #41137.
|
42
|
+
|
43
|
+
*Jonathan Bracy*
|
44
|
+
|
45
|
+
* Fix granular connection swapping when there are multiple abstract classes.
|
46
|
+
|
47
|
+
*Eileen M. Uchitelle*
|
48
|
+
|
49
|
+
* Fix `find_by` with custom primary key for belongs_to association.
|
50
|
+
|
51
|
+
*Ryuta Kamizono*
|
52
|
+
|
53
|
+
* Add support for `rails console --sandbox` for multiple database applications.
|
54
|
+
|
55
|
+
*alpaca-tc*
|
56
|
+
|
57
|
+
* Fix `where` on polymorphic association with empty array.
|
58
|
+
|
59
|
+
*Ryuta Kamizono*
|
60
|
+
|
61
|
+
* Fix preventing writes for `ApplicationRecord`.
|
62
|
+
|
63
|
+
*Eileen M. Uchitelle*
|
64
|
+
|
65
|
+
|
66
|
+
## Rails 6.1.1 (January 07, 2021) ##
|
67
|
+
|
68
|
+
* Fix fixtures loading when strict loading is enabled for the association.
|
69
|
+
|
70
|
+
*Alex Ghiculescu*
|
71
|
+
|
72
|
+
* Fix `where` with custom primary key for belongs_to association.
|
73
|
+
|
74
|
+
*Ryuta Kamizono*
|
75
|
+
|
76
|
+
* Fix `where` with aliased associations.
|
77
|
+
|
78
|
+
*Ryuta Kamizono*
|
79
|
+
|
80
|
+
* Fix `composed_of` with symbol mapping.
|
81
|
+
|
82
|
+
*Ryuta Kamizono*
|
83
|
+
|
84
|
+
* Don't skip money's type cast for pluck and calculations.
|
85
|
+
|
86
|
+
*Ryuta Kamizono*
|
87
|
+
|
88
|
+
* Fix `where` on polymorphic association with non Active Record object.
|
89
|
+
|
90
|
+
*Ryuta Kamizono*
|
91
|
+
|
92
|
+
* Make sure `db:prepare` works even the schema file doesn't exist.
|
93
|
+
|
94
|
+
*Rafael Mendonça França*
|
95
|
+
|
96
|
+
* Fix complicated `has_many :through` with nested where condition.
|
97
|
+
|
98
|
+
*Ryuta Kamizono*
|
99
|
+
|
100
|
+
* Handle STI models for `has_many dependent: :destroy_async`.
|
101
|
+
|
102
|
+
*Muhammad Usman*
|
103
|
+
|
104
|
+
* Restore possibility of passing `false` to :polymorphic option of `belongs_to`.
|
105
|
+
|
106
|
+
Previously, passing `false` would trigger the option validation logic
|
107
|
+
to throw an error saying :polymorphic would not be a valid option.
|
108
|
+
|
109
|
+
*glaszig*
|
110
|
+
|
111
|
+
* Allow adding nonnamed expression indexes to be revertible.
|
112
|
+
|
113
|
+
Fixes #40732.
|
114
|
+
|
115
|
+
Previously, the following code would raise an error, when executed while rolling back,
|
116
|
+
and the index name should be specified explicitly. Now, the index name is inferred
|
117
|
+
automatically.
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
add_index(:items, "to_tsvector('english', description)")
|
121
|
+
```
|
122
|
+
|
123
|
+
*fatkodima*
|
124
|
+
|
125
|
+
|
126
|
+
## Rails 6.1.0 (December 09, 2020) ##
|
127
|
+
|
128
|
+
* Only warn about negative enums if a positive form that would cause conflicts exists.
|
129
|
+
|
130
|
+
Fixes #39065.
|
131
|
+
|
132
|
+
*Alex Ghiculescu*
|
133
|
+
|
134
|
+
* Change `attribute_for_inspect` to take `filter_attributes` in consideration.
|
135
|
+
|
136
|
+
*Rafael Mendonça França*
|
137
|
+
|
138
|
+
* Fix odd behavior of inverse_of with multiple belongs_to to same class.
|
139
|
+
|
140
|
+
Fixes #35204.
|
141
|
+
|
142
|
+
*Tomoyuki Kai*
|
143
|
+
|
144
|
+
* Build predicate conditions with objects that delegate `#id` and primary key:
|
145
|
+
|
146
|
+
```ruby
|
147
|
+
class AdminAuthor
|
148
|
+
delegate_missing_to :@author
|
149
|
+
|
150
|
+
def initialize(author)
|
151
|
+
@author = author
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
Post.where(author: AdminAuthor.new(author))
|
156
|
+
```
|
157
|
+
|
158
|
+
*Sean Doyle*
|
2
159
|
|
3
160
|
* Add `connected_to_many` API.
|
4
161
|
|
@@ -6,21 +163,21 @@
|
|
6
163
|
|
7
164
|
Before:
|
8
165
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
166
|
+
AnimalsRecord.connected_to(role: :reading) do
|
167
|
+
MealsRecord.connected_to(role: :reading) do
|
168
|
+
Dog.first # read from animals replica
|
169
|
+
Dinner.first # read from meals replica
|
170
|
+
Person.first # read from primary writer
|
171
|
+
end
|
14
172
|
end
|
15
|
-
end
|
16
173
|
|
17
174
|
After:
|
18
175
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
176
|
+
ActiveRecord::Base.connected_to_many([AnimalsRecord, MealsRecord], role: :reading) do
|
177
|
+
Dog.first # read from animals replica
|
178
|
+
Dinner.first # read from meals replica
|
179
|
+
Person.first # read from primary writer
|
180
|
+
end
|
24
181
|
|
25
182
|
*Eileen M. Uchitelle*, *John Crepezzi*
|
26
183
|
|
@@ -108,7 +265,7 @@
|
|
108
265
|
|
109
266
|
* Named scope chain does no longer leak scope to class level querying methods.
|
110
267
|
|
111
|
-
class
|
268
|
+
class User < ActiveRecord::Base
|
112
269
|
scope :david, -> { User.where(name: "David") }
|
113
270
|
end
|
114
271
|
|
@@ -138,13 +295,13 @@
|
|
138
295
|
|
139
296
|
Before:
|
140
297
|
|
141
|
-
|
142
|
-
|
298
|
+
User.where.not(name: "Jon", role: "admin")
|
299
|
+
# SELECT * FROM users WHERE name != 'Jon' AND role != 'admin'
|
143
300
|
|
144
301
|
After:
|
145
302
|
|
146
|
-
|
147
|
-
|
303
|
+
User.where.not(name: "Jon", role: "admin")
|
304
|
+
# SELECT * FROM users WHERE NOT (name == 'Jon' AND role == 'admin')
|
148
305
|
|
149
306
|
*Rafael Mendonça França*
|
150
307
|
|
@@ -665,14 +822,14 @@
|
|
665
822
|
|
666
823
|
*Ryuta Kamizono*
|
667
824
|
|
668
|
-
* Inspect time attributes with subsec.
|
825
|
+
* Inspect time attributes with subsec and time zone offset.
|
669
826
|
|
670
827
|
```ruby
|
671
828
|
p Knot.create
|
672
|
-
=> #<Knot id: 1, created_at: "2016-05-05 01:29:47.116928000">
|
829
|
+
=> #<Knot id: 1, created_at: "2016-05-05 01:29:47.116928000 +0000">
|
673
830
|
```
|
674
831
|
|
675
|
-
*akinomaeni*
|
832
|
+
*akinomaeni*, *Jonathan Hefner*
|
676
833
|
|
677
834
|
* Deprecate passing a column to `type_cast`.
|
678
835
|
|
data/README.rdoc
CHANGED
@@ -194,7 +194,7 @@ The latest version of Active Record can be installed with RubyGems:
|
|
194
194
|
|
195
195
|
Source code can be downloaded as part of the Rails project on GitHub:
|
196
196
|
|
197
|
-
* https://github.com/rails/rails/tree/
|
197
|
+
* https://github.com/rails/rails/tree/main/activerecord
|
198
198
|
|
199
199
|
|
200
200
|
== License
|
@@ -244,8 +244,8 @@ module ActiveRecord
|
|
244
244
|
private
|
245
245
|
def reader_method(name, class_name, mapping, allow_nil, constructor)
|
246
246
|
define_method(name) do
|
247
|
-
if @aggregation_cache[name].nil? && (!allow_nil || mapping.any? { |key, _| !
|
248
|
-
attrs = mapping.collect { |key, _|
|
247
|
+
if @aggregation_cache[name].nil? && (!allow_nil || mapping.any? { |key, _| !read_attribute(key).nil? })
|
248
|
+
attrs = mapping.collect { |key, _| read_attribute(key) }
|
249
249
|
object = constructor.respond_to?(:call) ?
|
250
250
|
constructor.call(*attrs) :
|
251
251
|
class_name.constantize.send(constructor, *attrs)
|
@@ -271,10 +271,10 @@ module ActiveRecord
|
|
271
271
|
end
|
272
272
|
|
273
273
|
if part.nil? && allow_nil
|
274
|
-
mapping.each { |key, _|
|
274
|
+
mapping.each { |key, _| write_attribute(key, nil) }
|
275
275
|
@aggregation_cache[name] = nil
|
276
276
|
else
|
277
|
-
mapping.each { |key, value|
|
277
|
+
mapping.each { |key, value| write_attribute(key, part.send(value)) }
|
278
278
|
@aggregation_cache[name] = part.freeze
|
279
279
|
end
|
280
280
|
end
|
@@ -27,6 +27,16 @@ module ActiveRecord
|
|
27
27
|
RUBY
|
28
28
|
end
|
29
29
|
|
30
|
+
def build(attributes = nil, &block)
|
31
|
+
if attributes.is_a?(Array)
|
32
|
+
attributes.collect { |attr| build(attr, &block) }
|
33
|
+
else
|
34
|
+
block = current_scope_restoring_block(&block)
|
35
|
+
scoping { _new(attributes, &block) }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
alias new build
|
39
|
+
|
30
40
|
private
|
31
41
|
def _new(attributes, &block)
|
32
42
|
@association.build(attributes, &block)
|
@@ -1371,7 +1371,9 @@ module ActiveRecord
|
|
1371
1371
|
#
|
1372
1372
|
# * <tt>nil</tt> do nothing (default).
|
1373
1373
|
# * <tt>:destroy</tt> causes all the associated objects to also be destroyed.
|
1374
|
-
# * <tt>:destroy_async</tt> destroys all the associated objects in a background job.
|
1374
|
+
# * <tt>:destroy_async</tt> destroys all the associated objects in a background job. <b>WARNING:</b> Do not use
|
1375
|
+
# this option if the association is backed by foreign key constraints in your database. The foreign key
|
1376
|
+
# constraint actions will occur inside the same transaction that deletes its owner.
|
1375
1377
|
# * <tt>:delete_all</tt> causes all the associated objects to be deleted directly from the database (so callbacks will not be executed).
|
1376
1378
|
# * <tt>:nullify</tt> causes the foreign keys to be set to +NULL+. Polymorphic type will also be nullified
|
1377
1379
|
# on polymorphic associations. Callbacks are not executed.
|
@@ -1523,7 +1525,9 @@ module ActiveRecord
|
|
1523
1525
|
#
|
1524
1526
|
# * <tt>nil</tt> do nothing (default).
|
1525
1527
|
# * <tt>:destroy</tt> causes the associated object to also be destroyed
|
1526
|
-
# * <tt>:destroy_async</tt> causes
|
1528
|
+
# * <tt>:destroy_async</tt> causes the associated object to be destroyed in a background job. <b>WARNING:</b> Do not use
|
1529
|
+
# this option if the association is backed by foreign key constraints in your database. The foreign key
|
1530
|
+
# constraint actions will occur inside the same transaction that deletes its owner.
|
1527
1531
|
# * <tt>:delete</tt> causes the associated object to be deleted directly from the database (so callbacks will not execute)
|
1528
1532
|
# * <tt>:nullify</tt> causes the foreign key to be set to +NULL+. Polymorphic type column is also nullified
|
1529
1533
|
# on polymorphic associations. Callbacks are not executed.
|
@@ -131,6 +131,11 @@ module ActiveRecord
|
|
131
131
|
end
|
132
132
|
|
133
133
|
def inversed_from(record)
|
134
|
+
self.target = record
|
135
|
+
@inversed = !!record
|
136
|
+
end
|
137
|
+
|
138
|
+
def inversed_from_queries(record)
|
134
139
|
if inversable?(record)
|
135
140
|
self.target = record
|
136
141
|
@inversed = true
|
@@ -138,7 +143,6 @@ module ActiveRecord
|
|
138
143
|
@inversed = false
|
139
144
|
end
|
140
145
|
end
|
141
|
-
alias :inversed_from_queries :inversed_from
|
142
146
|
|
143
147
|
# Returns the class of the target. belongs_to polymorphic overrides this to look at the
|
144
148
|
# polymorphic_type field on the owner.
|
@@ -207,12 +211,8 @@ module ActiveRecord
|
|
207
211
|
|
208
212
|
private
|
209
213
|
def find_target
|
210
|
-
if owner.strict_loading?
|
211
|
-
Base.strict_loading_violation!(owner: owner.class,
|
212
|
-
end
|
213
|
-
|
214
|
-
if reflection.strict_loading?
|
215
|
-
Base.strict_loading_violation!(owner: owner.class, association: reflection.name)
|
214
|
+
if (owner.strict_loading? || reflection.strict_loading?) && owner.validation_context.nil?
|
215
|
+
Base.strict_loading_violation!(owner: owner.class, reflection: reflection)
|
216
216
|
end
|
217
217
|
|
218
218
|
scope = self.scope
|
@@ -327,7 +327,11 @@ module ActiveRecord
|
|
327
327
|
end
|
328
328
|
|
329
329
|
def enqueue_destroy_association(options)
|
330
|
-
owner.class.destroy_association_async_job
|
330
|
+
job_class = owner.class.destroy_association_async_job
|
331
|
+
|
332
|
+
if job_class
|
333
|
+
owner._after_commit_jobs.push([job_class, options])
|
334
|
+
end
|
331
335
|
end
|
332
336
|
|
333
337
|
def inversable?(record)
|
@@ -337,7 +341,8 @@ module ActiveRecord
|
|
337
341
|
|
338
342
|
def matches_foreign_key?(record)
|
339
343
|
if foreign_key_for?(record)
|
340
|
-
record.read_attribute(reflection.foreign_key) == owner.id
|
344
|
+
record.read_attribute(reflection.foreign_key) == owner.id ||
|
345
|
+
(foreign_key_for?(owner) && owner.read_attribute(reflection.foreign_key) == record.id)
|
341
346
|
else
|
342
347
|
owner.read_attribute(reflection.foreign_key) == record.id
|
343
348
|
end
|
@@ -131,11 +131,13 @@ module ActiveRecord
|
|
131
131
|
if scope_chain_item == chain_head.scope
|
132
132
|
scope.merge! item.except(:where, :includes, :unscope, :order)
|
133
133
|
elsif !item.references_values.empty?
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
134
|
+
scope.merge! item.only(:joins, :left_outer_joins)
|
135
|
+
|
136
|
+
associations = item.eager_load_values | item.includes_values
|
137
|
+
|
138
|
+
unless associations.empty?
|
139
|
+
scope.joins! item.construct_join_dependency(associations, Arel::Nodes::OuterJoin)
|
140
|
+
end
|
139
141
|
end
|
140
142
|
|
141
143
|
reflection.all_includes do
|
@@ -80,7 +80,7 @@ module ActiveRecord
|
|
80
80
|
@updated = true
|
81
81
|
end
|
82
82
|
|
83
|
-
replace_keys(record)
|
83
|
+
replace_keys(record, force: true)
|
84
84
|
|
85
85
|
self.target = record
|
86
86
|
end
|
@@ -108,8 +108,12 @@ module ActiveRecord
|
|
108
108
|
reflection.counter_cache_column && owner.persisted?
|
109
109
|
end
|
110
110
|
|
111
|
-
def replace_keys(record)
|
112
|
-
|
111
|
+
def replace_keys(record, force: false)
|
112
|
+
target_key = record ? record._read_attribute(primary_key(record.class)) : nil
|
113
|
+
|
114
|
+
if force || owner[reflection.foreign_key] != target_key
|
115
|
+
owner[reflection.foreign_key] = target_key
|
116
|
+
end
|
113
117
|
end
|
114
118
|
|
115
119
|
def primary_key(klass)
|
@@ -14,9 +14,14 @@ module ActiveRecord
|
|
14
14
|
end
|
15
15
|
|
16
16
|
private
|
17
|
-
def replace_keys(record)
|
17
|
+
def replace_keys(record, force: false)
|
18
18
|
super
|
19
|
-
|
19
|
+
|
20
|
+
target_type = record ? record.class.polymorphic_name : nil
|
21
|
+
|
22
|
+
if force || owner[reflection.foreign_type] != target_type
|
23
|
+
owner[reflection.foreign_type] = target_type
|
24
|
+
end
|
20
25
|
end
|
21
26
|
|
22
27
|
def inverse_reflection_for(record)
|
@@ -76,6 +76,7 @@ module ActiveRecord::Associations::Builder # :nodoc:
|
|
76
76
|
if dependent = reflection.options[:dependent]
|
77
77
|
check_dependent_options(dependent, model)
|
78
78
|
add_destroy_callbacks(model, reflection)
|
79
|
+
add_after_commit_jobs_callback(model, dependent)
|
79
80
|
end
|
80
81
|
|
81
82
|
Association.extensions.each do |extension|
|
@@ -132,11 +133,31 @@ module ActiveRecord::Associations::Builder # :nodoc:
|
|
132
133
|
|
133
134
|
def self.add_destroy_callbacks(model, reflection)
|
134
135
|
name = reflection.name
|
135
|
-
model.before_destroy
|
136
|
+
model.before_destroy(->(o) { o.association(name).handle_dependency })
|
137
|
+
end
|
138
|
+
|
139
|
+
def self.add_after_commit_jobs_callback(model, dependent)
|
140
|
+
if dependent == :destroy_async
|
141
|
+
mixin = model.generated_association_methods
|
142
|
+
|
143
|
+
unless mixin.method_defined?(:_after_commit_jobs)
|
144
|
+
model.after_commit(-> do
|
145
|
+
_after_commit_jobs.each do |job_class, job_arguments|
|
146
|
+
job_class.perform_later(**job_arguments)
|
147
|
+
end
|
148
|
+
end)
|
149
|
+
|
150
|
+
mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
|
151
|
+
def _after_commit_jobs
|
152
|
+
@_after_commit_jobs ||= []
|
153
|
+
end
|
154
|
+
CODE
|
155
|
+
end
|
156
|
+
end
|
136
157
|
end
|
137
158
|
|
138
159
|
private_class_method :build_scope, :macro, :valid_options, :validate_options, :define_extensions,
|
139
160
|
:define_callbacks, :define_accessors, :define_readers, :define_writers, :define_validations,
|
140
|
-
:valid_dependent_options, :check_dependent_options, :add_destroy_callbacks
|
161
|
+
:valid_dependent_options, :check_dependent_options, :add_destroy_callbacks, :add_after_commit_jobs_callback
|
141
162
|
end
|
142
163
|
end
|