jchupp-is_paranoid 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.textile +5 -5
- data/lib/is_paranoid.rb +4 -4
- data/spec/android_spec.rb +8 -8
- metadata +1 -1
data/README.textile
CHANGED
@@ -37,10 +37,10 @@ Now our automobiles are now soft-deleteable.
|
|
37
37
|
|
38
38
|
that_large_automobile.destroy
|
39
39
|
Automobile.count # => 0
|
40
|
-
Automobile.
|
40
|
+
Automobile.count_with_destroyed # => 1
|
41
41
|
|
42
42
|
# where is that large automobile?
|
43
|
-
that_large_automobile = Automobile.
|
43
|
+
that_large_automobile = Automobile.find_with_destroyed(:all).first
|
44
44
|
that_large_automobile.restore
|
45
45
|
Automobile.count # => 1
|
46
46
|
</pre>
|
@@ -50,11 +50,11 @@ One thing to note, destroying is always undo-able, but deleting is not.
|
|
50
50
|
<pre>
|
51
51
|
Automobile.destroy_all
|
52
52
|
Automobile.count # => 0
|
53
|
-
Automobile.
|
53
|
+
Automobile.count_with_destroyed # => 1
|
54
54
|
|
55
55
|
Automobile.delete_all
|
56
|
-
Automobile.
|
57
|
-
# And you may
|
56
|
+
Automobile.count_with_destroyed # => 0
|
57
|
+
# And you may say to yourself, "My god! What have I done?"
|
58
58
|
</pre>
|
59
59
|
|
60
60
|
h3. and you may ask yourself, where does that highway go to?
|
data/lib/is_paranoid.rb
CHANGED
@@ -17,8 +17,8 @@ module IsParanoid
|
|
17
17
|
class_eval do
|
18
18
|
# This is the real magic. All calls made to this model will append
|
19
19
|
# the conditions deleted_at => nil. Exceptions require using
|
20
|
-
# exclusive_scope (see self.delete_all, self.
|
21
|
-
# and self.
|
20
|
+
# exclusive_scope (see self.delete_all, self.count_with_destroyed,
|
21
|
+
# and self.find_with_destroyed )
|
22
22
|
default_scope :conditions => {:deleted_at => nil}
|
23
23
|
|
24
24
|
# Actually delete the model, bypassing the safety net. Because
|
@@ -32,13 +32,13 @@ module IsParanoid
|
|
32
32
|
end
|
33
33
|
|
34
34
|
# Return a count that includes the soft-deleted models.
|
35
|
-
def self.
|
35
|
+
def self.count_with_destroyed *args
|
36
36
|
self.with_exclusive_scope { count(*args) }
|
37
37
|
end
|
38
38
|
|
39
39
|
# Return instances of all models matching the query regardless
|
40
40
|
# of whether or not they have been soft-deleted.
|
41
|
-
def self.
|
41
|
+
def self.find_with_destroyed *args
|
42
42
|
self.with_exclusive_scope { find(*args) }
|
43
43
|
end
|
44
44
|
|
data/spec/android_spec.rb
CHANGED
@@ -19,16 +19,16 @@ describe Android do
|
|
19
19
|
end
|
20
20
|
|
21
21
|
it "should delete normally" do
|
22
|
-
Android.
|
22
|
+
Android.count_with_destroyed.should == 2
|
23
23
|
Android.delete_all
|
24
|
-
Android.
|
24
|
+
Android.count_with_destroyed.should == 0
|
25
25
|
end
|
26
26
|
|
27
27
|
it "should handle Model.destroy_all properly" do
|
28
28
|
lambda{
|
29
29
|
Android.destroy_all("owner_id = #{@luke.id}")
|
30
30
|
}.should change(Android, :count).from(2).to(0)
|
31
|
-
Android.
|
31
|
+
Android.count_with_destroyed.should == 2
|
32
32
|
end
|
33
33
|
|
34
34
|
it "should handle Model.destroy(id) properly" do
|
@@ -36,7 +36,7 @@ describe Android do
|
|
36
36
|
Android.destroy(@r2d2.id)
|
37
37
|
}.should change(Android, :count).from(2).to(1)
|
38
38
|
|
39
|
-
Android.
|
39
|
+
Android.count_with_destroyed.should == 2
|
40
40
|
end
|
41
41
|
|
42
42
|
it "should be not show up in the relationship to the owner once deleted" do
|
@@ -47,24 +47,24 @@ describe Android do
|
|
47
47
|
Android.first(:conditions => {:name => 'R2D2'}).should be_blank
|
48
48
|
end
|
49
49
|
|
50
|
-
it "should be able to find deleted items via
|
50
|
+
it "should be able to find deleted items via find_with_destroyed" do
|
51
51
|
@r2d2.destroy
|
52
52
|
Android.find(:first, :conditions => {:name => 'R2D2'}).should be_blank
|
53
|
-
Android.
|
53
|
+
Android.find_with_destroyed(:first, :conditions => {:name => 'R2D2'}).should_not be_blank
|
54
54
|
end
|
55
55
|
|
56
56
|
it "should have a proper count inclusively and exclusively of deleted items" do
|
57
57
|
@r2d2.destroy
|
58
58
|
@c3p0.destroy
|
59
59
|
Android.count.should == 0
|
60
|
-
Android.
|
60
|
+
Android.count_with_destroyed.should == 2
|
61
61
|
end
|
62
62
|
|
63
63
|
it "should mark deleted on dependent destroys" do
|
64
64
|
lambda{
|
65
65
|
@luke.destroy
|
66
66
|
}.should change(Android, :count).from(2).to(0)
|
67
|
-
Android.
|
67
|
+
Android.count_with_destroyed.should == 2
|
68
68
|
end
|
69
69
|
|
70
70
|
it "should allow restoring" do
|