delete_paranoid 2.0.0 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +3 -5
- data/delete_paranoid.gemspec +2 -10
- data/lib/delete_paranoid.rb +9 -0
- data/lib/delete_paranoid/version.rb +1 -1
- data/spec/database_setup.rb +6 -0
- data/spec/delete_paranoid_spec.rb +59 -31
- data/spec/matchers/callback_matcher.rb +16 -35
- metadata +9 -8
- data/.rvmrc +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 36e825a8483cb73e7f0beb5b20b332132e613379
|
4
|
+
data.tar.gz: ee1dd296733937f19f6a6d2b439c70ff1a5eda23
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 310386ee0526f9270ae1ce2dc73656f31ec2cf422feca673c16f6e3d0f13c994f5c9fcdd8a68d9cb78ab1a0d0c0863042ca871a417adde5999e45176906aef3f
|
7
|
+
data.tar.gz: 4ee2d7def7f6c91fc2ed0fb6e6e9f04452571833239a936e8abc80173e11b778a48de594ec427618375a4cd38efd933305fbc576a6b32aac5520c010e0152f3c
|
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
delete_paranoid
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.3.1
|
data/.travis.yml
CHANGED
data/delete_paranoid.gemspec
CHANGED
@@ -14,16 +14,8 @@ Gem::Specification.new do |s|
|
|
14
14
|
|
15
15
|
s.rubyforge_project = "delete_paranoid"
|
16
16
|
|
17
|
-
%
|
18
|
-
|
19
|
-
when 'stable', nil then nil
|
20
|
-
when /beta/ then ["= " + ENV[lib]]
|
21
|
-
when /(\d+\.)+\d+/ then ["~> " + ENV[lib]]
|
22
|
-
else [">= 3.0"]
|
23
|
-
end
|
24
|
-
s.add_runtime_dependency(lib, dep)
|
25
|
-
end
|
26
|
-
s.add_development_dependency(%q<rspec>, [">= 0"])
|
17
|
+
s.add_runtime_dependency(%q<activerecord>, [">= 4.2.7.1"])
|
18
|
+
s.add_development_dependency(%q<rspec>, [">= 3.5"])
|
27
19
|
s.add_development_dependency(%q<bundler>, [">= 0"])
|
28
20
|
s.add_development_dependency(%q<sqlite3-ruby>, ["~> 1.3.2"])
|
29
21
|
s.add_development_dependency(%q<rake>, [">= 0.9.2.2"])
|
data/lib/delete_paranoid.rb
CHANGED
@@ -48,6 +48,15 @@ module DeleteParanoid
|
|
48
48
|
# permanently delete this specific instance from the database
|
49
49
|
def destroy_permanently
|
50
50
|
result = destroy
|
51
|
+
return result unless result
|
52
|
+
self.class.with_deleted do
|
53
|
+
self.class.delete_permanently self.id
|
54
|
+
end
|
55
|
+
result
|
56
|
+
end
|
57
|
+
# permanently delete this specific instance from the database, using the AR #destroy! method
|
58
|
+
def destroy_permanently!
|
59
|
+
result = destroy!
|
51
60
|
self.class.with_deleted do
|
52
61
|
self.class.delete_permanently self.id
|
53
62
|
end
|
data/spec/database_setup.rb
CHANGED
@@ -23,8 +23,14 @@ end
|
|
23
23
|
class Blog < ActiveRecord::Base
|
24
24
|
has_many :comments, :dependent => :destroy
|
25
25
|
has_many :links, :dependent => :destroy
|
26
|
+
before_destroy :before_destroy_callback
|
26
27
|
acts_as_paranoid
|
27
28
|
include CallbackMatcher::ActiveRecordHooks
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def before_destroy_callback
|
33
|
+
end
|
28
34
|
end
|
29
35
|
|
30
36
|
class Comment < ActiveRecord::Base
|
@@ -4,28 +4,28 @@ describe DeleteParanoid do
|
|
4
4
|
|
5
5
|
shared_examples_for "soft-deleted" do
|
6
6
|
it do
|
7
|
-
subject.class.where(:id => subject.id).
|
7
|
+
expect(subject.class.where(:id => subject.id)).not_to exist
|
8
8
|
subject.class.with_deleted do
|
9
|
-
subject.class.where(:id => subject.id).
|
9
|
+
expect(subject.class.where(:id => subject.id)).to exist
|
10
10
|
end
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
14
|
shared_examples_for "permanently-deleted" do
|
15
15
|
it do
|
16
|
-
subject.class.where(:id => subject.id).
|
16
|
+
expect(subject.class.where(:id => subject.id)).not_to exist
|
17
17
|
subject.class.with_deleted do
|
18
|
-
subject.class.where(:id => subject.id).
|
18
|
+
expect(subject.class.where(:id => subject.id)).not_to exist
|
19
19
|
end
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
23
|
context 'with non-paranoid activerecord class' do
|
24
|
-
it { Link.
|
24
|
+
it { expect(Link).not_to be_paranoid }
|
25
25
|
end
|
26
26
|
|
27
27
|
context 'with paranoid activerecord class' do
|
28
|
-
it { Blog.
|
28
|
+
it { expect(Blog).to be_paranoid }
|
29
29
|
end
|
30
30
|
|
31
31
|
let!(:blog) { Blog.create! :title => 'foo' }
|
@@ -35,10 +35,10 @@ describe DeleteParanoid do
|
|
35
35
|
context 'when destroying instance with instance.destroy' do
|
36
36
|
before { blog.destroy }
|
37
37
|
it do
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
38
|
+
is_expected.to be_destroyed
|
39
|
+
is_expected.to be_frozen
|
40
|
+
is_expected.to trigger_callbacks_for :destroy
|
41
|
+
is_expected.to_not trigger_callbacks_for :update
|
42
42
|
end
|
43
43
|
it_behaves_like "soft-deleted"
|
44
44
|
end
|
@@ -61,16 +61,44 @@ describe DeleteParanoid do
|
|
61
61
|
before { blog.destroy_permanently }
|
62
62
|
it_behaves_like "permanently-deleted"
|
63
63
|
it do
|
64
|
-
|
65
|
-
|
64
|
+
is_expected.to trigger_callbacks_for :destroy
|
65
|
+
is_expected.not_to trigger_callbacks_for :update
|
66
|
+
end
|
67
|
+
end
|
68
|
+
context 'when destroying instance with instance.destroy_permanently and a before_destroy callback returns false' do
|
69
|
+
before do
|
70
|
+
expect(blog).to receive(:before_destroy_callback).and_return false
|
71
|
+
blog.destroy_permanently
|
72
|
+
end
|
73
|
+
it do
|
74
|
+
is_expected.not_to trigger_callbacks_for :update
|
75
|
+
expect(Blog.where(:id => blog.id)).to exist
|
76
|
+
end
|
77
|
+
end
|
78
|
+
context 'when destroying instance with instance.destroy_permanently!' do
|
79
|
+
before { blog.destroy_permanently! }
|
80
|
+
it_behaves_like "permanently-deleted"
|
81
|
+
it do
|
82
|
+
is_expected.to trigger_callbacks_for :destroy
|
83
|
+
is_expected.not_to trigger_callbacks_for :update
|
84
|
+
end
|
85
|
+
end
|
86
|
+
context 'when destroying instance with instance.destroy_permanently! and a before_destroy callback returns false' do
|
87
|
+
before { expect(blog).to receive(:before_destroy_callback).and_return false }
|
88
|
+
it do
|
89
|
+
expect do
|
90
|
+
blog.destroy_permanently!
|
91
|
+
end.to raise_error ActiveRecord::RecordNotDestroyed
|
92
|
+
|
93
|
+
expect(Blog.where(:id => blog.id)).to exist
|
66
94
|
end
|
67
95
|
end
|
68
96
|
context 'when destroying instance with instance.delete_permanently' do
|
69
97
|
before { blog.delete_permanently }
|
70
98
|
it_behaves_like "permanently-deleted"
|
71
99
|
it do
|
72
|
-
|
73
|
-
|
100
|
+
is_expected.not_to trigger_callbacks_for :destroy
|
101
|
+
is_expected.not_to trigger_callbacks_for :update
|
74
102
|
end
|
75
103
|
end
|
76
104
|
end
|
@@ -82,19 +110,19 @@ describe DeleteParanoid do
|
|
82
110
|
context 'when destroying parent paranoid instance with destroy' do
|
83
111
|
before { blog.destroy }
|
84
112
|
it do
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
113
|
+
is_expected.to be_destroyed
|
114
|
+
is_expected.to be_frozen
|
115
|
+
is_expected.to trigger_callbacks_for :destroy
|
116
|
+
is_expected.not_to trigger_callbacks_for :update
|
89
117
|
end
|
90
118
|
end
|
91
119
|
context 'when destroying parent paranoid instance with delete_all_permanently' do
|
92
120
|
before { Blog.where(:id => blog.id).delete_all_permanently }
|
93
121
|
it do
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
Comment.where(:id => comment.id).
|
122
|
+
is_expected.not_to be_destroyed
|
123
|
+
is_expected.not_to be_frozen
|
124
|
+
is_expected.not_to trigger_callbacks_for :destroy
|
125
|
+
expect(Comment.where(:id => comment.id)).to exist
|
98
126
|
end
|
99
127
|
end
|
100
128
|
end
|
@@ -106,21 +134,21 @@ describe DeleteParanoid do
|
|
106
134
|
context 'when destroying parent paranoid instance with destroy' do
|
107
135
|
before { blog.destroy }
|
108
136
|
it do
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
Link.where(:id => link.id).
|
137
|
+
is_expected.to be_destroyed
|
138
|
+
is_expected.to be_frozen
|
139
|
+
is_expected.to trigger_callbacks_for :destroy
|
140
|
+
is_expected.not_to trigger_callbacks_for :update
|
141
|
+
expect(Link.where(:id => link.id)).not_to exist
|
114
142
|
end
|
115
143
|
end
|
116
144
|
|
117
145
|
context 'when destroying parent paranoid instance with delete_all_permanently' do
|
118
146
|
before { Blog.where(:id => blog.id).delete_all_permanently }
|
119
147
|
it do
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
Link.where(:id => link.id).
|
148
|
+
is_expected.not_to be_destroyed
|
149
|
+
is_expected.not_to be_frozen
|
150
|
+
is_expected.not_to trigger_callbacks_for :destroy
|
151
|
+
expect(Link.where(:id => link.id)).to exist
|
124
152
|
end
|
125
153
|
end
|
126
154
|
end
|
@@ -4,7 +4,6 @@ class CallbackMatcher
|
|
4
4
|
CALLBACK_TYPES = [:create, :update, :destroy, :save, :commit]
|
5
5
|
|
6
6
|
module ActiveRecordHooks
|
7
|
-
|
8
7
|
def self.included(base)
|
9
8
|
base.class_eval do
|
10
9
|
class_attribute :callback_tester_attrs
|
@@ -24,65 +23,47 @@ class CallbackMatcher
|
|
24
23
|
}
|
25
24
|
end
|
26
25
|
end
|
27
|
-
alias_method_chain :initialize, :callback_init
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
def initialize_with_callback_init(*args)
|
32
|
-
reset_callback_flags!
|
33
|
-
initialize_without_callback_init(*args)
|
34
|
-
end
|
35
|
-
|
36
|
-
def reset_callback_flags!
|
37
|
-
self.class.callback_tester_attrs.each do |attr|
|
38
|
-
send("#{attr}=", false)
|
39
26
|
end
|
40
27
|
end
|
41
|
-
|
42
28
|
end
|
43
|
-
|
44
29
|
end
|
45
30
|
|
46
31
|
require 'rspec/matchers'
|
47
32
|
|
48
|
-
RSpec::Matchers.define :trigger_callbacks_for do |
|
49
|
-
|
50
|
-
check_for_match = ->(model_instance, types) {
|
33
|
+
RSpec::Matchers.define :trigger_callbacks_for do |expected_callback_type|
|
34
|
+
def check_for_match(model_instance, expected_callback_type)
|
51
35
|
@called = []
|
52
36
|
@not_called = []
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
@not_called << callback_name unless result
|
59
|
-
end
|
37
|
+
CallbackMatcher::CALLBACK_EVENTS.each do |ce|
|
38
|
+
callback_name = "#{ce}_#{expected_callback_type}"
|
39
|
+
result = model_instance.send("called_#{callback_name}".to_sym)
|
40
|
+
@called << callback_name if result
|
41
|
+
@not_called << callback_name unless result
|
60
42
|
end
|
61
|
-
|
43
|
+
end
|
62
44
|
|
63
|
-
|
64
|
-
check_for_match
|
45
|
+
match do |model_instance|
|
46
|
+
check_for_match(model_instance, expected_callback_type)
|
65
47
|
result = true
|
66
48
|
result = false unless @called.present?
|
67
49
|
result = false if @not_called.present?
|
68
50
|
result
|
69
51
|
end
|
70
52
|
|
71
|
-
|
72
|
-
check_for_match
|
53
|
+
match_when_negated do |model_instance|
|
54
|
+
check_for_match(model_instance, expected_callback_type)
|
73
55
|
result = true
|
74
56
|
result = false unless @not_called.present?
|
75
57
|
result = false if @called.present?
|
76
58
|
result
|
77
59
|
end
|
78
60
|
|
79
|
-
|
80
|
-
["Called:\t#{@called.join("\n\t")}", "Not called:\t#{@
|
61
|
+
failure_message do |actual|
|
62
|
+
["Called:\t#{@called.join("\n\t")}", "Not called:\t#{@not_called.join("\n\t")}"].join("\n")
|
81
63
|
end
|
82
64
|
|
83
|
-
|
84
|
-
["Called:\t#{@called.join("\n\t")}", "Not called:\t#{@
|
65
|
+
failure_message_when_negated do |actual|
|
66
|
+
["Called:\t#{@called.join("\n\t")}", "Not called:\t#{@not_called.join("\n\t")}"].join("\n")
|
85
67
|
end
|
86
|
-
|
87
68
|
end
|
88
69
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: delete_paranoid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Sonnek
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2016-09-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -17,28 +17,28 @@ dependencies:
|
|
17
17
|
requirements:
|
18
18
|
- - ">="
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version:
|
20
|
+
version: 4.2.7.1
|
21
21
|
type: :runtime
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
25
|
- - ">="
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
version:
|
27
|
+
version: 4.2.7.1
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: rspec
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
31
31
|
requirements:
|
32
32
|
- - ">="
|
33
33
|
- !ruby/object:Gem::Version
|
34
|
-
version: '
|
34
|
+
version: '3.5'
|
35
35
|
type: :development
|
36
36
|
prerelease: false
|
37
37
|
version_requirements: !ruby/object:Gem::Requirement
|
38
38
|
requirements:
|
39
39
|
- - ">="
|
40
40
|
- !ruby/object:Gem::Version
|
41
|
-
version: '
|
41
|
+
version: '3.5'
|
42
42
|
- !ruby/object:Gem::Dependency
|
43
43
|
name: bundler
|
44
44
|
requirement: !ruby/object:Gem::Requirement
|
@@ -89,7 +89,8 @@ extensions: []
|
|
89
89
|
extra_rdoc_files: []
|
90
90
|
files:
|
91
91
|
- ".gitignore"
|
92
|
-
- ".
|
92
|
+
- ".ruby-gemset"
|
93
|
+
- ".ruby-version"
|
93
94
|
- ".travis.yml"
|
94
95
|
- CONTRIBUTORS.txt
|
95
96
|
- Gemfile
|
@@ -123,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
123
124
|
version: '0'
|
124
125
|
requirements: []
|
125
126
|
rubyforge_project: delete_paranoid
|
126
|
-
rubygems_version: 2.
|
127
|
+
rubygems_version: 2.5.1
|
127
128
|
signing_key:
|
128
129
|
specification_version: 4
|
129
130
|
summary: soft delete Rails ActiveRecord objects
|
data/.rvmrc
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
rvm use "ruby-1.9.3-p484@delete_paranoid" --create
|