mongoid 5.4.1 → 6.1.1
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.
- checksums.yaml +5 -5
- data/README.md +3 -3
- data/lib/config/locales/en.yml +19 -0
- data/lib/mongoid.rb +4 -4
- data/lib/mongoid/atomic.rb +2 -2
- data/lib/mongoid/atomic/modifiers.rb +8 -12
- data/lib/mongoid/attributes.rb +22 -21
- data/lib/mongoid/attributes/readonly.rb +22 -0
- data/lib/mongoid/cacheable.rb +36 -0
- data/lib/mongoid/changeable.rb +36 -0
- data/lib/mongoid/clients.rb +8 -63
- data/lib/mongoid/clients/options.rb +55 -250
- data/lib/mongoid/clients/storage_options.rb +1 -69
- data/lib/mongoid/composable.rb +29 -3
- data/lib/mongoid/config.rb +1 -0
- data/lib/mongoid/contextual/atomic.rb +5 -8
- data/lib/mongoid/contextual/map_reduce.rb +0 -4
- data/lib/mongoid/contextual/memory.rb +2 -2
- data/lib/mongoid/contextual/mongo.rb +40 -22
- data/lib/mongoid/contextual/none.rb +12 -0
- data/lib/mongoid/copyable.rb +13 -6
- data/lib/mongoid/criteria.rb +5 -2
- data/lib/mongoid/criteria/marshalable.rb +2 -2
- data/lib/mongoid/criteria/modifiable.rb +17 -1
- data/lib/mongoid/criteria/options.rb +25 -0
- data/lib/mongoid/criteria/queryable.rb +87 -0
- data/lib/mongoid/criteria/queryable/aggregable.rb +120 -0
- data/lib/mongoid/criteria/queryable/extensions.rb +28 -0
- data/lib/mongoid/criteria/queryable/extensions/array.rb +185 -0
- data/lib/mongoid/criteria/queryable/extensions/big_decimal.rb +37 -0
- data/lib/mongoid/criteria/queryable/extensions/boolean.rb +34 -0
- data/lib/mongoid/criteria/queryable/extensions/date.rb +63 -0
- data/lib/mongoid/criteria/queryable/extensions/date_time.rb +53 -0
- data/lib/mongoid/criteria/queryable/extensions/hash.rb +200 -0
- data/lib/mongoid/criteria/queryable/extensions/nil_class.rb +86 -0
- data/lib/mongoid/criteria/queryable/extensions/numeric.rb +90 -0
- data/lib/mongoid/criteria/queryable/extensions/object.rb +206 -0
- data/lib/mongoid/criteria/queryable/extensions/range.rb +70 -0
- data/lib/mongoid/criteria/queryable/extensions/regexp.rb +79 -0
- data/lib/mongoid/criteria/queryable/extensions/set.rb +34 -0
- data/lib/mongoid/criteria/queryable/extensions/string.rb +137 -0
- data/lib/mongoid/criteria/queryable/extensions/symbol.rb +79 -0
- data/lib/mongoid/criteria/queryable/extensions/time.rb +60 -0
- data/lib/mongoid/criteria/queryable/extensions/time_with_zone.rb +54 -0
- data/lib/mongoid/criteria/queryable/forwardable.rb +65 -0
- data/lib/mongoid/criteria/queryable/key.rb +103 -0
- data/lib/mongoid/criteria/queryable/macroable.rb +27 -0
- data/lib/mongoid/criteria/queryable/mergeable.rb +271 -0
- data/lib/mongoid/criteria/queryable/optional.rb +429 -0
- data/lib/mongoid/criteria/queryable/options.rb +153 -0
- data/lib/mongoid/criteria/queryable/pipeline.rb +111 -0
- data/lib/mongoid/criteria/queryable/selectable.rb +662 -0
- data/lib/mongoid/criteria/queryable/selector.rb +212 -0
- data/lib/mongoid/criteria/queryable/smash.rb +104 -0
- data/lib/mongoid/document.rb +30 -37
- data/lib/mongoid/errors.rb +2 -0
- data/lib/mongoid/errors/ambiguous_relationship.rb +1 -1
- data/lib/mongoid/errors/in_memory_collation_not_supported.rb +1 -1
- data/lib/mongoid/errors/invalid_field.rb +2 -2
- data/lib/mongoid/errors/invalid_persistence_option.rb +29 -0
- data/lib/mongoid/errors/invalid_relation.rb +66 -0
- data/lib/mongoid/evolvable.rb +1 -1
- data/lib/mongoid/extensions.rb +0 -4
- data/lib/mongoid/extensions/big_decimal.rb +17 -8
- data/lib/mongoid/extensions/date.rb +4 -1
- data/lib/mongoid/extensions/decimal128.rb +3 -3
- data/lib/mongoid/extensions/hash.rb +1 -0
- data/lib/mongoid/extensions/string.rb +4 -3
- data/lib/mongoid/extensions/time.rb +4 -1
- data/lib/mongoid/fields/validators/macro.rb +18 -0
- data/lib/mongoid/findable.rb +2 -2
- data/lib/mongoid/indexable.rb +15 -13
- data/lib/mongoid/interceptable.rb +5 -22
- data/lib/mongoid/matchable.rb +13 -7
- data/lib/mongoid/matchable/all.rb +2 -2
- data/lib/mongoid/matchable/and.rb +3 -3
- data/lib/mongoid/matchable/default.rb +2 -2
- data/lib/mongoid/matchable/elem_match.rb +28 -0
- data/lib/mongoid/matchable/exists.rb +2 -2
- data/lib/mongoid/matchable/gt.rb +4 -2
- data/lib/mongoid/matchable/gte.rb +4 -2
- data/lib/mongoid/matchable/in.rb +2 -2
- data/lib/mongoid/matchable/lt.rb +4 -2
- data/lib/mongoid/matchable/lte.rb +4 -2
- data/lib/mongoid/matchable/ne.rb +2 -2
- data/lib/mongoid/matchable/nin.rb +2 -2
- data/lib/mongoid/matchable/or.rb +3 -3
- data/lib/mongoid/matchable/regexp.rb +3 -3
- data/lib/mongoid/matchable/size.rb +2 -2
- data/lib/mongoid/persistable.rb +3 -5
- data/lib/mongoid/persistable/creatable.rb +2 -2
- data/lib/mongoid/persistable/deletable.rb +1 -1
- data/lib/mongoid/persistable/settable.rb +1 -1
- data/lib/mongoid/persistable/updatable.rb +5 -12
- data/lib/mongoid/persistable/upsertable.rb +1 -1
- data/lib/mongoid/persistence_context.rb +215 -0
- data/lib/mongoid/query_cache.rb +3 -6
- data/lib/mongoid/relations/accessors.rb +3 -0
- data/lib/mongoid/relations/auto_save.rb +12 -4
- data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +4 -4
- data/lib/mongoid/relations/counter_cache.rb +15 -5
- data/lib/mongoid/relations/eager.rb +6 -11
- data/lib/mongoid/relations/eager/base.rb +3 -3
- data/lib/mongoid/relations/eager/has_and_belongs_to_many.rb +2 -2
- data/lib/mongoid/relations/eager/has_many.rb +1 -1
- data/lib/mongoid/relations/embedded/batchable.rb +12 -36
- data/lib/mongoid/relations/embedded/in.rb +13 -1
- data/lib/mongoid/relations/embedded/many.rb +28 -10
- data/lib/mongoid/relations/embedded/one.rb +14 -1
- data/lib/mongoid/relations/macros.rb +9 -1
- data/lib/mongoid/relations/metadata.rb +3 -3
- data/lib/mongoid/relations/options.rb +2 -2
- data/lib/mongoid/relations/proxy.rb +1 -31
- data/lib/mongoid/relations/referenced/in.rb +19 -10
- data/lib/mongoid/relations/referenced/many.rb +23 -17
- data/lib/mongoid/relations/referenced/many_to_many.rb +20 -13
- data/lib/mongoid/relations/referenced/one.rb +15 -1
- data/lib/mongoid/relations/synchronization.rb +11 -11
- data/lib/mongoid/relations/touchable.rb +6 -3
- data/lib/mongoid/reloadable.rb +1 -1
- data/lib/mongoid/serializable.rb +1 -1
- data/lib/mongoid/traversable.rb +1 -1
- data/lib/mongoid/validatable/uniqueness.rb +1 -2
- data/lib/mongoid/version.rb +1 -1
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +14 -3
- data/spec/app/models/album.rb +5 -1
- data/spec/app/models/artist.rb +21 -0
- data/spec/app/models/book.rb +2 -1
- data/spec/app/models/dokument.rb +1 -0
- data/spec/app/models/ordered_post.rb +5 -0
- data/spec/app/models/oscar.rb +1 -2
- data/spec/app/models/page.rb +1 -1
- data/spec/app/models/person.rb +3 -3
- data/spec/app/models/princess.rb +2 -0
- data/spec/app/models/record.rb +1 -0
- data/spec/app/models/subscription.rb +1 -0
- data/spec/app/models/thing.rb +1 -1
- data/spec/config/mongoid.yml +15 -0
- data/spec/mongoid/atomic/modifiers_spec.rb +17 -17
- data/spec/mongoid/atomic_spec.rb +17 -17
- data/spec/mongoid/attributes/nested_spec.rb +14 -14
- data/spec/mongoid/attributes/readonly_spec.rb +87 -44
- data/spec/mongoid/attributes_spec.rb +90 -5
- data/spec/mongoid/cacheable_spec.rb +112 -0
- data/spec/mongoid/changeable_spec.rb +58 -0
- data/spec/mongoid/clients/factory_spec.rb +31 -3
- data/spec/mongoid/clients/options_spec.rb +382 -96
- data/spec/mongoid/clients_spec.rb +243 -101
- data/spec/mongoid/composable_spec.rb +7 -0
- data/spec/mongoid/config_spec.rb +67 -11
- data/spec/mongoid/contextual/atomic_spec.rb +3 -3
- data/spec/mongoid/contextual/mongo_spec.rb +165 -20
- data/spec/mongoid/contextual/none_spec.rb +15 -0
- data/spec/mongoid/copyable_spec.rb +13 -4
- data/spec/mongoid/criteria/modifiable_spec.rb +239 -7
- data/spec/mongoid/criteria/options_spec.rb +29 -0
- data/spec/mongoid/criteria/queryable/aggregable_spec.rb +370 -0
- data/spec/mongoid/criteria/queryable/extensions/array_spec.rb +523 -0
- data/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb +59 -0
- data/spec/mongoid/criteria/queryable/extensions/bignum_spec.rb +58 -0
- data/spec/mongoid/criteria/queryable/extensions/boolean_spec.rb +213 -0
- data/spec/mongoid/criteria/queryable/extensions/date_spec.rb +330 -0
- data/spec/mongoid/criteria/queryable/extensions/date_time_spec.rb +405 -0
- data/spec/mongoid/criteria/queryable/extensions/fixnum_spec.rb +58 -0
- data/spec/mongoid/criteria/queryable/extensions/float_spec.rb +65 -0
- data/spec/mongoid/criteria/queryable/extensions/hash_spec.rb +327 -0
- data/spec/mongoid/criteria/queryable/extensions/integer_spec.rb +65 -0
- data/spec/mongoid/criteria/queryable/extensions/nil_class_spec.rb +77 -0
- data/spec/mongoid/criteria/queryable/extensions/object_spec.rb +108 -0
- data/spec/mongoid/criteria/queryable/extensions/range_spec.rb +309 -0
- data/spec/mongoid/{extensions/origin → criteria/queryable/extensions}/regexp_raw_spec.rb +2 -2
- data/spec/mongoid/criteria/queryable/extensions/regexp_spec.rb +90 -0
- data/spec/mongoid/criteria/queryable/extensions/set_spec.rb +39 -0
- data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +302 -0
- data/spec/mongoid/criteria/queryable/extensions/symbol_spec.rb +167 -0
- data/spec/mongoid/criteria/queryable/extensions/time_spec.rb +376 -0
- data/spec/mongoid/criteria/queryable/extensions/time_with_zone_spec.rb +347 -0
- data/spec/mongoid/criteria/queryable/forwardable_spec.rb +87 -0
- data/spec/mongoid/criteria/queryable/key_spec.rb +52 -0
- data/spec/mongoid/criteria/queryable/mergeable_spec.rb +49 -0
- data/spec/mongoid/criteria/queryable/optional_spec.rb +1799 -0
- data/spec/mongoid/criteria/queryable/options_spec.rb +360 -0
- data/spec/mongoid/criteria/queryable/pipeline_spec.rb +200 -0
- data/spec/mongoid/criteria/queryable/queryable_spec.rb +137 -0
- data/spec/mongoid/criteria/queryable/selectable_spec.rb +4174 -0
- data/spec/mongoid/criteria/queryable/selector_spec.rb +844 -0
- data/spec/mongoid/criteria/queryable/smash_spec.rb +30 -0
- data/spec/mongoid/criteria_spec.rb +152 -21
- data/spec/mongoid/document_spec.rb +37 -88
- data/spec/mongoid/errors/invalid_relation_spec.rb +37 -0
- data/spec/mongoid/errors/mongoid_error_spec.rb +6 -3
- data/spec/mongoid/extensions/big_decimal_spec.rb +320 -18
- data/spec/mongoid/extensions/boolean_spec.rb +14 -0
- data/spec/mongoid/extensions/date_spec.rb +2 -6
- data/spec/mongoid/extensions/date_time_spec.rb +2 -6
- data/spec/mongoid/extensions/decimal128_spec.rb +1 -1
- data/spec/mongoid/extensions/float_spec.rb +8 -1
- data/spec/mongoid/extensions/hash_spec.rb +15 -0
- data/spec/mongoid/extensions/integer_spec.rb +8 -1
- data/spec/mongoid/extensions/object_spec.rb +11 -0
- data/spec/mongoid/extensions/string_spec.rb +21 -0
- data/spec/mongoid/extensions/time_spec.rb +2 -6
- data/spec/mongoid/extensions/time_with_zone_spec.rb +2 -6
- data/spec/mongoid/findable_spec.rb +46 -1
- data/spec/mongoid/indexable_spec.rb +15 -3
- data/spec/mongoid/interceptable_spec.rb +68 -10
- data/spec/mongoid/matchable/all_spec.rb +4 -4
- data/spec/mongoid/matchable/and_spec.rb +10 -10
- data/spec/mongoid/matchable/default_spec.rb +12 -12
- data/spec/mongoid/matchable/elem_match_spec.rb +86 -0
- data/spec/mongoid/matchable/exists_spec.rb +5 -5
- data/spec/mongoid/matchable/gt_spec.rb +18 -7
- data/spec/mongoid/matchable/gte_spec.rb +17 -7
- data/spec/mongoid/matchable/in_spec.rb +5 -5
- data/spec/mongoid/matchable/lt_spec.rb +18 -7
- data/spec/mongoid/matchable/lte_spec.rb +18 -7
- data/spec/mongoid/matchable/ne_spec.rb +5 -5
- data/spec/mongoid/matchable/nin_spec.rb +5 -5
- data/spec/mongoid/matchable/or_spec.rb +7 -7
- data/spec/mongoid/matchable/regexp_spec.rb +5 -5
- data/spec/mongoid/matchable/size_spec.rb +3 -3
- data/spec/mongoid/matchable_spec.rb +173 -53
- data/spec/mongoid/persistable/creatable_spec.rb +7 -2
- data/spec/mongoid/persistable/deletable_spec.rb +16 -1
- data/spec/mongoid/persistable/destroyable_spec.rb +6 -2
- data/spec/mongoid/persistable/savable_spec.rb +35 -30
- data/spec/mongoid/persistable/settable_spec.rb +45 -29
- data/spec/mongoid/persistable/updatable_spec.rb +184 -5
- data/spec/mongoid/persistence_context_spec.rb +680 -0
- data/spec/mongoid/positional_spec.rb +10 -10
- data/spec/mongoid/query_cache_spec.rb +89 -0
- data/spec/mongoid/relations/accessors_spec.rb +1 -1
- data/spec/mongoid/relations/auto_save_spec.rb +39 -6
- data/spec/mongoid/relations/bindings/referenced/many_to_many_spec.rb +4 -4
- data/spec/mongoid/relations/builders_spec.rb +37 -10
- data/spec/mongoid/relations/counter_cache_spec.rb +64 -3
- data/spec/mongoid/relations/eager/has_and_belongs_to_many_spec.rb +16 -0
- data/spec/mongoid/relations/eager_spec.rb +40 -0
- data/spec/mongoid/relations/embedded/many_spec.rb +63 -47
- data/spec/mongoid/relations/embedded/one_spec.rb +2 -1
- data/spec/mongoid/relations/macros_spec.rb +395 -7
- data/spec/mongoid/relations/metadata_spec.rb +15 -1
- data/spec/mongoid/relations/proxy_spec.rb +27 -1
- data/spec/mongoid/relations/referenced/in_spec.rb +41 -1
- data/spec/mongoid/relations/referenced/many_spec.rb +13 -25
- data/spec/mongoid/relations/referenced/many_to_many_spec.rb +14 -26
- data/spec/mongoid/relations/synchronization_spec.rb +48 -2
- data/spec/mongoid/relations/touchable_spec.rb +40 -0
- data/spec/mongoid/reloadable_spec.rb +51 -0
- data/spec/mongoid/serializable_spec.rb +0 -50
- data/spec/mongoid/validatable/presence_spec.rb +1 -1
- data/spec/mongoid/validatable/uniqueness_spec.rb +18 -9
- data/spec/mongoid/validatable_spec.rb +16 -0
- data/spec/spec_helper.rb +20 -11
- metadata +524 -469
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/lib/mongoid/clients/thread_options.rb +0 -19
- data/lib/mongoid/extensions/origin/regexp_raw.rb +0 -43
- metadata.gz.sig +0 -0
data/lib/mongoid/query_cache.rb
CHANGED
@@ -183,7 +183,8 @@ module Mongoid
|
|
183
183
|
end
|
184
184
|
CODE
|
185
185
|
|
186
|
-
|
186
|
+
alias_method "#{method_name}_without_clear_cache", method_name
|
187
|
+
alias_method method_name, "#{method_name}_with_clear_cache"
|
187
188
|
end
|
188
189
|
end
|
189
190
|
end
|
@@ -221,7 +222,7 @@ module Mongoid
|
|
221
222
|
super
|
222
223
|
else
|
223
224
|
unless cursor = cached_cursor
|
224
|
-
server =
|
225
|
+
server = read.select_server(cluster)
|
225
226
|
cursor = CachedCursor.new(view, send_initial_query(server), server)
|
226
227
|
QueryCache.cache_table[cache_key] = cursor
|
227
228
|
end
|
@@ -234,10 +235,6 @@ module Mongoid
|
|
234
235
|
|
235
236
|
private
|
236
237
|
|
237
|
-
def read_or_server_selector
|
238
|
-
respond_to?(:server_selector, true) ? server_selector : read
|
239
|
-
end
|
240
|
-
|
241
238
|
def cached_cursor
|
242
239
|
if limit
|
243
240
|
key = [ collection.namespace, selector, nil, skip, sort, projection, collation ]
|
@@ -229,6 +229,9 @@ module Mongoid
|
|
229
229
|
if value = get_relation(name, metadata, object)
|
230
230
|
if value.respond_to?(:substitute)
|
231
231
|
set_relation(name, value.substitute(object.substitutable))
|
232
|
+
else
|
233
|
+
value = __build__(name, value, metadata)
|
234
|
+
set_relation(name, value.substitute(object.substitutable))
|
232
235
|
end
|
233
236
|
else
|
234
237
|
__build__(name, object.substitutable, metadata)
|
@@ -59,17 +59,25 @@ module Mongoid
|
|
59
59
|
if metadata.autosave? && !metadata.embedded?
|
60
60
|
save_method = :"autosave_documents_for_#{metadata.name}"
|
61
61
|
define_method(save_method) do
|
62
|
-
|
63
62
|
if before_callback_halted?
|
64
63
|
self.before_callback_halted = false
|
65
64
|
else
|
66
65
|
__autosaving__ do
|
67
66
|
if relation = ivar(metadata.name)
|
68
|
-
options = persistence_options || {}
|
69
67
|
if :belongs_to == metadata.macro
|
70
|
-
|
68
|
+
if changed_for_autosave?(relation)
|
69
|
+
relation.with(persistence_context) do |_relation|
|
70
|
+
_relation.save
|
71
|
+
end
|
72
|
+
end
|
71
73
|
else
|
72
|
-
Array(relation).each
|
74
|
+
Array(relation).each do |doc|
|
75
|
+
if changed_for_autosave?(doc)
|
76
|
+
doc.with(persistence_context) do |d|
|
77
|
+
d.save
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
73
81
|
end
|
74
82
|
end
|
75
83
|
end
|
@@ -26,8 +26,8 @@ module Mongoid
|
|
26
26
|
end
|
27
27
|
doc.reset_relation_criteria(metadata.inverse)
|
28
28
|
end
|
29
|
-
base.
|
30
|
-
doc.
|
29
|
+
base._synced[metadata.foreign_key] = true
|
30
|
+
doc._synced[metadata.inverse_foreign_key] = true
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
@@ -45,8 +45,8 @@ module Mongoid
|
|
45
45
|
inverse_keys.delete_one(inverse_record_id(doc))
|
46
46
|
doc.reset_relation_criteria(metadata.inverse)
|
47
47
|
end
|
48
|
-
base.
|
49
|
-
doc.
|
48
|
+
base._synced[metadata.foreign_key] = true
|
49
|
+
doc._synced[metadata.inverse_foreign_key] = true
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
@@ -15,7 +15,9 @@ module Mongoid
|
|
15
15
|
#
|
16
16
|
# @since 4.0.0
|
17
17
|
def reset_counters(*counters)
|
18
|
-
self.class.
|
18
|
+
self.class.with(persistence_context) do |_class|
|
19
|
+
_class.reset_counters(self, *counters)
|
20
|
+
end
|
19
21
|
end
|
20
22
|
|
21
23
|
module ClassMethods
|
@@ -111,12 +113,16 @@ module Mongoid
|
|
111
113
|
original, current = attribute_change(foreign_key)
|
112
114
|
|
113
115
|
unless original.nil?
|
114
|
-
record.class.
|
116
|
+
record.class.with(persistence_context) do |_class|
|
117
|
+
_class.decrement_counter(cache_column, original)
|
118
|
+
end
|
115
119
|
end
|
116
120
|
|
117
121
|
unless current.nil?
|
118
122
|
record[cache_column] = (record[cache_column] || 0) + 1
|
119
|
-
record.class.
|
123
|
+
record.class.with(record.persistence_context) do |_class|
|
124
|
+
_class.increment_counter(cache_column, current) if record.persisted?
|
125
|
+
end
|
120
126
|
end
|
121
127
|
end
|
122
128
|
end
|
@@ -127,7 +133,9 @@ module Mongoid
|
|
127
133
|
record[cache_column] = (record[cache_column] || 0) + 1
|
128
134
|
|
129
135
|
if record.persisted?
|
130
|
-
record.class.
|
136
|
+
record.class.with(record.persistence_context) do |_class|
|
137
|
+
_class.increment_counter(cache_column, record._id)
|
138
|
+
end
|
131
139
|
record.remove_change(cache_column)
|
132
140
|
end
|
133
141
|
end
|
@@ -138,7 +146,9 @@ module Mongoid
|
|
138
146
|
record[cache_column] = (record[cache_column] || 0) - 1 unless record.frozen?
|
139
147
|
|
140
148
|
if record.persisted?
|
141
|
-
record.class.
|
149
|
+
record.class.with(record.persistence_context) do |_class|
|
150
|
+
_class.decrement_counter(cache_column, record._id)
|
151
|
+
end
|
142
152
|
record.remove_change(cache_column)
|
143
153
|
end
|
144
154
|
end
|
@@ -22,19 +22,14 @@ module Mongoid
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def preload(relations, docs)
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
25
|
+
relations.group_by(&:inverse_class_name)
|
26
|
+
.values
|
27
|
+
.each do |associations|
|
28
|
+
associations.group_by(&:relation)
|
29
|
+
.each do |relation, association|
|
30
|
+
relation.eager_load_klass.new(association, docs).run
|
31
31
|
end
|
32
32
|
end
|
33
|
-
grouped_relations.each do |_klass, associations|
|
34
|
-
docs = associations.collect do |_relation, association|
|
35
|
-
_relation.eager_load_klass.new(association, docs).run
|
36
|
-
end.flatten
|
37
|
-
end
|
38
33
|
end
|
39
34
|
end
|
40
35
|
end
|
@@ -48,7 +48,7 @@ module Mongoid
|
|
48
48
|
@loaded = []
|
49
49
|
while shift_metadata
|
50
50
|
preload
|
51
|
-
@loaded << @docs.collect { |d| d.send(@metadata.name) }
|
51
|
+
@loaded << @docs.collect { |d| d.send(@metadata.name) if d.respond_to?(@metadata.name) }
|
52
52
|
end
|
53
53
|
@loaded.flatten
|
54
54
|
end
|
@@ -99,12 +99,12 @@ module Mongoid
|
|
99
99
|
# @example Return a hash with the current documents grouped by key.
|
100
100
|
# loader.grouped_docs
|
101
101
|
#
|
102
|
-
# @return [ Hash ] hash with
|
102
|
+
# @return [ Hash ] hash with grouped documents.
|
103
103
|
#
|
104
104
|
# @since 4.0.0
|
105
105
|
def grouped_docs
|
106
106
|
@grouped_docs[@metadata.name] ||= @docs.group_by do |doc|
|
107
|
-
doc.send(group_by_key)
|
107
|
+
doc.send(group_by_key) if doc.respond_to?(group_by_key)
|
108
108
|
end
|
109
109
|
end
|
110
110
|
|
@@ -17,7 +17,7 @@ module Mongoid
|
|
17
17
|
|
18
18
|
@docs.each do |d|
|
19
19
|
keys = d.send(group_by_key)
|
20
|
-
docs = entries.values_at(*keys)
|
20
|
+
docs = entries.values_at(*keys).compact
|
21
21
|
set_relation(d, docs)
|
22
22
|
end
|
23
23
|
end
|
@@ -31,7 +31,7 @@ module Mongoid
|
|
31
31
|
end
|
32
32
|
|
33
33
|
def set_relation(doc, element)
|
34
|
-
doc.__build__(@metadata.name, element, @metadata)
|
34
|
+
doc.__build__(@metadata.name, element, @metadata) unless doc.blank?
|
35
35
|
end
|
36
36
|
|
37
37
|
def group_by_key
|
@@ -13,15 +13,15 @@ module Mongoid
|
|
13
13
|
# made to the database.
|
14
14
|
#
|
15
15
|
# @example Execute the batch push.
|
16
|
-
# batchable.
|
16
|
+
# batchable.batch_insert([ doc_one, doc_two ])
|
17
17
|
#
|
18
18
|
# @param [ Array<Document> ] docs The docs to add.
|
19
19
|
#
|
20
20
|
# @return [ Array<Hash> ] The inserts.
|
21
21
|
#
|
22
|
-
# @since
|
23
|
-
def
|
24
|
-
|
22
|
+
# @since 3.0.0
|
23
|
+
def batch_insert(docs)
|
24
|
+
execute_batch_insert(docs, "$pushAll")
|
25
25
|
end
|
26
26
|
|
27
27
|
# Clear all of the docs out of the relation in a single swipe.
|
@@ -85,11 +85,11 @@ module Mongoid
|
|
85
85
|
else
|
86
86
|
batch_remove(target.dup)
|
87
87
|
end
|
88
|
-
|
88
|
+
elsif target != docs
|
89
89
|
base.delayed_atomic_sets.clear unless _assigning?
|
90
90
|
docs = normalize_docs(docs).compact
|
91
91
|
target.clear and _unscoped.clear
|
92
|
-
inserts =
|
92
|
+
inserts = execute_batch_insert(docs, "$set")
|
93
93
|
add_atomic_sets(inserts)
|
94
94
|
end
|
95
95
|
end
|
@@ -122,50 +122,26 @@ module Mongoid
|
|
122
122
|
# @api private
|
123
123
|
#
|
124
124
|
# @example Perform a batch operation.
|
125
|
-
# batchable.
|
125
|
+
# batchable.execute_batch(docs, "$set")
|
126
126
|
#
|
127
127
|
# @param [ Array<Document> ] docs The docs to persist.
|
128
128
|
# @param [ String ] operation The atomic operation.
|
129
129
|
#
|
130
130
|
# @return [ Array<Hash> ] The inserts.
|
131
131
|
#
|
132
|
-
# @since
|
133
|
-
def
|
132
|
+
# @since 3.0.0
|
133
|
+
def execute_batch_insert(docs, operation)
|
134
134
|
self.inserts_valid = true
|
135
135
|
inserts = pre_process_batch_insert(docs)
|
136
136
|
if insertable?
|
137
137
|
collection.find(selector).update_one(
|
138
|
-
positionally(selector,
|
138
|
+
positionally(selector, operation => { path => inserts })
|
139
139
|
)
|
140
140
|
post_process_batch_insert(docs)
|
141
141
|
end
|
142
142
|
inserts
|
143
143
|
end
|
144
144
|
|
145
|
-
# Perform a batch persist of the provided documents with $push and $each.
|
146
|
-
#
|
147
|
-
# @api private
|
148
|
-
#
|
149
|
-
# @example Perform a batch push.
|
150
|
-
# batchable.execute_batch_push(docs)
|
151
|
-
#
|
152
|
-
# @param [ Array<Document> ] docs The docs to persist.
|
153
|
-
# @param [ String ] operation The atomic operation.
|
154
|
-
#
|
155
|
-
# @return [ Array<Hash> ] The inserts.
|
156
|
-
#
|
157
|
-
# @since 5.4.0
|
158
|
-
def execute_batch_push(docs)
|
159
|
-
self.inserts_valid = true
|
160
|
-
pushes = pre_process_batch_insert(docs)
|
161
|
-
if insertable?
|
162
|
-
collection.find(selector).update_one(
|
163
|
-
positionally(selector, '$push' => { path => { '$each' => pushes } }))
|
164
|
-
post_process_batch_insert(docs)
|
165
|
-
end
|
166
|
-
pushes
|
167
|
-
end
|
168
|
-
|
169
145
|
# Are we in a state to be able to batch insert?
|
170
146
|
#
|
171
147
|
# @api private
|
@@ -303,7 +279,7 @@ module Mongoid
|
|
303
279
|
self.inserts_valid = false
|
304
280
|
end
|
305
281
|
end
|
306
|
-
doc.
|
282
|
+
doc.send(:as_attributes)
|
307
283
|
end
|
308
284
|
end
|
309
285
|
|
@@ -332,7 +308,7 @@ module Mongoid
|
|
332
308
|
_unscoped.delete_one(doc)
|
333
309
|
unbind_one(doc)
|
334
310
|
execute_callback :after_remove, doc
|
335
|
-
doc.
|
311
|
+
doc.send(:as_attributes)
|
336
312
|
end
|
337
313
|
end
|
338
314
|
|
@@ -8,6 +8,18 @@ module Mongoid
|
|
8
8
|
# multiple documents.
|
9
9
|
class In < Relations::One
|
10
10
|
|
11
|
+
# The allowed options when defining this relation.
|
12
|
+
#
|
13
|
+
# @return [ Array<Symbol> ] The allowed options when defining this relation.
|
14
|
+
#
|
15
|
+
# @since 6.0.0
|
16
|
+
VALID_OPTIONS = [
|
17
|
+
:autobuild,
|
18
|
+
:cyclic,
|
19
|
+
:polymorphic,
|
20
|
+
:touch
|
21
|
+
].freeze
|
22
|
+
|
11
23
|
# Instantiate a new embedded_in relation.
|
12
24
|
#
|
13
25
|
# @example Create the new relation.
|
@@ -207,7 +219,7 @@ module Mongoid
|
|
207
219
|
#
|
208
220
|
# @since 2.1.0
|
209
221
|
def valid_options
|
210
|
-
|
222
|
+
VALID_OPTIONS
|
211
223
|
end
|
212
224
|
|
213
225
|
# Get the default validation setting for the relation. Determines if
|
@@ -10,6 +10,23 @@ module Mongoid
|
|
10
10
|
class Many < Relations::Many
|
11
11
|
include Batchable
|
12
12
|
|
13
|
+
# The allowed options when defining this relation.
|
14
|
+
#
|
15
|
+
# @return [ Array<Symbol> ] The allowed options when defining this relation.
|
16
|
+
#
|
17
|
+
# @since 6.0.0
|
18
|
+
VALID_OPTIONS = [
|
19
|
+
:as,
|
20
|
+
:cascade_callbacks,
|
21
|
+
:cyclic,
|
22
|
+
:order,
|
23
|
+
:store_as,
|
24
|
+
:before_add,
|
25
|
+
:after_add,
|
26
|
+
:before_remove,
|
27
|
+
:after_remove
|
28
|
+
].freeze
|
29
|
+
|
13
30
|
# Appends a document or array of documents to the relation. Will set
|
14
31
|
# the parent and update the index in the process.
|
15
32
|
#
|
@@ -41,11 +58,7 @@ module Mongoid
|
|
41
58
|
#
|
42
59
|
# @since 2.0.0.rc.1
|
43
60
|
def as_document
|
44
|
-
|
45
|
-
_unscoped.each do |doc|
|
46
|
-
attributes.push(doc.as_document)
|
47
|
-
end
|
48
|
-
attributes
|
61
|
+
as_attributes.collect { |attrs| BSON::Document.new(attrs) }
|
49
62
|
end
|
50
63
|
|
51
64
|
# Appends an array of documents to the relation. Performs a batch
|
@@ -60,7 +73,7 @@ module Mongoid
|
|
60
73
|
#
|
61
74
|
# @since 2.4.0
|
62
75
|
def concat(docs)
|
63
|
-
|
76
|
+
batch_insert(docs) unless docs.empty?
|
64
77
|
self
|
65
78
|
end
|
66
79
|
|
@@ -496,6 +509,14 @@ module Mongoid
|
|
496
509
|
@_unscoped = docs
|
497
510
|
end
|
498
511
|
|
512
|
+
def as_attributes
|
513
|
+
attributes = []
|
514
|
+
_unscoped.each do |doc|
|
515
|
+
attributes.push(doc.as_document)
|
516
|
+
end
|
517
|
+
attributes
|
518
|
+
end
|
519
|
+
|
499
520
|
class << self
|
500
521
|
|
501
522
|
# Return the builder that is responsible for generating the documents
|
@@ -617,10 +638,7 @@ module Mongoid
|
|
617
638
|
#
|
618
639
|
# @since 2.1.0
|
619
640
|
def valid_options
|
620
|
-
|
621
|
-
:as, :cascade_callbacks, :cyclic, :order, :store_as,
|
622
|
-
:before_add, :after_add, :before_remove, :after_remove
|
623
|
-
]
|
641
|
+
VALID_OPTIONS
|
624
642
|
end
|
625
643
|
|
626
644
|
# Get the default validation setting for the relation. Determines if
|
@@ -7,6 +7,19 @@ module Mongoid
|
|
7
7
|
# relations.
|
8
8
|
class One < Relations::One
|
9
9
|
|
10
|
+
# The allowed options when defining this relation.
|
11
|
+
#
|
12
|
+
# @return [ Array<Symbol> ] The allowed options when defining this relation.
|
13
|
+
#
|
14
|
+
# @since 6.0.0
|
15
|
+
VALID_OPTIONS = [
|
16
|
+
:autobuild,
|
17
|
+
:as,
|
18
|
+
:cascade_callbacks,
|
19
|
+
:cyclic,
|
20
|
+
:store_as
|
21
|
+
].freeze
|
22
|
+
|
10
23
|
# Instantiate a new embeds_one relation.
|
11
24
|
#
|
12
25
|
# @example Create the new proxy.
|
@@ -200,7 +213,7 @@ module Mongoid
|
|
200
213
|
#
|
201
214
|
# @since 2.1.0
|
202
215
|
def valid_options
|
203
|
-
|
216
|
+
VALID_OPTIONS
|
204
217
|
end
|
205
218
|
|
206
219
|
# Get the default validation setting for the relation. Determines if
|