soft_deletion 0.5.6 → 0.6.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9866ca2df495d3a180f22f3d56451b1fda15e538
4
- data.tar.gz: 69fbbbd1217bd01038164a9b3f19f9fa327c330e
3
+ metadata.gz: 9ea041113a1bcda2beb027912c991004618fd65f
4
+ data.tar.gz: 688f94c0f45493796830547db7aa13da7d976578
5
5
  SHA512:
6
- metadata.gz: 7af7dfb1add14ea8cbe36db9f87406d750ec072a24bf0a3e2f4a3d8a477a889ad7e45b3d405451e6fe7c9be9412e6f465aed8d1fa4703b26458de1e029e3a85a
7
- data.tar.gz: 6012cd76f241b0f1f0423fa29aa3d0d2e19cfda02ad781384ed46ef51c171380e368fa7037c9df625bf92d6aa1e879a8d4e25c78bb2737a8fbd90c40aac20eef
6
+ metadata.gz: d953a3f005716ba02311c7bb969fcd2c4c47a5725472448a65abf725311b69aac6973d659ec269d98bb48e354fc82d00f6a4f9afd8b647380e84d0e24bd7d9c0
7
+ data.tar.gz: bffbb5365381cf0506ed69d27e7e20d9c8b90c531cdede45c292eae385a03ff6e3d3324823ea443a12cba1a58828cd779d8f585d658c268c9099c24333d718e5
data/Readme.md CHANGED
@@ -3,40 +3,46 @@ Not overwriting destroy or delete.
3
3
 
4
4
  Install
5
5
  =======
6
- gem install soft_deletion
6
+
7
+ ```Bash
8
+ gem install soft_deletion
9
+ ```
7
10
 
8
11
  Usage
9
12
  =====
10
- require 'soft_deletion'
11
13
 
12
- class User < ActiveRecord::Base
13
- has_soft_deletion :default_scope => true
14
+ ```Ruby
15
+ require 'soft_deletion'
16
+
17
+ class User < ActiveRecord::Base
18
+ has_soft_deletion :default_scope => true
14
19
 
15
- before_soft_delete :validate_deletability # soft_delete stops if this returns false
16
- after_soft_delete :send_deletion_emails
20
+ before_soft_delete :validate_deletability # soft_delete stops if this returns false
21
+ after_soft_delete :send_deletion_emails
17
22
 
18
- has_many :products
19
- end
23
+ has_many :products
24
+ end
20
25
 
21
- # soft delete them including all dependencies that are marked as :destroy, :delete_all, :nullify
22
- user = User.first
23
- user.products.count == 10
24
- user.soft_delete!
25
- user.deleted? # true
26
+ # soft delete them including all soft-deletable dependencies that are marked as :destroy, :delete_all, :nullify
27
+ user = User.first
28
+ user.products.count == 10
29
+ user.soft_delete!
30
+ user.deleted? # true
26
31
 
27
- # use special with_deleted scope to find them ...
28
- user.reload # ActiveRecord::RecordNotFound
29
- User.with_deleted do
30
- user.reload # there it is ...
31
- user.products.count == 0
32
- end
32
+ # use special with_deleted scope to find them ...
33
+ user.reload # ActiveRecord::RecordNotFound
34
+ User.with_deleted do
35
+ user.reload # there it is ...
36
+ user.products.count == 0
37
+ end
33
38
 
34
- # soft undelete them all
35
- user.soft_undelete!
36
- user.products.count == 10
39
+ # soft undelete them all
40
+ user.soft_undelete!
41
+ user.products.count == 10
37
42
 
38
- # soft delete many
39
- User.soft_delete_all!(1,2,3,4)
43
+ # soft delete many
44
+ User.soft_delete_all!(1,2,3,4)
45
+ ```
40
46
 
41
47
  TODO
42
48
  ====
@@ -1,3 +1,3 @@
1
1
  module SoftDeletion
2
- VERSION = '0.5.6'
2
+ VERSION = '0.6.0'
3
3
  end
metadata CHANGED
@@ -1,43 +1,131 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: soft_deletion
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.6
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zendesk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-13 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2015-08-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.2.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: 4.3.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 3.2.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: 4.3.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: rake
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: sqlite3
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rspec
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '2.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '2.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: database_cleaner
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: 1.0.0
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: 1.0.0
89
+ - !ruby/object:Gem::Dependency
90
+ name: bump
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: wwtd
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
13
117
  description:
14
118
  email: michael@grosser.it
15
119
  executables: []
16
120
  extensions: []
