dependent-auto-rails 0.1.9 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 39c2ce49224fd43ed1657864ba4f770c55d027b9a1d3fa31067f4a34496b49a0
4
- data.tar.gz: 7efbfee3f5f2a01d512c7d5e45cd7e7f80ae86cf901f907243a3b8f3247eadfa
3
+ metadata.gz: 6440f408848bb2bae6587ac5e84a6c1b8025e083d43bc0536512727fb11a6fcd
4
+ data.tar.gz: f2eda5a45cf5facbeb02ca99801fc1e6493083423bb06904ff05b2d89cb99af8
5
5
  SHA512:
6
- metadata.gz: e0515328b712cd07be349167fa70603142d2a052be4d47395ce9890cd9adaa2c88b498a2a3ab7985556f31ea2dafe4d951baca947b764e4901ec033a26fc1de1
7
- data.tar.gz: 0cc9aaa16aad850eb95f6a23d2625d8fbeacb74a05b2702bfb340f3782415cc04169f7b2f58faac0a8ccf4331e365f1d1e83c72de44142542264d672b4e57d7f
6
+ metadata.gz: 0f90fa81d2ea2a0a4478d380cfd9edc38fea534e5b88e975393c3c86ee43464fefb34dda23dd6a94d67abaf37a792e4002f2a1830c77d00ec7dce50322bf16e7
7
+ data.tar.gz: 2f9bfe5254002c37726e70aeb40067686f2277c1a76372fb5086a524fbb30fac85311f450cf77f817ea6fd890f4a1bb8082dcafb8afc4c3269a79fec36902bf1
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.
7
+
8
+ 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
9
 
8
10
  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
11
 
10
- **NOTE**: The `:auto` option **ONLY** decides between `:destroy` and `:delete` / `:delete_all`. It does not support any other `dependent` option, such as:
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`
@@ -15,6 +17,8 @@ It is also useful since a model's associations are rarely updated, but it's busi
15
17
 
16
18
  **NOTE**: If for some reason the `:auto` option is unable to decide between `:destroy` and `:delete` / `:delete_all`, it will default to `:destroy`.
17
19
 
20
+ **INFO**: If you're looking for a solution with a little less magic, check out the `incorrect_dependent_option` detector provided by https://github.com/gregnavis/active_record_doctor.
21
+
18
22
  ## Installation
19
23
 
20
24
  Install the gem and add it to the application's Gemfile by executing:
@@ -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.1"
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.1
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