mongoid_paranoia 0.5.0 → 0.7.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.
@@ -1,196 +1,202 @@
1
- # frozen_string_literal: true
2
-
3
- require 'mongoid/paranoia/monkey_patches'
4
- require 'mongoid/paranoia/configuration'
5
- require 'active_support'
6
- require 'active_support/deprecation'
7
-
8
- module Mongoid
9
- # Include this module to get soft deletion of root level documents.
10
- # This will add a deleted_at field to the +Document+, managed automatically.
11
- # Potentially incompatible with unique indices. (if collisions with deleted items)
12
- #
13
- # @example Make a document paranoid.
14
- # class Person
15
- # include Mongoid::Document
16
- # include Mongoid::Paranoia
17
- # end
18
- module Paranoia
19
- include Mongoid::Persistable::Deletable
20
- extend ActiveSupport::Concern
21
-
22
- class << self
23
- def configuration
24
- @configuration ||= Configuration.new
25
- end
26
-
27
- def reset
28
- @configuration = Configuration.new
29
- end
30
-
31
- # Allow the paranoid +Document+ to use an alternate field name for deleted_at.
32
- #
33
- # @example
34
- # Mongoid::Paranoia.configure do |c|
35
- # c.paranoid_field = :myFieldName
36
- # end
37
- def configure
38
- yield(configuration)
39
- end
40
- end
41
-
42
- included do
43
- field Paranoia.configuration.paranoid_field, as: :deleted_at, type: Time
44
-
45
- self.paranoid = true
46
-
47
- default_scope -> { where(deleted_at: nil) }
48
- scope :deleted, -> { ne(deleted_at: nil) }
49
- define_model_callbacks :restore
50
- define_model_callbacks :remove
51
- end
52
-
53
- # Override the persisted method to allow for the paranoia gem.
54
- # If a paranoid record is selected, then we only want to check
55
- # if it's a new record, not if it is "destroyed"
56
- #
57
- # @example
58
- # document.persisted?
59
- #
60
- # @return [ true, false ] If the operation succeeded.
61
- #
62
- # @since 4.0.0
63
- def persisted?
64
- !new_record?
65
- end
66
-
67
- # Delete the +Document+, will set the deleted_at timestamp and not actually
68
- # delete it.
69
- #
70
- # @example Soft remove the document.
71
- # document.remove
72
- #
73
- # @param [ Hash ] options The database options.
74
- #
75
- # @return [ true ] True.
76
- #
77
- # @since 1.0.0
78
- alias :orig_delete :delete
79
-
80
- def remove(_ = {})
81
- time = self.deleted_at = Time.now
82
- _paranoia_update('$set' => { paranoid_field => time })
83
- @destroyed = true
84
- true
85
- end
86
-
87
- alias :delete :remove
88
- alias :delete! :orig_delete
89
-
90
- # Delete the paranoid +Document+ from the database completely. This will
91
- # run the destroy and remove callbacks.
92
- #
93
- # @example Hard destroy the document.
94
- # document.destroy!
95
- #
96
- # @return [ true, false ] If the operation succeeded.
97
- #
98
- # @since 1.0.0
99
- def destroy!(options = {})
100
- raise Errors::ReadonlyDocument.new(self.class) if readonly?
101
- self.flagged_for_destroy = true
102
- result = run_callbacks(:destroy) do
103
- run_callbacks(:remove) do
104
- if catch(:abort) { apply_destroy_dependencies! }
105
- delete!(options || {})
106
- else
107
- false
108
- end
109
- end
110
- end
111
- self.flagged_for_destroy = false
112
- result
113
- end
114
-
115
- # Determines if this document is destroyed.
116
- #
117
- # @example Is the document destroyed?
118
- # person.destroyed?
119
- #
120
- # @return [ true, false ] If the document is destroyed.
121
- #
122
- # @since 1.0.0
123
- def destroyed?
124
- (@destroyed ||= false) || !!deleted_at
125
- end
126
- alias deleted? destroyed?
127
-
128
- # Restores a previously soft-deleted document. Handles this by removing the
129
- # deleted_at flag.
130
- #
131
- # @example Restore the document from deleted state.
132
- # document.restore
133
- #
134
- # For resoring associated documents use :recursive => true
135
- # @example Restore the associated documents from deleted state.
136
- # document.restore(:recursive => true)
137
- #
138
- # TODO: @return [ Time ] The time the document had been deleted.
139
- #
140
- # @since 1.0.0
141
- def restore(opts = {})
142
- run_callbacks(:restore) do
143
- _paranoia_update('$unset' => { paranoid_field => true })
144
- attributes.delete('deleted_at')
145
- @destroyed = false
146
- restore_relations if opts[:recursive]
147
- true
148
- end
149
- end
150
-
151
- # Returns a string representing the documents's key suitable for use in URLs.
152
- def to_param
153
- new_record? ? nil : to_key.join('-')
154
- end
155
-
156
- def restore_relations
157
- relations.each_pair do |name, association|
158
- next unless association.dependent == :destroy
159
- relation = send(name)
160
- next unless relation.present? && relation.paranoid?
161
- Array.wrap(relation).each do |doc|
162
- doc.restore(recursive: true)
163
- end
164
- end
165
- end
166
-
167
- private
168
-
169
- # Get the collection to be used for paranoid operations.
170
- #
171
- # @example Get the paranoid collection.
172
- # document.paranoid_collection
173
- #
174
- # @return [ Collection ] The root collection.
175
- def paranoid_collection
176
- embedded? ? _root.collection : collection
177
- end
178
-
179
- # Get the field to be used for paranoid operations.
180
- #
181
- # @example Get the paranoid field.
182
- # document.paranoid_field
183
- #
184
- # @return [ String ] The deleted at field.
185
- def paranoid_field
186
- field = Paranoia.configuration.paranoid_field
187
- embedded? ? "#{atomic_position}.#{field}" : field
188
- end
189
-
190
- # @return [ Object ] Update result.
191
- #
192
- def _paranoia_update(value)
193
- paranoid_collection.find(atomic_selector).update_one(value)
194
- end
195
- end
196
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'mongoid/paranoia/monkey_patches'
4
+ require 'mongoid/paranoia/configuration'
5
+ require 'active_support'
6
+ require 'active_support/deprecation'
7
+
8
+ module Mongoid
9
+ # Include this module to get soft deletion of root level documents.
10
+ # This will add a deleted_at field to the +Document+, managed automatically.
11
+ # Potentially incompatible with unique indices. (if collisions with deleted items)
12
+ #
13
+ # @example Make a document paranoid.
14
+ # class Person
15
+ # include Mongoid::Document
16
+ # include Mongoid::Paranoia
17
+ # end
18
+ module Paranoia
19
+ include Mongoid::Persistable::Deletable
20
+ extend ActiveSupport::Concern
21
+
22
+ class << self
23
+ def configuration
24
+ @configuration ||= Configuration.new
25
+ end
26
+
27
+ def reset
28
+ @configuration = Configuration.new
29
+ end
30
+
31
+ # Allow the paranoid +Document+ to use an alternate field name for deleted_at.
32
+ #
33
+ # @example
34
+ # Mongoid::Paranoia.configure do |c|
35
+ # c.paranoid_field = :myFieldName
36
+ # end
37
+ def configure
38
+ yield(configuration)
39
+ end
40
+ end
41
+
42
+ included do
43
+ field Paranoia.configuration.paranoid_field, as: :deleted_at, type: Time
44
+
45
+ self.paranoid = true
46
+
47
+ default_scope -> { where(deleted_at: nil) }
48
+ scope :deleted, -> { ne(deleted_at: nil) }
49
+ scope :with_deleted, lambda {
50
+ msg = 'This scope requires Mongoid >= 9 and allow_scopes_to_unset_default_scope to be set to true'
51
+ raise msg unless Mongoid.try(:allow_scopes_to_unset_default_scope)
52
+
53
+ criteria.remove_scoping(unscoped.where(deleted_at: nil))
54
+ }
55
+ define_model_callbacks :restore
56
+ define_model_callbacks :remove
57
+ end
58
+
59
+ # Override the persisted method to allow for the paranoia gem.
60
+ # If a paranoid record is selected, then we only want to check
61
+ # if it's a new record, not if it is "destroyed"
62
+ #
63
+ # @example
64
+ # document.persisted?
65
+ #
66
+ # @return [ true, false ] If the operation succeeded.
67
+ #
68
+ # @since 4.0.0
69
+ def persisted?
70
+ !new_record?
71
+ end
72
+
73
+ # Delete the +Document+, will set the deleted_at timestamp and not actually
74
+ # delete it.
75
+ #
76
+ # @example Soft remove the document.
77
+ # document.remove
78
+ #
79
+ # @param [ Hash ] options The database options.
80
+ #
81
+ # @return [ true ] True.
82
+ #
83
+ # @since 1.0.0
84
+ alias orig_delete delete
85
+
86
+ def remove(_ = {}) # rubocop:disable Naming/PredicateMethod
87
+ time = self.deleted_at = Time.now
88
+ _paranoia_update('$set' => { paranoid_field => time })
89
+ @destroyed = true
90
+ true
91
+ end
92
+
93
+ alias delete remove
94
+ alias delete! orig_delete
95
+
96
+ # Delete the paranoid +Document+ from the database completely. This will
97
+ # run the destroy and remove callbacks.
98
+ #
99
+ # @example Hard destroy the document.
100
+ # document.destroy!
101
+ #
102
+ # @return [ true, false ] If the operation succeeded.
103
+ #
104
+ # @since 1.0.0
105
+ def destroy!(options = {})
106
+ raise Errors::ReadonlyDocument.new(self.class) if readonly?
107
+ self.flagged_for_destroy = true
108
+ result = run_callbacks(:destroy) do
109
+ run_callbacks(:remove) do
110
+ if catch(:abort) { apply_destroy_dependencies! }
111
+ delete!(options || {})
112
+ else
113
+ false
114
+ end
115
+ end
116
+ end
117
+ self.flagged_for_destroy = false
118
+ result
119
+ end
120
+
121
+ # Determines if this document is destroyed.
122
+ #
123
+ # @example Is the document destroyed?
124
+ # person.destroyed?
125
+ #
126
+ # @return [ true, false ] If the document is destroyed.
127
+ #
128
+ # @since 1.0.0
129
+ def destroyed?
130
+ (@destroyed ||= false) || !!deleted_at
131
+ end
132
+ alias deleted? destroyed?
133
+
134
+ # Restores a previously soft-deleted document. Handles this by removing the
135
+ # deleted_at flag.
136
+ #
137
+ # @example Restore the document from deleted state.
138
+ # document.restore
139
+ #
140
+ # For resoring associated documents use :recursive => true
141
+ # @example Restore the associated documents from deleted state.
142
+ # document.restore(:recursive => true)
143
+ #
144
+ # TODO: @return [ Time ] The time the document had been deleted.
145
+ #
146
+ # @since 1.0.0
147
+ def restore(opts = {})
148
+ run_callbacks(:restore) do
149
+ _paranoia_update('$unset' => { paranoid_field => true })
150
+ attributes.delete('deleted_at')
151
+ @destroyed = false
152
+ restore_relations if opts[:recursive]
153
+ true
154
+ end
155
+ end
156
+
157
+ # Returns a string representing the documents's key suitable for use in URLs.
158
+ def to_param
159
+ new_record? ? nil : to_key.join('-')
160
+ end
161
+
162
+ def restore_relations
163
+ relations.each_pair do |name, association|
164
+ next unless association.dependent == :destroy
165
+ relation = send(name)
166
+ next unless relation.present? && relation.paranoid?
167
+ Array.wrap(relation).each do |doc|
168
+ doc.restore(recursive: true)
169
+ end
170
+ end
171
+ end
172
+
173
+ private
174
+
175
+ # Get the collection to be used for paranoid operations.
176
+ #
177
+ # @example Get the paranoid collection.
178
+ # document.paranoid_collection
179
+ #
180
+ # @return [ Collection ] The root collection.
181
+ def paranoid_collection
182
+ embedded? ? _root.collection : collection
183
+ end
184
+
185
+ # Get the field to be used for paranoid operations.
186
+ #
187
+ # @example Get the paranoid field.
188
+ # document.paranoid_field
189
+ #
190
+ # @return [ String ] The deleted at field.
191
+ def paranoid_field
192
+ field = Paranoia.configuration.paranoid_field
193
+ embedded? ? "#{atomic_position}.#{field}" : field
194
+ end
195
+
196
+ # @return [ Object ] Update result.
197
+ #
198
+ def _paranoia_update(value)
199
+ paranoid_collection.find(atomic_selector).update_one(value, session: _session)
200
+ end
201
+ end
202
+ end
@@ -1,3 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
- require 'mongoid/paranoia'
1
+ # frozen_string_literal: true
2
+
3
+ require 'mongoid/paranoia'
@@ -1,3 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
- require 'mongoid/paranoia'
1
+ # frozen_string_literal: true
2
+
3
+ require 'mongoid/paranoia'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_paranoia
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Durran Jordan
@@ -9,36 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-07-24 00:00:00.000000000 Z
12
+ date: 2026-07-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongoid
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - "~>"
18
+ - - ">="
19
19
  - !ruby/object:Gem::Version
