mm_soft_deleted 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/mongo_mapper/soft_deleted.rb +72 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 640b65e619333082f0a8ee8269629970993c6494
|
4
|
+
data.tar.gz: 7bfc8ac93f9a58f8f0c74a6b809ab8b1e39f0ac9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bd598388a77484e128b7a0acd346abf921d0aec9e24bb9653ac9d790cd5d8bbd7b9bcfce2d64343135627bbb017eaf0dafb536623f47a50b0096dabd870efb7e
|
7
|
+
data.tar.gz: 8abbbc902b475bb897b22d8445092c7eb02b0b82b7dee2ba87456c3d723f8727673d8aeb9695170779a00a186405a6f337e15c7e2113d2f8fc6ac225a0d5c193
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'mongo_mapper'
|
2
|
+
|
3
|
+
class SoftDeletedRecord
|
4
|
+
include MongoMapper::Document
|
5
|
+
|
6
|
+
key :_type, String
|
7
|
+
key :_original_type, String
|
8
|
+
key :_deleted_at, Time
|
9
|
+
end
|
10
|
+
|
11
|
+
module MongoMapper::SoftDeleted
|
12
|
+
def self.included(mod)
|
13
|
+
mod.extend(MongoMapper::SoftDeleted::ClassMethods)
|
14
|
+
mod.send(:include, MongoMapper::SoftDeleted::InstanceMethods)
|
15
|
+
|
16
|
+
mod.after_destroy do
|
17
|
+
soft_delete_destroy(self)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
module InstanceMethods
|
22
|
+
def soft_delete_destroy(obj)
|
23
|
+
db = MongoMapper.database
|
24
|
+
|
25
|
+
row = obj.attributes.dup
|
26
|
+
row[:_type] = obj.class.name
|
27
|
+
row[:_deleted_at] = Time.now
|
28
|
+
row[:_original_type] = obj.attributes[:_type]
|
29
|
+
|
30
|
+
db['soft_deleted_records'].insert(row)
|
31
|
+
end
|
32
|
+
|
33
|
+
def soft_delete_restore!
|
34
|
+
db = MongoMapper.database
|
35
|
+
source_class = _type.constantize
|
36
|
+
collection_name = source_class.collection_name
|
37
|
+
|
38
|
+
attrs_for_insertion = attributes.except(:_type, :_deleted_at, :_original_type)
|
39
|
+
if !attributes[:_original_type].blank?
|
40
|
+
attrs_for_insertion[:_type] = attributes[:_original_type]
|
41
|
+
end
|
42
|
+
|
43
|
+
db[collection_name].insert(attrs_for_insertion)
|
44
|
+
db['soft_deleted_records'].remove({
|
45
|
+
_id: id
|
46
|
+
})
|
47
|
+
|
48
|
+
source_class.find(id).on_soft_delete_restore
|
49
|
+
end
|
50
|
+
|
51
|
+
def soft_deleted?
|
52
|
+
respond_to?(:_deleted_at) && _deleted_at ? true : false
|
53
|
+
end
|
54
|
+
|
55
|
+
# override this on classes
|
56
|
+
def on_soft_delete_restore
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
module ClassMethods
|
61
|
+
def soft_deleted
|
62
|
+
soft_deleted_classes = [self, descendants].flatten
|
63
|
+
soft_deleted_class_names = soft_deleted_classes.map(&:name)
|
64
|
+
|
65
|
+
SoftDeletedRecord.where({
|
66
|
+
_type: {
|
67
|
+
'$in': soft_deleted_class_names
|
68
|
+
}
|
69
|
+
})
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mm_soft_deleted
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Scott Taylor
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-21 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Soft delete records with mongo mapper
|
14
|
+
email: scott@railsnewbie.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/mongo_mapper/soft_deleted.rb
|
20
|
+
homepage: http://github.com/GoLearnUp/mm_soft_deleted
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.4.8
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Soft delete records with mongo mapper
|
44
|
+
test_files: []
|