delete_recursively 0.9.3 → 0.9.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/delete_recursively.rb +84 -84
- metadata +14 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 60b6a4cc1cae62c705d0af8656a789f8839d5356
|
4
|
+
data.tar.gz: 408e3ff5f591eae523ca8d4b891e96f507323164
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fb4fe70e5eb85725fc696521c15d154250606a73f3104016995534c9b3610a2b6a387de67a695f73c8bbf9fd3cc9157d6572b7e576111ab1a02db00e54b79bae
|
7
|
+
data.tar.gz: 307924de1047c286cca034584b348954d4b90b8a4261a0fbb2e7ba6ef0dc841e8ef748750e42d66b81b565e6ad32e93ebc1830ffb750cff24daac1f6310661f2
|
data/lib/delete_recursively.rb
CHANGED
@@ -1,84 +1,84 @@
|
|
1
|
-
#
|
2
|
-
# DeleteRecursively
|
3
|
-
#
|
4
|
-
# Adds a new dependent: option to ActiveRecord associations.
|
5
|
-
#
|
6
|
-
module DeleteRecursively
|
7
|
-
NEW_DEPENDENT_OPTION = :delete_recursively
|
8
|
-
|
9
|
-
# override ::valid_dependent_options to make the new
|
10
|
-
# dependent option available to Association::Builder classes
|
11
|
-
module OptionPermission
|
12
|
-
def valid_dependent_options
|
13
|
-
super + [NEW_DEPENDENT_OPTION]
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
# override Association#handle_dependency to apply the new option if it is set
|
18
|
-
module DependencyHandling
|
19
|
-
def handle_dependency
|
20
|
-
return super unless DeleteRecursively.enabled_for?(self)
|
21
|
-
if reflection.belongs_to?
|
22
|
-
# can't use ::dependent_ids, owner is already destroyed at this point
|
23
|
-
ids = load_target ? target.id : []
|
24
|
-
else
|
25
|
-
ids = DeleteRecursively.dependent_ids(owner.class, owner.id, reflection)
|
26
|
-
end
|
27
|
-
DeleteRecursively.delete_recursively(reflection, ids)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
def self.delete_recursively(reflection, record_ids)
|
32
|
-
record_class = reflection.klass
|
33
|
-
record_class.reflect_on_all_associations.each do |sub_reflection|
|
34
|
-
next unless recurse_on?(sub_reflection)
|
35
|
-
dependent_ids = dependent_ids(record_class, record_ids, sub_reflection)
|
36
|
-
delete_recursively(sub_reflection, dependent_ids)
|
37
|
-
end
|
38
|
-
record_class.delete(record_ids) if enabled_for?(reflection)
|
39
|
-
record_class.destroy(record_ids) if destructive?(reflection)
|
40
|
-
end
|
41
|
-
|
42
|
-
def self.recurse_on?(reflection)
|
43
|
-
enabled_for?(reflection) || destructive?(reflection)
|
44
|
-
end
|
45
|
-
|
46
|
-
def self.enabled_for?(reflection)
|
47
|
-
reflection.options[:dependent] == NEW_DEPENDENT_OPTION
|
48
|
-
end
|
49
|
-
|
50
|
-
def self.destructive?(reflection)
|
51
|
-
[:destroy, :destroy_all].include?(reflection.options[:dependent])
|
52
|
-
end
|
53
|
-
|
54
|
-
def self.dependent_ids(owner_class, owner_ids, reflection)
|
55
|
-
if reflection.belongs_to?
|
56
|
-
owners_arel = owner_class.where(owner_class.primary_key => owner_ids)
|
57
|
-
owners_arel.pluck(reflection.association_foreign_key).compact
|
58
|
-
else
|
59
|
-
custom_foreign_key = reflection.options[:foreign_key]
|
60
|
-
foreign_key = custom_foreign_key || owner_class.to_s.foreign_key
|
61
|
-
reflection.klass.where(foreign_key => owner_ids).ids
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
def self.all(record_class, criteria = {})
|
66
|
-
applicable_criteria = criteria.select do |column_name, _value|
|
67
|
-
record_class.column_names.include?(column_name.to_s)
|
68
|
-
end
|
69
|
-
record_class.
|
70
|
-
record_class.reflect_on_all_associations.each do |reflection|
|
71
|
-
all(reflection.klass, criteria) if recurse_on?(reflection)
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
require 'active_record'
|
77
|
-
|
78
|
-
%w(BelongsTo HasMany HasOne).each do |assoc_name|
|
79
|
-
assoc_builder = ActiveRecord::Associations::Builder.const_get(assoc_name)
|
80
|
-
assoc_builder.singleton_class.prepend(DeleteRecursively::OptionPermission)
|
81
|
-
|
82
|
-
assoc_class = ActiveRecord::Associations.const_get("#{assoc_name}Association")
|
83
|
-
assoc_class.prepend(DeleteRecursively::DependencyHandling)
|
84
|
-
end
|
1
|
+
#
|
2
|
+
# DeleteRecursively
|
3
|
+
#
|
4
|
+
# Adds a new dependent: option to ActiveRecord associations.
|
5
|
+
#
|
6
|
+
module DeleteRecursively
|
7
|
+
NEW_DEPENDENT_OPTION = :delete_recursively
|
8
|
+
|
9
|
+
# override ::valid_dependent_options to make the new
|
10
|
+
# dependent option available to Association::Builder classes
|
11
|
+
module OptionPermission
|
12
|
+
def valid_dependent_options
|
13
|
+
super + [NEW_DEPENDENT_OPTION]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# override Association#handle_dependency to apply the new option if it is set
|
18
|
+
module DependencyHandling
|
19
|
+
def handle_dependency
|
20
|
+
return super unless DeleteRecursively.enabled_for?(self)
|
21
|
+
if reflection.belongs_to?
|
22
|
+
# can't use ::dependent_ids, owner is already destroyed at this point
|
23
|
+
ids = load_target ? target.id : []
|
24
|
+
else
|
25
|
+
ids = DeleteRecursively.dependent_ids(owner.class, owner.id, reflection)
|
26
|
+
end
|
27
|
+
DeleteRecursively.delete_recursively(reflection, ids)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.delete_recursively(reflection, record_ids)
|
32
|
+
record_class = reflection.klass
|
33
|
+
record_class.reflect_on_all_associations.each do |sub_reflection|
|
34
|
+
next unless recurse_on?(sub_reflection)
|
35
|
+
dependent_ids = dependent_ids(record_class, record_ids, sub_reflection)
|
36
|
+
delete_recursively(sub_reflection, dependent_ids)
|
37
|
+
end
|
38
|
+
record_class.delete(record_ids) if enabled_for?(reflection)
|
39
|
+
record_class.destroy(record_ids) if destructive?(reflection)
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.recurse_on?(reflection)
|
43
|
+
enabled_for?(reflection) || destructive?(reflection)
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.enabled_for?(reflection)
|
47
|
+
reflection.options[:dependent] == NEW_DEPENDENT_OPTION
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.destructive?(reflection)
|
51
|
+
[:destroy, :destroy_all].include?(reflection.options[:dependent])
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.dependent_ids(owner_class, owner_ids, reflection)
|
55
|
+
if reflection.belongs_to?
|
56
|
+
owners_arel = owner_class.where(owner_class.primary_key => owner_ids)
|
57
|
+
owners_arel.pluck(reflection.association_foreign_key).compact
|
58
|
+
else
|
59
|
+
custom_foreign_key = reflection.options[:foreign_key]
|
60
|
+
foreign_key = custom_foreign_key || owner_class.to_s.foreign_key
|
61
|
+
reflection.klass.where(foreign_key => owner_ids).ids
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.all(record_class, criteria = {})
|
66
|
+
applicable_criteria = criteria.select do |column_name, _value|
|
67
|
+
record_class.column_names.include?(column_name.to_s)
|
68
|
+
end
|
69
|
+
record_class.where(applicable_criteria).delete_all
|
70
|
+
record_class.reflect_on_all_associations.each do |reflection|
|
71
|
+
all(reflection.klass, criteria) if recurse_on?(reflection)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
require 'active_record'
|
77
|
+
|
78
|
+
%w(BelongsTo HasMany HasOne).each do |assoc_name|
|
79
|
+
assoc_builder = ActiveRecord::Associations::Builder.const_get(assoc_name)
|
80
|
+
assoc_builder.singleton_class.prepend(DeleteRecursively::OptionPermission)
|
81
|
+
|
82
|
+
assoc_class = ActiveRecord::Associations.const_get("#{assoc_name}Association")
|
83
|
+
assoc_class.prepend(DeleteRecursively::DependencyHandling)
|
84
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: delete_recursively
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Janosch Müller
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-07-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -16,84 +16,84 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '5.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '5.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: codeclimate-test-reporter
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '0.
|
33
|
+
version: '0.6'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '0.
|
40
|
+
version: '0.6'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rails
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '5.0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '5.0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rake
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
61
|
+
version: '11.2'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
68
|
+
version: '11.2'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rspec
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '3.
|
75
|
+
version: '3.5'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '3.
|
82
|
+
version: '3.5'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: rspec-rails
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
87
|
- - "~>"
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: '3.
|
89
|
+
version: '3.5'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: '3.
|
96
|
+
version: '3.5'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: sqlite3
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|