dependent-auto-rails 0.1.5 → 0.1.6

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: b0596b3547afda10b9f5dbde19305f2f82513c08f0e0f2cbba8ea61d6c7d3e36
4
+ data.tar.gz: 34d8c36bcec58674d9b1d98f130335e102896493ab7a86aba4ee7ff1aada0b28
5
5
  SHA512:
6
- metadata.gz: 6b01ed5650b3ac1667c72f350c9ab1ced6814fe77f6310328378cb8e9dae70b1bbe88366fd805c5cf64fb7ad66b7d5043717612974998c722904e62da52792f7
7
- data.tar.gz: 300d41e5b3b117090639f105bf3682881de682631775fc7485ecf9a8b8392fa67762df99fd4c69bf1b324505c713a0864f5d67c6976395dc5b3e41558d01f679
6
+ metadata.gz: fb17812a18ac89155498d56a7a11fbdc36076c9f0859765e18dd6abbaf33d26d7495582cf294b9f464fe49fc316eb23ed529c52dc94e91d7a0098782119a9bbb
7
+ data.tar.gz: e242a528349742be08232e9fae587d630de0d78db8962151ff64af97b414d668dc3f62db70b82cac136692a7bd9d345a8c99420384408f0a1430237191303469
@@ -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,63 @@
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
11
+
12
+ module ActiveRecord
13
+ module Associations
14
+ class HasManyAssociation < CollectionAssociation
15
+ def handle_dependency
16
+ case options[:dependent]
17
+ when :restrict_with_exception
18
+ raise ActiveRecord::DeleteRestrictionError.new(reflection.name) unless empty?
19
+
20
+ when :restrict_with_error
21
+ unless empty?
22
+ record = owner.class.human_attribute_name(reflection.name).downcase
23
+ owner.errors.add(:base, :"restrict_dependent_destroy.has_many", record: record)
24
+ throw(:abort)
25
+ end
26
+
27
+ when :destroy
28
+ # No point in executing the counter update since we're going to destroy the parent anyway
29
+ load_target.each { |t| t.destroyed_by_association = reflection }
30
+ destroy_all
31
+ when :destroy_async
32
+ load_target.each do |t|
33
+ t.destroyed_by_association = reflection
34
+ end
35
+
36
+ unless target.empty?
37
+ association_class = target.first.class
38
+ if association_class.query_constraints_list
39
+ primary_key_column = association_class.query_constraints_list.map(&:to_sym)
40
+ ids = target.collect { |assoc| primary_key_column.map { |col| assoc.public_send(col) } }
41
+ else
42
+ primary_key_column = association_class.primary_key.to_sym
43
+ ids = target.collect { |assoc| assoc.public_send(primary_key_column) }
44
+ end
45
+
46
+ ids.each_slice(owner.class.destroy_association_async_batch_size || ids.size) do |ids_batch|
47
+ enqueue_destroy_association(
48
+ owner_model_name: owner.class.to_s,
49
+ owner_id: owner.id,
50
+ association_class: reflection.klass.to_s,
51
+ association_ids: ids_batch,
52
+ association_primary_key_column: primary_key_column,
53
+ ensuring_owner_was_method: options.fetch(:ensuring_owner_was, nil)
54
+ )
55
+ end
56
+ end
57
+ else
58
+ delete_all
59
+ end
60
+ end
61
+ end
62
+ end
63
+ 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,80 @@
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
+ model = super(:association_model_name).constantize
24
+ return :destroy unless valid_destroy_callbacks(model).empty?
25
+
26
+ case super(:association_type)
27
+ when :singular then :delete
28
+ when :collection then :delete_all
29
+ else fallback_method
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def fallback_method
36
+ :destroy
37
+ end
38
+
39
+ def defining_dependent_callbacks?
40
+ caller.any? { |line| line.include?("active_record/associations/builder/association.rb") }
41
+ end
42
+
43
+ def valid_destroy_callbacks(model)
44
+ model._destroy_callbacks.reject do |callback|
45
+ # ignore #handle_dependency callback
46
+ callback.filter.to_s.include?("active_record/associations/builder/association.rb")
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ class BelongsToReflection < AssociationReflection
53
+ def initialize(...)
54
+ super(...)
55
+ convert_options_to_dynamic_reflection_options!
56
+ end
57
+ end
58
+
59
+ class HasOneReflection < AssociationReflection
60
+ def initialize(...)
61
+ super(...)
62
+ convert_options_to_dynamic_reflection_options!
63
+ end
64
+ end
65
+
66
+ class HasManyReflection < AssociationReflection
67
+ def initialize(...)
68
+ super(...)
69
+ convert_options_to_dynamic_reflection_options!
70
+ end
71
+ end
72
+
73
+ class HasAndBelongsToManyReflection < AssociationReflection
74
+ def initialize(...)
75
+ super(...)
76
+ convert_options_to_dynamic_reflection_options!
77
+ end
78
+ end
79
+ end
80
+ 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.6"
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.6
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:
@@ -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