awesome_delete 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +17 -0
- data/lib/awesome_delete/delete_extension.rb +63 -52
- data/lib/awesome_delete/version.rb +1 -1
- data/spec/support/models.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ba9cbdeebc43e62ca6d10090a883590ce7dfe94e
|
4
|
+
data.tar.gz: ccdf455f9826f451a11a7cdae586068437949e74
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 79fdb5020b7522a116b26dfd8738b82a96a160f64717a6c4fdf8348d120236554491bcdb1dff3bd7967b06e66d1f693b1969593570ce8b8db17ca168a813231a
|
7
|
+
data.tar.gz: a0147c17026f7a58745b2c0ed1ebe57c59681a79fdf6d86e1bb0ece74d8db8298b68fbec1b669b084dd8fd8ee26ee3c42758cc4be54203e54dff820f3d30db29
|
data/README.md
CHANGED
@@ -8,6 +8,12 @@ It thinks about the following
|
|
8
8
|
- counter_cache, touch (avoid to unnecessary handle)
|
9
9
|
- callbacks
|
10
10
|
|
11
|
+
## Install
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem install awesome_delete
|
15
|
+
```
|
16
|
+
|
11
17
|
## Example
|
12
18
|
|
13
19
|
```ruby
|
@@ -25,13 +31,24 @@ Overwriting it maybe a better choice.
|
|
25
31
|
eg:
|
26
32
|
```ruby
|
27
33
|
class CloudFile < ActiveRecord::Base
|
34
|
+
before_destroy :test
|
28
35
|
after_destroy :remove_file
|
29
36
|
|
30
37
|
def self.execute_callbacks ids
|
38
|
+
# before_destroy
|
39
|
+
put 'someting'
|
40
|
+
|
41
|
+
where(id: ids).delete_all # delete self
|
42
|
+
|
43
|
+
# after_destroy
|
31
44
|
keys = where(id: ids).pluck(:key)
|
32
45
|
# do something with all keys
|
33
46
|
end
|
34
47
|
|
48
|
+
def test
|
49
|
+
put 'someting'
|
50
|
+
end
|
51
|
+
|
35
52
|
def remove_file key
|
36
53
|
HttpClient.send_request key
|
37
54
|
end
|
@@ -3,24 +3,23 @@ module AwesomeDelete
|
|
3
3
|
def delete_collection ids, all_associations_name = []
|
4
4
|
return true if ids.blank?
|
5
5
|
|
6
|
+
#Need not to set value for all_associations_name
|
6
7
|
#Not handle counter_cache or touch association in all_associations_name
|
7
|
-
|
8
|
-
|
9
|
-
if @@all_associations_name.blank?
|
10
|
-
@@all_associations_name = get_associations_name << self.name
|
8
|
+
if all_associations_name.blank?
|
9
|
+
all_associations_name = get_associations_name << self.name
|
11
10
|
end
|
12
11
|
|
13
|
-
delete_assoicated_collection(ids, deleted_associations)
|
12
|
+
delete_assoicated_collection(ids, deleted_associations, all_associations_name)
|
14
13
|
|
15
14
|
# STI
|
16
15
|
if column_names.include? inheritance_column
|
17
16
|
where(id: ids).pluck(inheritance_column).uniq.each do |type|
|
18
17
|
subklass = type.constantize
|
19
|
-
subklass.delete_self_collection(ids)
|
20
|
-
delete_assoicated_collection(ids, subklass.deleted_associations - deleted_associations)
|
18
|
+
subklass.delete_self_collection(ids, all_associations_name)
|
19
|
+
delete_assoicated_collection(ids, subklass.deleted_associations - deleted_associations, all_associations_name)
|
21
20
|
end
|
22
21
|
else
|
23
|
-
delete_self_collection(ids)
|
22
|
+
delete_self_collection(ids, all_associations_name)
|
24
23
|
end
|
25
24
|
end
|
26
25
|
|
@@ -42,68 +41,34 @@ module AwesomeDelete
|
|
42
41
|
end
|
43
42
|
end
|
44
43
|
|
45
|
-
def delete_self_collection ids
|
44
|
+
def delete_self_collection ids, all_associations_name
|
46
45
|
#touch
|
47
46
|
need_handle_touch_associations = touch_associations.select do |asso|
|
48
|
-
|
47
|
+
!all_associations_name.include?(asso.class_name)
|
49
48
|
end
|
50
|
-
|
51
|
-
if asso.options[:polymorphic]
|
52
|
-
types_ids = where(id: ids).pluck(asso.foreign_type, asso.foreign_key).uniq
|
53
|
-
types = types_ids.map(&:first).uniq
|
54
|
-
types.each do |type|
|
55
|
-
type_ids = types_ids.select { |type_id| type_id.first == type }.map(&:last).uniq.compact
|
56
|
-
type.constantize.where(id: type_ids).update_all updated_at: Time.now
|
57
|
-
end
|
58
|
-
else
|
59
|
-
asso.klass.where(id: where(id: ids).pluck(asso.foreign_key)).update_all updated_at: Time.now
|
60
|
-
end
|
61
|
-
end
|
49
|
+
cache_ids_with_types_of_touch = get_ids_with_types(need_handle_touch_associations)
|
62
50
|
|
63
51
|
#counter_cache
|
64
52
|
need_handle_counter_cache_associations = counter_cache_associations.select do |asso|
|
65
|
-
|
53
|
+
!all_associations_name.include?(asso.class_name)
|
66
54
|
end
|
67
|
-
|
68
|
-
need_handle_counter_cache_associations.each do |asso|
|
69
|
-
if asso.options[:polymorphic]
|
70
|
-
cache_ids_with_possible_types << where(id: ids).pluck(asso.foreign_type, asso.foreign_key).uniq
|
71
|
-
else
|
72
|
-
cache_ids_with_possible_types << where(id: ids).pluck(asso.foreign_key).uniq
|
73
|
-
end
|
74
|
-
end
|
55
|
+
cache_ids_with_types_of_counter_cache = get_ids_with_types(need_handle_counter_cache_associations)
|
75
56
|
|
76
57
|
execute_callbacks(ids)
|
77
58
|
|
78
|
-
|
79
|
-
need_handle_counter_cache_associations
|
80
|
-
if asso.options[:polymorphic]
|
81
|
-
types_ids = cache_ids_with_possible_types[index]
|
82
|
-
types = types_ids.map(&:first).uniq
|
83
|
-
types.each do |type|
|
84
|
-
ids = types_ids.select { |type_id| type_id.first == type }.map(&:last).uniq.compact
|
85
|
-
ids.each do |id|
|
86
|
-
type.constantize.where(id: id).update_all asso.counter_cache_column => where(asso.foreign_key => id).count
|
87
|
-
end
|
88
|
-
end
|
89
|
-
else
|
90
|
-
asso_ids = cache_ids_with_possible_types[index]
|
91
|
-
asso_ids.each do |id|
|
92
|
-
asso.klass.where(id: id).update_all asso.counter_cache_column => where(asso.foreign_key => id).count
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
59
|
+
handle_touch(need_handle_touch_associations, cache_ids_with_types_of_touch)
|
60
|
+
handle_counter_cache(need_handle_counter_cache_associations, cache_ids_with_types_of_counter_cache)
|
96
61
|
end
|
97
62
|
|
98
|
-
def delete_assoicated_collection ids, associations
|
63
|
+
def delete_assoicated_collection ids, associations, all_associations_name
|
99
64
|
associations.each do |association|
|
100
65
|
association_class = association.klass
|
101
66
|
|
102
67
|
#polymorphic
|
103
68
|
if association.type
|
104
|
-
association_class.delete_collection association_class.where(association.foreign_key => ids, association.type => self.name).pluck(:id),
|
69
|
+
association_class.delete_collection association_class.where(association.foreign_key => ids, association.type => self.name).pluck(:id), all_associations_name
|
105
70
|
else
|
106
|
-
association_class.delete_collection association_class.where(association.foreign_key => ids).pluck(:id),
|
71
|
+
association_class.delete_collection association_class.where(association.foreign_key => ids).pluck(:id), all_associations_name
|
107
72
|
end
|
108
73
|
end
|
109
74
|
end
|
@@ -119,6 +84,16 @@ module AwesomeDelete
|
|
119
84
|
associations_name
|
120
85
|
end
|
121
86
|
|
87
|
+
def get_ids_with_types associations
|
88
|
+
associations.map do |asso|
|
89
|
+
if asso.options[:polymorphic]
|
90
|
+
where(id: ids).pluck(asso.foreign_type, asso.foreign_key).uniq
|
91
|
+
else
|
92
|
+
where(id: ids).pluck(asso.foreign_key).uniq
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
122
97
|
def execute_callbacks ids
|
123
98
|
#overwriting this method may be a better choice
|
124
99
|
collection = where(id: ids).to_a
|
@@ -149,5 +124,41 @@ module AwesomeDelete
|
|
149
124
|
end
|
150
125
|
end
|
151
126
|
end
|
127
|
+
|
128
|
+
def handle_touch associations, ids_with_types
|
129
|
+
associations.each_with_index do |asso, index|
|
130
|
+
if asso.options[:polymorphic]
|
131
|
+
types_ids = ids_with_types[index]
|
132
|
+
types = types_ids.map(&:first).uniq
|
133
|
+
types.each do |type|
|
134
|
+
type_ids = types_ids.select { |type_id| type_id.first == type }.map(&:last).uniq.compact
|
135
|
+
type.constantize.where(id: type_ids).map(&:touch)
|
136
|
+
end
|
137
|
+
else
|
138
|
+
asso_ids = ids_with_types[index]
|
139
|
+
asso.klass.where(id: asso_ids).map(&:touch)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
def handle_counter_cache associations, ids_with_types
|
145
|
+
associations.each_with_index do |asso, index|
|
146
|
+
if asso.options[:polymorphic]
|
147
|
+
types_ids = ids_with_types[index]
|
148
|
+
types = types_ids.map(&:first).uniq
|
149
|
+
types.each do |type|
|
150
|
+
type_ids = types_ids.select { |type_id| type_id.first == type }.map(&:last).uniq.compact
|
151
|
+
type_ids.each do |id|
|
152
|
+
type.constantize.where(id: id).update_all asso.counter_cache_column => where(asso.foreign_key => id).count
|
153
|
+
end
|
154
|
+
end
|
155
|
+
else
|
156
|
+
asso_ids = ids_with_types[index]
|
157
|
+
asso_ids.each do |id|
|
158
|
+
asso.klass.where(id: id).update_all asso.counter_cache_column => where(asso.foreign_key => id).count
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
152
163
|
end
|
153
164
|
end
|
data/spec/support/models.rb
CHANGED
@@ -58,7 +58,7 @@ end
|
|
58
58
|
class ActiveRecord::Relation
|
59
59
|
def update_all updates
|
60
60
|
#for test
|
61
|
-
if updates[:updated_at]
|
61
|
+
if updates[:updated_at] || updates['updated_at']
|
62
62
|
Logger.send "#{model.name.downcase}_touch", 'Touching'
|
63
63
|
elsif updates.keys.find { |key| key =~ /.*_count$/ }
|
64
64
|
Logger.send "#{model.name.downcase}_update_counter", 'Updating counter'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: awesome_delete
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- hw93
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-12-
|
11
|
+
date: 2015-12-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|