dependent-auto-rails 0.1.9 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +4 -2
- data/lib/dependent-auto-rails/activerecord/reflection.rb +16 -8
- data/lib/dependent-auto-rails/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4bace913fbaee41d6bd8330049633d641488b44e15d6881c8cb59c3e236e992d
|
4
|
+
data.tar.gz: 0c4d1c52f650c89d701df483e7e4fa430cf4c0ca4a77c6b5c14946a101d48591
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 62a535b5da5d1f75c73a650b3505f0f5d51b60ce1b2b66bec050670b855f5756dd9bb8b6a9f559016366e834c0a8c8a5000f1330dba9ec87062e3dd7e801f204
|
7
|
+
data.tar.gz: aa0cabec1f5a7eebb977c85f9b98e30872c2e0c7acb8d7e6ec08ff642239bff43c5a153d8cec81ed0ca31d5e964d571eb98fd60f1a6fb6416c167d0b0bbca86c
|
data/README.md
CHANGED
@@ -3,11 +3,13 @@
|
|
3
3
|
|
4
4
|
# dependent-auto-rails
|
5
5
|
|
6
|
-
This gem provides a new `dependent` option for ActiveRecord associations, `:auto`. Using this option will automatically select between `:destroy` and `:delete` / `:delete_all` during runtime based on whether or not the associated model has any
|
6
|
+
This gem provides a new `dependent` option for ActiveRecord associations, `:auto`. Using this option will automatically select between `:destroy` and `:delete` / `:delete_all` during runtime based on whether or not the associated model has any callbacks defined which would usually be executed as part of the destroy lifecycle. This is useful since `dependent: :destroy` always initialises the associated records in order to execute their callbacks regardless of whether or not there are any defined, but is often the go-to option since it is the safest. This can be expensive if there are many records to destroy.
|
7
7
|
|
8
8
|
It is also useful since a model's associations are rarely updated, but it's business logic can change frequently. This means that if destroy callbacks are added or removed on the associated model, the `dependent` option on the parent model's association may need to be updated to reflect this. Using `dependent: :auto` will automatically select the appropriate `dependent` option based on the current state of the model.
|
9
9
|
|
10
|
-
|
10
|
+
If you're looking for a solution with little less magic, check out https://github.com/gregnavis/active_record_doctor's `incorrect_dependent_option` detector.
|
11
|
+
|
12
|
+
**NOTE**: The `:auto` option **ONLY** decides between `:destroy` and `:delete` / `:delete_all`. It does not use any of the other `dependent` options:
|
11
13
|
- `:nullify`
|
12
14
|
- `:destroy_async`
|
13
15
|
- `:restrict_with_error`
|
@@ -20,16 +20,16 @@ module ActiveRecord
|
|
20
20
|
return super unless key == :dependent && super(:dependent) == :auto
|
21
21
|
|
22
22
|
return @dependent_auto if defined?(@dependent_auto)
|
23
|
-
# The method returned here is only used
|
24
|
-
# for :destroy_async, so it doesn't really matter what we return
|
25
|
-
# This helps us
|
23
|
+
# The method returned here is only used to define an after_commit callback
|
24
|
+
# for :destroy_async, so it doesn't really matter what we return.
|
25
|
+
# This helps us since we can't yet determine the correct method as
|
26
26
|
# the associated model might not have been evaluated.
|
27
27
|
return fallback_method if defining_dependent_callbacks?
|
28
28
|
|
29
29
|
@dependent_auto = begin
|
30
30
|
model = super(:association_model_name).constantize
|
31
31
|
|
32
|
-
if model.
|
32
|
+
if executable_callbacks_on_destroy_for(model).empty?
|
33
33
|
case super(:association_type)
|
34
34
|
when :singular then :delete
|
35
35
|
when :collection then :delete_all
|
@@ -43,13 +43,21 @@ module ActiveRecord
|
|
43
43
|
|
44
44
|
private
|
45
45
|
|
46
|
-
def fallback_method
|
47
|
-
:destroy # The safest, also common to all supported associations
|
48
|
-
end
|
49
|
-
|
50
46
|
def defining_dependent_callbacks?
|
51
47
|
caller.any? { |line| line.include?("active_record/associations/builder/association.rb") }
|
52
48
|
end
|
49
|
+
|
50
|
+
def executable_callbacks_on_destroy_for(model)
|
51
|
+
model._find_callbacks.to_a +
|
52
|
+
model._initialize_callbacks.to_a +
|
53
|
+
model._destroy_callbacks.to_a +
|
54
|
+
model._commit_callbacks.to_a +
|
55
|
+
model._rollback_callbacks.to_a
|
56
|
+
end
|
57
|
+
|
58
|
+
def fallback_method
|
59
|
+
:destroy # The safest, also common to all supported associations
|
60
|
+
end
|
53
61
|
end
|
54
62
|
end
|
55
63
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dependent-auto-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joshua Young
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-02
|
11
|
+
date: 2024-03-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|