mongoid_paranoia 0.4.1 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +11 -2
- data/lib/mongoid/paranoia/version.rb +1 -1
- data/lib/mongoid/paranoia.rb +21 -31
- metadata +6 -63
- data/perf/scope.rb +0 -65
- data/spec/app/models/address.rb +0 -71
- data/spec/app/models/appointment.rb +0 -7
- data/spec/app/models/author.rb +0 -6
- data/spec/app/models/fish.rb +0 -8
- data/spec/app/models/paranoid_phone.rb +0 -25
- data/spec/app/models/paranoid_post.rb +0 -65
- data/spec/app/models/person.rb +0 -21
- data/spec/app/models/phone.rb +0 -11
- data/spec/app/models/relations.rb +0 -247
- data/spec/app/models/tag.rb +0 -6
- data/spec/app/models/title.rb +0 -4
- data/spec/mongoid/configuration_spec.rb +0 -19
- data/spec/mongoid/document_spec.rb +0 -21
- data/spec/mongoid/nested_attributes_spec.rb +0 -164
- data/spec/mongoid/paranoia_spec.rb +0 -887
- data/spec/mongoid/scoping_spec.rb +0 -55
- data/spec/mongoid/validatable/uniqueness_spec.rb +0 -74
- data/spec/spec_helper.rb +0 -43
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c03b7dee0f348835ab02ae6dd3543c6af25a9593a2b3655192d35516dbe6eeec
|
4
|
+
data.tar.gz: 43bf43f4fa43bff574b843213beb54e5a2d3ed4b6c80205d3443212e6ac1ca2a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b5a5b3bf5cf3074a00565798032c1b3fae4c23e2c4ed80091600305a11a52e981ad735503a1ddb6369aa17da4900e010b366e2af8b46db842a0c1b55bdff3e3c
|
7
|
+
data.tar.gz: fceeb94c3cf50a52dd90e500988f63c9e4e850153d31808604bf226a7099b33401df37e04448474f07511905371fd9f068390689509c2480f8b5862b521b4d44
|
data/README.md
CHANGED
@@ -1,12 +1,21 @@
|
|
1
1
|
# Paranoid Documents for Mongoid
|
2
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
3
|
|
4
|
-
`Mongoid::Paranoia` enables a "soft delete" of Mongoid documents.
|
4
|
+
`Mongoid::Paranoia` enables a "soft delete" of Mongoid documents.
|
5
|
+
Instead of being removed from the database, paranoid docs are flagged
|
6
|
+
with a `deleted_at` timestamp and are ignored from queries by default.
|
5
7
|
|
6
|
-
The `Mongoid::Paranoia` functionality was originally supported in Mongoid
|
8
|
+
The `Mongoid::Paranoia` functionality was originally supported in Mongoid
|
9
|
+
itself, but was dropped from version 4.0 onwards. This gem was extracted
|
10
|
+
from the [Mongoid 3.0.0-stable branch](https://github.com/mongodb/mongoid/tree/3.0.0-stable).
|
7
11
|
|
8
12
|
**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
13
|
|
14
|
+
## Version Support
|
15
|
+
|
16
|
+
* The current release is compatible with Mongoid 7.3 and later, and Ruby 2.7 and later.
|
17
|
+
* Earlier Mongoid and Ruby versions are supported on earlier releases.
|
18
|
+
|
10
19
|
## Installation
|
11
20
|
|
12
21
|
Add this line to your application's Gemfile:
|
data/lib/mongoid/paranoia.rb
CHANGED
@@ -50,23 +50,6 @@ module Mongoid
|
|
50
50
|
define_model_callbacks :remove
|
51
51
|
end
|
52
52
|
|
53
|
-
# Delete the paranoid +Document+ from the database completely. This will
|
54
|
-
# run the destroy callbacks.
|
55
|
-
#
|
56
|
-
# @example Hard destroy the document.
|
57
|
-
# document.destroy!
|
58
|
-
#
|
59
|
-
# @return [ true, false ] If the operation succeeded.
|
60
|
-
#
|
61
|
-
# @since 1.0.0
|
62
|
-
def destroy!
|
63
|
-
run_callbacks(:destroy) do
|
64
|
-
run_callbacks(:remove) do
|
65
|
-
delete!
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
53
|
# Override the persisted method to allow for the paranoia gem.
|
71
54
|
# If a paranoid record is selected, then we only want to check
|
72
55
|
# if it's a new record, not if it is "destroyed"
|
@@ -92,34 +75,41 @@ module Mongoid
|
|
92
75
|
# @return [ true ] True.
|
93
76
|
#
|
94
77
|
# @since 1.0.0
|
95
|
-
alias
|
78
|
+
alias orig_delete delete
|
96
79
|
|
97
80
|
def remove(_ = {})
|
98
|
-
return false unless catch(:abort) do
|
99
|
-
if respond_to?(:apply_destroy_dependencies!)
|
100
|
-
apply_destroy_dependencies!
|
101
|
-
else
|
102
|
-
apply_delete_dependencies!
|
103
|
-
end
|
104
|
-
end
|
105
81
|
time = self.deleted_at = Time.now
|
106
82
|
_paranoia_update('$set' => { paranoid_field => time })
|
107
83
|
@destroyed = true
|
108
84
|
true
|
109
85
|
end
|
110
86
|
|
111
|
-
alias delete
|
87
|
+
alias delete remove
|
88
|
+
alias delete! orig_delete
|
112
89
|
|
113
|
-
# Delete the paranoid +Document+ from the database completely.
|
90
|
+
# Delete the paranoid +Document+ from the database completely. This will
|
91
|
+
# run the destroy and remove callbacks.
|
114
92
|
#
|
115
|
-
# @example Hard
|
116
|
-
# document.
|
93
|
+
# @example Hard destroy the document.
|
94
|
+
# document.destroy!
|
117
95
|
#
|
118
96
|
# @return [ true, false ] If the operation succeeded.
|
119
97
|
#
|
120
98
|
# @since 1.0.0
|
121
|
-
def
|
122
|
-
|
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
|
123
113
|
end
|
124
114
|
|
125
115
|
# Determines if this document is destroyed.
|
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.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Durran Jordan
|
@@ -9,16 +9,13 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2023-09-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
|
-
- !ruby/object:Gem::Version
|
20
|
-
version: '7.0'
|
21
|
-
- - "<"
|
22
19
|
- !ruby/object:Gem::Version
|
23
20
|
version: '7.3'
|
24
21
|
type: :runtime
|
@@ -26,25 +23,8 @@ dependencies:
|
|
26
23
|
version_requirements: !ruby/object:Gem::Requirement
|
27
24
|
requirements:
|
28
25
|
- - ">="
|
29
|
-
- !ruby/object:Gem::Version
|
30
|
-
version: '7.0'
|
31
|
-
- - "<"
|
32
26
|
- !ruby/object:Gem::Version
|
33
27
|
version: '7.3'
|
34
|
-
- !ruby/object:Gem::Dependency
|
35
|
-
name: rubocop
|
36
|
-
requirement: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - ">="
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: 1.8.1
|
41
|
-
type: :development
|
42
|
-
prerelease: false
|
43
|
-
version_requirements: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - ">="
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: 1.8.1
|
48
28
|
description: Provides a Paranoia module documents which soft-deletes documents.
|
49
29
|
email:
|
50
30
|
- durran@gmail.com
|
@@ -61,29 +41,11 @@ files:
|
|
61
41
|
- lib/mongoid/paranoia/monkey_patches.rb
|
62
42
|
- lib/mongoid/paranoia/version.rb
|
63
43
|
- lib/mongoid_paranoia.rb
|
64
|
-
- perf/scope.rb
|
65
|
-
- spec/app/models/address.rb
|
66
|
-
- spec/app/models/appointment.rb
|
67
|
-
- spec/app/models/author.rb
|
68
|
-
- spec/app/models/fish.rb
|
69
|
-
- spec/app/models/paranoid_phone.rb
|
70
|
-
- spec/app/models/paranoid_post.rb
|
71
|
-
- spec/app/models/person.rb
|
72
|
-
- spec/app/models/phone.rb
|
73
|
-
- spec/app/models/relations.rb
|
74
|
-
- spec/app/models/tag.rb
|
75
|
-
- spec/app/models/title.rb
|
76
|
-
- spec/mongoid/configuration_spec.rb
|
77
|
-
- spec/mongoid/document_spec.rb
|
78
|
-
- spec/mongoid/nested_attributes_spec.rb
|
79
|
-
- spec/mongoid/paranoia_spec.rb
|
80
|
-
- spec/mongoid/scoping_spec.rb
|
81
|
-
- spec/mongoid/validatable/uniqueness_spec.rb
|
82
|
-
- spec/spec_helper.rb
|
83
44
|
homepage: https://github.com/simi/mongoid-paranoia
|
84
45
|
licenses:
|
85
46
|
- MIT
|
86
|
-
metadata:
|
47
|
+
metadata:
|
48
|
+
rubygems_mfa_required: 'true'
|
87
49
|
post_install_message:
|
88
50
|
rdoc_options: []
|
89
51
|
require_paths:
|
@@ -99,27 +61,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
99
61
|
- !ruby/object:Gem::Version
|
100
62
|
version: '0'
|
101
63
|
requirements: []
|
102
|
-
rubygems_version: 3.
|
64
|
+
rubygems_version: 3.4.4
|
103
65
|
signing_key:
|
104
66
|
specification_version: 4
|
105
67
|
summary: Paranoid documents
|
106
|
-
test_files:
|
107
|
-
- perf/scope.rb
|
108
|
-
- spec/app/models/address.rb
|
109
|
-
- spec/app/models/appointment.rb
|
110
|
-
- spec/app/models/author.rb
|
111
|
-
- spec/app/models/fish.rb
|
112
|
-
- spec/app/models/paranoid_phone.rb
|
113
|
-
- spec/app/models/paranoid_post.rb
|
114
|
-
- spec/app/models/person.rb
|
115
|
-
- spec/app/models/phone.rb
|
116
|
-
- spec/app/models/relations.rb
|
117
|
-
- spec/app/models/tag.rb
|
118
|
-
- spec/app/models/title.rb
|
119
|
-
- spec/mongoid/configuration_spec.rb
|
120
|
-
- spec/mongoid/document_spec.rb
|
121
|
-
- spec/mongoid/nested_attributes_spec.rb
|
122
|
-
- spec/mongoid/paranoia_spec.rb
|
123
|
-
- spec/mongoid/scoping_spec.rb
|
124
|
-
- spec/mongoid/validatable/uniqueness_spec.rb
|
125
|
-
- 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
|
data/spec/app/models/address.rb
DELETED
@@ -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
|
data/spec/app/models/author.rb
DELETED
data/spec/app/models/fish.rb
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
class ParanoidPhone
|
2
|
-
include Mongoid::Document
|
3
|
-
include Mongoid::Paranoia
|
4
|
-
|
5
|
-
attr_accessor :after_destroy_called, :before_destroy_called
|
6
|
-
|
7
|
-
field :number, type: String
|
8
|
-
|
9
|
-
embedded_in :person
|
10
|
-
|
11
|
-
before_destroy :before_destroy_stub, :halt_me
|
12
|
-
after_destroy :after_destroy_stub
|
13
|
-
|
14
|
-
def before_destroy_stub
|
15
|
-
self.before_destroy_called = true
|
16
|
-
end
|
17
|
-
|
18
|
-
def after_destroy_stub
|
19
|
-
self.after_destroy_called = true
|
20
|
-
end
|
21
|
-
|
22
|
-
def halt_me
|
23
|
-
throw :abort if person.age == 42
|
24
|
-
end
|
25
|
-
end
|
@@ -1,65 +0,0 @@
|
|
1
|
-
class ParanoidPost
|
2
|
-
include Mongoid::Document
|
3
|
-
include Mongoid::Paranoia
|
4
|
-
|
5
|
-
field :title, type: String
|
6
|
-
|
7
|
-
attr_accessor :after_destroy_called, :before_destroy_called,
|
8
|
-
:after_restore_called, :before_restore_called,
|
9
|
-
:after_remove_called, :before_remove_called,
|
10
|
-
:around_before_restore_called, :around_after_restore_called
|
11
|
-
|
12
|
-
belongs_to :person
|
13
|
-
|
14
|
-
has_and_belongs_to_many :tags
|
15
|
-
has_many :authors, dependent: :delete_all, inverse_of: :post
|
16
|
-
has_many :titles, dependent: :restrict_with_error
|
17
|
-
|
18
|
-
scope :recent, -> {where(created_at: { "$lt" => Time.now, "$gt" => 30.days.ago })}
|
19
|
-
|
20
|
-
before_destroy :before_destroy_stub
|
21
|
-
after_destroy :after_destroy_stub
|
22
|
-
|
23
|
-
before_remove :before_remove_stub
|
24
|
-
after_remove :after_remove_stub
|
25
|
-
|
26
|
-
before_restore :before_restore_stub
|
27
|
-
after_restore :after_restore_stub
|
28
|
-
around_restore :around_restore_stub
|
29
|
-
|
30
|
-
def before_destroy_stub
|
31
|
-
self.before_destroy_called = true
|
32
|
-
end
|
33
|
-
|
34
|
-
def after_destroy_stub
|
35
|
-
self.after_destroy_called = true
|
36
|
-
end
|
37
|
-
|
38
|
-
def before_remove_stub
|
39
|
-
self.before_remove_called = true
|
40
|
-
end
|
41
|
-
|
42
|
-
def after_remove_stub
|
43
|
-
self.after_remove_called = true
|
44
|
-
end
|
45
|
-
|
46
|
-
def before_restore_stub
|
47
|
-
self.before_restore_called = true
|
48
|
-
end
|
49
|
-
|
50
|
-
def after_restore_stub
|
51
|
-
self.after_restore_called = true
|
52
|
-
end
|
53
|
-
|
54
|
-
def around_restore_stub
|
55
|
-
self.around_before_restore_called = true
|
56
|
-
yield
|
57
|
-
self.around_after_restore_called = true
|
58
|
-
end
|
59
|
-
|
60
|
-
class << self
|
61
|
-
def old
|
62
|
-
where(created_at: { "$lt" => 30.days.ago })
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
data/spec/app/models/person.rb
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
class Person
|
2
|
-
include Mongoid::Document
|
3
|
-
|
4
|
-
field :age, type: Integer, default: "100"
|
5
|
-
field :score, type: Integer
|
6
|
-
|
7
|
-
attr_reader :rescored
|
8
|
-
|
9
|
-
embeds_many :phone_numbers, class_name: "Phone", validate: false
|
10
|
-
embeds_many :phones, store_as: :mobile_phones, validate: false
|
11
|
-
embeds_many :addresses, as: :addressable, validate: false
|
12
|
-
|
13
|
-
embeds_many :appointments, validate: false
|
14
|
-
embeds_many :paranoid_phones, validate: false
|
15
|
-
|
16
|
-
has_many :paranoid_posts, validate: false
|
17
|
-
belongs_to :paranoid_post
|
18
|
-
|
19
|
-
accepts_nested_attributes_for :addresses
|
20
|
-
accepts_nested_attributes_for :paranoid_phones
|
21
|
-
end
|