17
121
  extra_rdoc_files: []
18
122
  files:
19
- - .gitignore
20
- - .travis.yml
21
- - Appraisals
22
- - Gemfile
23
- - Gemfile.lock
24
- - MIT-LICENSE
25
- - Rakefile
26
123
  - Readme.md
27
- - gem-public_cert.pem
28
- - gemfiles/rails2.gemfile
29
- - gemfiles/rails3.gemfile
30
- - gemfiles/rails30.gemfile
31
- - gemfiles/rails32.gemfile
32
- - gemfiles/rails4.gemfile
33
124
  - lib/soft_deletion.rb
34
125
  - lib/soft_deletion/core.rb
35
126
  - lib/soft_deletion/dependency.rb
36
127
  - lib/soft_deletion/setup.rb
37
128
  - lib/soft_deletion/version.rb
38
- - soft_deletion.gemspec
39
- - spec/soft_deletion_spec.rb
40
- - spec/spec_helper.rb
41
129
  homepage: https://github.com/grosser/soft_deletion
42
130
  licenses:
43
131
  - MIT
@@ -48,17 +136,17 @@ require_paths:
48
136
  - lib
49
137
  required_ruby_version: !ruby/object:Gem::Requirement
50
138
  requirements:
51
- - - '>='
139
+ - - ">="
52
140
  - !ruby/object:Gem::Version
53
141
  version: '0'
54
142
  required_rubygems_version: !ruby/object:Gem::Requirement
55
143
  requirements:
56
- - - '>='
144
+ - - ">="
57
145
  - !ruby/object:Gem::Version
58
146
  version: '0'
59
147
  requirements: []
60
148
  rubyforge_project:
61
- rubygems_version: 2.0.3
149
+ rubygems_version: 2.2.2
62
150
  signing_key:
63
151
  specification_version: 4
64
152
  summary: Explicit soft deletion for ActiveRecord via deleted_at and default scope.
