delete_recursively 0.9.3 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/delete_recursively/version.rb +5 -0
- data/lib/delete_recursively.rb +87 -34
- metadata +52 -27
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8dd51a14780db543c8f623dcc7d65a1c64a0711c43e9f903b2dd2c1003290145
|
4
|
+
data.tar.gz: ccaac450fe4293eb267bc0570cc055b097bf78d7f179349a351798578c828e57
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 604ba86881bd30346afa016ff584e1f94b8fa100ea5157e58750e3ff4fce9eb29ba24202b2c84ea1e3bcb0ddcfcac4abf0c5f60f093c6359bc121a952442dc87
|
7
|
+
data.tar.gz: 61307162f1cf6f673371d04c32d75b6c920a653254c6d54d3e6806abfc059336a17437b2bd523acd8669de0a9e77506275bb341cd88ce8e84221ee1486832f68
|
data/lib/delete_recursively.rb
CHANGED
@@ -1,9 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
#
|
2
4
|
# DeleteRecursively
|
3
5
|
#
|
4
6
|
# Adds a new dependent: option to ActiveRecord associations.
|
5
7
|
#
|
6
8
|
module DeleteRecursively
|
9
|
+
require_relative File.join('delete_recursively', 'version')
|
10
|
+
|
7
11
|
NEW_DEPENDENT_OPTION = :delete_recursively
|
8
12
|
|
9
13
|
# override ::valid_dependent_options to make the new
|
@@ -28,54 +32,103 @@ module DeleteRecursively
|
|
28
32
|
end
|
29
33
|
end
|
30
34
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
35
|
+
class << self
|
36
|
+
def delete_recursively(reflection, record_ids)
|
37
|
+
record_class = reflection.klass
|
38
|
+
if recurse_on?(reflection)
|
39
|
+
record_class.reflect_on_all_associations.each do |sub_reflection|
|
40
|
+
sub_ref_ids = dependent_ids(record_class, record_ids, sub_reflection)
|
41
|
+
delete_recursively(sub_reflection, sub_ref_ids)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
destroy_or_delete(reflection, record_class, record_ids)
|
37
45
|
end
|
38
|
-
record_class.delete(record_ids) if enabled_for?(reflection)
|
39
|
-
record_class.destroy(record_ids) if destructive?(reflection)
|
40
|
-
end
|
41
46
|
|
42
|
-
|
43
|
-
|
44
|
-
|
47
|
+
def destroy_or_delete(reflection, record_class, record_ids)
|
48
|
+
if destructive?(reflection)
|
49
|
+
record_class.destroy(record_ids)
|
50
|
+
elsif enabled_for?(reflection) || deleting?(reflection)
|
51
|
+
record_class.delete(record_ids)
|
52
|
+
end
|
53
|
+
end
|
45
54
|
|
46
|
-
|
47
|
-
|
48
|
-
|
55
|
+
def recurse_on?(reflection)
|
56
|
+
enabled_for?(reflection) || destructive?(reflection)
|
57
|
+
end
|
49
58
|
|
50
|
-
|
51
|
-
|
52
|
-
|
59
|
+
def enabled_for?(reflection)
|
60
|
+
reflection.options[:dependent] == NEW_DEPENDENT_OPTION
|
61
|
+
end
|
53
62
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
63
|
+
def destructive?(reflection)
|
64
|
+
%i[destroy destroy_all].include?(reflection.options[:dependent])
|
65
|
+
end
|
66
|
+
|
67
|
+
def deleting?(reflection)
|
68
|
+
%i[delete delete_all].include?(reflection.options[:dependent])
|
69
|
+
end
|
70
|
+
|
71
|
+
def dependent_ids(owner_class, owner_ids, reflection)
|
72
|
+
if reflection.belongs_to?
|
73
|
+
owners_arel = owner_class.where(owner_class.primary_key => owner_ids)
|
74
|
+
owners_arel.pluck(reflection.association_foreign_key).compact
|
75
|
+
elsif reflection.through_reflection
|
76
|
+
dependent_ids_with_through_option(owner_class, owner_ids, reflection)
|
77
|
+
else
|
78
|
+
owner_foreign_key = foreign_key(owner_class, reflection)
|
79
|
+
reflection.klass.where(owner_foreign_key => owner_ids).ids
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def dependent_ids_with_through_option(owner_class, owner_ids, reflection)
|
84
|
+
through_reflection = reflection.through_reflection
|
85
|
+
owner_foreign_key = foreign_key(owner_class, through_reflection)
|
86
|
+
|
87
|
+
dependent_class = reflection.klass
|
88
|
+
dependent_through_reflection = inverse_through_reflection(reflection)
|
89
|
+
dependent_foreign_key =
|
90
|
+
foreign_key(dependent_class, dependent_through_reflection)
|
91
|
+
|
92
|
+
through_reflection.klass
|
93
|
+
.where(owner_foreign_key => owner_ids)
|
94
|
+
.pluck(dependent_foreign_key)
|
62
95
|
end
|
63
|
-
end
|
64
96
|
|
65
|
-
|
66
|
-
|
67
|
-
|
97
|
+
def inverse_through_reflection(reflection)
|
98
|
+
through_class = reflection.through_reflection.klass
|
99
|
+
reflection.klass.reflect_on_all_associations
|
100
|
+
.map(&:through_reflection)
|
101
|
+
.find { |thr_ref| thr_ref && thr_ref.klass == through_class }
|
68
102
|
end
|
69
|
-
|
70
|
-
|
71
|
-
|
103
|
+
|
104
|
+
def foreign_key(owner_class, reflection)
|
105
|
+
custom_foreign_key = reflection && reflection.options[:foreign_key]
|
106
|
+
custom_foreign_key || owner_class.to_s.foreign_key
|
107
|
+
end
|
108
|
+
|
109
|
+
def all(record_class, criteria = {})
|
110
|
+
record_class.reflect_on_all_associations.each do |reflection|
|
111
|
+
if recurse_on?(reflection)
|
112
|
+
all(reflection.klass, criteria)
|
113
|
+
elsif deleting?(reflection)
|
114
|
+
delete_with_applicable_criteria(reflection.klass, criteria)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
delete_with_applicable_criteria(record_class, criteria)
|
118
|
+
end
|
119
|
+
|
120
|
+
def delete_with_applicable_criteria(record_class, criteria)
|
121
|
+
applicable_criteria = criteria.select do |column_name, _value|
|
122
|
+
record_class.column_names.include?(column_name.to_s)
|
123
|
+
end
|
124
|
+
record_class.where(applicable_criteria).delete_all
|
72
125
|
end
|
73
126
|
end
|
74
127
|
end
|
75
128
|
|
76
129
|
require 'active_record'
|
77
130
|
|
78
|
-
%w
|
131
|
+
%w[BelongsTo HasMany HasOne].each do |assoc_name|
|
79
132
|
assoc_builder = ActiveRecord::Associations::Builder.const_get(assoc_name)
|
80
133
|
assoc_builder.singleton_class.prepend(DeleteRecursively::OptionPermission)
|
81
134
|
|
metadata
CHANGED
@@ -1,113 +1,139 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: delete_recursively
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Janosch Müller
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-12-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.1.14
|
20
|
+
- - "<"
|
18
21
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
22
|
+
version: 8.0.0
|
20
23
|
type: :runtime
|
21
24
|
prerelease: false
|
22
25
|
version_requirements: !ruby/object:Gem::Requirement
|
23
26
|
requirements:
|
24
|
-
- - "
|
27
|
+
- - ">="
|
25
28
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
29
|
+
version: 4.1.14
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 8.0.0
|
27
33
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
34
|
+
name: appraisal
|
29
35
|
requirement: !ruby/object:Gem::Requirement
|
30
36
|
requirements:
|
31
37
|
- - "~>"
|
32
38
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
39
|
+
version: '2.3'
|
34
40
|
type: :development
|
35
41
|
prerelease: false
|
36
42
|
version_requirements: !ruby/object:Gem::Requirement
|
37
43
|
requirements:
|
38
44
|
- - "~>"
|
39
45
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
46
|
+
version: '2.3'
|
41
47
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
48
|
+
name: codecov
|
43
49
|
requirement: !ruby/object:Gem::Requirement
|
44
50
|
requirements:
|
45
51
|
- - "~>"
|
46
52
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
53
|
+
version: '0.2'
|
48
54
|
type: :development
|
49
55
|
prerelease: false
|
50
56
|
version_requirements: !ruby/object:Gem::Requirement
|
51
57
|
requirements:
|
52
58
|
- - "~>"
|
53
59
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
60
|
+
version: '0.2'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rails
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: 4.1.14
|
68
|
+
- - "<"
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: 8.0.0
|
71
|
+
type: :development
|
72
|
+
prerelease: false
|
73
|
+
version_requirements: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 4.1.14
|
78
|
+
- - "<"
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: 8.0.0
|
55
81
|
- !ruby/object:Gem::Dependency
|
56
82
|
name: rake
|
57
83
|
requirement: !ruby/object:Gem::Requirement
|
58
84
|
requirements:
|
59
85
|
- - "~>"
|
60
86
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
87
|
+
version: '13.0'
|
62
88
|
type: :development
|
63
89
|
prerelease: false
|
64
90
|
version_requirements: !ruby/object:Gem::Requirement
|
65
91
|
requirements:
|
66
92
|
- - "~>"
|
67
93
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
94
|
+
version: '13.0'
|
69
95
|
- !ruby/object:Gem::Dependency
|
70
96
|
name: rspec
|
71
97
|
requirement: !ruby/object:Gem::Requirement
|
72
98
|
requirements:
|
73
99
|
- - "~>"
|
74
100
|
- !ruby/object:Gem::Version
|
75
|
-
version: '3.
|
101
|
+
version: '3.10'
|
76
102
|
type: :development
|
77
103
|
prerelease: false
|
78
104
|
version_requirements: !ruby/object:Gem::Requirement
|
79
105
|
requirements:
|
80
106
|
- - "~>"
|
81
107
|
- !ruby/object:Gem::Version
|
82
|
-
version: '3.
|
108
|
+
version: '3.10'
|
83
109
|
- !ruby/object:Gem::Dependency
|
84
110
|
name: rspec-rails
|
85
111
|
requirement: !ruby/object:Gem::Requirement
|
86
112
|
requirements:
|
87
113
|
- - "~>"
|
88
114
|
- !ruby/object:Gem::Version
|
89
|
-
version: '
|
115
|
+
version: '4.0'
|
90
116
|
type: :development
|
91
117
|
prerelease: false
|
92
118
|
version_requirements: !ruby/object:Gem::Requirement
|
93
119
|
requirements:
|
94
120
|
- - "~>"
|
95
121
|
- !ruby/object:Gem::Version
|
96
|
-
version: '
|
122
|
+
version: '4.0'
|
97
123
|
- !ruby/object:Gem::Dependency
|
98
124
|
name: sqlite3
|
99
125
|
requirement: !ruby/object:Gem::Requirement
|
100
126
|
requirements:
|
101
127
|
- - "~>"
|
102
128
|
- !ruby/object:Gem::Version
|
103
|
-
version: '1.
|
129
|
+
version: '1.4'
|
104
130
|
type: :development
|
105
131
|
prerelease: false
|
106
132
|
version_requirements: !ruby/object:Gem::Requirement
|
107
133
|
requirements:
|
108
134
|
- - "~>"
|
109
135
|
- !ruby/object:Gem::Version
|
110
|
-
version: '1.
|
136
|
+
version: '1.4'
|
111
137
|
description: Adds a new :dependent option for ActiveRecord associations that recursively
|
112
138
|
deletes all dependent records without instantiating them.
|
113
139
|
email: janosch84@gmail.com
|
@@ -116,11 +142,12 @@ extensions: []
|
|
116
142
|
extra_rdoc_files: []
|
117
143
|
files:
|
118
144
|
- lib/delete_recursively.rb
|
119
|
-
|
145
|
+
- lib/delete_recursively/version.rb
|
146
|
+
homepage: https://github.com/jaynetics/delete_recursively
|
120
147
|
licenses:
|
121
148
|
- MIT
|
122
149
|
metadata: {}
|
123
|
-
post_install_message:
|
150
|
+
post_install_message:
|
124
151
|
rdoc_options: []
|
125
152
|
require_paths:
|
126
153
|
- lib
|
@@ -135,10 +162,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
135
162
|
- !ruby/object:Gem::Version
|
136
163
|
version: '0'
|
137
164
|
requirements: []
|
138
|
-
|
139
|
-
|
140
|
-
signing_key:
|
165
|
+
rubygems_version: 3.2.32
|
166
|
+
signing_key:
|
141
167
|
specification_version: 4
|
142
168
|
summary: Delete ActiveRecords efficiently
|
143
169
|
test_files: []
|
144
|
-
has_rdoc:
|