20
20
  version: '7.3'
21
21
  type: :runtime
22
22
  prerelease: false
23
- version_requirements: !ruby/object:Gem::Requirement
24
- requirements:
25
- - - "~>"
26
- - !ruby/object:Gem::Version
27
- version: '7.3'
28
- - !ruby/object:Gem::Dependency
29
- name: rubocop
30
- requirement: !ruby/object:Gem::Requirement
31
- requirements:
32
- - - ">="
33
- - !ruby/object:Gem::Version
34
- version: 1.8.1
35
- type: :development
36
- prerelease: false
37
23
  version_requirements: !ruby/object:Gem::Requirement
38
24
  requirements:
39
25
  - - ">="
40
26
  - !ruby/object:Gem::Version
41
- version: 1.8.1
27
+ version: '7.3'
42
28
  description: Provides a Paranoia module documents which soft-deletes documents.
43
29
  email:
44
30
  - durran@gmail.com
@@ -55,29 +41,11 @@ files:
55
41
  - lib/mongoid/paranoia/monkey_patches.rb
56
42
  - lib/mongoid/paranoia/version.rb
57
43
  - lib/mongoid_paranoia.rb
58
- - perf/scope.rb
59
- - spec/app/models/address.rb
60
- - spec/app/models/appointment.rb
61
- - spec/app/models/author.rb
62
- - spec/app/models/fish.rb
63
- - spec/app/models/paranoid_phone.rb
64
- - spec/app/models/paranoid_post.rb
65
- - spec/app/models/person.rb
66
- - spec/app/models/phone.rb
67
- - spec/app/models/relations.rb
68
- - spec/app/models/tag.rb
69
- - spec/app/models/title.rb
70
- - spec/mongoid/configuration_spec.rb
71
- - spec/mongoid/document_spec.rb
72
- - spec/mongoid/nested_attributes_spec.rb
73
- - spec/mongoid/paranoia_spec.rb
74
- - spec/mongoid/scoping_spec.rb
75
- - spec/mongoid/validatable/uniqueness_spec.rb
76
- - spec/spec_helper.rb
77
44
  homepage: https://github.com/simi/mongoid-paranoia
