dependent-auto-rails 0.1.8 → 0.2.0

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: f413fd93d4790779d9b0376755b188e1e62f5130bed767c0ff89dbb14a8a07cc
4
- data.tar.gz: 34fdbdfc0246bc176827767b0273442edc4d49d64c3adbdefb2f11fc410aedee
3
+ metadata.gz: 4bace913fbaee41d6bd8330049633d641488b44e15d6881c8cb59c3e236e992d
4
+ data.tar.gz: 0c4d1c52f650c89d701df483e7e4fa430cf4c0ca4a77c6b5c14946a101d48591
5
5
  SHA512:
6
- metadata.gz: 0f80bffdcfc4ad25719648bbdb3626e1a9e8c66b06819ff31c62eba80c260c71bf40bdf8b03eaf54132077caba29eaf9514d943bbf2c642dac319d6e96c6d187
7
- data.tar.gz: 40801ab7610aaa5c837b0a1eeda0ef9591bcc87e70476c9bf3d1f632ad8be92f10f499a59d86dd25daab028b3f38a3b086657a6a4a52429c234ca1322824cf45
6
+ metadata.gz: 62a535b5da5d1f75c73a650b3505f0f5d51b60ce1b2b66bec050670b855f5756dd9bb8b6a9f559016366e834c0a8c8a5000f1330dba9ec87062e3dd7e801f204
7
+ data.tar.gz: aa0cabec1f5a7eebb977c85f9b98e30872c2e0c7acb8d7e6ec08ff642239bff43c5a153d8cec81ed0ca31d5e964d571eb98fd60f1a6fb6416c167d0b0bbca86c
data/CHANGELOG.md CHANGED
@@ -1,5 +1,3 @@
1
- ## [Unreleased]
2
-
3
- ## [0.1.0] - 2024-02-18
1
+ ## [0.1.9] - 21/02/2024
4
2
 
5
3
  - Initial release
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.8"
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.8
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