ohm-contrib 0.0.38 → 0.0.39
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/lib/ohm/contrib.rb +2 -1
- data/lib/ohm/contrib/soft_delete.rb +69 -0
- data/test/soft_delete_test.rb +72 -0
- metadata +5 -3
data/lib/ohm/contrib.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module Ohm
|
2
2
|
module Contrib
|
3
|
-
VERSION = "0.0.
|
3
|
+
VERSION = "0.0.39"
|
4
4
|
end
|
5
5
|
|
6
6
|
autoload :Boundaries, "ohm/contrib/boundaries"
|
@@ -16,5 +16,6 @@ module Ohm
|
|
16
16
|
autoload :LunarMacros, "ohm/contrib/lunar_macros"
|
17
17
|
autoload :Slug, "ohm/contrib/slug"
|
18
18
|
autoload :Scope, "ohm/contrib/scope"
|
19
|
+
autoload :SoftDelete, "ohm/contrib/soft_delete"
|
19
20
|
end
|
20
21
|
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module Ohm
|
2
|
+
# Provides support for soft deletion
|
3
|
+
#
|
4
|
+
# @example
|
5
|
+
#
|
6
|
+
# class Post < Ohm::Model
|
7
|
+
# include Ohm::SoftDelete
|
8
|
+
#
|
9
|
+
# attribute :title
|
10
|
+
# index :title
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# post = Post.create(:title => 'Title')
|
14
|
+
#
|
15
|
+
# post.deleted?
|
16
|
+
# # => false
|
17
|
+
#
|
18
|
+
# post.delete
|
19
|
+
#
|
20
|
+
# post.deleted?
|
21
|
+
# # => true
|
22
|
+
#
|
23
|
+
# Post.all.empty?
|
24
|
+
# # => true
|
25
|
+
#
|
26
|
+
# Post.find(:title => 'Title').empty?
|
27
|
+
# # => true
|
28
|
+
#
|
29
|
+
# Post.exists?(post.id)
|
30
|
+
# # => true
|
31
|
+
#
|
32
|
+
# post = Post[post.id]
|
33
|
+
#
|
34
|
+
# post.deleted?
|
35
|
+
# # => true
|
36
|
+
module SoftDelete
|
37
|
+
IS_DELETED = "1"
|
38
|
+
|
39
|
+
def self.included(base)
|
40
|
+
base.attribute :deleted
|
41
|
+
base.index :deleted
|
42
|
+
base.extend ClassMethods
|
43
|
+
end
|
44
|
+
|
45
|
+
def delete
|
46
|
+
update(:deleted => IS_DELETED)
|
47
|
+
end
|
48
|
+
|
49
|
+
def deleted?
|
50
|
+
deleted == IS_DELETED
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
def create_model_membership
|
55
|
+
self.class.key[:all].sadd(self.id)
|
56
|
+
end
|
57
|
+
|
58
|
+
def delete_model_membership
|
59
|
+
key.del
|
60
|
+
self.class.key[:all].srem(self.id)
|
61
|
+
end
|
62
|
+
|
63
|
+
module ClassMethods
|
64
|
+
def all
|
65
|
+
super.except(:deleted => IS_DELETED)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require File.expand_path("./helper", File.dirname(__FILE__))
|
4
|
+
|
5
|
+
class Person < Ohm::Model
|
6
|
+
include Ohm::SoftDelete
|
7
|
+
|
8
|
+
attribute :name
|
9
|
+
index :name
|
10
|
+
|
11
|
+
attribute :age
|
12
|
+
index :age
|
13
|
+
end
|
14
|
+
|
15
|
+
test "deleted?" do
|
16
|
+
person = Person.create
|
17
|
+
|
18
|
+
assert !person.deleted?
|
19
|
+
|
20
|
+
person.delete
|
21
|
+
|
22
|
+
assert person.deleted?
|
23
|
+
end
|
24
|
+
|
25
|
+
test "all excludes deleted records" do
|
26
|
+
person = Person.create(:name => "matz")
|
27
|
+
|
28
|
+
assert Person.all.first == person
|
29
|
+
|
30
|
+
person.delete
|
31
|
+
|
32
|
+
assert Person.all.empty?
|
33
|
+
end
|
34
|
+
|
35
|
+
test "find excludes deleted records" do
|
36
|
+
person = Person.create(:name => "matz")
|
37
|
+
|
38
|
+
assert Person.find(:name => "matz").first == person
|
39
|
+
|
40
|
+
person.delete
|
41
|
+
|
42
|
+
assert Person.find(:name => "matz").empty?
|
43
|
+
assert Person.all.find(:name => "matz").empty?
|
44
|
+
end
|
45
|
+
|
46
|
+
test "find with many criteria excludes deleted records" do
|
47
|
+
person = Person.create(:name => "matz", :age => 38)
|
48
|
+
|
49
|
+
assert Person.find(:name => "matz", :age => 38).first == person
|
50
|
+
|
51
|
+
person.delete
|
52
|
+
|
53
|
+
assert Person.find(:name => "matz", :age => 38).empty?
|
54
|
+
assert Person.all.find(:name => "matz", :age => 38).empty?
|
55
|
+
end
|
56
|
+
|
57
|
+
test "exists? returns true for deleted records" do
|
58
|
+
person = Person.create(:name => "matz")
|
59
|
+
|
60
|
+
person.delete
|
61
|
+
|
62
|
+
assert Person.exists?(person.id)
|
63
|
+
end
|
64
|
+
|
65
|
+
test "Model[n] can be used to retrieve deleted records" do
|
66
|
+
person = Person.create(:name => "matz")
|
67
|
+
|
68
|
+
person.delete
|
69
|
+
|
70
|
+
assert Person[person.id].deleted?
|
71
|
+
assert Person[person.id].name == "matz"
|
72
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 39
|
9
|
+
version: 0.0.39
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Cyril David
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-10-
|
17
|
+
date: 2010-10-27 00:00:00 +08:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -101,6 +101,7 @@ files:
|
|
101
101
|
- lib/ohm/contrib/number_validations.rb
|
102
102
|
- lib/ohm/contrib/scope.rb
|
103
103
|
- lib/ohm/contrib/slug.rb
|
104
|
+
- lib/ohm/contrib/soft_delete.rb
|
104
105
|
- lib/ohm/contrib/timestamping.rb
|
105
106
|
- lib/ohm/contrib/typecast.rb
|
106
107
|
- lib/ohm/contrib/web_validations.rb
|
@@ -121,6 +122,7 @@ files:
|
|
121
122
|
- test/number_validations_test.rb
|
122
123
|
- test/scope_test.rb
|
123
124
|
- test/slug_test.rb
|
125
|
+
- test/soft_delete_test.rb
|
124
126
|
- test/timestamping_test.rb
|
125
127
|
- test/typecast_array_test.rb
|
126
128
|
- test/typecast_boolean_test.rb
|