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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 39c2ce49224fd43ed1657864ba4f770c55d027b9a1d3fa31067f4a34496b49a0
4
- data.tar.gz: 7efbfee3f5f2a01d512c7d5e45cd7e7f80ae86cf901f907243a3b8f3247eadfa
3
+ metadata.gz: 4bace913fbaee41d6bd8330049633d641488b44e15d6881c8cb59c3e236e992d
4
+ data.tar.gz: 0c4d1c52f650c89d701df483e7e4fa430cf4c0ca4a77c6b5c14946a101d48591
5
5
  SHA512:
6
- metadata.gz: e0515328b712cd07be349167fa70603142d2a052be4d47395ce9890cd9adaa2c88b498a2a3ab7985556f31ea2dafe4d951baca947b764e4901ec033a26fc1de1
7
- data.tar.gz: 0cc9aaa16aad850eb95f6a23d2625d8fbeacb74a05b2702bfb340f3782415cc04169f7b2f58faac0a8ccf4331e365f1d1e83c72de44142542264d672b4e57d7f
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 destroy callbacks defined. This is useful since `dependent: :destroy` always initialises the associated records in order to execute their destroy 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.
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
- **NOTE**: The `:auto` option **ONLY** decides between `:destroy` and `:delete` / `:delete_all`. It does not support any other `dependent` option, such as:
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 for defining an after_commit callback
24
- # for :destroy_async, so it doesn't really matter what we return here.
25
- # This helps us cause we can't yet determine the correct method as
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._destroy_callbacks.empty?
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
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DependentAutoRails
4
- VERSION = "0.1.9"
4
+ VERSION = "0.2.0"
5
5
  end
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.1.9
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-21 00:00:00.000000000 Z
11
+ date: 2024-03-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord