dependent-auto-rails 0.1.7 → 0.1.9

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: a862d80808c8cc6d619f6c5d2b2b2eb064a54f1382ee27a806f81608e5d83165
4
- data.tar.gz: e6c078c89f423af15585de3a697688fb314aa24ffb3995005f479cb2c6ac4650
3
+ metadata.gz: 39c2ce49224fd43ed1657864ba4f770c55d027b9a1d3fa31067f4a34496b49a0
4
+ data.tar.gz: 7efbfee3f5f2a01d512c7d5e45cd7e7f80ae86cf901f907243a3b8f3247eadfa
5
5
  SHA512:
6
- metadata.gz: bee37a9ce116600e8458bd1af1580011e0fd91ea9f2cf70eb91b5e62ce9f3ab353cce542e0d6f8ce3fd973aaea70149df915a4411f5d47841a728cd6ac4de988
7
- data.tar.gz: 6aceba442aae030f9eeebb8735623a89823b018ddf531ffcde1c9d179fffbe1b4b892bd8c2b046eccc20a819018c4fddcf3904089cf0de30512959958d239354
6
+ metadata.gz: e0515328b712cd07be349167fa70603142d2a052be4d47395ce9890cd9adaa2c88b498a2a3ab7985556f31ea2dafe4d951baca947b764e4901ec033a26fc1de1
7
+ data.tar.gz: 0cc9aaa16aad850eb95f6a23d2625d8fbeacb74a05b2702bfb340f3782415cc04169f7b2f58faac0a8ccf4331e365f1d1e83c72de44142542264d672b4e57d7f
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
@@ -1,10 +1,13 @@
1
+ [![Version](https://img.shields.io/gem/v/dependent-auto-rails)][badges:0-gem]
2
+ [![Build](https://img.shields.io/github/actions/workflow/status/joshuay03/dependent-auto-rails/.github/workflows/main.yml?branch=main)][badges:1-workflow]
3
+
1
4
  # dependent-auto-rails
2
5
 
3
- 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. 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 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.
4
7
 
5
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.
6
9
 
7
- **NOTE**: The `:auto` option **ONLY** decides between `:destroy` and `:delete` / `:delete_all`. It does not support any other `dependent` options such as:
10
+ **NOTE**: The `:auto` option **ONLY** decides between `:destroy` and `:delete` / `:delete_all`. It does not support any other `dependent` option, such as:
8
11
  - `:nullify`
9
12
  - `:destroy_async`
10
13
  - `:restrict_with_error`
@@ -57,3 +60,6 @@ The gem is available as open source under the terms of the [MIT License](https:/
57
60
  ## Code of Conduct
58
61
 
59
62
  Everyone interacting in the Dependent::Auto::Rails project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/dependent-auto-rails/blob/main/CODE_OF_CONDUCT.md).
63
+
64
+ [badges:0-gem]: https://rubygems.org/gems/dependent-auto-rails
65
+ [badges:1-workflow]: https://github.com/joshuay03/dependent-auto-rails/blob/main/.github/workflows/main.yml
@@ -18,23 +18,33 @@ module ActiveRecord
18
18
  class DynamicReflectionOptionsHash < Hash
19
19
  def [](key)
20
20
  return super unless key == :dependent && super(:dependent) == :auto
21
+
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
26
+ # the associated model might not have been evaluated.
21
27
  return fallback_method if defining_dependent_callbacks?
22
28
 
23
- # TODO: This path can be memoized
24
- model = super(:association_model_name).constantize
25
- return :destroy unless model._destroy_callbacks.empty?
29
+ @dependent_auto = begin
30
+ model = super(:association_model_name).constantize
26
31
 
27
- case super(:association_type)
28
- when :singular then :delete
29
- when :collection then :delete_all
30
- else fallback_method
32
+ if model._destroy_callbacks.empty?
33
+ case super(:association_type)
34
+ when :singular then :delete
35
+ when :collection then :delete_all
36
+ else fallback_method
37
+ end
38
+ else
39
+ :destroy
40
+ end
31
41
  end
32
42
  end
33
43
 
34
44
  private
35
45
 
36
46
  def fallback_method
37
- :destroy
47
+ :destroy # The safest, also common to all supported associations
38
48
  end
39
49
 
40
50
  def defining_dependent_callbacks?
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DependentAutoRails
4
- VERSION = "0.1.7"
4
+ VERSION = "0.1.9"
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.7
4
+ version: 0.1.9
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-20 00:00:00.000000000 Z
11
+ date: 2024-02-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord