jchupp-is_paranoid 0.6.0 → 0.7.0
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/VERSION.yml +1 -1
- data/lib/is_paranoid.rb +22 -4
- data/spec/is_paranoid_spec.rb +29 -8
- data/spec/schema.rb +8 -0
- metadata +3 -3
data/VERSION.yml
CHANGED
data/lib/is_paranoid.rb
CHANGED
@@ -56,20 +56,38 @@ module IsParanoid
|
|
56
56
|
end
|
57
57
|
|
58
58
|
# Use update_all with an exclusive scope to restore undo the soft-delete.
|
59
|
-
# This bypasses update-related callbacks
|
60
|
-
|
59
|
+
# This bypasses update-related callbacks.
|
60
|
+
#
|
61
|
+
# By default, restores cascade through associations that are
|
62
|
+
# :dependent => :destroy and under is_paranoid. You can prevent restoration
|
63
|
+
# of associated models by passing :include_destroyed_dependents => false,
|
64
|
+
# for example:
|
65
|
+
# Android.restore(:include_destroyed_dependents => false)
|
66
|
+
def self.restore(id, options = {})
|
67
|
+
options.reverse_merge!({:include_destroyed_dependents => true})
|
61
68
|
with_exclusive_scope do
|
62
69
|
update_all(
|
63
70
|
"#{destroyed_field} = #{connection.quote(field_not_destroyed)}",
|
64
71
|
"id = #{id}"
|
65
72
|
)
|
66
73
|
end
|
74
|
+
if options[:include_destroyed_dependents]
|
75
|
+
self.reflect_on_all_associations.each do |association|
|
76
|
+
if association.options[:dependent] == :destroy and association.klass.respond_to?(:restore)
|
77
|
+
association.klass.find_destroyed_only(:all,
|
78
|
+
:conditions => ["#{association.primary_key_name} = ?", id]
|
79
|
+
).each do |model|
|
80
|
+
model.restore
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
67
85
|
end
|
68
86
|
|
69
87
|
# Set deleted_at flag on a model to field_not_destroyed, effectively
|
70
88
|
# undoing the soft-deletion.
|
71
|
-
def restore
|
72
|
-
self.class.restore(id)
|
89
|
+
def restore(options = {})
|
90
|
+
self.class.restore(id, options)
|
73
91
|
end
|
74
92
|
|
75
93
|
# find_with_destroyed and other blah_with_destroyed and
|
data/spec/is_paranoid_spec.rb
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
|
-
class Person < ActiveRecord::Base
|
3
|
+
class Person < ActiveRecord::Base #:nodoc:
|
4
4
|
validates_uniqueness_of :name
|
5
5
|
has_many :androids, :foreign_key => :owner_id, :dependent => :destroy
|
6
6
|
end
|
7
7
|
|
8
|
-
class Android < ActiveRecord::Base
|
8
|
+
class Android < ActiveRecord::Base #:nodoc:
|
9
9
|
validates_uniqueness_of :name
|
10
|
+
has_many :components, :dependent => :destroy
|
10
11
|
|
11
12
|
is_paranoid
|
12
13
|
|
@@ -16,7 +17,11 @@ class Android < ActiveRecord::Base
|
|
16
17
|
end
|
17
18
|
end
|
18
19
|
|
19
|
-
class
|
20
|
+
class Component < ActiveRecord::Base #:nodoc:
|
21
|
+
is_paranoid
|
22
|
+
end
|
23
|
+
|
24
|
+
class AndroidWithScopedUniqueness < ActiveRecord::Base #:nodoc:
|
20
25
|
set_table_name :androids
|
21
26
|
validates_uniqueness_of :name, :scope => :deleted_at
|
22
27
|
is_paranoid
|
@@ -26,10 +31,12 @@ describe Android do
|
|
26
31
|
before(:each) do
|
27
32
|
Android.delete_all
|
28
33
|
Person.delete_all
|
34
|
+
Component.delete_all
|
29
35
|
|
30
36
|
@luke = Person.create(:name => 'Luke Skywalker')
|
31
37
|
@r2d2 = Android.create(:name => 'R2D2', :owner_id => @luke.id)
|
32
38
|
@c3p0 = Android.create(:name => 'C3P0', :owner_id => @luke.id)
|
39
|
+
@r2d2.components.create(:name => 'Rotors')
|
33
40
|
end
|
34
41
|
|
35
42
|
it "should delete normally" do
|
@@ -94,6 +101,20 @@ describe Android do
|
|
94
101
|
}.should change(Android, :count).from(1).to(2)
|
95
102
|
end
|
96
103
|
|
104
|
+
it "should restore dependent models when being restored" do
|
105
|
+
@r2d2.destroy
|
106
|
+
lambda{
|
107
|
+
@r2d2.restore
|
108
|
+
}.should change(Component, :count).from(0).to(1)
|
109
|
+
end
|
110
|
+
|
111
|
+
it "should allow the option to not restore dependent models when being restored" do
|
112
|
+
@r2d2.destroy
|
113
|
+
lambda{
|
114
|
+
@r2d2.restore(:include_destroyed_dependents => false)
|
115
|
+
}.should_not change(Component, :count)
|
116
|
+
end
|
117
|
+
|
97
118
|
it "should respond to various calculations" do
|
98
119
|
@r2d2.destroy
|
99
120
|
Android.sum('id').should == @c3p0.id
|
@@ -119,21 +140,21 @@ describe Android do
|
|
119
140
|
end
|
120
141
|
end
|
121
142
|
|
122
|
-
class Ninja < ActiveRecord::Base
|
143
|
+
class Ninja < ActiveRecord::Base #:nodoc:
|
123
144
|
validates_uniqueness_of :name, :scope => :visible
|
124
145
|
is_paranoid :field => [:visible, false, true]
|
125
146
|
end
|
126
147
|
|
127
|
-
class Pirate < ActiveRecord::Base
|
148
|
+
class Pirate < ActiveRecord::Base #:nodoc:
|
128
149
|
is_paranoid :field => [:alive, false, true]
|
129
150
|
end
|
130
151
|
|
131
|
-
class DeadPirate < ActiveRecord::Base
|
152
|
+
class DeadPirate < ActiveRecord::Base #:nodoc:
|
132
153
|
set_table_name :pirates
|
133
154
|
is_paranoid :field => [:alive, true, false]
|
134
155
|
end
|
135
156
|
|
136
|
-
class RandomPirate < ActiveRecord::Base
|
157
|
+
class RandomPirate < ActiveRecord::Base #:nodoc:
|
137
158
|
set_table_name :pirates
|
138
159
|
|
139
160
|
def after_destroy
|
@@ -141,7 +162,7 @@ class RandomPirate < ActiveRecord::Base
|
|
141
162
|
end
|
142
163
|
end
|
143
164
|
|
144
|
-
class UndestroyablePirate < ActiveRecord::Base
|
165
|
+
class UndestroyablePirate < ActiveRecord::Base #:nodoc:
|
145
166
|
set_table_name :pirates
|
146
167
|
is_paranoid :field => [:alive, false, true]
|
147
168
|
|
data/spec/schema.rb
CHANGED
@@ -13,6 +13,14 @@ ActiveRecord::Schema.define(:version => 20090317164830) do
|
|
13
13
|
t.datetime "updated_at"
|
14
14
|
end
|
15
15
|
|
16
|
+
create_table "components", :force => true do |t|
|
17
|
+
t.string "name"
|
18
|
+
t.integer "android_id"
|
19
|
+
t.datetime "deleted_at"
|
20
|
+
t.datetime "created_at"
|
21
|
+
t.datetime "updated_at"
|
22
|
+
end
|
23
|
+
|
16
24
|
create_table "ninjas", :force => true do |t|
|
17
25
|
t.string "name"
|
18
26
|
t.boolean "visible", :default => true
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jchupp-is_paranoid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeffrey Chupp
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-04-
|
12
|
+
date: 2009-04-27 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -55,7 +55,7 @@ requirements: []
|
|
55
55
|
rubyforge_project:
|
56
56
|
rubygems_version: 1.2.0
|
57
57
|
signing_key:
|
58
|
-
specification_version:
|
58
|
+
specification_version: 3
|
59
59
|
summary: ActiveRecord 2.3 compatible gem "allowing you to hide and restore records without actually deleting them." Yes, like acts_as_paranoid, only with less code and less complexity.
|
60
60
|
test_files: []
|
61
61
|
|