dependent-auto-rails 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c1982e7a765a8ef3c1f24c32b132d561b64995e02c8ee593d6e932ff10d4105b
4
- data.tar.gz: c57c2666e92829fc28847f483d6cdc117a14063d6db999c4f767aa282a394670
3
+ metadata.gz: 9f580a4dd1d2381f710091f77c3f788db32d52454cbbc15a8d19234cfac2b6cc
4
+ data.tar.gz: 2b93ef9a7b4d3bf9a51040710f6f0234a5688286ea1f80b2dd77e16e46387f06
5
5
  SHA512:
6
- metadata.gz: 010013ca9d2b1c30d36cebce8d79a60e2c97eff4757df3180774dce02cb3a4f7aa32e45480d4995ae0a8028b06f2b50f0adae15208105a068434351cafa6d149
7
- data.tar.gz: ca790ffffa4bbad4e3b9adf2d9a32832f346f3460d7b8807e0fb7a989780ed5e042c2bb86e242c1036c43df63c73af4ef77be327197f0745a67fdcd190beb10c
6
+ metadata.gz: 3cff57105377c3bce1c4d21a89201ee53b9a902041f24954ed7a6ad5df5d614adc409969538c9e7aaf4dc2f0c99f0f1daceff34764ce10084408257c51aa718f
7
+ data.tar.gz: b07c8991a259bcf4e0bab63e11927b848a4ef138495f827a02a790c42b4ac52b52a2bd09ce3a09349d982556daef63220911ad7e16c83759a37fc8c839bede84
@@ -2,27 +2,66 @@
2
2
 
3
3
  module ActiveRecord::Associations::Builder
4
4
  class Association
5
- def self.build(model, name, scope, options, &block)
6
- if model.dangerous_attribute_method?(name)
7
- raise ArgumentError, "You tried to define an association named #{name} on the model #{model.name}, but " \
8
- "this will conflict with a method #{name} already defined by Active Record. " \
9
- "Please choose a different association name."
10
- end
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
+
34
+ class DynamicReflectionOptionsHash < Hash
35
+ def [](key)
36
+ return super unless key == :dependent && super(:dependent) == :auto
37
+ return fallback_method if defining_dependent_callbacks?
11
38
 
12
- if options[:dependent] == :auto
13
- options[:dependent] = if model._destroy_callbacks.empty?
14
- :destroy
15
- else
16
- :delete
39
+ model = super(:model_name).constantize
40
+ return :destroy unless valid_destroy_callbacks(model).empty?
41
+
42
+ case super(:association_type)
43
+ when :singular then :delete
44
+ when :collection then :delete_all
45
+ else fallback_method
17
46
  end
18
47
  end
19
48
 
20
- reflection = create_reflection(model, name, scope, options, &block)
21
- define_accessors model, reflection
22
- define_callbacks model, reflection
23
- define_validations model, reflection
24
- define_change_tracking_methods model, reflection
25
- reflection
49
+ private
50
+
51
+ def fallback_method
52
+ :destroy
53
+ end
54
+
55
+ def defining_dependent_callbacks?
56
+ caller.any? { |line| line.include?("active_record/associations/builder/association.rb") }
57
+ end
58
+
59
+ def valid_destroy_callbacks(model)
60
+ model._destroy_callbacks.reject do |callback|
61
+ # ignore #handle_dependency callback
62
+ callback.filter.to_s.include?("active_record/associations/builder/association.rb")
63
+ end
64
+ end
26
65
  end
27
66
  end
28
67
  end
@@ -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 :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
@@ -1,7 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "active_record"
4
+ require "active_support"
4
5
 
5
6
  ActiveSupport.on_load(:active_record) do
7
+ require "dependent-auto-rails/activerecord/associations/builder/belongs_to"
8
+ require "dependent-auto-rails/activerecord/associations/builder/has_one"
9
+ require "dependent-auto-rails/activerecord/associations/builder/has_many"
6
10
  require "dependent-auto-rails/activerecord/associations/builder/association"
7
11
  end
12
+
13
+ module DependentAutoRails
14
+ class Error < StandardError; end
15
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DependentAutoRails
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.3"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dependent-auto-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Young
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  description:
28
42
  email:
29
43
  - djry1999@gmail.com
@@ -36,6 +50,9 @@ files:
36
50
  - LICENSE.txt
37
51
  - README.md
38
52
  - 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
39
56
  - lib/dependent-auto-rails/dependent_auto_rails.rb
40
57
  - lib/dependent-auto-rails/version.rb
41
58
  homepage: https://github.com/joshuay03/dependent-auto-rails
@@ -64,6 +81,6 @@ requirements: []
64
81
  rubygems_version: 3.5.3
65
82
  signing_key:
66
83
  specification_version: 4
67
- summary: Automatically and safely decides between :destroy and :delete for your Rails
68
- associations.
84
+ summary: Automatically and safely decides between :destroy and :delete / :delete_all
85
+ for your Rails associations.
69
86
  test_files: []