dependent-auto-rails 0.1.1 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/dependent-auto-rails/activerecord/associations/builder/association.rb +56 -17
- data/lib/dependent-auto-rails/activerecord/associations/builder/belongs_to.rb +10 -0
- data/lib/dependent-auto-rails/activerecord/associations/builder/has_many.rb +10 -0
- data/lib/dependent-auto-rails/activerecord/associations/builder/has_one.rb +10 -0
- data/lib/dependent-auto-rails/dependent_auto_rails.rb +13 -1
- data/lib/dependent-auto-rails/version.rb +1 -1
- metadata +20 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9f580a4dd1d2381f710091f77c3f788db32d52454cbbc15a8d19234cfac2b6cc
|
4
|
+
data.tar.gz: 2b93ef9a7b4d3bf9a51040710f6f0234a5688286ea1f80b2dd77e16e46387f06
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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.
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
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
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
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
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
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,3 +1,15 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "
|
3
|
+
require "active_record"
|
4
|
+
require "active_support"
|
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"
|
10
|
+
require "dependent-auto-rails/activerecord/associations/builder/association"
|
11
|
+
end
|
12
|
+
|
13
|
+
module DependentAutoRails
|
14
|
+
class Error < StandardError; end
|
15
|
+
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.
|
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
|
68
|
-
associations.
|
84
|
+
summary: Automatically and safely decides between :destroy and :delete / :delete_all
|
85
|
+
for your Rails associations.
|
69
86
|
test_files: []
|