mongoid 2.1.8 → 2.1.9
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mongoid/hierarchy.rb +1 -1
- data/lib/mongoid/relations/accessors.rb +3 -2
- data/lib/mongoid/relations/auto_save.rb +5 -1
- data/lib/mongoid/relations/binding.rb +2 -2
- data/lib/mongoid/relations/bindings/referenced/in.rb +8 -5
- data/lib/mongoid/relations/builders.rb +2 -2
- data/lib/mongoid/relations/embedded/atomic.rb +2 -0
- data/lib/mongoid/relations/embedded/in.rb +16 -1
- data/lib/mongoid/relations/embedded/many.rb +13 -0
- data/lib/mongoid/relations/embedded/one.rb +13 -0
- data/lib/mongoid/relations/macros.rb +19 -19
- data/lib/mongoid/relations/metadata.rb +6 -2
- data/lib/mongoid/relations/referenced/batch.rb +2 -0
- data/lib/mongoid/relations/referenced/in.rb +13 -1
- data/lib/mongoid/relations/referenced/many.rb +14 -1
- data/lib/mongoid/relations/referenced/many_to_many.rb +14 -21
- data/lib/mongoid/relations/referenced/one.rb +13 -0
- data/lib/mongoid/relations/targets/enumerable.rb +2 -0
- data/lib/mongoid/threaded.rb +133 -23
- data/lib/mongoid/validations.rb +21 -3
- data/lib/mongoid/validations/associated.rb +12 -9
- data/lib/mongoid/version.rb +1 -1
- metadata +19 -19
data/lib/mongoid/hierarchy.rb
CHANGED
@@ -84,7 +84,7 @@ module Mongoid #:nodoc
|
|
84
84
|
# @since 2.0.0.beta.1
|
85
85
|
def remove_child(child)
|
86
86
|
name = child.metadata.name
|
87
|
-
child.embedded_one? ? remove_ivar(name) : send(name).
|
87
|
+
child.embedded_one? ? remove_ivar(name) : send(name).delete_one(child)
|
88
88
|
end
|
89
89
|
|
90
90
|
# After children are persisted we can call this to move all their changes
|
@@ -121,10 +121,11 @@ module Mongoid # :nodoc:
|
|
121
121
|
def setter(name, metadata)
|
122
122
|
tap do
|
123
123
|
define_method("#{name}=") do |object|
|
124
|
-
if relation_exists?(name) || metadata.many?
|
124
|
+
if relation_exists?(name) || metadata.many? ||
|
125
|
+
(object.blank? && send(name))
|
125
126
|
set_relation(name, send(name).substitute(object.substitutable))
|
126
127
|
else
|
127
|
-
|
128
|
+
build(name, object.substitutable, metadata)
|
128
129
|
end
|
129
130
|
end
|
130
131
|
end
|
@@ -24,7 +24,11 @@ module Mongoid # :nodoc:
|
|
24
24
|
if metadata.autosave?
|
25
25
|
set_callback :save, :after do |document|
|
26
26
|
relation = document.send(metadata.name)
|
27
|
-
|
27
|
+
if relation
|
28
|
+
(relation.do_or_do_not(:in_memory) || relation.to_a).each do |doc|
|
29
|
+
doc.save
|
30
|
+
end
|
31
|
+
end
|
28
32
|
end
|
29
33
|
end
|
30
34
|
end
|
@@ -27,11 +27,14 @@ module Mongoid # :nodoc:
|
|
27
27
|
base.send(metadata.inverse_type_setter, target.class.model_name)
|
28
28
|
end
|
29
29
|
if inverse
|
30
|
-
|
31
|
-
if
|
32
|
-
|
33
|
-
|
34
|
-
|
30
|
+
inverse_metadata = metadata.inverse_metadata(target)
|
31
|
+
if inverse_metadata != metadata
|
32
|
+
base.metadata = inverse_metadata
|
33
|
+
if base.referenced_many?
|
34
|
+
target.send(inverse).push(base)
|
35
|
+
else
|
36
|
+
target.do_or_do_not(metadata.inverse_setter(target), base)
|
37
|
+
end
|
35
38
|
end
|
36
39
|
end
|
37
40
|
end
|
@@ -46,6 +46,8 @@ module Mongoid #:nodoc:
|
|
46
46
|
# person.addresses.push([ address_one, address_two ])
|
47
47
|
# end
|
48
48
|
#
|
49
|
+
# @todo Durran: Move executions to thread local stack.
|
50
|
+
#
|
49
51
|
# @param [ Symbol ] modifier The atomic modifier to perform.
|
50
52
|
# @param [ Proc ] block The block to execute.
|
51
53
|
#
|
@@ -75,7 +75,9 @@ module Mongoid # :nodoc:
|
|
75
75
|
#
|
76
76
|
# @since 2.1.0
|
77
77
|
def characterize_one(document)
|
78
|
-
base.metadata
|
78
|
+
unless base.metadata
|
79
|
+
base.metadata = metadata.inverse_metadata(document)
|
80
|
+
end
|
79
81
|
end
|
80
82
|
|
81
83
|
# Are we able to persist this relation?
|
@@ -198,6 +200,19 @@ module Mongoid # :nodoc:
|
|
198
200
|
def valid_options
|
199
201
|
[ :cyclic, :polymorphic ]
|
200
202
|
end
|
203
|
+
|
204
|
+
# Get the default validation setting for the relation. Determines if
|
205
|
+
# by default a validates associated will occur.
|
206
|
+
#
|
207
|
+
# @example Get the validation default.
|
208
|
+
# Proxy.validation_default
|
209
|
+
#
|
210
|
+
# @return [ true, false ] The validation default.
|
211
|
+
#
|
212
|
+
# @since 2.1.9
|
213
|
+
def validation_default
|
214
|
+
false
|
215
|
+
end
|
201
216
|
end
|
202
217
|
end
|
203
218
|
end
|
@@ -494,6 +494,19 @@ module Mongoid # :nodoc:
|
|
494
494
|
def valid_options
|
495
495
|
[ :as, :cyclic, :order, :versioned ]
|
496
496
|
end
|
497
|
+
|
498
|
+
# Get the default validation setting for the relation. Determines if
|
499
|
+
# by default a validates associated will occur.
|
500
|
+
#
|
501
|
+
# @example Get the validation default.
|
502
|
+
# Proxy.validation_default
|
503
|
+
#
|
504
|
+
# @return [ true, false ] The validation default.
|
505
|
+
#
|
506
|
+
# @since 2.1.9
|
507
|
+
def validation_default
|
508
|
+
true
|
509
|
+
end
|
497
510
|
end
|
498
511
|
end
|
499
512
|
end
|
@@ -181,6 +181,19 @@ module Mongoid # :nodoc:
|
|
181
181
|
def valid_options
|
182
182
|
[ :as, :cyclic ]
|
183
183
|
end
|
184
|
+
|
185
|
+
# Get the default validation setting for the relation. Determines if
|
186
|
+
# by default a validates associated will occur.
|
187
|
+
#
|
188
|
+
# @example Get the validation default.
|
189
|
+
# Proxy.validation_default
|
190
|
+
#
|
191
|
+
# @return [ true, false ] The validation default.
|
192
|
+
#
|
193
|
+
# @since 2.1.9
|
194
|
+
def validation_default
|
195
|
+
true
|
196
|
+
end
|
184
197
|
end
|
185
198
|
end
|
186
199
|
end
|
@@ -116,18 +116,18 @@ module Mongoid # :nodoc:
|
|
116
116
|
#
|
117
117
|
# class Game
|
118
118
|
# include Mongoid::Document
|
119
|
-
#
|
119
|
+
# belongs_to :person
|
120
120
|
# end
|
121
121
|
#
|
122
122
|
# class Person
|
123
123
|
# include Mongoid::Document
|
124
|
-
#
|
124
|
+
# has_one :game
|
125
125
|
# end
|
126
126
|
#
|
127
127
|
# @param [ Symbol ] name The name of the relation.
|
128
128
|
# @param [ Hash ] options The relation options.
|
129
129
|
# @param [ Proc ] block Optional block for defining extensions.
|
130
|
-
def
|
130
|
+
def belongs_to(name, options = {}, &block)
|
131
131
|
characterize(name, Referenced::In, options, &block).tap do |meta|
|
132
132
|
relate(name, meta)
|
133
133
|
reference(meta)
|
@@ -135,8 +135,8 @@ module Mongoid # :nodoc:
|
|
135
135
|
validates_relation(meta)
|
136
136
|
end
|
137
137
|
end
|
138
|
-
alias :belongs_to_related :
|
139
|
-
alias :
|
138
|
+
alias :belongs_to_related :belongs_to
|
139
|
+
alias :referenced_in :belongs_to
|
140
140
|
|
141
141
|
# Adds a relational association from a parent Document to many
|
142
142
|
# Documents in another database or collection.
|
@@ -145,18 +145,18 @@ module Mongoid # :nodoc:
|
|
145
145
|
#
|
146
146
|
# class Person
|
147
147
|
# include Mongoid::Document
|
148
|
-
#
|
148
|
+
# has_many :posts
|
149
149
|
# end
|
150
150
|
#
|
151
151
|
# class Game
|
152
152
|
# include Mongoid::Document
|
153
|
-
#
|
153
|
+
# belongs_to :person
|
154
154
|
# end
|
155
155
|
#
|
156
156
|
# @param [ Symbol ] name The name of the relation.
|
157
157
|
# @param [ Hash ] options The relation options.
|
158
158
|
# @param [ Proc ] block Optional block for defining extensions.
|
159
|
-
def
|
159
|
+
def has_many(name, options = {}, &block)
|
160
160
|
characterize(name, Referenced::Many, options, &block).tap do |meta|
|
161
161
|
relate(name, meta)
|
162
162
|
reference(meta)
|
@@ -164,8 +164,8 @@ module Mongoid # :nodoc:
|
|
164
164
|
validates_relation(meta)
|
165
165
|
end
|
166
166
|
end
|
167
|
-
alias :has_many_related :
|
168
|
-
alias :
|
167
|
+
alias :has_many_related :has_many
|
168
|
+
alias :references_many :has_many
|
169
169
|
|
170
170
|
# Adds a relational many-to-many association between many of this
|
171
171
|
# Document and many of another Document.
|
@@ -174,12 +174,12 @@ module Mongoid # :nodoc:
|
|
174
174
|
#
|
175
175
|
# class Person
|
176
176
|
# include Mongoid::Document
|
177
|
-
#
|
177
|
+
# has_and_belongs_to_many :preferences
|
178
178
|
# end
|
179
179
|
#
|
180
180
|
# class Preference
|
181
181
|
# include Mongoid::Document
|
182
|
-
#
|
182
|
+
# has_and_belongs_to_many :people
|
183
183
|
# end
|
184
184
|
#
|
185
185
|
# @param [ Symbol ] name The name of the relation.
|
@@ -187,7 +187,7 @@ module Mongoid # :nodoc:
|
|
187
187
|
# @param [ Proc ] block Optional block for defining extensions.
|
188
188
|
#
|
189
189
|
# @since 2.0.0.rc.1
|
190
|
-
def
|
190
|
+
def has_and_belongs_to_many(name, options = {}, &block)
|
191
191
|
characterize(name, Referenced::ManyToMany, options, &block).tap do |meta|
|
192
192
|
relate(name, meta)
|
193
193
|
reference(meta, Array)
|
@@ -196,7 +196,7 @@ module Mongoid # :nodoc:
|
|
196
196
|
synced(meta)
|
197
197
|
end
|
198
198
|
end
|
199
|
-
alias :
|
199
|
+
alias :references_and_referenced_in_many :has_and_belongs_to_many
|
200
200
|
|
201
201
|
# Adds a relational association from the child Document to a Document in
|
202
202
|
# another database or collection.
|
@@ -205,18 +205,18 @@ module Mongoid # :nodoc:
|
|
205
205
|
#
|
206
206
|
# class Game
|
207
207
|
# include Mongoid::Document
|
208
|
-
#
|
208
|
+
# belongs_to :person
|
209
209
|
# end
|
210
210
|
#
|
211
211
|
# class Person
|
212
212
|
# include Mongoid::Document
|
213
|
-
#
|
213
|
+
# has_one :game
|
214
214
|
# end
|
215
215
|
#
|
216
216
|
# @param [ Symbol ] name The name of the relation.
|
217
217
|
# @param [ Hash ] options The relation options.
|
218
218
|
# @param [ Proc ] block Optional block for defining extensions.
|
219
|
-
def
|
219
|
+
def has_one(name, options = {}, &block)
|
220
220
|
characterize(name, Referenced::One, options, &block).tap do |meta|
|
221
221
|
relate(name, meta)
|
222
222
|
reference(meta)
|
@@ -224,8 +224,8 @@ module Mongoid # :nodoc:
|
|
224
224
|
validates_relation(meta)
|
225
225
|
end
|
226
226
|
end
|
227
|
-
alias :has_one_related :
|
228
|
-
alias :
|
227
|
+
alias :has_one_related :has_one
|
228
|
+
alias :references_one :has_one
|
229
229
|
|
230
230
|
private
|
231
231
|
|
@@ -334,7 +334,7 @@ module Mongoid # :nodoc:
|
|
334
334
|
#
|
335
335
|
# @since 2.0.0.rc.1
|
336
336
|
def inverse(other = nil)
|
337
|
-
return self[:inverse_of] if inverse_of
|
337
|
+
return self[:inverse_of] if has_key?(:inverse_of)
|
338
338
|
return self[:as] || lookup_inverse(other) if polymorphic?
|
339
339
|
@inverse ||= (cyclic? ? cyclic_inverse : inverse_relation)
|
340
340
|
end
|
@@ -659,7 +659,11 @@ module Mongoid # :nodoc:
|
|
659
659
|
#
|
660
660
|
# @since 2.0.0.rc.1
|
661
661
|
def validate?
|
662
|
-
self[:validate]
|
662
|
+
unless self[:validate].nil?
|
663
|
+
self[:validate]
|
664
|
+
else
|
665
|
+
self[:validate] = relation.validation_default
|
666
|
+
end
|
663
667
|
end
|
664
668
|
|
665
669
|
# Is this relation using Mongoid's internal versioning system?
|
@@ -30,6 +30,8 @@ module Mongoid #:nodoc:
|
|
30
30
|
# person.posts << [ post_one, post_two, post_three ]
|
31
31
|
# end
|
32
32
|
#
|
33
|
+
# @todo Durran: Move executions to thread local stack.
|
34
|
+
#
|
33
35
|
# @param [ Proc ] block The block to execute.
|
34
36
|
#
|
35
37
|
# @return [ Object ] The result of the operation.
|
@@ -41,7 +41,6 @@ module Mongoid # :nodoc:
|
|
41
41
|
tap do |proxy|
|
42
42
|
proxy.unbind_one
|
43
43
|
return nil unless replacement
|
44
|
-
proxy.target.delete if persistable?
|
45
44
|
proxy.target = replacement
|
46
45
|
proxy.bind_one
|
47
46
|
end
|
@@ -222,6 +221,19 @@ module Mongoid # :nodoc:
|
|
222
221
|
def valid_options
|
223
222
|
[ :autosave, :foreign_key, :index, :polymorphic ]
|
224
223
|
end
|
224
|
+
|
225
|
+
# Get the default validation setting for the relation. Determines if
|
226
|
+
# by default a validates associated will occur.
|
227
|
+
#
|
228
|
+
# @example Get the validation default.
|
229
|
+
# Proxy.validation_default
|
230
|
+
#
|
231
|
+
# @return [ true, false ] The validation default.
|
232
|
+
#
|
233
|
+
# @since 2.1.9
|
234
|
+
def validation_default
|
235
|
+
false
|
236
|
+
end
|
225
237
|
end
|
226
238
|
end
|
227
239
|
end
|
@@ -400,7 +400,7 @@ module Mongoid #:nodoc:
|
|
400
400
|
#
|
401
401
|
# @since 2.1.0
|
402
402
|
def persistable?
|
403
|
-
base.persisted? && !binding?
|
403
|
+
base.persisted? && !binding? && !building?
|
404
404
|
end
|
405
405
|
|
406
406
|
# Deletes all related documents from the database given the supplied
|
@@ -576,6 +576,19 @@ module Mongoid #:nodoc:
|
|
576
576
|
def valid_options
|
577
577
|
[ :as, :autosave, :dependent, :foreign_key, :order ]
|
578
578
|
end
|
579
|
+
|
580
|
+
# Get the default validation setting for the relation. Determines if
|
581
|
+
# by default a validates associated will occur.
|
582
|
+
#
|
583
|
+
# @example Get the validation default.
|
584
|
+
# Proxy.validation_default
|
585
|
+
#
|
586
|
+
# @return [ true, false ] The validation default.
|
587
|
+
#
|
588
|
+
# @since 2.1.9
|
589
|
+
def validation_default
|
590
|
+
true
|
591
|
+
end
|
579
592
|
end
|
580
593
|
end
|
581
594
|
end
|
@@ -154,27 +154,7 @@ module Mongoid # :nodoc:
|
|
154
154
|
end
|
155
155
|
alias :nullify_all :nullify
|
156
156
|
alias :clear :nullify
|
157
|
-
|
158
|
-
# Clear the relation. Will delete the documents from the db if they are
|
159
|
-
# already persisted.
|
160
|
-
#
|
161
|
-
# @example Clear the relation.
|
162
|
-
# person.posts.clear
|
163
|
-
#
|
164
|
-
# @return [ Many ] The relation emptied.
|
165
|
-
#
|
166
|
-
# @since 2.0.0.beta.1
|
167
|
-
def purge
|
168
|
-
criteria.delete_all
|
169
|
-
base.set(
|
170
|
-
metadata.foreign_key,
|
171
|
-
base.send(metadata.foreign_key).clear
|
172
|
-
)
|
173
|
-
target.clear do |doc|
|
174
|
-
unbind_one(doc)
|
175
|
-
doc.destroyed = true
|
176
|
-
end
|
177
|
-
end
|
157
|
+
alias :purge :nullify
|
178
158
|
|
179
159
|
private
|
180
160
|
|
@@ -364,6 +344,19 @@ module Mongoid # :nodoc:
|
|
364
344
|
def valid_options
|
365
345
|
[ :autosave, :dependent, :foreign_key, :index, :order ]
|
366
346
|
end
|
347
|
+
|
348
|
+
# Get the default validation setting for the relation. Determines if
|
349
|
+
# by default a validates associated will occur.
|
350
|
+
#
|
351
|
+
# @example Get the validation default.
|
352
|
+
# Proxy.validation_default
|
353
|
+
#
|
354
|
+
# @return [ true, false ] The validation default.
|
355
|
+
#
|
356
|
+
# @since 2.1.9
|
357
|
+
def validation_default
|
358
|
+
true
|
359
|
+
end
|
367
360
|
end
|
368
361
|
end
|
369
362
|
end
|
@@ -234,6 +234,19 @@ module Mongoid # :nodoc:
|
|
234
234
|
def valid_options
|
235
235
|
[ :as, :autosave, :dependent, :foreign_key ]
|
236
236
|
end
|
237
|
+
|
238
|
+
# Get the default validation setting for the relation. Determines if
|
239
|
+
# by default a validates associated will occur.
|
240
|
+
#
|
241
|
+
# @example Get the validation default.
|
242
|
+
# Proxy.validation_default
|
243
|
+
#
|
244
|
+
# @return [ true, false ] The validation default.
|
245
|
+
#
|
246
|
+
# @since 2.1.9
|
247
|
+
def validation_default
|
248
|
+
true
|
249
|
+
end
|
237
250
|
end
|
238
251
|
end
|
239
252
|
end
|
@@ -16,6 +16,8 @@ module Mongoid #:nodoc:
|
|
16
16
|
# @attribute [rw] unloaded A criteria representing persisted docs.
|
17
17
|
attr_accessor :added, :loaded, :unloaded
|
18
18
|
|
19
|
+
delegate :===, :is_a?, :kind_of?, :to => :added
|
20
|
+
|
19
21
|
# Check if the enumerable is equal to the other object.
|
20
22
|
#
|
21
23
|
# @example Check equality.
|
data/lib/mongoid/threaded.rb
CHANGED
@@ -6,30 +6,52 @@ module Mongoid #:nodoc:
|
|
6
6
|
module Threaded
|
7
7
|
extend self
|
8
8
|
|
9
|
-
#
|
9
|
+
# Begins a binding block.
|
10
10
|
#
|
11
|
-
# @example
|
12
|
-
# Threaded.
|
11
|
+
# @example Begin the bind.
|
12
|
+
# Threaded.begin_bind
|
13
13
|
#
|
14
|
-
# @return [ true
|
14
|
+
# @return [ true ] Always true.
|
15
15
|
#
|
16
|
-
# @since 2.1.
|
17
|
-
def
|
18
|
-
|
16
|
+
# @since 2.1.9
|
17
|
+
def begin_bind
|
18
|
+
bind_stack.push(true)
|
19
19
|
end
|
20
20
|
|
21
|
-
#
|
21
|
+
# Begins a building block.
|
22
22
|
#
|
23
|
-
# @example
|
24
|
-
# Threaded.
|
23
|
+
# @example Begin the build.
|
24
|
+
# Threaded.begin_build
|
25
25
|
#
|
26
|
-
# @
|
26
|
+
# @return [ true ] Always true.
|
27
27
|
#
|
28
|
-
# @
|
28
|
+
# @since 2.1.9
|
29
|
+
def begin_build
|
30
|
+
build_stack.push(true)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Begin validating a document on the current thread.
|
34
|
+
#
|
35
|
+
# @example Begin validation.
|
36
|
+
# Threaded.begin_validate(doc)
|
37
|
+
#
|
38
|
+
# @param [ Document ] document The document to validate.
|
39
|
+
#
|
40
|
+
# @since 2.1.9
|
41
|
+
def begin_validate(document)
|
42
|
+
validations_for(document.class).push(document.id)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Is the current thread in binding mode?
|
46
|
+
#
|
47
|
+
# @example Is the thread in binding mode?
|
48
|
+
# Threaded.binding?
|
49
|
+
#
|
50
|
+
# @return [ true, false ] If the thread is in binding mode?
|
29
51
|
#
|
30
52
|
# @since 2.1.0
|
31
|
-
def binding
|
32
|
-
|
53
|
+
def binding?
|
54
|
+
!bind_stack.empty?
|
33
55
|
end
|
34
56
|
|
35
57
|
# Is the current thread in building mode?
|
@@ -41,21 +63,33 @@ module Mongoid #:nodoc:
|
|
41
63
|
#
|
42
64
|
# @since 2.1.0
|
43
65
|
def building?
|
44
|
-
|
66
|
+
!build_stack.empty?
|
45
67
|
end
|
46
68
|
|
47
|
-
#
|
69
|
+
# Get the bind stack for the current thread. Is simply an array of calls
|
70
|
+
# to Mongoid's binding method.
|
71
|
+
#
|
72
|
+
# @example Get the bind stack.
|
73
|
+
# Threaded.bind_stack
|
74
|
+
#
|
75
|
+
# @return [ Array ] The array of bind calls.
|
48
76
|
#
|
49
|
-
# @
|
50
|
-
|
77
|
+
# @since 2.1.9
|
78
|
+
def bind_stack
|
79
|
+
Thread.current[:"[mongoid]:bind-stack"] ||= []
|
80
|
+
end
|
81
|
+
|
82
|
+
# Get the build stack for the current thread. Is simply an array of calls
|
83
|
+
# to Mongoid's building method.
|
51
84
|
#
|
52
|
-
# @
|
85
|
+
# @example Get the build stack.
|
86
|
+
# Threaded.build_stack
|
53
87
|
#
|
54
|
-
# @return [
|
88
|
+
# @return [ Array ] The array of build calls.
|
55
89
|
#
|
56
|
-
# @since 2.1.
|
57
|
-
def
|
58
|
-
Thread.current[:"[mongoid]:
|
90
|
+
# @since 2.1.9
|
91
|
+
def build_stack
|
92
|
+
Thread.current[:"[mongoid]:build-stack"] ||= []
|
59
93
|
end
|
60
94
|
|
61
95
|
# Clear out all the safety options set using the safely proxy.
|
@@ -70,6 +104,42 @@ module Mongoid #:nodoc:
|
|
70
104
|
Thread.current[:"[mongoid]:safety-options"] = nil
|
71
105
|
end
|
72
106
|
|
107
|
+
# Exit the binding block.
|
108
|
+
#
|
109
|
+
# @example Exit the binding block.
|
110
|
+
# Threaded.exit_bind
|
111
|
+
#
|
112
|
+
# @return [ true ] The last element in the stack.
|
113
|
+
#
|
114
|
+
# @since 2.1.9
|
115
|
+
def exit_bind
|
116
|
+
bind_stack.pop
|
117
|
+
end
|
118
|
+
|
119
|
+
# Exit the building block.
|
120
|
+
#
|
121
|
+
# @example Exit the building block.
|
122
|
+
# Threaded.exit_build
|
123
|
+
#
|
124
|
+
# @return [ true ] The last element in the stack.
|
125
|
+
#
|
126
|
+
# @since 2.1.9
|
127
|
+
def exit_build
|
128
|
+
build_stack.pop
|
129
|
+
end
|
130
|
+
|
131
|
+
# Exit validating a document on the current thread.
|
132
|
+
#
|
133
|
+
# @example Exit validation.
|
134
|
+
# Threaded.exit_validate(doc)
|
135
|
+
#
|
136
|
+
# @param [ Document ] document The document to validate.
|
137
|
+
#
|
138
|
+
# @since 2.1.9
|
139
|
+
def exit_validate(document)
|
140
|
+
validations_for(document.class).delete_one(document.id)
|
141
|
+
end
|
142
|
+
|
73
143
|
# Get the identity map off the current thread.
|
74
144
|
#
|
75
145
|
# @example Get the identity map.
|
@@ -171,5 +241,45 @@ module Mongoid #:nodoc:
|
|
171
241
|
def set_update_consumer(klass, consumer)
|
172
242
|
Thread.current[:"[mongoid][#{klass}]:update-consumer"] = consumer
|
173
243
|
end
|
244
|
+
|
245
|
+
# Is the document validated on the current thread?
|
246
|
+
#
|
247
|
+
# @example Is the document validated?
|
248
|
+
# Threaded.validated?(doc)
|
249
|
+
#
|
250
|
+
# @param [ Document ] document The document to check.
|
251
|
+
#
|
252
|
+
# @return [ true, false ] If the document is validated.
|
253
|
+
#
|
254
|
+
# @since 2.1.9
|
255
|
+
def validated?(document)
|
256
|
+
validations_for(document.class).include?(document.id)
|
257
|
+
end
|
258
|
+
|
259
|
+
# Get all validations on the current thread.
|
260
|
+
#
|
261
|
+
# @example Get all validations.
|
262
|
+
# Threaded.validations
|
263
|
+
#
|
264
|
+
# @return [ Hash ] The current validations.
|
265
|
+
#
|
266
|
+
# @since 2.1.9
|
267
|
+
def validations
|
268
|
+
Thread.current[:"[mongoid]:validations"] ||= {}
|
269
|
+
end
|
270
|
+
|
271
|
+
# Get all validations on the current thread for the class.
|
272
|
+
#
|
273
|
+
# @example Get all validations.
|
274
|
+
# Threaded.validations_for(Person)
|
275
|
+
#
|
276
|
+
# @param [ Class ] The class to check.
|
277
|
+
#
|
278
|
+
# @return [ Array ] The current validations.
|
279
|
+
#
|
280
|
+
# @since 2.1.9
|
281
|
+
def validations_for(klass)
|
282
|
+
validations[klass] ||= []
|
283
|
+
end
|
174
284
|
end
|
175
285
|
end
|
data/lib/mongoid/validations.rb
CHANGED
@@ -10,7 +10,25 @@ module Mongoid #:nodoc:
|
|
10
10
|
extend ActiveSupport::Concern
|
11
11
|
include ActiveModel::Validations
|
12
12
|
|
13
|
-
|
13
|
+
# Begin the associated validation.
|
14
|
+
#
|
15
|
+
# @example Begin validation.
|
16
|
+
# document.begin_validate
|
17
|
+
#
|
18
|
+
# @since 2.1.9
|
19
|
+
def begin_validate
|
20
|
+
Threaded.begin_validate(self)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Exit the associated validation.
|
24
|
+
#
|
25
|
+
# @example Exit validation.
|
26
|
+
# document.exit_validate
|
27
|
+
#
|
28
|
+
# @since 2.1.9
|
29
|
+
def exit_validate
|
30
|
+
Threaded.exit_validate(self)
|
31
|
+
end
|
14
32
|
|
15
33
|
# Overrides the default ActiveModel behaviour since we need to handle
|
16
34
|
# validations of relations slightly different than just calling the
|
@@ -27,7 +45,7 @@ module Mongoid #:nodoc:
|
|
27
45
|
def read_attribute_for_validation(attr)
|
28
46
|
if relations[attr.to_s]
|
29
47
|
relation = send(attr)
|
30
|
-
relation
|
48
|
+
relation.do_or_do_not(:in_memory) || relation
|
31
49
|
else
|
32
50
|
send(attr)
|
33
51
|
end
|
@@ -59,7 +77,7 @@ module Mongoid #:nodoc:
|
|
59
77
|
#
|
60
78
|
# @since 2.0.0.rc.2
|
61
79
|
def validated?
|
62
|
-
|
80
|
+
Threaded.validated?(self)
|
63
81
|
end
|
64
82
|
|
65
83
|
module ClassMethods #:nodoc:
|
@@ -27,15 +27,18 @@ module Mongoid #:nodoc:
|
|
27
27
|
# @param [ Symbol ] attribute The relation to validate.
|
28
28
|
# @param [ Object ] value The value of the relation.
|
29
29
|
def validate_each(document, attribute, value)
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
30
|
+
begin
|
31
|
+
document.begin_validate
|
32
|
+
valid = Array.wrap(value).collect do |doc|
|
33
|
+
if doc.nil?
|
34
|
+
true
|
35
|
+
else
|
36
|
+
doc.validated? ? true : doc.valid?
|
37
|
+
end
|
38
|
+
end.all?
|
39
|
+
ensure
|
40
|
+
document.exit_validate
|
41
|
+
end
|
39
42
|
return if valid
|
40
43
|
document.errors.add(attribute, :invalid, options.merge(:value => value))
|
41
44
|
end
|
data/lib/mongoid/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.1.
|
4
|
+
version: 2.1.9
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-08-
|
12
|
+
date: 2011-08-21 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activemodel
|
16
|
-
requirement: &
|
16
|
+
requirement: &70212863380720 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '3.0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70212863380720
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: tzinfo
|
27
|
-
requirement: &
|
27
|
+
requirement: &70212863379760 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 0.3.22
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70212863379760
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: mongo
|
38
|
-
requirement: &
|
38
|
+
requirement: &70212863378720 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '1.3'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70212863378720
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rdoc
|
49
|
-
requirement: &
|
49
|
+
requirement: &70212863378060 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: 3.5.0
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70212863378060
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: bson_ext
|
60
|
-
requirement: &
|
60
|
+
requirement: &70212863377500 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ~>
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: '1.3'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70212863377500
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: mocha
|
71
|
-
requirement: &
|
71
|
+
requirement: &70212863376960 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ~>
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: 0.9.8
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *70212863376960
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: rspec
|
82
|
-
requirement: &
|
82
|
+
requirement: &70212863376320 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ~>
|
@@ -87,10 +87,10 @@ dependencies:
|
|
87
87
|
version: '2.6'
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *70212863376320
|
91
91
|
- !ruby/object:Gem::Dependency
|
92
92
|
name: watchr
|
93
|
-
requirement: &
|
93
|
+
requirement: &70212863375660 !ruby/object:Gem::Requirement
|
94
94
|
none: false
|
95
95
|
requirements:
|
96
96
|
- - ~>
|
@@ -98,7 +98,7 @@ dependencies:
|
|
98
98
|
version: '0.6'
|
99
99
|
type: :development
|
100
100
|
prerelease: false
|
101
|
-
version_requirements: *
|
101
|
+
version_requirements: *70212863375660
|
102
102
|
description: Mongoid is an ODM (Object Document Mapper) Framework for MongoDB, written
|
103
103
|
in Ruby.
|
104
104
|
email:
|
@@ -380,7 +380,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
380
380
|
version: '0'
|
381
381
|
segments:
|
382
382
|
- 0
|
383
|
-
hash:
|
383
|
+
hash: 4312449490515662776
|
384
384
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
385
385
|
none: false
|
386
386
|
requirements:
|