-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathmonkey_patches.rb
114 lines (108 loc) · 3.28 KB
/
monkey_patches.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# frozen_string_literal: true
module Mongoid
module Paranoia
module Document
extend ActiveSupport::Concern
included do
# Indicates whether or not the document includes Mongoid::Paranoia.
# In Mongoid 3, this method was defined on all Mongoid::Documents.
# In Mongoid 4, it is no longer defined, hence we are shimming it here.
class_attribute :paranoid
end
end
end
end
Mongoid::Document.include Mongoid::Paranoia::Document
module Mongoid
module Association
module Nested
class Many
# Destroy the child document, needs to do some checking for embedded
# relations and delay the destroy in case parent validation fails.
#
# @api private
#
# @example Destroy the child.
# builder.destroy(parent, relation, doc)
#
# @param [ Document ] parent The parent document.
# @param [ Proxy ] relation The relation proxy.
# @param [ Document ] doc The doc to destroy.
#
# @since 3.0.10
def destroy(parent, relation, doc)
doc.flagged_for_destroy = true
if !doc.embedded? || parent.new_record? || doc.paranoid?
destroy_document(relation, doc)
else
parent.flagged_destroys.push(-> { destroy_document(relation, doc) })
end
end
end
end
end
end
module Mongoid
module Association
module Embedded
class EmbedsMany
class Proxy < Association::Many
# Delete the supplied document from the target. This method is proxied
# in order to reindex the array after the operation occurs.
#
# @example Delete the document from the relation.
# person.addresses.delete(address)
#
# @param [ Document ] document The document to be deleted.
#
# @return [ Document, nil ] The deleted document or nil if nothing deleted.
#
# @since 2.0.0.rc.1
def delete(document)
execute_callback :before_remove, document
doc = _target.delete_one(document)
if doc && !_binding?
_unscoped.delete_one(doc) unless doc.paranoid?
if _assigning?
if doc.paranoid?
doc.destroy(suppress: true)
else
_base.add_atomic_pull(doc)
end
else
doc.delete(suppress: true)
unbind_one(doc)
end
end
reindex
execute_callback :after_remove, document
doc
end
end
end
end
end
end
module Mongoid
module Association
module Embedded
class EmbedsMany
class Proxy < Association::Many
# For use only with Mongoid::Paranoia - will be removed in 4.0.
#
# @example Get the deleted documents from the relation.
# person.paranoid_phones.deleted
#
# @return [ Criteria ] The deleted documents.
#
# @since 3.0.10
def deleted
unscoped.deleted
end
# This class handles the behaviour for a document that embeds many other
# documents within in it as an array.
end
end
end
end
end