paranoid 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION.yml +2 -2
- data/lib/paranoid/base.rb +23 -4
- data/lib/paranoid/relation.rb +1 -1
- data/paranoid.gemspec +3 -3
- data/spec/models.rb +26 -5
- data/spec/paranoid_spec.rb +76 -3
- metadata +21 -9
data/VERSION.yml
CHANGED
data/lib/paranoid/base.rb
CHANGED
@@ -15,16 +15,35 @@ module Paranoid
|
|
15
15
|
# === Options
|
16
16
|
#
|
17
17
|
# [:field]
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
18
|
+
# The field used to recognize a record as destroyed.
|
19
|
+
# Default: :deleted_at
|
20
|
+
# IsParanoid Compatibility: Also accepts an Array of form
|
21
|
+
# [field_name, destroyed_value, not_destroyed_value]
|
22
|
+
# however :destroyed_value and :not_destroyed_value will
|
23
|
+
# be ignored
|
24
|
+
#
|
25
|
+
# [:destroyed_value]
|
26
|
+
# The value to set the paranoid field to on destroy.
|
27
|
+
# Can be either a static value or a Proc which will be
|
28
|
+
# evaluated when destroy is called.
|
29
|
+
# Default: Proc.new{Time.now.utc}
|
30
|
+
#
|
31
|
+
# [:not_destroyed_value]
|
32
|
+
# The value used to recognize a record as not destroyed.
|
33
|
+
# Default: nil
|
21
34
|
def paranoid(opts = {})
|
22
35
|
return if paranoid?
|
23
36
|
@paranoid = true
|
24
37
|
|
25
38
|
opts[:field] ||= [:deleted_at, Proc.new{Time.now.utc}, nil]
|
26
39
|
class_inheritable_accessor :destroyed_field, :field_destroyed, :field_not_destroyed
|
27
|
-
|
40
|
+
if opts[:field].is_a?(Array)
|
41
|
+
self.destroyed_field, self.field_destroyed, self.field_not_destroyed = opts[:field]
|
42
|
+
else
|
43
|
+
self.destroyed_field = opts.key?(:field) ? opts[:field] : :deleted_at
|
44
|
+
self.field_destroyed = opts.key?(:destroyed_value) ? opts[:destroyed_value] : Proc.new{Time.now.utc}
|
45
|
+
self.field_not_destroyed = opts.key?(:not_destroyed_value) ? opts[:not_destroyed_value] : nil
|
46
|
+
end
|
28
47
|
|
29
48
|
include Paranoid::ParanoidMethods
|
30
49
|
|
data/lib/paranoid/relation.rb
CHANGED
@@ -51,7 +51,7 @@ module Paranoid
|
|
51
51
|
|
52
52
|
# Returns a new relation scoped to include soft deleted records
|
53
53
|
def with_destroyed
|
54
|
-
|
54
|
+
clone.tap {|relation| relation.skip_paranoid_condition }
|
55
55
|
end
|
56
56
|
|
57
57
|
# Returns a new relation scoped to include only deleted records
|
data/paranoid.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{paranoid}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.4"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["David Genord II"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-03-23}
|
13
13
|
s.description = %q{}
|
14
14
|
s.email = %q{github@xspond.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -38,7 +38,7 @@ Gem::Specification.new do |s|
|
|
38
38
|
s.homepage = %q{http://github.com/xspond/paranoid/}
|
39
39
|
s.rdoc_options = ["--charset=UTF-8"]
|
40
40
|
s.require_paths = ["lib"]
|
41
|
-
s.rubygems_version = %q{1.3.
|
41
|
+
s.rubygems_version = %q{1.3.6}
|
42
42
|
s.summary = %q{Enable soft delete of ActiveRecord records. Based off defunct ActsAsParanoid and IsParanoid}
|
43
43
|
s.test_files = [
|
44
44
|
"spec/models.rb",
|
data/spec/models.rb
CHANGED
@@ -30,7 +30,7 @@ class Dent < ActiveRecord::Base #:nodoc:
|
|
30
30
|
end
|
31
31
|
|
32
32
|
class Ding < ActiveRecord::Base #:nodoc:
|
33
|
-
paranoid :field =>
|
33
|
+
paranoid :field => :not_deleted, :destroyed_value => true, :not_destroyed_value => false
|
34
34
|
belongs_to :dent
|
35
35
|
end
|
36
36
|
|
@@ -83,6 +83,13 @@ end
|
|
83
83
|
class Place < ActiveRecord::Base #:nodoc:
|
84
84
|
paranoid
|
85
85
|
has_and_belongs_to_many :androids
|
86
|
+
|
87
|
+
# this code is to ensure that our destroy and restore methods
|
88
|
+
# work without triggering before/after_update callbacks
|
89
|
+
before_update :raise_hell
|
90
|
+
def raise_hell
|
91
|
+
raise "hell"
|
92
|
+
end
|
86
93
|
end
|
87
94
|
|
88
95
|
class AndroidsPlaces < ActiveRecord::Base #:nodoc:
|
@@ -91,17 +98,31 @@ end
|
|
91
98
|
class Ninja < ActiveRecord::Base #:nodoc:
|
92
99
|
validates_uniqueness_of :name, :scope => :visible
|
93
100
|
paranoid :field => [:visible, false, true]
|
94
|
-
|
101
|
+
|
95
102
|
alias_method :vanish, :destroy
|
103
|
+
|
104
|
+
# this code is to ensure that our destroy and restore methods
|
105
|
+
# work without triggering before/after_update callbacks
|
106
|
+
before_update :raise_hell
|
107
|
+
def raise_hell
|
108
|
+
raise "hell"
|
109
|
+
end
|
96
110
|
end
|
97
111
|
|
98
112
|
class Pirate < ActiveRecord::Base #:nodoc:
|
99
|
-
paranoid :field =>
|
113
|
+
paranoid :field => :alive, :destroyed_value => false, :not_destroyed_value => true
|
114
|
+
|
115
|
+
# this code is to ensure that our destroy and restore methods
|
116
|
+
# work without triggering before/after_update callbacks
|
117
|
+
before_update :raise_hell
|
118
|
+
def raise_hell
|
119
|
+
raise "hell"
|
120
|
+
end
|
100
121
|
end
|
101
122
|
|
102
123
|
class DeadPirate < ActiveRecord::Base #:nodoc:
|
103
124
|
set_table_name :pirates
|
104
|
-
paranoid :field =>
|
125
|
+
paranoid :field => :alive, :destroyed_value => true, :not_destroyed_value => false
|
105
126
|
end
|
106
127
|
|
107
128
|
class RandomPirate < ActiveRecord::Base #:nodoc:
|
@@ -115,7 +136,7 @@ end
|
|
115
136
|
|
116
137
|
class UndestroyablePirate < ActiveRecord::Base #:nodoc:
|
117
138
|
set_table_name :pirates
|
118
|
-
paranoid :field =>
|
139
|
+
paranoid :field => :alive, :destroyed_value => false, :not_destroyed_value => true
|
119
140
|
|
120
141
|
before_destroy :ret_false
|
121
142
|
def ret_false
|
data/spec/paranoid_spec.rb
CHANGED
@@ -35,17 +35,17 @@ describe Paranoid do
|
|
35
35
|
end
|
36
36
|
|
37
37
|
it 'should hide destroyed records' do
|
38
|
-
@tatooine.
|
38
|
+
@tatooine.destroy
|
39
39
|
Place.first(:conditions => {:name => 'Tatooine'}).should be_nil
|
40
40
|
end
|
41
41
|
|
42
42
|
it 'should reveal destroyed records when with_destroyed' do
|
43
|
-
@tatooine.
|
43
|
+
@tatooine.destroy
|
44
44
|
Place.with_destroyed.first(:conditions => {:name => 'Tatooine'}).should_not be_nil
|
45
45
|
end
|
46
46
|
|
47
47
|
it 'should restore the destroyed record' do
|
48
|
-
@tatooine.
|
48
|
+
@tatooine.destroy
|
49
49
|
|
50
50
|
@tatooine = Place.with_destroyed.first(:conditions => {:name => 'Tatooine'})
|
51
51
|
@tatooine.restore
|
@@ -89,6 +89,58 @@ describe Paranoid do
|
|
89
89
|
end
|
90
90
|
|
91
91
|
describe 'for alternate field information' do
|
92
|
+
before(:each) do
|
93
|
+
Pirate.delete_all
|
94
|
+
@roberts, @jack, @hook = Pirate.create([{:name => 'Roberts'}, {:name => 'Jack'}, {:name => 'Hook'}])
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'should have 3 alive pirates' do
|
98
|
+
Pirate.all.size.should == 3
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'should kill the pirate' do
|
102
|
+
@roberts.destroy
|
103
|
+
|
104
|
+
record = Pirate.first(:conditions => {:name => 'Roberts'})
|
105
|
+
record.should be_nil
|
106
|
+
end
|
107
|
+
|
108
|
+
it 'should not remove the pirate record' do
|
109
|
+
@roberts.destroy
|
110
|
+
|
111
|
+
record = Pirate.with_destroyed.first(:conditions => {:name => 'Roberts'})
|
112
|
+
record.should_not be_nil
|
113
|
+
record.alive.should be_false
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'should mark the pirate dead' do
|
117
|
+
@roberts.destroy
|
118
|
+
@roberts.destroyed?.should be_true
|
119
|
+
end
|
120
|
+
|
121
|
+
it 'should set alive to false' do
|
122
|
+
@roberts.destroy
|
123
|
+
@roberts.alive.should be_false
|
124
|
+
end
|
125
|
+
|
126
|
+
it 'should show deleted_only' do
|
127
|
+
@roberts.destroy
|
128
|
+
destroyed = Pirate.with_destroyed_only.all
|
129
|
+
destroyed.size.should == 1
|
130
|
+
destroyed[0].should == @roberts
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'should properly count records' do
|
134
|
+
Pirate.count.should == 3
|
135
|
+
|
136
|
+
@roberts.destroy
|
137
|
+
Pirate.count.should == 2
|
138
|
+
Pirate.with_destroyed.count.should == 3
|
139
|
+
Pirate.with_destroyed_only.count.should == 1
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
describe 'for alternate field information with is_paranoid format field information' do
|
92
144
|
before(:each) do
|
93
145
|
Ninja.delete_all
|
94
146
|
@steve, @bob, @tim = Ninja.create([{:name => 'Steve', :visible => true}, {:name => 'Bob', :visible => true}, {:name => 'Tim', :visible => true}])
|
@@ -192,4 +244,25 @@ describe Paranoid do
|
|
192
244
|
@nil.should be_nil
|
193
245
|
end
|
194
246
|
end
|
247
|
+
|
248
|
+
describe 'callbacks' do
|
249
|
+
before(:each) do
|
250
|
+
Pirate.delete_all
|
251
|
+
end
|
252
|
+
|
253
|
+
it 'should not destroy the record when before_destroy returns false' do
|
254
|
+
pirate = UndestroyablePirate.create!(:name => 'Roberts')
|
255
|
+
lambda { pirate.destroy }.should_not change(UndestroyablePirate, :count)
|
256
|
+
end
|
257
|
+
|
258
|
+
it 'should run after_destroy callbacks' do
|
259
|
+
pirate = RandomPirate.create!(:name => 'Roberts')
|
260
|
+
lambda { pirate.destroy }.should raise_error(/after_destroy works/)
|
261
|
+
end
|
262
|
+
|
263
|
+
it 'should rollback on after_destroy error' do
|
264
|
+
pirate = RandomPirate.create!(:name => 'Roberts')
|
265
|
+
lambda { pirate.destroy rescue nil }.should_not change(RandomPirate, :count)
|
266
|
+
end
|
267
|
+
end
|
195
268
|
end
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: paranoid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 4
|
9
|
+
version: 0.0.4
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- David Genord II
|
@@ -9,19 +14,24 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date: 2010-
|
17
|
+
date: 2010-03-23 00:00:00 -04:00
|
13
18
|
default_executable:
|
14
19
|
dependencies:
|
15
20
|
- !ruby/object:Gem::Dependency
|
16
21
|
name: activerecord
|
17
|
-
|
18
|
-
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
24
|
requirements:
|
21
25
|
- - ~>
|
22
26
|
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 3
|
29
|
+
- 0
|
30
|
+
- 0
|
31
|
+
- beta
|
23
32
|
version: 3.0.0.beta
|
24
|
-
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
25
35
|
description: ""
|
26
36
|
email: github@xspond.com
|
27
37
|
executables: []
|
@@ -62,18 +72,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
62
72
|
requirements:
|
63
73
|
- - ">="
|
64
74
|
- !ruby/object:Gem::Version
|
75
|
+
segments:
|
76
|
+
- 0
|
65
77
|
version: "0"
|
66
|
-
version:
|
67
78
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
79
|
requirements:
|
69
80
|
- - ">="
|
70
81
|
- !ruby/object:Gem::Version
|
82
|
+
segments:
|
83
|
+
- 0
|
71
84
|
version: "0"
|
72
|
-
version:
|
73
85
|
requirements: []
|
74
86
|
|
75
87
|
rubyforge_project:
|
76
|
-
rubygems_version: 1.3.
|
88
|
+
rubygems_version: 1.3.6
|
77
89
|
signing_key:
|
78
90
|
specification_version: 3
|
79
91
|
summary: Enable soft delete of ActiveRecord records. Based off defunct ActsAsParanoid and IsParanoid
|