soft_deletion 0.5.1 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/Readme.md +6 -0
- data/lib/soft_deletion/core.rb +2 -2
- data/lib/soft_deletion/dependency.rb +9 -14
- data/lib/soft_deletion/version.rb +1 -1
- data/spec/soft_deletion_spec.rb +23 -0
- data/spec/spec_helper.rb +18 -0
- metadata +3 -3
data/Gemfile.lock
CHANGED
data/Readme.md
CHANGED
@@ -38,6 +38,12 @@ Usage
|
|
38
38
|
# soft delete many
|
39
39
|
User.soft_delete_all!(1,2,3,4)
|
40
40
|
|
41
|
+
TODO
|
42
|
+
====
|
43
|
+
- nullify should use update_all on has_many on soft_delete (performance)
|
44
|
+
- has_many :through should delete join associations on soft_delete
|
45
|
+
- cascading soft_deletes should use the same timestamp for easy reverts
|
46
|
+
|
41
47
|
Authors
|
42
48
|
=======
|
43
49
|
|
data/lib/soft_deletion/core.rb
CHANGED
@@ -21,9 +21,9 @@ module SoftDeletion
|
|
21
21
|
|
22
22
|
module ClassMethods
|
23
23
|
def soft_delete_dependents
|
24
|
-
|
24
|
+
reflect_on_all_associations.
|
25
25
|
select { |a| [:destroy, :delete_all, :nullify].include?(a.options[:dependent]) }.
|
26
|
-
map(&:name)
|
26
|
+
select { |a| a.klass.method_defined?(:soft_delete!) }.map(&:name)
|
27
27
|
end
|
28
28
|
|
29
29
|
def with_deleted
|
@@ -8,18 +8,17 @@ module SoftDeletion
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def soft_delete!
|
11
|
-
|
12
|
-
|
13
|
-
if nullify?
|
11
|
+
case association.options[:dependent]
|
12
|
+
when :nullify
|
14
13
|
nullify_dependencies
|
14
|
+
when :delete_all
|
15
|
+
dependency.update_all(:deleted_at => Time.now)
|
15
16
|
else
|
16
17
|
dependencies.each(&:soft_delete!)
|
17
18
|
end
|
18
19
|
end
|
19
20
|
|
20
21
|
def soft_undelete!(limit)
|
21
|
-
return unless can_soft_delete?
|
22
|
-
|
23
22
|
klass.with_deleted do
|
24
23
|
dependencies.reject { |m| m.deleted_at < limit }.each(&:soft_undelete!)
|
25
24
|
end
|
@@ -27,10 +26,6 @@ module SoftDeletion
|
|
27
26
|
|
28
27
|
protected
|
29
28
|
|
30
|
-
def nullify?
|
31
|
-
association.options[:dependent] == :nullify
|
32
|
-
end
|
33
|
-
|
34
29
|
def nullify_dependencies
|
35
30
|
dependencies.each do |dependency|
|
36
31
|
foreign_key = if association.respond_to?(:foreign_key) # rails 3.1+
|
@@ -43,10 +38,6 @@ module SoftDeletion
|
|
43
38
|
end
|
44
39
|
end
|
45
40
|
|
46
|
-
def can_soft_delete?
|
47
|
-
klass.method_defined? :soft_delete!
|
48
|
-
end
|
49
|
-
|
50
41
|
def klass
|
51
42
|
association.klass
|
52
43
|
end
|
@@ -55,8 +46,12 @@ module SoftDeletion
|
|
55
46
|
record.class.reflect_on_association(association_name.to_sym)
|
56
47
|
end
|
57
48
|
|
49
|
+
def dependency
|
50
|
+
record.send(association_name)
|
51
|
+
end
|
52
|
+
|
58
53
|
def dependencies
|
59
|
-
Array.wrap(
|
54
|
+
Array.wrap(dependency)
|
60
55
|
end
|
61
56
|
end
|
62
57
|
end
|
data/spec/soft_deletion_spec.rb
CHANGED
@@ -330,6 +330,29 @@ describe SoftDeletion do
|
|
330
330
|
end
|
331
331
|
end
|
332
332
|
|
333
|
+
context "a soft-deleted has-many category that delete_all forum references on delete" do
|
334
|
+
it "use update_all to delete references" do
|
335
|
+
category = DDACategory.create!
|
336
|
+
forum = category.forums.create!
|
337
|
+
category.soft_delete!
|
338
|
+
|
339
|
+
forum.should_not be_deleted # just did an update_all
|
340
|
+
Forum.find(forum.id).should be_deleted
|
341
|
+
end
|
342
|
+
end
|
343
|
+
|
344
|
+
context "a soft-deleted has-many category that defaults dependent forum references on delete" do
|
345
|
+
it "does nothing to those references" do
|
346
|
+
category = XDACategory.create!
|
347
|
+
forum = category.forums.create!
|
348
|
+
category.soft_delete!
|
349
|
+
|
350
|
+
forum.reload
|
351
|
+
forum.should_not be_deleted
|
352
|
+
forum.category_id.should_not be_nil
|
353
|
+
end
|
354
|
+
end
|
355
|
+
|
333
356
|
context "a soft-deleted has-many category that nullifies forum references on delete without foreign_key" do
|
334
357
|
it "should nullify those references" do
|
335
358
|
organization = Organization.create!
|
data/spec/spec_helper.rb
CHANGED
@@ -121,6 +121,24 @@ class NDACategory < ActiveRecord::Base
|
|
121
121
|
has_many :forums, :dependent => :nullify, :foreign_key => :category_id
|
122
122
|
end
|
123
123
|
|
124
|
+
# Delete dependent association
|
125
|
+
class DDACategory < ActiveRecord::Base
|
126
|
+
silent_set_table_name 'categories'
|
127
|
+
|
128
|
+
has_soft_deletion
|
129
|
+
|
130
|
+
has_many :forums, :dependent => :delete_all, :foreign_key => :category_id
|
131
|
+
end
|
132
|
+
|
133
|
+
# default dependent association
|
134
|
+
class XDACategory < ActiveRecord::Base
|
135
|
+
silent_set_table_name 'categories'
|
136
|
+
|
137
|
+
has_soft_deletion
|
138
|
+
|
139
|
+
has_many :forums, :foreign_key => :category_id
|
140
|
+
end
|
141
|
+
|
124
142
|
# Has ome association
|
125
143
|
class HOACategory < ActiveRecord::Base
|
126
144
|
silent_set_table_name 'categories'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: soft_deletion
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -54,7 +54,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
54
54
|
version: '0'
|
55
55
|
segments:
|
56
56
|
- 0
|
57
|
-
hash: -
|
57
|
+
hash: -1879420123943437209
|
58
58
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
59
|
none: false
|
60
60
|
requirements:
|
@@ -63,7 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
63
63
|
version: '0'
|
64
64
|
segments:
|
65
65
|
- 0
|
66
|
-
hash: -
|
66
|
+
hash: -1879420123943437209
|
67
67
|
requirements: []
|
68
68
|
rubyforge_project:
|
69
69
|
rubygems_version: 1.8.25
|