data/.gitignore DELETED
@@ -1 +0,0 @@
1
- gemfiles/*.gemfile.lock
data/.travis.yml DELETED
@@ -1,17 +0,0 @@
1
- rvm:
2
- - 1.9.3
3
- - ree
4
- - 2.0
5
- gemfile:
6
- - gemfiles/rails2.gemfile
7
- - gemfiles/rails32.gemfile
8
- - gemfiles/rails4.gemfile
9
- matrix:
10
- exclude:
11
- - rvm: 2.0
12
- gemfile: gemfiles/rails2.gemfile
13
- - rvm: 2.0
14
- gemfile: gemfiles/rails30.gemfile
15
- - rvm: ree
16
- gemfile: gemfiles/rails4.gemfile
17
- script: "bundle exec rake spec"
data/Appraisals DELETED
@@ -1,23 +0,0 @@
1
- appraise "rails2" do
2
- version = "2.3.18"
3
- gem 'activerecord', version
4
- gem 'activesupport', version
5
- end
6
-
7
- appraise "rails30" do
8
- version = "3.0.20"
9
- gem 'activerecord', version
10
- gem 'activesupport', version
11
- end
12
-
13
- appraise "rails32" do
14
- version = "3.2.13"
15
- gem 'activerecord', version
16
- gem 'activesupport', version
17
- end
18
-
19
- appraise "rails4" do
20
- version = "4.0.0"
21
- gem 'activerecord', version
22
- gem 'activesupport', version
23
- end
data/Gemfile DELETED
@@ -1,11 +0,0 @@
1
- source "https://rubygems.org"
2
- gemspec
3
-
4
- gem 'activerecord'
5
- gem 'activesupport'
6
- gem 'appraisal'
7
- gem 'rake'
8
- gem 'sqlite3'
9
- gem 'rspec'
10
- gem 'database_cleaner', '1.0.0.RC1' # or rails 4 tests fail
11
- gem 'bump'
data/Gemfile.lock DELETED
@@ -1,54 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- soft_deletion (0.5.6)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
- activemodel (3.2.7)
10
- activesupport (= 3.2.7)
11
- builder (~> 3.0.0)
12
- activerecord (3.2.7)
13
- activemodel (= 3.2.7)
14
- activesupport (= 3.2.7)
15
- arel (~> 3.0.2)
16
- tzinfo (~> 0.3.29)
17
- activesupport (3.2.7)
18
- i18n (~> 0.6)
19
- multi_json (~> 1.0)
20
- appraisal (0.4.1)
21
- bundler
22
- rake
23
- arel (3.0.2)
24
- builder (3.0.0)
25
- bump (0.3.8)
26
- database_cleaner (1.0.0.RC1)
27
- diff-lcs (1.1.3)
28
- i18n (0.6.0)
29
- multi_json (1.3.6)
30
- rake (0.9.2.2)
31
- rspec (2.11.0)
32
- rspec-core (~> 2.11.0)
33
- rspec-expectations (~> 2.11.0)
34
- rspec-mocks (~> 2.11.0)
35
- rspec-core (2.11.1)
36
- rspec-expectations (2.11.3)
37
- diff-lcs (~> 1.1.3)
38
- rspec-mocks (2.11.3)
39
- sqlite3 (1.3.6)
40
- tzinfo (0.3.33)
41
-
42
- PLATFORMS
43
- ruby
44
-
45
- DEPENDENCIES
46
- activerecord
47
- activesupport
48
- appraisal
49
- bump
50
- database_cleaner (= 1.0.0.RC1)
51
- rake
52
- rspec
53
- soft_deletion!
54
- sqlite3
data/MIT-LICENSE DELETED
@@ -1,20 +0,0 @@
1
- Copyright (c) NOW-PRESENT Zendesk
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
10
-
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile DELETED
@@ -1,11 +0,0 @@
1
- require 'bundler/setup'
2
- require 'appraisal'
3
- require 'bundler/gem_tasks'
4
- require 'rspec/core/rake_task'
5
- require 'bump/tasks'
6
-
7
- task :default do
8
- sh "bundle exec rake appraisal:install && bundle exec rake appraisal spec"
9
- end
10
-
11
- RSpec::Core::RakeTask.new(:spec)
data/gem-public_cert.pem DELETED
@@ -1,20 +0,0 @@
1
- -----BEGIN CERTIFICATE-----
2
- MIIDMjCCAhqgAwIBAgIBADANBgkqhkiG9w0BAQUFADA/MRAwDgYDVQQDDAdtaWNo
3
- YWVsMRcwFQYKCZImiZPyLGQBGRYHZ3Jvc3NlcjESMBAGCgmSJomT8ixkARkWAml0
4
- MB4XDTEzMDIwMzE4MTMxMVoXDTE0MDIwMzE4MTMxMVowPzEQMA4GA1UEAwwHbWlj
5
- aGFlbDEXMBUGCgmSJomT8ixkARkWB2dyb3NzZXIxEjAQBgoJkiaJk/IsZAEZFgJp
6
- dDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMorXo/hgbUq97+kII9H
7
- MsQcLdC/7wQ1ZP2OshVHPkeP0qH8MBHGg6eYisOX2ubNagF9YTCZWnhrdKrwpLOO
8
- cPLaZbjUjljJ3cQR3B8Yn1veV5IhG86QseTBjymzJWsLpqJ1UZGpfB9tXcsFtuxO
9
- 6vHvcIHdzvc/OUkICttLbH+1qb6rsHUceqh+JrH4GrsJ5H4hAfIdyS2XMK7YRKbh
10
- h+IBu6dFWJJByzFsYmV1PDXln3UBmgAt65cmCu4qPfThioCGDzbSJrGDGLmw/pFX
11
- FPpVCm1zgYSb1v6Qnf3cgXa2f2wYGm17+zAVyIDpwryFru9yF/jJxE38z/DRsd9R
12
- /88CAwEAAaM5MDcwCQYDVR0TBAIwADAdBgNVHQ4EFgQUsiNnXHtKeMYYcr4yJVmQ
13
- WONL+IwwCwYDVR0PBAQDAgSwMA0GCSqGSIb3DQEBBQUAA4IBAQAlyN7kKo/NQCQ0
14
- AOzZLZ3WAePvStkCFIJ53tsv5Kyo4pMAllv+BgPzzBt7qi605mFSL6zBd9uLou+W
15
- Co3s48p1dy7CjjAfVQdmVNHF3MwXtfC2OEyvSQPi4xKR8iba8wa3xp9LVo1PuLpw
16
- /6DsrChWw74HfsJN6qJOK684hJeT8lBYAUfiC3wD0owoPSg+XtyAAddisR+KV5Y1
17
- NmVHuLtQcNTZy+gRht3ahJRMuC6QyLmkTsf+6MaenwAMkAgHdswGsJztOnNnBa3F
18
- y0kCSWmK6D+x/SbfS6r7Ke07MRqziJdB9GuE1+0cIRuFh8EQ+LN6HXCKM5pon/GU
19
- ycwMXfl0
20
- -----END CERTIFICATE-----
@@ -1,14 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "appraisal"
6
- gem "rake"
7
- gem "sqlite3"
8
- gem "rspec"
9
- gem "database_cleaner", "1.0.0.RC1"
10
- gem "bump"
11
- gem "activerecord", "2.3.18"
12
- gem "activesupport", "2.3.18"
13
-
14
- gemspec :path=>"../"
@@ -1,14 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "appraisal"
6
- gem "rake"
7
- gem "sqlite3"
8
- gem "rspec"
9
- gem "database_cleaner", "1.0.0.RC1"
10
- gem "bump"
11
- gem "activerecord", "3.2.3"
12
- gem "activesupport", "3.2.3"
13
-
14
- gemspec :path=>"../"
@@ -1,14 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "appraisal"
6
- gem "rake"
7
- gem "sqlite3"
8
- gem "rspec"
9
- gem "database_cleaner", "1.0.0.RC1"
10
- gem "bump"
11
- gem "activerecord", "3.0.20"
12
- gem "activesupport", "3.0.20"
13
-
14
- gemspec :path=>"../"
@@ -1,14 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "appraisal"
6
- gem "rake"
7
- gem "sqlite3"
8
- gem "rspec"
9
- gem "database_cleaner", "1.0.0.RC1"
10
- gem "bump"
11
- gem "activerecord", "3.2.13"
12
- gem "activesupport", "3.2.13"
13
-
14
- gemspec :path=>"../"
@@ -1,14 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "appraisal"
6
- gem "rake"
7
- gem "sqlite3"
8
- gem "rspec"
9
- gem "database_cleaner", "1.0.0.RC1"
10
- gem "bump"
11
- gem "activerecord", "4.0.0"
12
- gem "activesupport", "4.0.0"
13
-
14
- gemspec :path=>"../"
@@ -1,17 +0,0 @@
1
- $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
2
- name = "soft_deletion"
3
- require "#{name}/version"
4
-
5
- Gem::Specification.new name, SoftDeletion::VERSION do |s|
6
- s.summary = "Explicit soft deletion for ActiveRecord via deleted_at and default scope."
7
- s.authors = ["Zendesk"]
8
- s.email = "michael@grosser.it"
9
- s.homepage = "https://github.com/grosser/#{name}"
10
- s.files = `git ls-files`.split("\n")
11
- s.license = "MIT"
12
- key = File.expand_path("~/.ssh/gem-private_key.pem")
13
- if File.exist?(key)
14
- s.signing_key = key
15
- s.cert_chain = ["gem-public_cert.pem"]
16
- end
17
- end
@@ -1,510 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe SoftDeletion do
4
- def self.successfully_soft_deletes
5
- context "successfully soft deleted" do
6
- before do
7
- @category.soft_delete!
8
- end
9
-
10
- it "should mark itself as deleted" do
11
- @category.reload
12
- @category.should be_deleted
13
- end
14
-
15
- it "should soft delete its dependent associations" do
16
- @forum.reload
17
- @forum.should be_deleted
18
- end
19
- end
20
- end
21
-
22
- def self.successfully_bulk_soft_deletes
23
- context "successfully bulk soft deleted" do
24
- before do
25
- Category.soft_delete_all!(@category)
26
- end
27
-
28
- it "should mark itself as deleted" do
29
- @category.reload
30
- @category.should be_deleted
31
- end
32
-
33
- it "should soft delete its dependent associations" do
34
- @forum.reload
35
- @forum.should be_deleted
36
- end
37
- end
38
- end
39
-
40
- before do
41
- clear_callbacks Category, :soft_delete
42
- clear_callbacks Category, :soft_undelete
43
-
44
- # Stub dump method calls
45
- Category.any_instance.stub(:foo)
46
- Category.any_instance.stub(:bar)
47
- end
48
-
49
- describe "callbacks" do
50
- describe ".before_soft_delete" do
51
- it "is called on soft-deletion" do
52
- Category.before_soft_delete :foo
53
- category = Category.create!
54
-
55
- category.should_receive(:foo)
56
-
57
- category.soft_delete!
58
- end
59
-
60
- it "stops execution chain if false is returned" do
61
- Category.before_soft_delete :foo, :bar
62
- category = Category.create!
63
-
64
- category.should_receive(:foo).and_return(false)
65
- category.should_not_receive(:bar)
66
-
67
- category.soft_delete!.should == false
68
- category.reload
69
- category.should_not be_deleted
70
- end
71
- end
72
-
73
- describe ".after_soft_delete" do
74
- it "is called after soft-deletion" do
75
- Category.after_soft_delete :foo
76
- category = Category.create!
77
-
78
- category.should_receive(:foo)
79
-
80
- category.soft_delete!
81
- end
82
-
83
- it "is called after bulk soft-deletion" do
84
- Category.after_soft_delete :foo
85
- category = Category.create!
86
-
87
- category.should_receive(:foo)
88
-
89
- Category.soft_delete_all!(category)
90
- end
91
-
92
- it "is called with a block" do
93
- Category.after_soft_delete{|c| c.foo }
94
- category = Category.create!
95
-
96
- category.should_receive(:foo)
97
-
98
- category.soft_delete!
99
- end
100
-
101
- it "calls multiple after soft-deletion" do
102
- Category.after_soft_delete :foo, :bar
103
- category = Category.create!
104
-
105
- category.should_receive(:foo)
106
- category.should_receive(:bar)
107
-
108
- category.soft_delete!
109
- end
110
-
111
- it "does not stop deletion when returning false" do
112
- Category.after_soft_delete :foo
113
- category = Category.create!
114
-
115
- category.should_receive(:foo).and_return false
116
-
117
- category.soft_delete!
118
-
119
- category.reload
120
- category.should be_deleted
121
- end
122
-
123
- it "is not called after normal destroy" do
124
- Category.after_soft_delete :foo
125
- category = Category.create!
126
-
127
- category.should_not_receive(:foo)
128
-
129
- category.destroy
130
- end
131
- end
132
-
133
- describe ".before_soft_undelete" do
134
- it "is called on soft-undeletion" do
135
- Category.before_soft_undelete :foo
136
- category = Category.create!(:deleted_at => Time.now)
137
-
138
- category.should_receive(:foo)
139
-
140
- category.soft_undelete!
141
- end
142
-
143
- it "stops execution chain if false is returned" do
144
- Category.before_soft_undelete :foo, :bar
145
- category = Category.create!(:deleted_at => Time.now)
146
-
147
- category.should_receive(:foo).and_return(false)
148
- category.should_not_receive(:bar)
149
-
150
- category.soft_undelete!.should == false
151
- category.reload
152
- category.should be_deleted
153
- end
154
- end
155
-
156
- describe ".after_soft_undelete" do
157
- it "is called after soft-undeletion" do
158
- Category.after_soft_undelete :foo
159
- category = Category.create!(:deleted_at => Time.now)
160
-
161
- category.should_receive(:foo)
162
-
163
- category.soft_undelete!
164
- end
165
-
166
- it "is called with a block" do
167
- Category.after_soft_undelete{|c| c.foo }
168
- category = Category.create!(:deleted_at => Time.now)
169
-
170
- category.should_receive(:foo)
171
-
172
- category.soft_undelete!
173
- end
174
-
175
- it "calls multiple after soft-undeletion" do
176
- Category.after_soft_undelete :foo, :bar
177
- category = Category.create!(:deleted_at => Time.now)
178
-
179
- category.should_receive(:foo)
180
- category.should_receive(:bar)
181
-
182
- category.soft_undelete!
183
- end
184
-
185
- it "does not stop undeletion when returning false" do
186
- Category.after_soft_undelete :foo
187
- category = Category.create!(:deleted_at => Time.now)
188
-
189
- category.should_receive(:foo).and_return false
190
-
191
- category.soft_undelete!
192
-
193
- category.reload
194
- category.should_not be_deleted
195
- end
196
-
197
- it "is not called after normal destroy" do
198
- Category.after_soft_undelete :foo
199
- category = Category.create!(:deleted_at => Time.now)
200
-
201
- category.should_not_receive(:foo)
202
-
203
- category.destroy
204
- end
205
- end
206
- end
207
-
208
- describe "association" do
209
- context "without dependent associations" do
210
- it "should only soft-delete itself" do
211
- category = NACategory.create!
212
- category.soft_delete!
213
-
214
- category.reload
215
- category.should be_deleted
216
- end
217
- end
218
-
219
- context "with independent associations" do
220
- it "should not delete associations" do
221
- category = IDACategory.create!
222
- forum = category.forums.create!
223
- category.soft_delete!
224
-
225
- forum.reload
226
- forum.should be_deleted
227
- end
228
- end
229
-
230
- context "with dependent has_one association" do
231
- before do
232
- @category = HOACategory.create!
233
- @forum = @category.create_forum
234
- end
235
-
236
- successfully_soft_deletes
237
- successfully_bulk_soft_deletes
238
- end
239
-
240
- context "with dependent association that doesn't have soft deletion" do
241
- before do
242
- @category = DACategory.create!
243
- @forum = @category.destroyable_forums.create!
244
- end
245
-
246
- context "successfully soft deleted" do
247
- before do
248
- @category.soft_delete!
249
- end
250
-
251
- it "should mark itself as deleted" do
252
- @category.reload
253
- @category.should be_deleted
254
- end
255
-
256
- it "should not destroy dependent association" do
257
- DestroyableForum.exists?(@forum.id).should be_true
258
- end
259
- end
260
- end
261
-
262
- context "with dependent has_many associations" do
263
- before do
264
- @category = Category.create!
265
- @forum = @category.forums.create!
266
- end
267
-
268
- context "failing to soft delete" do
269
- before do
270
- @category.stub(:valid?).and_return(false)
271
- expect{ @category.soft_delete! }.to raise_error(ActiveRecord::RecordInvalid)
272
- end
273
-
274
- it "should not mark itself as deleted" do
275
- @category.reload
276
- @category.should_not be_deleted
277
- end
278
-
279
- it "should not soft delete its dependent associations" do
280
- @forum.reload
281
- @forum.should_not be_deleted
282
- end
283
- end
284
-
285
- successfully_soft_deletes
286
- successfully_bulk_soft_deletes
287
-
288
- context "being restored from soft deletion" do
289
- def undelete!
290
- Category.with_deleted do
291
- @category.reload
292
- @category.soft_undelete!
293
- end
294
- end
295
-
296
- before do
297
- @category.soft_delete!
298
- Category.with_deleted { @category = Category.find(@category.id) }
299
- end
300
-
301
- it "does not mark itself as deleted" do
302
- undelete!
303
- @category.reload
304
- @category.should_not be_deleted
305
- end
306
-
307
- it "restores its dependent associations" do
308
- undelete!
309
- @forum.reload
310
- @forum.should_not be_deleted
311
- end
312
-
313
- it "does not fail if dependent associations are not deleted" do
314
- @forum.reload.soft_undelete!
315
- undelete!
316
- @forum.reload
317
- @forum.should_not be_deleted
318
- end
319
-
320
- it "does not restore far previous deletions" do
321
- @forum.update_attributes(:deleted_at => 1.year.ago)
322
- undelete!
323
- @forum.reload.should be_deleted
324
- end
325
- end
326
- end
327
-
328
- context "a soft-deleted has-many category that nullifies forum references on delete" do
329
- it "should nullify those references" do
330
- category = NDACategory.create!
331
- forum = category.forums.create!
332
- category.soft_delete!
333
-
334
- forum.reload
335
- forum.should_not be_deleted
336
- forum.category_id.should be_nil
337
- end
338
- end
339
-
340
- context "a soft-deleted has-many category that delete_all forum references on delete" do
341
- it "use update_all to delete references" do
342
- category = DDACategory.create!
343
- forum = category.forums.create!
344
- category.soft_delete!
345
-
346
- forum.should_not be_deleted # just did an update_all
347
- Forum.find(forum.id).should be_deleted
348
- end
349
-
350
- it "custom sql to delete all" do
351
- category = DDACategory.create!
352
- forum = category.forums.create!
353
- Forum.should_receive(:mark_as_soft_deleted_sql).and_return "fooo"
354
- category.forums.should_receive(:update_all).with("fooo")
355
- category.soft_delete!
356
- end
357
- end
358
-
359
- context "a soft-deleted has-many category that defaults dependent forum references on delete" do
360
- it "does nothing to those references" do
361
- category = XDACategory.create!
362
- forum = category.forums.create!
363
- category.soft_delete!
364
-
365
- forum.reload
366
- forum.should_not be_deleted
367
- forum.category_id.should_not be_nil
368
- end
369
- end
370
-
371
- context "a soft-deleted has-many category that nullifies forum references on delete without foreign_key" do
372
- it "should nullify those references" do
373
- organization = Organization.create!
374
- forum = organization.forums.create!
375
- organization.soft_delete!
376
-
377
- forum.reload
378
- forum.should_not be_deleted
379
- forum.organization_id.should be_nil
380
- end
381
- end
382
- end
383
-
384
- context "without deleted_at column" do
385
- it "does not provoke an error" do
386
- expect do
387
- OriginalCategory.create!
388
- end.to_not raise_error
389
- end
390
- end
391
-
392
- context "default_scope" do
393
- let(:forum) do
394
- # create! does not work on rails 2
395
- f = Cat2Forum.new
396
- f.deleted_at = Time.now
397
- f.save!
398
- f
399
- end
400
-
401
- it "prevents find when deleted" do
402
- Cat2Forum.find_by_id(forum.id).should == nil
403
- end
404
-
405
- it "can find without deleted" do
406
- forum.update_attributes(:deleted_at => nil)
407
- Cat2Forum.find_by_id(forum.id).should_not == nil
408
- end
409
- end
410
-
411
- describe ".soft_delete_all!" do
412
- before do
413
- @categories = 2.times.map { Category.create! }
414
- end
415
-
416
- context "by id" do
417
- before do
418
- Category.soft_delete_all!(@categories.map(&:id))
419
- end
420
-
421
- it "should delete all models" do
422
- @categories.each do |category|
423
- category.reload
424
- category.should be_deleted
425
- end
426
- end
427
- end
428
-
429
- context "by model" do
430
- before do
431
- Category.soft_delete_all!(@categories)
432
- end
433
-
434
- it "should delete all models" do
435
- @categories.each do |category|
436
- category.reload
437
- category.should be_deleted
438
- end
439
- end
440
- end
441
- end
442
-
443
- describe "overwritten default scope" do
444
- it "should find even with deleted_at" do
445
- forum = Cat1Forum.create(:deleted_at => Time.now)
446
-
447
- Cat1Forum.find_by_id(forum.id).should_not be_nil
448
- end
449
-
450
- it "should not find by new scope" do
451
- # create! does not work here on rails 2
452
- forum = Cat1Forum.new
453
- forum.category_id = 2
454
- forum.save!
455
-
456
- Cat1Forum.find_by_id(forum.id).should be_nil
457
- end
458
- end
459
-
460
- describe "validations" do
461
- it "should fail when validations fail" do
462
- forum = ValidatedForum.create!(:category_id => 1)
463
- forum.category_id = nil
464
-
465
- expect do
466
- forum.soft_delete!
467
- end.to raise_error(ActiveRecord::RecordInvalid)
468
-
469
- forum.reload
470
- forum.should_not be_deleted
471
- end
472
-
473
- it "should pass when validations pass" do
474
- forum = ValidatedForum.create!(:category_id => 1)
475
- forum.soft_delete!
476
-
477
- forum.reload
478
- forum.should be_deleted
479
- end
480
- end
481
-
482
- describe "#soft_delete" do
483
- it "should return true if it succeeds" do
484
- forum = ValidatedForum.create!(:category_id => 1)
485
-
486
- forum.soft_delete.should be_true
487
- forum.reload
488
- forum.should be_deleted
489
- end
490
-
491
- it "should return false if validations fail" do
492
- forum = ValidatedForum.create!(:category_id => 1)
493
- forum.category_id = nil
494
-
495
- forum.soft_delete.should be_false
496
- forum.reload
497
- forum.should_not be_deleted
498
- end
499
-
500
- it "should return true if validations are prevented and it succeeds" do
501
- forum = ValidatedForum.create!(:category_id => 1)
502
- forum.category_id = nil
503
- skip_validations = (ActiveRecord::VERSION::MAJOR == 2 ? false : {:validate => false})
504
-
505
- forum.soft_delete(skip_validations).should be_true
506
- forum.reload
507
- forum.should be_deleted
508
- end
509
- end
510
- end
data/spec/spec_helper.rb DELETED
@@ -1,194 +0,0 @@
1
- require 'active_record'
2
- require 'soft_deletion'
3
- require 'database_cleaner'
4
- require 'logger'
5
-
6
- # ActiveRecord::Base.logger = Logger.new(STDOUT) # for easier debugging
7
-
8
- RSpec.configure do |config|
9
- config.before(:suite) do
10
- # would be nicer to use transaction, but Rails 2 does not open a new transaction or savepoint
11
- # when already inside a transaction
12
- DatabaseCleaner.strategy = :truncation
13
- end
14
-
15
- config.before do
16
- DatabaseCleaner.start
17
- end
18
-
19
- config.after do
20
- DatabaseCleaner.clean
21
- end
22
- end
23
-
24
- def clear_callbacks(model, callback)
25
- if ActiveRecord::VERSION::MAJOR > 2
26
- model.reset_callbacks callback
27
- else
28
- model.class_eval do
29
- instance_variable_set "@before_#{callback}_callbacks", nil
30
- instance_variable_set "@after_#{callback}_callbacks", nil
31
- end
32
- end
33
- end
34
-
35
- # connect
36
- ActiveRecord::Base.establish_connection(
37
- :adapter => "sqlite3",
38
- :database => ":memory:"
39
- )
40
-
41
- # create tables
42
- ActiveRecord::Schema.verbose = false
43
- ActiveRecord::Schema.define(:version => 1) do
44
- create_table :forums do |t|
45
- t.integer :category_id
46
- t.integer :organization_id
47
- t.timestamp :deleted_at
48
- end
49
-
50
- create_table :categories do |t|
51
- t.timestamp :deleted_at
52
- end
53
-
54
- create_table :original_categories do |t|
55
- end
56
- end
57
-
58
- class ActiveRecord::Base
59
- def self.silent_set_table_name(name)
60
- if ActiveRecord::VERSION::MAJOR > 2
61
- self.table_name = name
62
- else
63
- set_table_name name
64
- end
65
- end
66
- end
67
-
68
- # setup models
69
-
70
- class Forum < ActiveRecord::Base
71
- has_soft_deletion
72
-
73
- belongs_to :category
74
- end
75
-
76
- class ValidatedForum < ActiveRecord::Base
77
- silent_set_table_name 'forums'
78
-
79
- has_soft_deletion
80
-
81
- belongs_to :category
82
- validates_presence_of :category_id
83
- end
84
-
85
- class Category < ActiveRecord::Base
86
- has_soft_deletion
87
-
88
- has_many :forums, :dependent => :destroy
89
- end
90
-
91
- class Organization < ActiveRecord::Base
92
- silent_set_table_name 'categories'
93
- has_soft_deletion
94
-
95
- has_many :forums, :dependent => :nullify
96
- end
97
-
98
-
99
- # No association
100
- class NACategory < ActiveRecord::Base
101
- silent_set_table_name 'categories'
102
-
103
- has_soft_deletion
104
- end
105
-
106
- # Independent association
107
- class IDACategory < ActiveRecord::Base
108
- silent_set_table_name 'categories'
109
-
110
- has_soft_deletion
111
-
112
- has_many :forums, :dependent => :destroy, :foreign_key => :category_id
113
- end
114
-
115
- # Nullified dependent association
116
- class NDACategory < ActiveRecord::Base
117
- silent_set_table_name 'categories'
118
-
119
- has_soft_deletion
120
-
121
- has_many :forums, :dependent => :nullify, :foreign_key => :category_id
122
- end
123
-
124
- # Delete dependent association
125
- class DDACategory < ActiveRecord::Base
126
- silent_set_table_name 'categories'
127
-
128
- has_soft_deletion
129
-
130
- has_many :forums, :dependent => :delete_all, :foreign_key => :category_id
131
- end
132
-
133
- # default dependent association
134
- class XDACategory < ActiveRecord::Base
135
- silent_set_table_name 'categories'
136
-
137
- has_soft_deletion
138
-
139
- has_many :forums, :foreign_key => :category_id
140
- end
141
-
142
- # Has ome association
143
- class HOACategory < ActiveRecord::Base
144
- silent_set_table_name 'categories'
145
-
146
- has_soft_deletion
147
-
148
- has_one :forum, :dependent => :destroy, :foreign_key => :category_id
149
- end
150
-
151
- # Class without column deleted_at
152
- class OriginalCategory < ActiveRecord::Base
153
- has_soft_deletion
154
- end
155
-
156
- # Has many destroyable association
157
- class DACategory < ActiveRecord::Base
158
- silent_set_table_name 'categories'
159
-
160
- has_soft_deletion
161
-
162
- has_many :destroyable_forums, :dependent => :destroy, :foreign_key => :category_id
163
- end
164
-
165
- # Forum that isn't soft deletable for association checking
166
- class DestroyableForum < ActiveRecord::Base
167
- silent_set_table_name 'forums'
168
- end
169
-
170
- # test that it does not blow up when the table is not yet defined (e.g. in rake db:reset)
171
- class NoTable < ActiveRecord::Base
172
- has_soft_deletion
173
- end
174
-
175
- # Forum with other default scope
176
- class Cat1Forum < ActiveRecord::Base
177
- silent_set_table_name 'forums'
178
-
179
- has_soft_deletion
180
- if ActiveRecord::VERSION::MAJOR >= 4
181
- default_scope { where(:category_id => 1) }
182
- else
183
- default_scope :conditions => {:category_id => 1}
184
- end
185
-
186
- belongs_to :category
187
- end
188
-
189
- class Cat2Forum < ActiveRecord::Base
190
- silent_set_table_name 'forums'
191
-
192
- has_soft_deletion :default_scope => true
193
- end
194
-