mongoid_paranoia 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 43d1da1cdc376721364043d5031170e7958fffcb759f9c0e64990a51ba0f5480
4
- data.tar.gz: b5d1105826ee2edfa11a1b1432230200986ddb8d55b844dc80487c27ccb7cc48
3
+ metadata.gz: ec456afdb66a1a23fdb5dc73db06b9dc6344932f8aa3c95b378ca60560aea474
4
+ data.tar.gz: b80b6cfaedfb9523b9dbfe791d586493eb4c9d3e65b7286d3cdc2010d8f9874e
5
5
  SHA512:
6
- metadata.gz: 306b10fc581eb21195661f6303bf76316854c1cfb5be09c33443b51b4fa865851d4b4c819b7402d7e269684358f62773adbaca4003c7c0b87ef856a9ddcb608e
7
- data.tar.gz: 1466019a022ce6a6a419d49794ba6c9a00121183dacbe982519174e2b6cf155261e44245bff87711e6333f42cc0a86352dbe4f4ef180f86a3cd8717073c54b6b
6
+ metadata.gz: 3aff3ad8f7f06f0a1498143baf6cf72d58c24b99b3a4f0bfb19fd314ec836625356291989293ddee2dc230d148dda65f84bc153660bd00566ba8bc727e21ee9f
7
+ data.tar.gz: 5322c2f12d84591a8e0bc3463faf1e82571f09c5877bc3a3f7bcfbf7c10c87e198bb8be2add2ab42c6e71341df77fdbe7dbe368ccc476d7e44c8f41839c283cb
data/LICENSE CHANGED
@@ -1,22 +1,22 @@
1
- Copyright (c) 2013 Josef Šimánek
2
-
3
- MIT License
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining
6
- a copy of this software and associated documentation files (the
7
- "Software"), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to
10
- permit persons to whom the Software is furnished to do so, subject to
11
- the following conditions:
12
-
13
- The above copyright notice and this permission notice shall be
14
- included in all copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
1
+ Copyright (c) 2013 Josef Šimánek
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
22
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,117 +1,117 @@
1
- # Paranoid Documents for Mongoid
2
- [![Build Status](https://travis-ci.org/simi/mongoid_paranoia.svg?branch=master)](https://travis-ci.org/simi/mongoid_paranoia) [![Gem Version](https://img.shields.io/gem/v/mongoid_paranoia.svg)](https://rubygems.org/gems/mongoid_paranoia) [![Gitter chat](https://badges.gitter.im/simi/mongoid_paranoia.svg)](https://gitter.im/simi/mongoid_paranoia)
3
-
4
- `Mongoid::Paranoia` enables a "soft delete" of Mongoid documents. Instead of being removed from the database, paranoid docs are flagged with a `deleted_at` timestamp and are ignored from queries by default.
5
-
6
- The `Mongoid::Paranoia` functionality was originally supported in Mongoid itself, but was dropped from version 4.0.0 onwards. This gem was extracted from the [Mongoid 3.0.0-stable branch](https://github.com/mongoid/mongoid/tree/3.0.0-stable). This gem should not be used with Mongoid versions 3.x and prior. Current master branch targeted on Mongoid 6.0. With release 0.3.0 Mongoid 4 and 5 versions will be dropped.
7
-
8
- **Caution:** This repo/gem `mongoid_paranoia` (underscored) is different than [mongoid-paranoia](https://github.com/haihappen/mongoid-paranoia) (hyphenated). The goal of `mongoid-paranoia` (hyphenated) is to stay API compatible and it only accepts security fixes.
9
-
10
- ## Installation
11
-
12
- Add this line to your application's Gemfile:
13
-
14
- ```ruby
15
- gem 'mongoid_paranoia'
16
- ```
17
-
18
- ## Usage
19
-
20
- ```ruby
21
- class Person
22
- include Mongoid::Document
23
- include Mongoid::Paranoia
24
- end
25
-
26
- person.delete # Sets the deleted_at field to the current time, ignoring callbacks.
27
- person.delete! # Permanently deletes the document, ignoring callbacks.
28
- person.destroy # Sets the deleted_at field to the current time, firing callbacks.
29
- person.destroy! # Permanently deletes the document, firing callbacks.
30
- person.restore # Brings the "deleted" document back to life.
31
- person.restore(:recursive => true) # Brings "deleted" associated documents back to life recursively
32
- ```
33
-
34
- The documents that have been "flagged" as deleted (soft deleted) can be accessed at any time by calling the deleted class method on the class.
35
-
36
- ```ruby
37
- Person.deleted # Returns documents that have been "flagged" as deleted.
38
- ```
39
-
40
- You can also access all documents (both deleted and non-deleted) at any time by using the `unscoped` class method:
41
-
42
- ```ruby
43
- Person.unscoped.all # Returns all documents, both deleted and non-deleted
44
- ```
45
-
46
- You can also configure the paranoid field naming on a global basis. Within the context of a Rails app this is done via an initializer.
47
-
48
- ```ruby
49
- # config/initializers/mongoid_paranoid.rb
50
-
51
- Mongoid::Paranoia.configure do |c|
52
- c.paranoid_field = :myFieldName
53
- end
54
- ```
55
-
56
- ### Validations
57
- #### You need override uniqueness validates
58
-
59
- ```ruby
60
- validates :title, uniqueness: { conditions: -> { where(deleted_at: nil) } }
61
- ```
62
-
63
- ### Callbacks
64
-
65
- #### Restore
66
- `before_restore`, `after_restore` and `around_restore` callbacks are added to your model. They work similarly to the `before_destroy`, `after_destroy` and `around_destroy` callbacks.
67
-
68
- #### Remove
69
- `before_remove`, `after_remove` and `around_remove` are added to your model. They are called when record is deleted permanently .
70
-
71
- #### Example
72
- ```ruby
73
- class User
74
- include Mongoid::Document
75
- include Mongoid::Paranoia
76
-
77
- before_restore :before_restore_action
78
- after_restore :after_restore_action
79
- around_restore :around_restore_action
80
-
81
- private
82
-
83
- def before_restore_action
84
- puts "BEFORE"
85
- end
86
-
87
- def after_restore_action
88
- puts "AFTER"
89
- end
90
-
91
- def around_restore_action
92
- puts "AROUND - BEFORE"
93
- yield # restoring
94
- puts "AROUND - AFTER"
95
- end
96
- end
97
- ```
98
-
99
- ## TODO
100
- - get rid of [monkey_patches.rb](https://github.com/simi/mongoid_paranoia/blob/master/lib/mongoid/paranoia/monkey_patches.rb)
101
- - [review persisted? behaviour](https://github.com/simi/mongoid_paranoia/issues/2)
102
-
103
- ## Authors
104
-
105
- * original [Mongoid](https://github.com/mongoid/mongoid) implementation by [@durran](https://github.com/durran)
106
- * extracted from [Mongoid](https://github.com/mongoid/mongoid) by [@simi](https://github.com/simi)
107
- * [documentation improvements](https://github.com/simi/mongoid_paranoia/pull/3) by awesome [@loopj](https://github.com/loopj)
108
- * [latest mongoid support, restore_callback support](https://github.com/simi/mongoid_paranoia/pull/8) by fabulous [@zhouguangming](https://github.com/zhouguangming)
109
-
110
-
111
- ## Contributing
112
-
113
- 1. Fork it
114
- 2. Create your feature branch (`git checkout -b my-new-feature`)
115
- 3. Commit your changes (`git commit -am 'Add some feature'`)
116
- 4. Push to the branch (`git push origin my-new-feature`)
117
- 5. Create new Pull Request
1
+ # Paranoid Documents for Mongoid
2
+ [![Build Status](https://travis-ci.org/simi/mongoid_paranoia.svg?branch=master)](https://travis-ci.org/simi/mongoid_paranoia) [![Gem Version](https://img.shields.io/gem/v/mongoid_paranoia.svg)](https://rubygems.org/gems/mongoid_paranoia) [![Gitter chat](https://badges.gitter.im/simi/mongoid_paranoia.svg)](https://gitter.im/simi/mongoid_paranoia)
3
+
4
+ `Mongoid::Paranoia` enables a "soft delete" of Mongoid documents. Instead of being removed from the database, paranoid docs are flagged with a `deleted_at` timestamp and are ignored from queries by default.
5
+
6
+ The `Mongoid::Paranoia` functionality was originally supported in Mongoid itself, but was dropped from version 4.0.0 onwards. This gem was extracted from the [Mongoid 3.0.0-stable branch](https://github.com/mongoid/mongoid/tree/3.0.0-stable). This gem should not be used with Mongoid versions 3.x and prior. Current master branch targeted on Mongoid 6.0. With release 0.3.0 Mongoid 4 and 5 versions will be dropped.
7
+
8
+ **Caution:** This repo/gem `mongoid_paranoia` (underscored) is different than [mongoid-paranoia](https://github.com/haihappen/mongoid-paranoia) (hyphenated). The goal of `mongoid-paranoia` (hyphenated) is to stay API compatible and it only accepts security fixes.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'mongoid_paranoia'
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ ```ruby
21
+ class Person
22
+ include Mongoid::Document
23
+ include Mongoid::Paranoia
24
+ end
25
+
26
+ person.delete # Sets the deleted_at field to the current time, ignoring callbacks.
27
+ person.delete! # Permanently deletes the document, ignoring callbacks.
28
+ person.destroy # Sets the deleted_at field to the current time, firing callbacks.
29
+ person.destroy! # Permanently deletes the document, firing callbacks.
30
+ person.restore # Brings the "deleted" document back to life.
31
+ person.restore(:recursive => true) # Brings "deleted" associated documents back to life recursively
32
+ ```
33
+
34
+ The documents that have been "flagged" as deleted (soft deleted) can be accessed at any time by calling the deleted class method on the class.
35
+
36
+ ```ruby
37
+ Person.deleted # Returns documents that have been "flagged" as deleted.
38
+ ```
39
+
40
+ You can also access all documents (both deleted and non-deleted) at any time by using the `unscoped` class method:
41
+
42
+ ```ruby
43
+ Person.unscoped.all # Returns all documents, both deleted and non-deleted
44
+ ```
45
+
46
+ You can also configure the paranoid field naming on a global basis. Within the context of a Rails app this is done via an initializer.
47
+
48
+ ```ruby
49
+ # config/initializers/mongoid_paranoid.rb
50
+
51
+ Mongoid::Paranoia.configure do |c|
52
+ c.paranoid_field = :myFieldName
53
+ end
54
+ ```
55
+
56
+ ### Validations
57
+ #### You need override uniqueness validates
58
+
59
+ ```ruby
60
+ validates :title, uniqueness: { conditions: -> { where(deleted_at: nil) } }
61
+ ```
62
+
63
+ ### Callbacks
64
+
65
+ #### Restore
66
+ `before_restore`, `after_restore` and `around_restore` callbacks are added to your model. They work similarly to the `before_destroy`, `after_destroy` and `around_destroy` callbacks.
67
+
68
+ #### Remove
69
+ `before_remove`, `after_remove` and `around_remove` are added to your model. They are called when record is deleted permanently .
70
+
71
+ #### Example
72
+ ```ruby
73
+ class User
74
+ include Mongoid::Document
75
+ include Mongoid::Paranoia
76
+
77
+ before_restore :before_restore_action
78
+ after_restore :after_restore_action
79
+ around_restore :around_restore_action
80
+
81
+ private
82
+
83
+ def before_restore_action
84
+ puts "BEFORE"
85
+ end
86
+
87
+ def after_restore_action
88
+ puts "AFTER"
89
+ end
90
+
91
+ def around_restore_action
92
+ puts "AROUND - BEFORE"
93
+ yield # restoring
94
+ puts "AROUND - AFTER"
95
+ end
96
+ end
97
+ ```
98
+
99
+ ## TODO
100
+ - get rid of [monkey_patches.rb](https://github.com/simi/mongoid_paranoia/blob/master/lib/mongoid/paranoia/monkey_patches.rb)
101
+ - [review persisted? behaviour](https://github.com/simi/mongoid_paranoia/issues/2)
102
+
103
+ ## Authors
104
+
105
+ * original [Mongoid](https://github.com/mongoid/mongoid) implementation by [@durran](https://github.com/durran)
106
+ * extracted from [Mongoid](https://github.com/mongoid/mongoid) by [@simi](https://github.com/simi)
107
+ * [documentation improvements](https://github.com/simi/mongoid_paranoia/pull/3) by awesome [@loopj](https://github.com/loopj)
108
+ * [latest mongoid support, restore_callback support](https://github.com/simi/mongoid_paranoia/pull/8) by fabulous [@zhouguangming](https://github.com/zhouguangming)
109
+
110
+
111
+ ## Contributing
112
+
113
+ 1. Fork it
114
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
115
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
116
+ 4. Push to the branch (`git push origin my-new-feature`)
117
+ 5. Create new Pull Request
@@ -1,11 +1,13 @@
1
- module Mongoid
2
- module Paranoia
3
- class Configuration
4
- attr_accessor :paranoid_field
5
-
6
- def initialize
7
- @paranoid_field = :deleted_at
8
- end
9
- end
10
- end
11
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Mongoid
4
+ module Paranoia
5
+ class Configuration
6
+ attr_accessor :paranoid_field
7
+
8
+ def initialize
9
+ @paranoid_field = :deleted_at
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,113 +1,114 @@
1
- # encoding: utf-8
2
- module Mongoid
3
- module Paranoia
4
- module Document
5
- extend ActiveSupport::Concern
6
-
7
- included do
8
- # Indicates whether or not the document includes Mongoid::Paranoia.
9
- # In Mongoid 3, this method was defined on all Mongoid::Documents.
10
- # In Mongoid 4, it is no longer defined, hence we are shimming it here.
11
- class_attribute :paranoid
12
- end
13
- end
14
- end
15
- end
16
-
17
- Mongoid::Document.send(:include, Mongoid::Paranoia::Document)
18
-
19
- module Mongoid
20
- module Association
21
- module Nested
22
- class Many
23
- # Destroy the child document, needs to do some checking for embedded
24
- # relations and delay the destroy in case parent validation fails.
25
- #
26
- # @api private
27
- #
28
- # @example Destroy the child.
29
- # builder.destroy(parent, relation, doc)
30
- #
31
- # @param [ Document ] parent The parent document.
32
- # @param [ Proxy ] relation The relation proxy.
33
- # @param [ Document ] doc The doc to destroy.
34
- #
35
- # @since 3.0.10
36
- def destroy(parent, relation, doc)
37
- doc.flagged_for_destroy = true
38
- if !doc.embedded? || parent.new_record? || doc.paranoid?
39
- destroy_document(relation, doc)
40
- else
41
- parent.flagged_destroys.push(->{ destroy_document(relation, doc) })
42
- end
43
- end
44
- end
45
- end
46
- end
47
- end
48
-
49
- module Mongoid
50
- module Association
51
- module Embedded
52
- class EmbedsMany
53
- class Proxy < Association::Many
54
- # Delete the supplied document from the target. This method is proxied
55
- # in order to reindex the array after the operation occurs.
56
- #
57
- # @example Delete the document from the relation.
58
- # person.addresses.delete(address)
59
- #
60
- # @param [ Document ] document The document to be deleted.
61
- #
62
- # @return [ Document, nil ] The deleted document or nil if nothing deleted.
63
- #
64
- # @since 2.0.0.rc.1
65
- def delete(document)
66
- execute_callback :before_remove, document
67
- doc = _target.delete_one(document)
68
- if doc && !_binding?
69
- _unscoped.delete_one(doc) unless doc.paranoid?
70
- if _assigning?
71
- if doc.paranoid?
72
- doc.destroy(suppress: true)
73
- else
74
- _base.add_atomic_pull(doc)
75
- end
76
- else
77
- doc.delete(suppress: true)
78
- unbind_one(doc)
79
- end
80
- end
81
- reindex
82
- execute_callback :after_remove, document
83
- doc
84
- end
85
- end
86
- end
87
- end
88
- end
89
- end
90
-
91
- module Mongoid
92
- module Association
93
- module Embedded
94
- class EmbedsMany
95
- class Proxy < Association::Many
96
- # For use only with Mongoid::Paranoia - will be removed in 4.0.
97
- #
98
- # @example Get the deleted documents from the relation.
99
- # person.paranoid_phones.deleted
100
- #
101
- # @return [ Criteria ] The deleted documents.
102
- #
103
- # @since 3.0.10
104
- def deleted
105
- unscoped.deleted
106
- end
107
- # This class handles the behaviour for a document that embeds many other
108
- # documents within in it as an array.
109
- end
110
- end
111
- end
112
- end
113
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Mongoid
4
+ module Paranoia
5
+ module Document
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ # Indicates whether or not the document includes Mongoid::Paranoia.
10
+ # In Mongoid 3, this method was defined on all Mongoid::Documents.
11
+ # In Mongoid 4, it is no longer defined, hence we are shimming it here.
12
+ class_attribute :paranoid
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ Mongoid::Document.include Mongoid::Paranoia::Document
19
+
20
+ module Mongoid
21
+ module Association
22
+ module Nested
23
+ class Many
24
+ # Destroy the child document, needs to do some checking for embedded
25
+ # relations and delay the destroy in case parent validation fails.
26
+ #
27
+ # @api private
28
+ #
29
+ # @example Destroy the child.
30
+ # builder.destroy(parent, relation, doc)
31
+ #
32
+ # @param [ Document ] parent The parent document.
33
+ # @param [ Proxy ] relation The relation proxy.
34
+ # @param [ Document ] doc The doc to destroy.
35
+ #
36
+ # @since 3.0.10
37
+ def destroy(parent, relation, doc)
38
+ doc.flagged_for_destroy = true
39
+ if !doc.embedded? || parent.new_record? || doc.paranoid?
40
+ destroy_document(relation, doc)
41
+ else
42
+ parent.flagged_destroys.push(-> { destroy_document(relation, doc) })
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ module Mongoid
51
+ module Association
52
+ module Embedded
53
+ class EmbedsMany
54
+ class Proxy < Association::Many
55
+ # Delete the supplied document from the target. This method is proxied
56
+ # in order to reindex the array after the operation occurs.
57
+ #
58
+ # @example Delete the document from the relation.
59
+ # person.addresses.delete(address)
60
+ #
61
+ # @param [ Document ] document The document to be deleted.
62
+ #
63
+ # @return [ Document, nil ] The deleted document or nil if nothing deleted.
64
+ #
65
+ # @since 2.0.0.rc.1
66
+ def delete(document)
67
+ execute_callback :before_remove, document
68
+ doc = _target.delete_one(document)
69
+ if doc && !_binding?
70
+ _unscoped.delete_one(doc) unless doc.paranoid?
71
+ if _assigning?
72
+ if doc.paranoid?
73
+ doc.destroy(suppress: true)
74
+ else
75
+ _base.add_atomic_pull(doc)
76
+ end
77
+ else
78
+ doc.delete(suppress: true)
79
+ unbind_one(doc)
80
+ end
81
+ end
82
+ reindex
83
+ execute_callback :after_remove, document
84
+ doc
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
91
+
92
+ module Mongoid
93
+ module Association
94
+ module Embedded
95
+ class EmbedsMany
96
+ class Proxy < Association::Many
97
+ # For use only with Mongoid::Paranoia - will be removed in 4.0.
98
+ #
99
+ # @example Get the deleted documents from the relation.
100
+ # person.paranoid_phones.deleted
101
+ #
102
+ # @return [ Criteria ] The deleted documents.
103
+ #
104
+ # @since 3.0.10
105
+ def deleted
106
+ unscoped.deleted
107
+ end
108
+ # This class handles the behaviour for a document that embeds many other
109
+ # documents within in it as an array.
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
@@ -1,5 +1,7 @@
1
- module Mongoid
2
- module Paranoia
3
- VERSION = '0.4.0'.freeze
4
- end
5
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Mongoid
4
+ module Paranoia
5
+ VERSION = '0.5.0'
6
+ end
7
+ end