jchupp-is_paranoid 0.8.1 → 0.8.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +2 -0
- data/VERSION.yml +1 -1
- data/lib/is_paranoid.rb +45 -11
- data/spec/is_paranoid_spec.rb +10 -1
- data/spec/models.rb +7 -0
- data/spec/schema.rb +7 -1
- metadata +2 -2
data/Rakefile
CHANGED
data/VERSION.yml
CHANGED
data/lib/is_paranoid.rb
CHANGED
@@ -38,13 +38,26 @@ module IsParanoid
|
|
38
38
|
# Use update_all with an exclusive scope to restore undo the soft-delete.
|
39
39
|
# This bypasses update-related callbacks.
|
40
40
|
#
|
41
|
-
# By default, restores cascade through associations that are
|
41
|
+
# By default, restores cascade through associations that are belongs_to
|
42
42
|
# :dependent => :destroy and under is_paranoid. You can prevent restoration
|
43
43
|
# of associated models by passing :include_destroyed_dependents => false,
|
44
44
|
# for example:
|
45
|
-
#
|
45
|
+
#
|
46
|
+
# Android.restore(:include_destroyed_dependents => false)
|
47
|
+
#
|
48
|
+
# Alternatively you can specify which relationships to restore via :include,
|
49
|
+
# for example:
|
50
|
+
#
|
51
|
+
# Android.restore(:include => [:parts, memories])
|
52
|
+
#
|
53
|
+
# Please note that specifying :include means you're not using
|
54
|
+
# :include_destroyed_dependents by default, though you can explicitly use
|
55
|
+
# both if you want all has_* relationships and specific belongs_to
|
56
|
+
# relationships, for example
|
57
|
+
#
|
58
|
+
# Android.restore(:include => [:home, :planet], :include_destroyed_dependents => true)
|
46
59
|
def restore(id, options = {})
|
47
|
-
options.reverse_merge!({:include_destroyed_dependents => true})
|
60
|
+
options.reverse_merge!({:include_destroyed_dependents => true}) unless options[:include]
|
48
61
|
with_exclusive_scope do
|
49
62
|
update_all(
|
50
63
|
"#{destroyed_field} = #{connection.quote(field_not_destroyed)}",
|
@@ -52,13 +65,19 @@ module IsParanoid
|
|
52
65
|
)
|
53
66
|
end
|
54
67
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
68
|
+
self.reflect_on_all_associations.each do |association|
|
69
|
+
if association.options[:dependent] == :destroy and association.klass.respond_to?(:restore)
|
70
|
+
dependent_relationship = association.macro.to_s =~ /^has/
|
71
|
+
if should_restore?(association.name, dependent_relationship, options)
|
72
|
+
if dependent_relationship
|
73
|
+
restore_related(association.klass, association.primary_key_name, id, options)
|
74
|
+
else
|
75
|
+
restore_related(
|
76
|
+
association.klass,
|
77
|
+
association.klass.primary_key,
|
78
|
+
self.first(id).send(association.primary_key_name),
|
79
|
+
options
|
80
|
+
)
|
62
81
|
end
|
63
82
|
end
|
64
83
|
end
|
@@ -103,6 +122,21 @@ module IsParanoid
|
|
103
122
|
super(name, *args)
|
104
123
|
end
|
105
124
|
end
|
125
|
+
|
126
|
+
protected
|
127
|
+
|
128
|
+
def should_restore?(association_name, dependent_relationship, options) #:nodoc:
|
129
|
+
([*options[:include]] || []).include?(association_name) or
|
130
|
+
(options[:include_destroyed_dependents] and dependent_relationship)
|
131
|
+
end
|
132
|
+
|
133
|
+
def restore_related klass, key_name, id, options #:nodoc:
|
134
|
+
klass.find_destroyed_only(:all,
|
135
|
+
:conditions => ["#{key_name} = ?", id]
|
136
|
+
).each do |model|
|
137
|
+
model.restore(options)
|
138
|
+
end
|
139
|
+
end
|
106
140
|
end
|
107
141
|
|
108
142
|
module InstanceMethods
|
@@ -166,4 +200,4 @@ module IsParanoid
|
|
166
200
|
|
167
201
|
end
|
168
202
|
|
169
|
-
ActiveRecord::Base.send(:extend, IsParanoid)
|
203
|
+
ActiveRecord::Base.send(:extend, IsParanoid)
|
data/spec/is_paranoid_spec.rb
CHANGED
@@ -147,6 +147,15 @@ describe IsParanoid do
|
|
147
147
|
@r2d2.restore(:include_destroyed_dependents => false)
|
148
148
|
}.should_not change(Component, :count)
|
149
149
|
end
|
150
|
+
|
151
|
+
it "should restore parent and child models specified via :include" do
|
152
|
+
sub_component = SubComponent.create(:name => 'part', :component_id => @r2d2.components.first.id)
|
153
|
+
@r2d2.destroy
|
154
|
+
SubComponent.first(:conditions => {:id => sub_component.id}).should be_nil
|
155
|
+
@r2d2.components.first.restore(:include => [:android, :sub_components])
|
156
|
+
SubComponent.first(:conditions => {:id => sub_component.id}).should_not be_nil
|
157
|
+
Android.find(@r2d2.id).should_not be_nil
|
158
|
+
end
|
150
159
|
end
|
151
160
|
|
152
161
|
describe 'validations' do
|
@@ -245,4 +254,4 @@ describe IsParanoid do
|
|
245
254
|
end
|
246
255
|
|
247
256
|
end
|
248
|
-
end
|
257
|
+
end
|
data/spec/models.rb
CHANGED
@@ -21,6 +21,8 @@ end
|
|
21
21
|
|
22
22
|
class Component < ActiveRecord::Base #:nodoc:
|
23
23
|
is_paranoid
|
24
|
+
belongs_to :android, :dependent => :destroy
|
25
|
+
has_many :sub_components, :dependent => :destroy
|
24
26
|
NEW_NAME = 'Something Else!'
|
25
27
|
|
26
28
|
after_destroy :change_name
|
@@ -29,6 +31,11 @@ class Component < ActiveRecord::Base #:nodoc:
|
|
29
31
|
end
|
30
32
|
end
|
31
33
|
|
34
|
+
class SubComponent < ActiveRecord::Base #:nodoc:
|
35
|
+
is_paranoid
|
36
|
+
belongs_to :component, :dependent => :destroy
|
37
|
+
end
|
38
|
+
|
32
39
|
class Memory < ActiveRecord::Base #:nodoc:
|
33
40
|
is_paranoid
|
34
41
|
belongs_to :android, :class_name => "Android", :foreign_key => "parent_id"
|
data/spec/schema.rb
CHANGED
@@ -21,6 +21,12 @@ ActiveRecord::Schema.define(:version => 20090317164830) do
|
|
21
21
|
t.datetime "updated_at"
|
22
22
|
end
|
23
23
|
|
24
|
+
create_table "sub_components", :force => true do |t|
|
25
|
+
t.string "name"
|
26
|
+
t.integer "component_id"
|
27
|
+
t.datetime "deleted_at"
|
28
|
+
end
|
29
|
+
|
24
30
|
create_table "memories", :force => true do |t|
|
25
31
|
t.string "name"
|
26
32
|
t.integer "parent_id"
|
@@ -42,4 +48,4 @@ ActiveRecord::Schema.define(:version => 20090317164830) do
|
|
42
48
|
t.string "name"
|
43
49
|
t.boolean "alive", :default => true
|
44
50
|
end
|
45
|
-
end
|
51
|
+
end
|
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.8.
|
4
|
+
version: 0.8.2
|
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-05-
|
12
|
+
date: 2009-05-19 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|