dependent-auto-rails 0.1.5 → 0.1.7

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: 15f2727d8f6862ab56a05007412045843f69c69e88f0d12772e339c10b70e93b
4
- data.tar.gz: 5cd26614eca9b617e50fe4ab32cda5dcef69e425037612a4a2bc43675f1e5cdb
3
+ metadata.gz: a862d80808c8cc6d619f6c5d2b2b2eb064a54f1382ee27a806f81608e5d83165
4
+ data.tar.gz: e6c078c89f423af15585de3a697688fb314aa24ffb3995005f479cb2c6ac4650
5
5
  SHA512:
6
- metadata.gz: 6b01ed5650b3ac1667c72f350c9ab1ced6814fe77f6310328378cb8e9dae70b1bbe88366fd805c5cf64fb7ad66b7d5043717612974998c722904e62da52792f7
7
- data.tar.gz: 300d41e5b3b117090639f105bf3682881de682631775fc7485ecf9a8b8392fa67762df99fd4c69bf1b324505c713a0864f5d67c6976395dc5b3e41558d01f679
6
+ metadata.gz: bee37a9ce116600e8458bd1af1580011e0fd91ea9f2cf70eb91b5e62ce9f3ab353cce542e0d6f8ce3fd973aaea70149df915a4411f5d47841a728cd6ac4de988
7
+ data.tar.gz: 6aceba442aae030f9eeebb8735623a89823b018ddf531ffcde1c9d179fffbe1b4b892bd8c2b046eccc20a819018c4fddcf3904089cf0de30512959958d239354
data/README.md CHANGED
@@ -1,24 +1,44 @@
1
- # [WIP] Dependent::Auto::Rails
1
+ # dependent-auto-rails
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
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.
4
4
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/dependent/auto/rails`. To experiment with that code, run `bin/console` for an interactive prompt.
5
+ 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
6
 
7
- ## Installation
7
+ **NOTE**: The `:auto` option **ONLY** decides between `:destroy` and `:delete` / `:delete_all`. It does not support any other `dependent` options such as:
8
+ - `:nullify`
9
+ - `:destroy_async`
10
+ - `:restrict_with_error`
11
+ - `:restrict_with_exception`
12
+
13
+ **NOTE**: If for some reason the `:auto` option is unable to decide between `:destroy` and `:delete` / `:delete_all`, it will default to `:destroy`.
8
14
 
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
15
+ ## Installation
10
16
 
11
- Install the gem and add to the application's Gemfile by executing:
17
+ Install the gem and add it to the application's Gemfile by executing:
12
18
 
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
19
+ $ bundle add dependent-auto-rails
14
20
 
15
21
  If bundler is not being used to manage dependencies, install the gem by executing:
16
22
 
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
23
+ $ gem installdependent-auto-rails
18
24
 
19
25
  ## Usage
20
26
 
21
- TODO: Write usage instructions here
27
+ To use this gem, all you need to do is add `dependent: :auto` to your model associations where you would usually use one of:
28
+ - `dependent: :destroy` (`belongs_to`, `has_one`, `has_many`)
29
+ - `dependent: :delete` (`belongs_to`, `has_one`)
30
+ - `dependent: :delete_all` (`has_many`).
31
+
32
+ ```ruby
33
+ class Post < ApplicationRecord
34
+ belongs_to :author, dependent: :auto
35
+
36
+ has_one :pinned_comment, dependent: :auto
37
+
38
+ has_many :comments, dependent: :auto
39
+ end
40
+
41
+ ```
22
42
 
23
43
  ## Development
24
44
 
@@ -28,7 +48,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
28
48
 
29
49
  ## Contributing
30
50
 
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/dependent-auto-rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/dependent-auto-rails/blob/main/CODE_OF_CONDUCT.md).
51
+ Bug reports and pull requests are welcome on GitHub at https://github.com/joshuay03/dependent-auto-rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/dependent-auto-rails/blob/main/CODE_OF_CONDUCT.md).
32
52
 
33
53
  ## License
34
54
 
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord::Associations::Builder
4
+ class BelongsTo < SingularAssociation
5
+ def self.valid_dependent_options
6
+ [:auto, :destroy, :delete, :destroy_async]
7
+ end
8
+ private_class_method :valid_dependent_options
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord::Associations::Builder
4
+ class HasMany < CollectionAssociation
5
+ def self.valid_dependent_options
6
+ [:auto, :destroy, :delete_all, :nullify, :restrict_with_error, :restrict_with_exception, :destroy_async]
7
+ end
8
+ private_class_method :macro, :valid_options, :valid_dependent_options
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord::Associations::Builder
4
+ class HasOne < SingularAssociation
5
+ def self.valid_dependent_options
6
+ [:auto, :destroy, :destroy_async, :delete, :nullify, :restrict_with_error, :restrict_with_exception]
7
+ end
8
+ private_class_method :valid_dependent_options
9
+ end
10
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module Reflection
5
+ class AssociationReflection < MacroReflection
6
+ private
7
+
8
+ def convert_options_to_dynamic_reflection_options!
9
+ @options = DynamicReflectionOptionsHash.new.merge!(
10
+ @options,
11
+ {
12
+ association_model_name: class_name,
13
+ association_type: collection? ? :collection : :singular
14
+ }
15
+ )
16
+ end
17
+
18
+ class DynamicReflectionOptionsHash < Hash
19
+ def [](key)
20
+ return super unless key == :dependent && super(:dependent) == :auto
21
+ return fallback_method if defining_dependent_callbacks?
22
+
23
+ # TODO: This path can be memoized
24
+ model = super(:association_model_name).constantize
25
+ return :destroy unless model._destroy_callbacks.empty?
26
+
27
+ case super(:association_type)
28
+ when :singular then :delete
29
+ when :collection then :delete_all
30
+ else fallback_method
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def fallback_method
37
+ :destroy
38
+ end
39
+
40
+ def defining_dependent_callbacks?
41
+ caller.any? { |line| line.include?("active_record/associations/builder/association.rb") }
42
+ end
43
+ end
44
+ end
45
+
46
+ class BelongsToReflection < AssociationReflection
47
+ def initialize(...)
48
+ super(...)
49
+ convert_options_to_dynamic_reflection_options!
50
+ end
51
+ end
52
+
53
+ class HasOneReflection < AssociationReflection
54
+ def initialize(...)
55
+ super(...)
56
+ convert_options_to_dynamic_reflection_options!
57
+ end
58
+ end
59
+
60
+ class HasManyReflection < AssociationReflection
61
+ def initialize(...)
62
+ super(...)
63
+ convert_options_to_dynamic_reflection_options!
64
+ end
65
+ end
66
+
67
+ class HasAndBelongsToManyReflection < AssociationReflection
68
+ def initialize(...)
69
+ super(...)
70
+ convert_options_to_dynamic_reflection_options!
71
+ end
72
+ end
73
+ end
74
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DependentAutoRails
4
- VERSION = "0.1.5"
4
+ VERSION = "0.1.7"
5
5
  end
@@ -3,7 +3,10 @@
3
3
  require "active_record"
4
4
 
5
5
  ActiveSupport.on_load(:active_record) do
6
- require_relative "dependent-auto-rails/activerecord/associations/builder/association"
6
+ require_relative "dependent-auto-rails/activerecord/associations/builder/belongs_to"
7
+ require_relative "dependent-auto-rails/activerecord/associations/builder/has_one"
8
+ require_relative "dependent-auto-rails/activerecord/associations/builder/has_many"
9
+ require_relative "dependent-auto-rails/activerecord/reflection"
7
10
  end
8
11
 
9
12
  module DependentAutoRails
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.5
4
+ version: 0.1.7
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-19 00:00:00.000000000 Z
11
+ date: 2024-02-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -50,7 +50,10 @@ files:
50
50
  - LICENSE.txt
51
51
  - README.md
52
52
  - lib/dependent-auto-rails.rb
53
- - lib/dependent-auto-rails/activerecord/associations/builder/association.rb
53
+ - lib/dependent-auto-rails/activerecord/associations/builder/belongs_to.rb
54
+ - lib/dependent-auto-rails/activerecord/associations/builder/has_many.rb
55
+ - lib/dependent-auto-rails/activerecord/associations/builder/has_one.rb
56
+ - lib/dependent-auto-rails/activerecord/reflection.rb
54
57
  - lib/dependent-auto-rails/version.rb
55
58
  homepage: https://github.com/joshuay03/dependent-auto-rails
56
59
  licenses:
@@ -77,6 +80,6 @@ requirements: []
77
80
  rubygems_version: 3.5.3
78
81
  signing_key:
79
82
  specification_version: 4
80
- summary: Automatically and safely decides between :destroy and :delete / :delete_all
81
- for your Rails associations.
83
+ summary: Automatically decides between :destroy and :delete / :delete_all for your
84
+ Rails associations.
82
85
  test_files: []
@@ -1,68 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ActiveRecord::Associations::Builder
4
- class Association
5
- def self.create_reflection(model, name, scope, options, &block)
6
- raise ArgumentError, "association names must be a Symbol" unless name.is_a?(Symbol)
7
-
8
- validate_options(options)
9
-
10
- extension = define_extensions(model, name, &block)
11
- options[:extend] = [*options[:extend], extension] if extension
12
-
13
- scope = build_scope(scope)
14
-
15
- ActiveRecord::Reflection.create(macro, name, scope, dynamic_reflection_options(model, options), model)
16
- end
17
-
18
- def self.dynamic_reflection_options(model, options)
19
- DynamicReflectionOptionsHash.new.merge!(
20
- options,
21
- {
22
- model_name: model.name,
23
- association_type: if self < ActiveRecord::Associations::Builder::SingularAssociation
24
- :singular
25
- elsif self < ActiveRecord::Associations::Builder::CollectionAssociation
26
- :collection
27
- else
28
- raise DependentAutoRails::Error, "Unsupported association type"
29
- end
30
- }
31
- )
32
- end
33
- private_class_method :dynamic_reflection_options
34
-
35
- class DynamicReflectionOptionsHash < Hash
36
- def [](key)
37
- return super unless key == :dependent && super(:dependent) == :auto
38
- return fallback_method if defining_dependent_callbacks?
39
-
40
- model = super(:model_name).constantize
41
- return :destroy unless valid_destroy_callbacks(model).empty?
42
-
43
- case super(:association_type)
44
- when :singular then :delete
45
- when :collection then :delete_all
46
- else fallback_method
47
- end
48
- end
49
-
50
- private
51
-
52
- def fallback_method
53
- :destroy
54
- end
55
-
56
- def defining_dependent_callbacks?
57
- caller.any? { |line| line.include?("active_record/associations/builder/association.rb") }
58
- end
59
-
60
- def valid_destroy_callbacks(model)
61
- model._destroy_callbacks.reject do |callback|
62
- # ignore #handle_dependency callback
63
- callback.filter.to_s.include?("active_record/associations/builder/association.rb")
64
- end
65
- end
66
- end
67
- end
68
- end