78
45
  licenses:
79
46
  - MIT
80
- metadata: {}
47
+ metadata:
48
+ rubygems_mfa_required: 'true'
81
49
  post_install_message:
82
50
  rdoc_options: []
83
51
  require_paths:
@@ -93,27 +61,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
61
  - !ruby/object:Gem::Version
94
62
  version: '0'
95
63
  requirements: []
96
- rubygems_version: 3.1.2
64
+ rubygems_version: 3.4.19
97
65
  signing_key:
98
66
  specification_version: 4
99
67
  summary: Paranoid documents
100
- test_files:
101
- - perf/scope.rb
102
- - spec/app/models/address.rb
103
- - spec/app/models/appointment.rb
104
- - spec/app/models/author.rb
105
- - spec/app/models/fish.rb
106
- - spec/app/models/paranoid_phone.rb
107
- - spec/app/models/paranoid_post.rb
108
- - spec/app/models/person.rb
109
- - spec/app/models/phone.rb
110
- - spec/app/models/relations.rb
111
- - spec/app/models/tag.rb
112
- - spec/app/models/title.rb
113
- - spec/mongoid/configuration_spec.rb
114
- - spec/mongoid/document_spec.rb
115
- - spec/mongoid/nested_attributes_spec.rb
116
- - spec/mongoid/paranoia_spec.rb
117
- - spec/mongoid/scoping_spec.rb
118
- - spec/mongoid/validatable/uniqueness_spec.rb
119
- - spec/spec_helper.rb
68
+ test_files: []
data/perf/scope.rb DELETED
@@ -1,65 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bundler/setup'
4
- require 'mongoid'
5
- require 'mongoid/paranoia'
6
- require 'benchmark'
7
-
8
- Mongoid.configure do |config|
9
- config.connect_to('my_little_test')
10
- end
11
-
12
- class Model
13
- include Mongoid::Document
14
- field :text, type: String
15
-
16
- index({ text: 'text' })
17
- end
18
-
19
- class ParanoidModel
20
- include Mongoid::Document
21
- include Mongoid::Paranoia
22
- field :text, type: String
23
-
24
- index({ text: 'text' })
25
- end
26
-
27
- class MetaParanoidModel
28
- include Mongoid::Document
29
- field :text, type: String
30
- field :deleted_at, type: Time
31
- default_scope -> { where(deleted_at: nil) }
32
-
33
- index({ text: 'text' })
34
- end
35
-
36
- if ENV['FORCE']
37
- Mongoid.purge!
38
- ::Mongoid::Tasks::Database.create_indexes
39
-
40
- n = 50_000
41
- n.times {|i| Model.create(text: "text #{i}") }
42
- n.times {|i| ParanoidModel.create(text: "text #{i}") }
43
- n.times {|i| MetaParanoidModel.create(text: "text #{i}") }
44
- end
45
-
46
- n = 100
47
-
48
- puts 'text_search benchmark ***'
49
- Benchmark.bm(20) do |x|
50
- x.report('without') { n.times { Model.text_search('text').execute } }
51
- x.report('with') { n.times { ParanoidModel.text_search('text').execute } }
52
- x.report('meta') { n.times { MetaParanoidModel.text_search('text').execute } }
53
- x.report('unscoped meta') { n.times { MetaParanoidModel.unscoped.text_search('text').execute } }
54
- x.report('unscoped paranoid') { n.times { ParanoidModel.unscoped.text_search('text').execute } }
55
- end
56
-
57
- puts ''
58
- puts 'Pluck all ids benchmark ***'
59
- Benchmark.bm(20) do |x|
60
- x.report('without') { n.times { Model.all.pluck(:id) } }
61
- x.report('with') { n.times { ParanoidModel.all.pluck(:id) } }
62
- x.report('meta') { n.times { MetaParanoidModel.all.pluck(:id) } }
63
- x.report('unscoped meta') { n.times { MetaParanoidModel.unscoped.all.pluck(:id) } }
64
- x.report('unscoped paranoid') { n.times { ParanoidModel.unscoped.all.pluck(:id) } }
65
- end
@@ -1,71 +0,0 @@
1
- class Address
2
- include Mongoid::Document
3
-
4
- field :_id, type: String, default: ->{ street.try(:parameterize) }
5
-
6
- attr_accessor :mode
7
-
8
- field :address_type
9
- field :number, type: Integer
10
- field :street
11
- field :city
12
- field :state
13
- field :post_code
14
- field :parent_title
15
- field :services, type: Array
16
- field :latlng, type: Array
17
- field :map, type: Hash
18
- field :move_in, type: DateTime
19
- field :s, type: String, as: :suite
20
- field :name, localize: true
21
-
22
- embeds_one :code, validate: false
23
- embeds_one :target, as: :targetable, validate: false
24
-
25
- embedded_in :addressable, polymorphic: true do
26
- def extension
27
- "Testing"
28
- end
29
- def doctor?
30
- title == "Dr"
31
- end
32
- end
33
-
34
- accepts_nested_attributes_for :code, :target
35
-
36
- belongs_to :account
37
-
38
- scope :without_postcode, -> {where(postcode: nil)}
39
- scope :rodeo, -> {
40
- where(street: "Rodeo Dr") do
41
- def mansion?
42
- all? { |address| address.street == "Rodeo Dr" }
43
- end
44
- end
45
- }
46
-
47
- validates_presence_of :street, on: :update
48
- validates_format_of :street, with: /\D/, allow_nil: true
49
-
50
- def set_parent=(set = false)
51
- self.parent_title = addressable.title if set
52
- end
53
-
54
- def <=>(other)
55
- street <=> other.street
56
- end
57
-
58
- class << self
59
- def california
60
- where(state: "CA")
61
- end
62
-
63
- def homes
64
- where(address_type: "Home")
65
- end
66
-
67
- def streets
68
- all.map(&:street)
69
- end
70
- end
71
- end
@@ -1,7 +0,0 @@
1
- class Appointment
2
- include Mongoid::Document
3
- field :active, type: Boolean, default: true
4
- field :timed, type: Boolean, default: true
5
- embedded_in :person
6
- default_scope ->{where(active: true)}
7
- end
@@ -1,6 +0,0 @@
1
- class Author
2
- include Mongoid::Document
3
- field :name, type: String
4
-
5
- belongs_to :post, class_name: "ParanoidPost"
6
- end