/
creatable.rb
187 lines (175 loc) · 5.83 KB
/
creatable.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# frozen_string_literal: true
# rubocop:todo all
module Mongoid
module Persistable
# Defines behavior for persistence operations that create new documents.
module Creatable
extend ActiveSupport::Concern
# Insert a new document into the database. Will return the document
# itself whether or not the save was successful.
#
# @example Insert a document.
# document.insert
#
# @param [ Hash ] options Options to pass to insert.
#
# @return [ Document ] The persisted document.
def insert(options = {})
prepare_insert(options) do
if embedded?
insert_as_embedded
else
insert_as_root
end
end
end
private
# Get the atomic insert for embedded documents, either a push or set.
#
# @api private
#
# @example Get the inserts.
# document.inserts
#
# @return [ Hash ] The insert ops.
def atomic_inserts
{ atomic_insert_modifier => { atomic_position => as_attributes }}
end
# Insert the embedded document.
#
# @api private
#
# @example Insert the document as embedded.
# document.insert_as_embedded
#
# @return [ Document ] The document.
def insert_as_embedded
raise Errors::NoParent.new(self.class.name) unless _parent
if _parent.new_record?
_parent.insert
else
selector = _parent.atomic_selector
_root.collection.find(selector).update_one(
positionally(selector, atomic_inserts),
session: _session)
end
end
# Insert the root document.
#
# @api private
#
# @example Insert the document as root.
# document.insert_as_root
#
# @return [ Document ] The document.
def insert_as_root
collection.insert_one(as_attributes, session: _session)
end
# Post process an insert, which sets the new record attribute to false
# and flags all the children as persisted.
#
# @api private
#
# @example Post process the insert.
# document.post_process_insert
#
# @return [ true ] true.
def post_process_insert
self.new_record = false
remember_storage_options!
flag_descendants_persisted
true
end
# Prepare the insert for execution. Validates and runs callbacks, etc.
#
# @api private
#
# @example Prepare for insertion.
# document.prepare_insert do
# collection.insert(as_document)
# end
#
# @param [ Hash ] options The options.
#
# @return [ Document ] The document.
def prepare_insert(options = {})
raise Errors::ReadonlyDocument.new(self.class) if readonly? && !Mongoid.legacy_readonly
return self if performing_validations?(options) &&
invalid?(options[:context] || :create)
ensure_client_compatibility!
run_callbacks(:commit, with_children: true, skip_if: -> { in_transaction? }) do
run_callbacks(:save, with_children: false) do
run_callbacks(:create, with_children: false) do
run_callbacks(:persist_parent, with_children: false) do
_mongoid_run_child_callbacks(:save) do
_mongoid_run_child_callbacks(:create) do
result = yield(self)
if !result.is_a?(Document) || result.errors.empty?
post_process_insert
post_process_persist(result, options)
end
end
end
end
end
end
end
self
end
module ClassMethods
# Create a new document. This will instantiate a new document and
# insert it in a single call. Will always return the document
# whether save passed or not.
#
# @example Create a new document.
# Person.create(:title => "Mr")
#
# @example Create multiple new documents.
# Person.create({ title: "Mr" }, { title: "Mrs" })
#
# @param [ Hash | Array ] attributes The attributes to create with, or an
# Array of multiple attributes for multiple documents.
#
# @return [ Document | Array<Document> ] The newly created document(s).
def create(attributes = nil, &block)
_creating do
if attributes.is_a?(::Array)
attributes.map { |attrs| create(attrs, &block) }
else
doc = new(attributes, &block)
doc.save
doc
end
end
end
# Create a new document. This will instantiate a new document and
# insert it in a single call. Will always return the document
# whether save passed or not, and if validation fails an error will be
# raise.
#
# @example Create a new document.
# Person.create!(:title => "Mr")
#
# @example Create multiple new documents.
# Person.create!({ title: "Mr" }, { title: "Mrs" })
#
# @param [ Hash | Array ] attributes The attributes to create with, or an
# Array of multiple attributes for multiple documents.
#
# @return [ Document | Array<Document> ] The newly created document(s).
def create!(attributes = nil, &block)
_creating do
if attributes.is_a?(::Array)
attributes.map { |attrs| create!(attrs, &block) }
else
doc = new(attributes, &block)
doc.fail_due_to_validation! unless doc.insert.errors.empty?
doc.fail_due_to_callback!(:create!) if doc.new_record?
doc
end
end
end
end
end
end
end