mongoid 7.0.13 → 9.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 +4 -4
- data/CHANGELOG.md +41 -41
- data/LICENSE +1 -1
- data/README.md +29 -25
- data/Rakefile +150 -40
- data/lib/config/locales/en.yml +364 -108
- data/lib/mongoid/association/accessors.rb +205 -107
- data/lib/mongoid/association/bindable.rb +102 -56
- data/lib/mongoid/association/builders.rb +24 -26
- data/lib/mongoid/association/constrainable.rb +8 -11
- data/lib/mongoid/association/depending.rb +65 -59
- data/lib/mongoid/association/eager.rb +203 -0
- data/lib/mongoid/association/eager_load/discriminated_inclusion.rb +88 -0
- data/lib/mongoid/association/eager_load/embedded_distributor.rb +151 -0
- data/lib/mongoid/association/eager_load/inclusion.rb +173 -0
- data/lib/mongoid/association/eager_load/inclusion_tree.rb +96 -0
- data/lib/mongoid/association/eager_load/lookup_pipeline.rb +121 -0
- data/lib/mongoid/association/eager_load/polymorphic_preloader.rb +61 -0
- data/lib/mongoid/association/eager_load/polymorphic_targets.rb +151 -0
- data/lib/mongoid/association/eager_loadable.rb +135 -15
- data/lib/mongoid/association/embedded/batchable.rb +87 -75
- data/lib/mongoid/association/embedded/cyclic.rb +16 -25
- data/lib/mongoid/association/embedded/eager.rb +18 -0
- data/lib/mongoid/association/embedded/embedded_in/binding.rb +30 -14
- data/lib/mongoid/association/embedded/embedded_in/buildable.rb +10 -7
- data/lib/mongoid/association/embedded/embedded_in/proxy.rb +39 -32
- data/lib/mongoid/association/embedded/embedded_in.rb +37 -46
- data/lib/mongoid/association/embedded/embeds_many/binding.rb +7 -11
- data/lib/mongoid/association/embedded/embeds_many/buildable.rb +11 -7
- data/lib/mongoid/association/embedded/embeds_many/proxy.rb +309 -243
- data/lib/mongoid/association/embedded/embeds_many.rb +32 -56
- data/lib/mongoid/association/embedded/embeds_one/binding.rb +6 -11
- data/lib/mongoid/association/embedded/embeds_one/buildable.rb +25 -11
- data/lib/mongoid/association/embedded/embeds_one/proxy.rb +145 -53
- data/lib/mongoid/association/embedded/embeds_one.rb +30 -50
- data/lib/mongoid/association/embedded.rb +3 -0
- data/lib/mongoid/association/macros.rb +97 -56
- data/lib/mongoid/association/many.rb +99 -36
- data/lib/mongoid/association/marshalable.rb +5 -7
- data/lib/mongoid/association/nested/many.rb +86 -47
- data/lib/mongoid/association/nested/nested_buildable.rb +58 -21
- data/lib/mongoid/association/nested/one.rb +81 -36
- data/lib/mongoid/association/nested.rb +4 -5
- data/lib/mongoid/association/one.rb +5 -14
- data/lib/mongoid/association/options.rb +48 -53
- data/lib/mongoid/association/proxy.rb +65 -54
- data/lib/mongoid/association/referenced/auto_save.rb +59 -20
- data/lib/mongoid/association/referenced/belongs_to/binding.rb +21 -22
- data/lib/mongoid/association/referenced/belongs_to/buildable.rb +32 -11
- data/lib/mongoid/association/referenced/belongs_to/eager.rb +47 -6
- data/lib/mongoid/association/referenced/belongs_to/proxy.rb +35 -42
- data/lib/mongoid/association/referenced/belongs_to.rb +77 -77
- data/lib/mongoid/association/referenced/counter_cache.rb +24 -35
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/binding.rb +26 -15
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb +12 -7
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb +3 -4
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb +202 -133
- data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +81 -83
- data/lib/mongoid/association/referenced/has_many/binding.rb +3 -7
- data/lib/mongoid/association/referenced/has_many/buildable.rb +15 -8
- data/lib/mongoid/association/referenced/has_many/eager.rb +3 -4
- data/lib/mongoid/association/referenced/has_many/enumerable.rb +601 -456
- data/lib/mongoid/association/referenced/has_many/proxy.rb +344 -216
- data/lib/mongoid/association/referenced/has_many.rb +58 -74
- data/lib/mongoid/association/referenced/has_many_through/eager.rb +99 -0
- data/lib/mongoid/association/referenced/has_many_through/proxy.rb +68 -0
- data/lib/mongoid/association/referenced/has_many_through.rb +201 -0
- data/lib/mongoid/association/referenced/has_one/binding.rb +4 -10
- data/lib/mongoid/association/referenced/has_one/buildable.rb +29 -17
- data/lib/mongoid/association/referenced/has_one/eager.rb +4 -6
- data/lib/mongoid/association/referenced/has_one/proxy.rb +51 -47
- data/lib/mongoid/association/referenced/has_one.rb +45 -55
- data/lib/mongoid/association/referenced/has_one_through/eager.rb +65 -0
- data/lib/mongoid/association/referenced/has_one_through/proxy.rb +30 -0
- data/lib/mongoid/association/referenced/has_one_through.rb +167 -0
- data/lib/mongoid/association/referenced/syncable.rb +44 -62
- data/lib/mongoid/association/referenced/with_polymorphic_criteria.rb +41 -0
- data/lib/mongoid/association/referenced.rb +4 -0
- data/lib/mongoid/association/reflections.rb +11 -12
- data/lib/mongoid/association/relatable.rb +173 -146
- data/lib/mongoid/association.rb +35 -40
- data/lib/mongoid/atomic/modifiers.rb +91 -74
- data/lib/mongoid/atomic/paths/embedded/many.rb +25 -11
- data/lib/mongoid/atomic/paths/embedded/one.rb +6 -10
- data/lib/mongoid/atomic/paths/embedded.rb +5 -8
- data/lib/mongoid/atomic/paths/root.rb +5 -10
- data/lib/mongoid/atomic/paths.rb +4 -3
- data/lib/mongoid/atomic.rb +41 -89
- data/lib/mongoid/atomic_update_preparer.rb +89 -0
- data/lib/mongoid/attributes/dynamic.rb +12 -27
- data/lib/mongoid/attributes/embedded.rb +34 -0
- data/lib/mongoid/attributes/nested.rb +22 -25
- data/lib/mongoid/attributes/processing.rb +51 -34
- data/lib/mongoid/attributes/projector.rb +107 -0
- data/lib/mongoid/attributes/readonly.rb +15 -17
- data/lib/mongoid/attributes.rb +119 -126
- data/lib/mongoid/cacheable.rb +30 -14
- data/lib/mongoid/changeable.rb +196 -99
- data/lib/mongoid/clients/factory.rb +73 -17
- data/lib/mongoid/clients/options.rb +140 -19
- data/lib/mongoid/clients/sessions.rb +239 -87
- data/lib/mongoid/clients/storage_options.rb +42 -17
- data/lib/mongoid/clients/validators/storage.rb +6 -25
- data/lib/mongoid/clients/validators.rb +3 -2
- data/lib/mongoid/clients.rb +44 -22
- data/lib/mongoid/collection_configurable.rb +63 -0
- data/lib/mongoid/composable.rb +40 -39
- data/lib/mongoid/config/defaults.rb +46 -0
- data/lib/mongoid/config/encryption.rb +228 -0
- data/lib/mongoid/config/environment.rb +26 -13
- data/lib/mongoid/config/introspection.rb +150 -0
- data/lib/mongoid/config/options.rb +37 -26
- data/lib/mongoid/config/validators/async_query_executor.rb +31 -0
- data/lib/mongoid/config/validators/client.rb +20 -37
- data/lib/mongoid/config/validators/option.rb +5 -7
- data/lib/mongoid/config/validators.rb +5 -3
- data/lib/mongoid/config.rb +418 -60
- data/lib/mongoid/contextual/aggregable/memory.rb +45 -28
- data/lib/mongoid/contextual/aggregable/mongo.rb +49 -47
- data/lib/mongoid/contextual/aggregable/none.rb +64 -0
- data/lib/mongoid/contextual/aggregable.rb +16 -0
- data/lib/mongoid/contextual/atomic.rb +126 -45
- data/lib/mongoid/contextual/command.rb +3 -6
- data/lib/mongoid/contextual/map_reduce.rb +23 -49
- data/lib/mongoid/contextual/memory.rb +448 -121
- data/lib/mongoid/contextual/mongo/documents_loader.rb +177 -0
- data/lib/mongoid/contextual/mongo.rb +707 -351
- data/lib/mongoid/contextual/none.rb +249 -33
- data/lib/mongoid/contextual/queryable.rb +6 -6
- data/lib/mongoid/contextual.rb +40 -15
- data/lib/mongoid/copyable.rb +45 -21
- data/lib/mongoid/criteria/findable.rb +65 -27
- data/lib/mongoid/criteria/includable.rb +97 -45
- data/lib/mongoid/criteria/inspectable.rb +6 -6
- data/lib/mongoid/criteria/marshalable.rb +15 -10
- data/lib/mongoid/criteria/modifiable.rb +8 -28
- data/lib/mongoid/criteria/options.rb +2 -5
- data/lib/mongoid/criteria/permission.rb +40 -37
- data/lib/mongoid/criteria/queryable/aggregable.rb +13 -24
- data/lib/mongoid/criteria/queryable/expandable.rb +64 -0
- data/lib/mongoid/criteria/queryable/extensions/array.rb +14 -50
- data/lib/mongoid/criteria/queryable/extensions/big_decimal.rb +29 -13
- data/lib/mongoid/criteria/queryable/extensions/boolean.rb +6 -9
- data/lib/mongoid/criteria/queryable/extensions/date.rb +13 -16
- data/lib/mongoid/criteria/queryable/extensions/date_time.rb +14 -18
- data/lib/mongoid/criteria/queryable/extensions/hash.rb +5 -44
- data/lib/mongoid/criteria/queryable/extensions/nil_class.rb +23 -24
- data/lib/mongoid/criteria/queryable/extensions/numeric.rb +31 -29
- data/lib/mongoid/criteria/queryable/extensions/object.rb +15 -37
- data/lib/mongoid/criteria/queryable/extensions/range.rb +52 -22
- data/lib/mongoid/criteria/queryable/extensions/regexp.rb +22 -25
- data/lib/mongoid/criteria/queryable/extensions/set.rb +8 -10
- data/lib/mongoid/criteria/queryable/extensions/string.rb +24 -41
- data/lib/mongoid/criteria/queryable/extensions/symbol.rb +10 -28
- data/lib/mongoid/criteria/queryable/extensions/time.rb +11 -14
- data/lib/mongoid/criteria/queryable/extensions/time_with_zone.rb +12 -20
- data/lib/mongoid/criteria/queryable/extensions.rb +19 -24
- data/lib/mongoid/criteria/queryable/key.rb +49 -30
- data/lib/mongoid/criteria/queryable/macroable.rb +3 -6
- data/lib/mongoid/criteria/queryable/mergeable.rb +203 -46
- data/lib/mongoid/criteria/queryable/optional.rb +32 -81
- data/lib/mongoid/criteria/queryable/options.rb +24 -29
- data/lib/mongoid/criteria/queryable/pipeline.rb +11 -23
- data/lib/mongoid/criteria/queryable/selectable.rb +558 -242
- data/lib/mongoid/criteria/queryable/selector.rb +134 -43
- data/lib/mongoid/criteria/queryable/smash.rb +41 -21
- data/lib/mongoid/criteria/queryable/storable.rb +219 -0
- data/lib/mongoid/criteria/queryable.rb +41 -27
- data/lib/mongoid/criteria/scopable.rb +37 -30
- data/lib/mongoid/criteria/translator.rb +43 -0
- data/lib/mongoid/criteria.rb +257 -152
- data/lib/mongoid/deprecable.rb +78 -0
- data/lib/mongoid/deprecation.rb +25 -0
- data/lib/mongoid/document.rb +277 -171
- data/lib/mongoid/encryptable.rb +98 -0
- data/lib/mongoid/equality.rb +29 -31
- data/lib/mongoid/errors/ambiguous_relationship.rb +6 -9
- data/lib/mongoid/errors/attribute_not_loaded.rb +31 -0
- data/lib/mongoid/errors/callback.rb +3 -6
- data/lib/mongoid/errors/config_redactor.rb +43 -0
- data/lib/mongoid/errors/create_collection_failure.rb +31 -0
- data/lib/mongoid/errors/criteria_argument_required.rb +16 -0
- data/lib/mongoid/errors/delete_restriction.rb +11 -16
- data/lib/mongoid/errors/document_not_destroyed.rb +5 -10
- data/lib/mongoid/errors/document_not_found.rb +37 -27
- data/lib/mongoid/errors/drop_collection_failure.rb +25 -0
- data/lib/mongoid/errors/empty_config_file.rb +23 -0
- data/lib/mongoid/errors/immutable_attribute.rb +24 -0
- data/lib/mongoid/errors/in_memory_collation_not_supported.rb +3 -6
- data/lib/mongoid/errors/in_memory_regexp_timeout.rb +26 -0
- data/lib/mongoid/errors/invalid_around_callback.rb +16 -0
- data/lib/mongoid/errors/invalid_async_query_executor.rb +23 -0
- data/lib/mongoid/errors/invalid_auto_encryption_configuration.rb +30 -0
- data/lib/mongoid/errors/invalid_collection.rb +3 -3
- data/lib/mongoid/errors/invalid_config_file.rb +23 -0
- data/lib/mongoid/errors/invalid_config_option.rb +5 -8
- data/lib/mongoid/errors/invalid_dependent_strategy.rb +9 -13
- data/lib/mongoid/errors/invalid_discriminator_key_target.rb +23 -0
- data/lib/mongoid/errors/invalid_dot_dollar_assignment.rb +21 -0
- data/lib/mongoid/errors/invalid_elem_match_operator.rb +29 -0
- data/lib/mongoid/errors/invalid_estimated_count_criteria.rb +24 -0
- data/lib/mongoid/errors/invalid_estimated_count_scoping.rb +24 -0
- data/lib/mongoid/errors/invalid_expression_operator.rb +24 -0
- data/lib/mongoid/errors/invalid_field.rb +10 -11
- data/lib/mongoid/errors/invalid_field_operator.rb +29 -0
- data/lib/mongoid/errors/invalid_field_option.rb +4 -7
- data/lib/mongoid/errors/invalid_field_type.rb +24 -0
- data/lib/mongoid/errors/invalid_find.rb +3 -6
- data/lib/mongoid/errors/invalid_global_executor_concurrency.rb +20 -0
- data/lib/mongoid/errors/invalid_includes.rb +5 -8
- data/lib/mongoid/errors/invalid_index.rb +3 -6
- data/lib/mongoid/errors/invalid_options.rb +5 -8
- data/lib/mongoid/errors/invalid_path.rb +3 -6
- data/lib/mongoid/errors/invalid_persistence_option.rb +6 -11
- data/lib/mongoid/errors/invalid_query.rb +36 -0
- data/lib/mongoid/errors/invalid_relation.rb +15 -22
- data/lib/mongoid/errors/invalid_relation_option.rb +8 -11
- data/lib/mongoid/errors/invalid_scope.rb +3 -6
- data/lib/mongoid/errors/invalid_session_nesting.rb +14 -0
- data/lib/mongoid/errors/invalid_set_polymorphic_relation.rb +8 -9
- data/lib/mongoid/errors/invalid_storage_options.rb +4 -7
- data/lib/mongoid/errors/invalid_time.rb +3 -6
- data/lib/mongoid/errors/invalid_transaction_nesting.rb +14 -0
- data/lib/mongoid/errors/inverse_not_found.rb +4 -7
- data/lib/mongoid/errors/mixed_client_configuration.rb +4 -7
- data/lib/mongoid/errors/mixed_relations.rb +3 -5
- data/lib/mongoid/errors/mongoid_error.rb +13 -23
- data/lib/mongoid/errors/nested_attributes_metadata_not_found.rb +4 -7
- data/lib/mongoid/errors/no_client_config.rb +5 -8
- data/lib/mongoid/errors/no_client_database.rb +5 -8
- data/lib/mongoid/errors/no_client_hosts.rb +5 -8
- data/lib/mongoid/errors/no_clients_config.rb +3 -6
- data/lib/mongoid/errors/no_default_client.rb +4 -7
- data/lib/mongoid/errors/no_encryption_schema.rb +28 -0
- data/lib/mongoid/errors/no_environment.rb +3 -6
- data/lib/mongoid/errors/no_map_reduce_output.rb +3 -6
- data/lib/mongoid/errors/no_metadata.rb +3 -6
- data/lib/mongoid/errors/no_parent.rb +3 -6
- data/lib/mongoid/errors/readonly_association.rb +29 -0
- data/lib/mongoid/errors/readonly_attribute.rb +4 -7
- data/lib/mongoid/errors/readonly_document.rb +4 -9
- data/lib/mongoid/errors/rollback.rb +13 -0
- data/lib/mongoid/errors/scope_overwrite.rb +4 -4
- data/lib/mongoid/errors/sessions_not_supported.rb +14 -0
- data/lib/mongoid/errors/too_many_nested_attribute_records.rb +4 -3
- data/lib/mongoid/errors/transaction_error.rb +22 -0
- data/lib/mongoid/errors/transactions_not_supported.rb +14 -0
- data/lib/mongoid/errors/unknown_attribute.rb +4 -7
- data/lib/mongoid/errors/unknown_model.rb +3 -6
- data/lib/mongoid/errors/unrecognized_model_alias.rb +53 -0
- data/lib/mongoid/errors/unrecognized_resolver.rb +27 -0
- data/lib/mongoid/errors/unregistered_class.rb +47 -0
- data/lib/mongoid/errors/unsaved_document.rb +4 -4
- data/lib/mongoid/errors/unsupported_isolation_level.rb +22 -0
- data/lib/mongoid/errors/unsupported_javascript.rb +3 -6
- data/lib/mongoid/errors/validations.rb +5 -5
- data/lib/mongoid/errors.rb +82 -51
- data/lib/mongoid/evolvable.rb +4 -7
- data/lib/mongoid/extensions/array.rb +32 -55
- data/lib/mongoid/extensions/big_decimal.rb +45 -24
- data/lib/mongoid/extensions/binary.rb +42 -0
- data/lib/mongoid/extensions/boolean.rb +12 -8
- data/lib/mongoid/extensions/bson_document.rb +28 -0
- data/lib/mongoid/extensions/date.rb +38 -30
- data/lib/mongoid/extensions/date_time.rb +8 -22
- data/lib/mongoid/extensions/decimal128.rb +4 -8
- data/lib/mongoid/extensions/false_class.rb +11 -13
- data/lib/mongoid/extensions/float.rb +15 -19
- data/lib/mongoid/extensions/hash.rb +31 -116
- data/lib/mongoid/extensions/integer.rb +17 -21
- data/lib/mongoid/extensions/module.rb +6 -7
- data/lib/mongoid/extensions/nil_class.rb +6 -7
- data/lib/mongoid/extensions/object.rb +42 -84
- data/lib/mongoid/extensions/object_id.rb +5 -11
- data/lib/mongoid/extensions/range.rb +49 -25
- data/lib/mongoid/extensions/raw_value.rb +31 -0
- data/lib/mongoid/extensions/regexp.rb +16 -10
- data/lib/mongoid/extensions/set.rb +27 -15
- data/lib/mongoid/extensions/string.rb +40 -67
- data/lib/mongoid/extensions/symbol.rb +11 -26
- data/lib/mongoid/extensions/time.rb +49 -47
- data/lib/mongoid/extensions/time_with_zone.rb +30 -12
- data/lib/mongoid/extensions/true_class.rb +11 -13
- data/lib/mongoid/extensions/vector.rb +64 -0
- data/lib/mongoid/extensions.rb +28 -41
- data/lib/mongoid/factory.rb +205 -28
- data/lib/mongoid/field_readable.rb +69 -0
- data/lib/mongoid/fields/encrypted.rb +47 -0
- data/lib/mongoid/fields/foreign_key.rb +48 -35
- data/lib/mongoid/fields/localized.rb +33 -21
- data/lib/mongoid/fields/standard.rb +31 -48
- data/lib/mongoid/fields/validators/macro.rb +48 -45
- data/lib/mongoid/fields/validators.rb +3 -2
- data/lib/mongoid/fields.rb +499 -117
- data/lib/mongoid/findable.rb +174 -64
- data/lib/mongoid/identifiable.rb +28 -0
- data/lib/mongoid/indexable/specification.rb +76 -39
- data/lib/mongoid/indexable/validators/options.rb +36 -43
- data/lib/mongoid/indexable.rb +57 -58
- data/lib/mongoid/inspectable.rb +37 -15
- data/lib/mongoid/interceptable.rb +235 -66
- data/lib/mongoid/loadable.rb +146 -0
- data/lib/mongoid/loggable.rb +12 -18
- data/lib/mongoid/matchable.rb +7 -154
- data/lib/mongoid/matcher/all.rb +34 -0
- data/lib/mongoid/matcher/and.rb +32 -0
- data/lib/mongoid/matcher/bits.rb +65 -0
- data/lib/mongoid/matcher/bits_all_clear.rb +39 -0
- data/lib/mongoid/matcher/bits_all_set.rb +39 -0
- data/lib/mongoid/matcher/bits_any_clear.rb +39 -0
- data/lib/mongoid/matcher/bits_any_set.rb +39 -0
- data/lib/mongoid/matcher/elem_match.rb +49 -0
- data/lib/mongoid/matcher/elem_match_expression.rb +29 -0
- data/lib/mongoid/matcher/eq.rb +23 -0
- data/lib/mongoid/matcher/eq_impl.rb +79 -0
- data/lib/mongoid/matcher/eq_impl_with_regexp.rb +33 -0
- data/lib/mongoid/matcher/exists.rb +27 -0
- data/lib/mongoid/matcher/expression.rb +43 -0
- data/lib/mongoid/matcher/expression_operator.rb +31 -0
- data/lib/mongoid/matcher/field_expression.rb +74 -0
- data/lib/mongoid/matcher/field_operator.rb +76 -0
- data/lib/mongoid/matcher/gt.rb +29 -0
- data/lib/mongoid/matcher/gte.rb +29 -0
- data/lib/mongoid/matcher/in.rb +37 -0
- data/lib/mongoid/matcher/lt.rb +29 -0
- data/lib/mongoid/matcher/lte.rb +29 -0
- data/lib/mongoid/matcher/mod.rb +29 -0
- data/lib/mongoid/matcher/ne.rb +28 -0
- data/lib/mongoid/matcher/nin.rb +23 -0
- data/lib/mongoid/matcher/nor.rb +34 -0
- data/lib/mongoid/matcher/not.rb +42 -0
- data/lib/mongoid/matcher/or.rb +32 -0
- data/lib/mongoid/matcher/regex.rb +61 -0
- data/lib/mongoid/matcher/regexp_budget.rb +372 -0
- data/lib/mongoid/matcher/size.rb +39 -0
- data/lib/mongoid/matcher/type.rb +119 -0
- data/lib/mongoid/matcher.rb +141 -0
- data/lib/mongoid/model_resolver.rb +155 -0
- data/lib/mongoid/persistable/creatable.rb +50 -36
- data/lib/mongoid/persistable/deletable.rb +18 -30
- data/lib/mongoid/persistable/destroyable.rb +38 -14
- data/lib/mongoid/persistable/incrementable.rb +8 -10
- data/lib/mongoid/persistable/logical.rb +7 -10
- data/lib/mongoid/persistable/maxable.rb +35 -0
- data/lib/mongoid/persistable/minable.rb +35 -0
- data/lib/mongoid/persistable/multipliable.rb +34 -0
- data/lib/mongoid/persistable/poppable.rb +5 -9
- data/lib/mongoid/persistable/pullable.rb +6 -12
- data/lib/mongoid/persistable/pushable.rb +8 -14
- data/lib/mongoid/persistable/renamable.rb +11 -10
- data/lib/mongoid/persistable/savable.rb +16 -11
- data/lib/mongoid/persistable/settable.rb +7 -18
- data/lib/mongoid/persistable/unsettable.rb +11 -12
- data/lib/mongoid/persistable/updatable.rb +113 -35
- data/lib/mongoid/persistable/upsertable.rb +37 -12
- data/lib/mongoid/persistable.rb +169 -60
- data/lib/mongoid/persistence_context.rb +189 -75
- data/lib/mongoid/pluckable.rb +130 -0
- data/lib/mongoid/positional.rb +7 -15
- data/lib/mongoid/railtie.rb +36 -32
- data/lib/mongoid/railties/bson_object_id_serializer.rb +46 -0
- data/lib/mongoid/railties/console_sandbox.rb +40 -0
- data/lib/mongoid/railties/controller_runtime.rb +34 -20
- data/lib/mongoid/railties/database.rake +53 -29
- data/lib/mongoid/reloadable.rb +56 -45
- data/lib/mongoid/scopable.rb +50 -79
- data/lib/mongoid/search_indexable.rb +538 -0
- data/lib/mongoid/selectable.rb +8 -19
- data/lib/mongoid/serializable.rb +46 -40
- data/lib/mongoid/shardable.rb +102 -23
- data/lib/mongoid/stateful.rb +67 -26
- data/lib/mongoid/stringified_symbol.rb +61 -0
- data/lib/mongoid/tasks/database.rake +46 -15
- data/lib/mongoid/tasks/database.rb +204 -31
- data/lib/mongoid/tasks/encryption.rake +59 -0
- data/lib/mongoid/tasks/encryption.rb +103 -0
- data/lib/mongoid/threaded/lifecycle.rb +13 -34
- data/lib/mongoid/threaded.rb +353 -108
- data/lib/mongoid/timestamps/created/short.rb +4 -3
- data/lib/mongoid/timestamps/created.rb +15 -7
- data/lib/mongoid/timestamps/short.rb +2 -1
- data/lib/mongoid/timestamps/timeless.rb +127 -29
- data/lib/mongoid/timestamps/updated/short.rb +4 -3
- data/lib/mongoid/timestamps/updated.rb +6 -9
- data/lib/mongoid/timestamps.rb +7 -7
- data/lib/mongoid/touchable.rb +176 -40
- data/lib/mongoid/traversable.rb +309 -106
- data/lib/mongoid/utils.rb +52 -0
- data/lib/mongoid/validatable/associated.rb +115 -22
- data/lib/mongoid/validatable/format.rb +2 -2
- data/lib/mongoid/validatable/length.rb +2 -2
- data/lib/mongoid/validatable/localizable.rb +3 -6
- data/lib/mongoid/validatable/macros.rb +25 -14
- data/lib/mongoid/validatable/numericality.rb +40 -0
- data/lib/mongoid/validatable/presence.rb +15 -21
- data/lib/mongoid/validatable/queryable.rb +9 -4
- data/lib/mongoid/validatable/uniqueness.rb +40 -53
- data/lib/mongoid/validatable.rb +41 -52
- data/lib/mongoid/version.rb +6 -2
- data/lib/mongoid/warnings.rb +40 -0
- data/lib/mongoid.rb +105 -44
- data/lib/rails/generators/mongoid/config/config_generator.rb +18 -5
- data/lib/rails/generators/mongoid/config/templates/mongoid.rb +24 -0
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +51 -56
- data/lib/rails/generators/mongoid/model/model_generator.rb +10 -8
- data/lib/rails/generators/mongoid_generator.rb +17 -7
- data/lib/rails/mongoid.rb +4 -32
- data/spec/README.md +19 -4
- data/spec/config/mongoid.yml +17 -1
- data/spec/config/mongoid_with_schema_map_uuid.yml +27 -0
- data/spec/integration/active_job_spec.rb +35 -0
- data/spec/integration/app_spec.rb +327 -144
- data/spec/integration/associations/belongs_to_spec.rb +165 -0
- data/spec/integration/associations/embedded_dirty_spec.rb +57 -0
- data/spec/integration/associations/embedded_spec.rb +241 -67
- data/spec/integration/associations/embeds_many_spec.rb +347 -0
- data/spec/integration/associations/embeds_one_spec.rb +62 -0
- data/spec/integration/associations/foreign_key_spec.rb +107 -0
- data/spec/integration/associations/foreign_key_spec_models.rb +64 -0
- data/spec/integration/associations/has_and_belongs_to_many_spec.rb +161 -0
- data/spec/integration/associations/has_many_spec.rb +184 -0
- data/spec/integration/associations/has_one_spec.rb +322 -0
- data/spec/integration/associations/nested_attributes_assignment_spec.rb +113 -0
- data/spec/integration/associations/reverse_population_spec.rb +34 -0
- data/spec/integration/associations/reverse_population_spec_models.rb +36 -0
- data/spec/integration/associations/scope_option_spec.rb +100 -0
- data/spec/integration/atomic/modifiers_spec.rb +137 -0
- data/spec/integration/bson_regexp_raw_spec.rb +19 -0
- data/spec/integration/caching_spec.rb +68 -0
- data/spec/integration/callbacks_models.rb +149 -3
- data/spec/integration/callbacks_spec.rb +422 -34
- data/spec/integration/contextual/empty_spec.rb +141 -0
- data/spec/integration/criteria/alias_query_spec.rb +91 -0
- data/spec/integration/criteria/date_field_spec.rb +1 -2
- data/spec/integration/criteria/default_scope_spec.rb +78 -0
- data/spec/integration/criteria/logical_spec.rb +105 -0
- data/spec/integration/criteria/range_spec.rb +349 -0
- data/spec/integration/criteria/raw_value_spec.rb +538 -0
- data/spec/integration/criteria/time_with_zone_spec.rb +135 -15
- data/spec/integration/discriminator_key_spec.rb +392 -0
- data/spec/integration/discriminator_value_spec.rb +204 -0
- data/spec/integration/document_spec.rb +32 -3
- data/spec/integration/dots_and_dollars_spec.rb +273 -0
- data/spec/integration/encryption_spec.rb +248 -0
- data/spec/integration/i18n_fallbacks_spec.rb +47 -0
- data/spec/integration/isolation_state_spec.rb +241 -0
- data/spec/integration/matcher_examples_spec.rb +743 -0
- data/spec/integration/matcher_operator_data/all.yml +140 -0
- data/spec/integration/matcher_operator_data/and.yml +93 -0
- data/spec/integration/matcher_operator_data/bits_all_clear.yml +144 -0
- data/spec/integration/matcher_operator_data/bits_all_set.yml +144 -0
- data/spec/integration/matcher_operator_data/bits_any_clear.yml +144 -0
- data/spec/integration/matcher_operator_data/bits_any_set.yml +144 -0
- data/spec/integration/matcher_operator_data/comment.yml +22 -0
- data/spec/integration/matcher_operator_data/elem_match.yml +409 -0
- data/spec/integration/matcher_operator_data/elem_match_expr.yml +213 -0
- data/spec/integration/matcher_operator_data/eq.yml +191 -0
- data/spec/integration/matcher_operator_data/exists.yml +213 -0
- data/spec/integration/matcher_operator_data/generic_op.yml +17 -0
- data/spec/integration/matcher_operator_data/gt.yml +132 -0
- data/spec/integration/matcher_operator_data/gt_types.yml +63 -0
- data/spec/integration/matcher_operator_data/gte.yml +132 -0
- data/spec/integration/matcher_operator_data/gte_types.yml +15 -0
- data/spec/integration/matcher_operator_data/implicit.yml +331 -0
- data/spec/integration/matcher_operator_data/implicit_traversal.yml +112 -0
- data/spec/integration/matcher_operator_data/in.yml +210 -0
- data/spec/integration/matcher_operator_data/invalid_op.yml +59 -0
- data/spec/integration/matcher_operator_data/invalid_syntax.yml +39 -0
- data/spec/integration/matcher_operator_data/lt.yml +132 -0
- data/spec/integration/matcher_operator_data/lt_types.yml +15 -0
- data/spec/integration/matcher_operator_data/lte.yml +132 -0
- data/spec/integration/matcher_operator_data/lte_types.yml +15 -0
- data/spec/integration/matcher_operator_data/mod.yml +55 -0
- data/spec/integration/matcher_operator_data/multiple.yml +29 -0
- data/spec/integration/matcher_operator_data/ne.yml +150 -0
- data/spec/integration/matcher_operator_data/ne_types.yml +15 -0
- data/spec/integration/matcher_operator_data/nin.yml +114 -0
- data/spec/integration/matcher_operator_data/nor.yml +126 -0
- data/spec/integration/matcher_operator_data/not.yml +196 -0
- data/spec/integration/matcher_operator_data/or.yml +137 -0
- data/spec/integration/matcher_operator_data/regex.yml +195 -0
- data/spec/integration/matcher_operator_data/regex_options.yml +72 -0
- data/spec/integration/matcher_operator_data/size.yml +171 -0
- data/spec/integration/matcher_operator_data/type.yml +64 -0
- data/spec/integration/matcher_operator_data/type_array.yml +15 -0
- data/spec/integration/matcher_operator_data/type_binary.yml +18 -0
- data/spec/integration/matcher_operator_data/type_boolean.yml +39 -0
- data/spec/integration/matcher_operator_data/type_code.yml +26 -0
- data/spec/integration/matcher_operator_data/type_code_with_scope.yml +26 -0
- data/spec/integration/matcher_operator_data/type_date.yml +39 -0
- data/spec/integration/matcher_operator_data/type_db_pointer.yml +19 -0
- data/spec/integration/matcher_operator_data/type_decimal.yml +37 -0
- data/spec/integration/matcher_operator_data/type_double.yml +15 -0
- data/spec/integration/matcher_operator_data/type_int32.yml +33 -0
- data/spec/integration/matcher_operator_data/type_int64.yml +33 -0
- data/spec/integration/matcher_operator_data/type_max_key.yml +17 -0
- data/spec/integration/matcher_operator_data/type_min_key.yml +17 -0
- data/spec/integration/matcher_operator_data/type_null.yml +23 -0
- data/spec/integration/matcher_operator_data/type_object.yml +23 -0
- data/spec/integration/matcher_operator_data/type_object_id.yml +25 -0
- data/spec/integration/matcher_operator_data/type_regex.yml +44 -0
- data/spec/integration/matcher_operator_data/type_string.yml +15 -0
- data/spec/integration/matcher_operator_data/type_symbol.yml +32 -0
- data/spec/integration/matcher_operator_data/type_timestamp.yml +25 -0
- data/spec/integration/matcher_operator_data/type_undefined.yml +17 -0
- data/spec/integration/matcher_operator_spec.rb +113 -0
- data/spec/integration/matcher_regexp_timeout_spec.rb +215 -0
- data/spec/integration/matcher_spec.rb +237 -0
- data/spec/integration/persistence/collection_options_spec.rb +34 -0
- data/spec/integration/persistence/range_field_spec.rb +373 -0
- data/spec/integration/query_operator_guard_spec.rb +89 -0
- data/spec/integration/server_query_spec.rb +148 -0
- data/spec/integration/shardable_spec.rb +148 -0
- data/spec/integration/stringified_symbol_field_spec.rb +189 -0
- data/spec/lite_spec_helper.rb +56 -19
- data/spec/mongoid/association/accessors_spec.rb +375 -273
- data/spec/mongoid/association/auto_save_spec.rb +215 -106
- data/spec/mongoid/association/builders_spec.rb +124 -74
- data/spec/mongoid/association/constrainable_spec.rb +11 -21
- data/spec/mongoid/association/counter_cache_spec.rb +101 -131
- data/spec/mongoid/association/depending_spec.rb +454 -402
- data/spec/mongoid/association/eager_loadable_spec.rb +47 -0
- data/spec/mongoid/association/eager_spec.rb +471 -60
- data/spec/mongoid/association/embedded/cyclic_spec.rb +40 -49
- data/spec/mongoid/association/embedded/dirty_spec.rb +19 -21
- data/spec/mongoid/association/embedded/embedded_in/binding_spec.rb +39 -53
- data/spec/mongoid/association/embedded/embedded_in/buildable_spec.rb +58 -8
- data/spec/mongoid/association/embedded/embedded_in/proxy_spec.rb +208 -158
- data/spec/mongoid/association/embedded/embedded_in_spec.rb +46 -134
- data/spec/mongoid/association/embedded/embeds_many/binding_spec.rb +11 -13
- data/spec/mongoid/association/embedded/embeds_many/buildable_spec.rb +137 -35
- data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +1716 -1327
- data/spec/mongoid/association/embedded/embeds_many_models.rb +191 -2
- data/spec/mongoid/association/embedded/embeds_many_query_spec.rb +68 -0
- data/spec/mongoid/association/embedded/embeds_many_spec.rb +77 -119
- data/spec/mongoid/association/embedded/embeds_one/binding_spec.rb +17 -22
- data/spec/mongoid/association/embedded/embeds_one/buildable_spec.rb +44 -24
- data/spec/mongoid/association/embedded/embeds_one/proxy_spec.rb +234 -287
- data/spec/mongoid/association/embedded/embeds_one_dnl_models.rb +2 -0
- data/spec/mongoid/association/embedded/embeds_one_models.rb +27 -1
- data/spec/mongoid/association/embedded/embeds_one_query_spec.rb +28 -0
- data/spec/mongoid/association/embedded/embeds_one_spec.rb +50 -134
- data/spec/mongoid/association/fallback_spec.rb +425 -0
- data/spec/mongoid/association/macros_spec.rb +250 -308
- data/spec/mongoid/association/nested/many_spec.rb +53 -72
- data/spec/mongoid/association/nested/one_spec.rb +68 -85
- data/spec/mongoid/association/options_spec.rb +189 -309
- data/spec/mongoid/association/polymorphic_spec.rb +36 -51
- data/spec/mongoid/association/referenced/belongs_to/binding_spec.rb +58 -75
- data/spec/mongoid/association/referenced/belongs_to/buildable_spec.rb +131 -40
- data/spec/mongoid/association/referenced/belongs_to/eager_spec.rb +280 -52
- data/spec/mongoid/association/referenced/belongs_to/proxy_spec.rb +345 -370
- data/spec/mongoid/association/referenced/belongs_to_models.rb +13 -0
- data/spec/mongoid/association/referenced/belongs_to_query_spec.rb +57 -0
- data/spec/mongoid/association/referenced/belongs_to_spec.rb +171 -329
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/binding_spec.rb +45 -56
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/buildable_spec.rb +66 -29
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/eager_spec.rb +75 -36
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_persistence_spec.rb +28 -8
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +1151 -1283
- data/spec/mongoid/association/referenced/has_and_belongs_to_many_models.rb +69 -2
- data/spec/mongoid/association/referenced/has_and_belongs_to_many_query_spec.rb +39 -0
- data/spec/mongoid/association/referenced/has_and_belongs_to_many_spec.rb +176 -166
- data/spec/mongoid/association/referenced/has_many/binding_spec.rb +32 -43
- data/spec/mongoid/association/referenced/has_many/buildable_spec.rb +161 -24
- data/spec/mongoid/association/referenced/has_many/eager_spec.rb +169 -67
- data/spec/mongoid/association/referenced/has_many/enumerable_spec.rb +1021 -689
- data/spec/mongoid/association/referenced/has_many/proxy_query_spec.rb +1 -1
- data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +1743 -1955
- data/spec/mongoid/association/referenced/has_many_models.rb +85 -1
- data/spec/mongoid/association/referenced/has_many_query_spec.rb +37 -0
- data/spec/mongoid/association/referenced/has_many_spec.rb +83 -178
- data/spec/mongoid/association/referenced/has_many_through/eager_spec.rb +58 -0
- data/spec/mongoid/association/referenced/has_many_through/proxy_spec.rb +62 -0
- data/spec/mongoid/association/referenced/has_many_through_spec.rb +399 -0
- data/spec/mongoid/association/referenced/has_one/binding_spec.rb +28 -37
- data/spec/mongoid/association/referenced/has_one/buildable_spec.rb +74 -28
- data/spec/mongoid/association/referenced/has_one/eager_spec.rb +60 -42
- data/spec/mongoid/association/referenced/has_one/proxy_spec.rb +380 -333
- data/spec/mongoid/association/referenced/has_one_models.rb +73 -0
- data/spec/mongoid/association/referenced/has_one_query_spec.rb +37 -0
- data/spec/mongoid/association/referenced/has_one_spec.rb +61 -185
- data/spec/mongoid/association/referenced/has_one_through/eager_spec.rb +58 -0
- data/spec/mongoid/association/referenced/has_one_through/proxy_spec.rb +49 -0
- data/spec/mongoid/association/referenced/has_one_through_spec.rb +211 -0
- data/spec/mongoid/association/reflections_spec.rb +16 -23
- data/spec/mongoid/association/syncable_spec.rb +132 -138
- data/spec/mongoid/association_spec.rb +59 -65
- data/spec/mongoid/atomic/modifiers_spec.rb +217 -180
- data/spec/mongoid/atomic/paths/embedded/many_spec.rb +41 -51
- data/spec/mongoid/atomic/paths/embedded/one_spec.rb +37 -48
- data/spec/mongoid/atomic/paths/root_spec.rb +11 -14
- data/spec/mongoid/atomic/paths_spec.rb +133 -128
- data/spec/mongoid/atomic_spec.rb +187 -154
- data/spec/mongoid/atomic_update_preparer_spec.rb +83 -0
- data/spec/mongoid/attributes/dynamic_spec.rb +5 -6
- data/spec/mongoid/attributes/embedded_spec.rb +118 -0
- data/spec/mongoid/attributes/nested_spec.rb +1604 -1737
- data/spec/mongoid/attributes/nested_spec_models.rb +48 -0
- data/spec/mongoid/attributes/projector_data/embedded.yml +105 -0
- data/spec/mongoid/attributes/projector_data/fields.yml +93 -0
- data/spec/mongoid/attributes/projector_spec.rb +37 -0
- data/spec/mongoid/attributes/readonly_spec.rb +106 -108
- data/spec/mongoid/attributes_spec.rb +1217 -715
- data/spec/mongoid/cacheable_spec.rb +78 -45
- data/spec/mongoid/changeable_spec.rb +914 -638
- data/spec/mongoid/clients/factory_spec.rb +314 -137
- data/spec/mongoid/clients/options_spec.rb +205 -76
- data/spec/mongoid/clients/sessions_spec.rb +73 -116
- data/spec/mongoid/clients/transactions_spec.rb +1128 -186
- data/spec/mongoid/clients/transactions_spec_models.rb +251 -0
- data/spec/mongoid/clients_spec.rb +433 -312
- data/spec/mongoid/collection_configurable_spec.rb +156 -0
- data/spec/mongoid/composable_spec.rb +4 -8
- data/spec/mongoid/config/defaults_spec.rb +125 -0
- data/spec/mongoid/config/encryption_spec.rb +377 -0
- data/spec/mongoid/config/environment_spec.rb +129 -49
- data/spec/mongoid/config/introspection_spec.rb +119 -0
- data/spec/mongoid/config/options_spec.rb +36 -23
- data/spec/mongoid/config_spec.rb +497 -148
- data/spec/mongoid/contextual/aggregable/memory_spec.rb +507 -161
- data/spec/mongoid/contextual/aggregable/memory_table.yml +88 -0
- data/spec/mongoid/contextual/aggregable/memory_table_spec.rb +60 -0
- data/spec/mongoid/contextual/aggregable/mongo_spec.rb +242 -158
- data/spec/mongoid/contextual/aggregable/none_spec.rb +47 -0
- data/spec/mongoid/contextual/atomic_spec.rb +510 -229
- data/spec/mongoid/contextual/map_reduce_spec.rb +114 -153
- data/spec/mongoid/contextual/memory_spec.rb +1947 -563
- data/spec/mongoid/contextual/mongo/documents_loader_spec.rb +193 -0
- data/spec/mongoid/contextual/mongo_spec.rb +3317 -1268
- data/spec/mongoid/contextual/none_spec.rb +98 -89
- data/spec/mongoid/contextual/short_circuit_spec.rb +139 -0
- data/spec/mongoid/copyable_spec.rb +769 -191
- data/spec/mongoid/copyable_spec_models.rb +30 -0
- data/spec/mongoid/criteria/findable_spec.rb +565 -506
- data/spec/mongoid/criteria/includable_spec.rb +2417 -0
- data/spec/mongoid/criteria/includable_spec_models.rb +64 -0
- data/spec/mongoid/criteria/inspectable_spec.rb +13 -13
- data/spec/mongoid/criteria/marshalable_spec.rb +29 -12
- data/spec/mongoid/criteria/modifiable_spec.rb +388 -530
- data/spec/mongoid/criteria/options_spec.rb +4 -6
- data/spec/mongoid/criteria/queryable/aggregable_spec.rb +114 -140
- data/spec/mongoid/criteria/queryable/expandable_spec.rb +59 -0
- data/spec/mongoid/criteria/queryable/extensions/array_spec.rb +132 -198
- data/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb +199 -25
- data/spec/mongoid/criteria/queryable/extensions/boolean_spec.rb +86 -116
- data/spec/mongoid/criteria/queryable/extensions/date_spec.rb +69 -67
- data/spec/mongoid/criteria/queryable/extensions/date_time_spec.rb +121 -113
- data/spec/mongoid/criteria/queryable/extensions/float_spec.rb +96 -32
- data/spec/mongoid/criteria/queryable/extensions/hash_spec.rb +105 -152
- data/spec/mongoid/criteria/queryable/extensions/integer_spec.rb +97 -33
- data/spec/mongoid/criteria/queryable/extensions/nil_class_spec.rb +27 -37
- data/spec/mongoid/criteria/queryable/extensions/numeric_spec.rb +79 -15
- data/spec/mongoid/criteria/queryable/extensions/object_spec.rb +34 -53
- data/spec/mongoid/criteria/queryable/extensions/range_spec.rb +259 -191
- data/spec/mongoid/criteria/queryable/extensions/regexp_raw_spec.rb +18 -34
- data/spec/mongoid/criteria/queryable/extensions/regexp_spec.rb +16 -33
- data/spec/mongoid/criteria/queryable/extensions/set_spec.rb +14 -18
- data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +91 -189
- data/spec/mongoid/criteria/queryable/extensions/symbol_spec.rb +34 -100
- data/spec/mongoid/criteria/queryable/extensions/time_spec.rb +99 -103
- data/spec/mongoid/criteria/queryable/extensions/time_with_zone_spec.rb +78 -85
- data/spec/mongoid/criteria/queryable/key_spec.rb +34 -35
- data/spec/mongoid/criteria/queryable/mergeable_spec.rb +150 -21
- data/spec/mongoid/criteria/queryable/optional_spec.rb +375 -954
- data/spec/mongoid/criteria/queryable/options_spec.rb +175 -123
- data/spec/mongoid/criteria/queryable/pipeline_spec.rb +67 -86
- data/spec/mongoid/criteria/queryable/queryable_spec.rb +43 -52
- data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +2101 -414
- data/spec/mongoid/criteria/queryable/selectable_shared_examples.rb +35 -0
- data/spec/mongoid/criteria/queryable/selectable_spec.rb +1283 -3140
- data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +754 -0
- data/spec/mongoid/criteria/queryable/selector_spec.rb +404 -317
- data/spec/mongoid/criteria/queryable/smash_spec.rb +8 -10
- data/spec/mongoid/criteria/queryable/storable_spec.rb +249 -0
- data/spec/mongoid/criteria/queryable_mql_spec.rb +92 -0
- data/spec/mongoid/criteria/scopable_spec.rb +212 -118
- data/spec/mongoid/criteria/translator_spec.rb +132 -0
- data/spec/mongoid/criteria_projection_spec.rb +389 -0
- data/spec/mongoid/criteria_spec.rb +1942 -2301
- data/spec/mongoid/document_fields_spec.rb +187 -14
- data/spec/mongoid/document_persistence_context_spec.rb +80 -6
- data/spec/mongoid/document_query_spec.rb +87 -0
- data/spec/mongoid/document_spec.rb +394 -538
- data/spec/mongoid/encryptable_spec.rb +61 -0
- data/spec/mongoid/equality_spec.rb +77 -103
- data/spec/mongoid/errors/ambiguous_relationship_spec.rb +10 -10
- data/spec/mongoid/errors/attribute_not_loaded_spec.rb +29 -0
- data/spec/mongoid/errors/callback_spec.rb +10 -10
- data/spec/mongoid/errors/config_redactor_spec.rb +112 -0
- data/spec/mongoid/errors/delete_restriction_spec.rb +9 -9
- data/spec/mongoid/errors/document_not_destroyed_spec.rb +9 -9
- data/spec/mongoid/errors/document_not_found_spec.rb +108 -32
- data/spec/mongoid/errors/invalid_collection_spec.rb +11 -12
- data/spec/mongoid/errors/invalid_config_file_spec.rb +29 -0
- data/spec/mongoid/errors/invalid_config_option_spec.rb +10 -10
- data/spec/mongoid/errors/invalid_field_option_spec.rb +9 -9
- data/spec/mongoid/errors/invalid_field_spec.rb +13 -11
- data/spec/mongoid/errors/invalid_field_type_spec.rb +53 -0
- data/spec/mongoid/errors/invalid_find_spec.rb +10 -10
- data/spec/mongoid/errors/invalid_includes_spec.rb +11 -12
- data/spec/mongoid/errors/invalid_index_spec.rb +10 -10
- data/spec/mongoid/errors/invalid_options_spec.rb +10 -10
- data/spec/mongoid/errors/invalid_path_spec.rb +9 -9
- data/spec/mongoid/errors/invalid_relation_spec.rb +17 -15
- data/spec/mongoid/errors/invalid_scope_spec.rb +10 -10
- data/spec/mongoid/errors/invalid_set_polymorphic_relation_spec.rb +5 -5
- data/spec/mongoid/errors/invalid_storage_options_spec.rb +10 -10
- data/spec/mongoid/errors/invalid_time_spec.rb +10 -10
- data/spec/mongoid/errors/inverse_not_found_spec.rb +11 -11
- data/spec/mongoid/errors/mixed_client_configuration_spec.rb +42 -11
- data/spec/mongoid/errors/mixed_relations_spec.rb +10 -10
- data/spec/mongoid/errors/mongoid_error_spec.rb +23 -23
- data/spec/mongoid/errors/nested_attributes_metadata_not_found_spec.rb +10 -10
- data/spec/mongoid/errors/no_client_config_spec.rb +11 -11
- data/spec/mongoid/errors/no_client_database_spec.rb +40 -11
- data/spec/mongoid/errors/no_client_hosts_spec.rb +37 -11
- data/spec/mongoid/errors/no_clients_config_spec.rb +9 -9
- data/spec/mongoid/errors/no_environment_spec.rb +10 -10
- data/spec/mongoid/errors/no_map_reduce_output_spec.rb +10 -10
- data/spec/mongoid/errors/no_metadata_spec.rb +9 -9
- data/spec/mongoid/errors/no_parent_spec.rb +10 -10
- data/spec/mongoid/errors/readonly_association_spec.rb +34 -0
- data/spec/mongoid/errors/readonly_attribute_spec.rb +9 -9
- data/spec/mongoid/errors/readonly_document_spec.rb +9 -9
- data/spec/mongoid/errors/scope_overwrite_spec.rb +11 -11
- data/spec/mongoid/errors/too_many_nested_attribute_records_spec.rb +11 -11
- data/spec/mongoid/errors/unknown_attribute_spec.rb +9 -9
- data/spec/mongoid/errors/unsaved_document_spec.rb +10 -10
- data/spec/mongoid/errors/unsupported_javascript_spec.rb +11 -11
- data/spec/mongoid/errors/validations_spec.rb +13 -13
- data/spec/mongoid/extensions/array_spec.rb +92 -377
- data/spec/mongoid/extensions/big_decimal_spec.rb +640 -217
- data/spec/mongoid/extensions/binary_spec.rb +50 -23
- data/spec/mongoid/extensions/boolean_spec.rb +73 -107
- data/spec/mongoid/extensions/date_class_mongoize_spec.rb +329 -0
- data/spec/mongoid/extensions/date_spec.rb +62 -173
- data/spec/mongoid/extensions/date_time_spec.rb +42 -95
- data/spec/mongoid/extensions/decimal128_spec.rb +14 -17
- data/spec/mongoid/extensions/false_class_spec.rb +13 -24
- data/spec/mongoid/extensions/float_spec.rb +66 -100
- data/spec/mongoid/extensions/hash_spec.rb +79 -165
- data/spec/mongoid/extensions/integer_spec.rb +63 -91
- data/spec/mongoid/extensions/module_spec.rb +15 -17
- data/spec/mongoid/extensions/nil_class_spec.rb +4 -4
- data/spec/mongoid/extensions/object_id_spec.rb +161 -246
- data/spec/mongoid/extensions/object_spec.rb +36 -238
- data/spec/mongoid/extensions/range_spec.rb +259 -69
- data/spec/mongoid/extensions/raw_value_spec.rb +65 -0
- data/spec/mongoid/extensions/regexp_spec.rb +56 -41
- data/spec/mongoid/extensions/set_spec.rb +110 -14
- data/spec/mongoid/extensions/string_spec.rb +139 -237
- data/spec/mongoid/extensions/stringified_symbol_spec.rb +71 -0
- data/spec/mongoid/extensions/symbol_spec.rb +19 -62
- data/spec/mongoid/extensions/time_spec.rb +394 -192
- data/spec/mongoid/extensions/time_with_zone_spec.rb +111 -176
- data/spec/mongoid/extensions/true_class_spec.rb +13 -24
- data/spec/mongoid/extensions/vector_spec.rb +175 -0
- data/spec/mongoid/factory_spec.rb +378 -80
- data/spec/mongoid/fields/foreign_key_spec.rb +387 -267
- data/spec/mongoid/fields/localized_spec.rb +197 -196
- data/spec/mongoid/fields/standard_spec.rb +45 -58
- data/spec/mongoid/fields_spec.rb +1448 -525
- data/spec/mongoid/findable_spec.rb +659 -230
- data/spec/mongoid/indexable/specification_spec.rb +145 -40
- data/spec/mongoid/indexable_spec.rb +551 -188
- data/spec/mongoid/inspectable_spec.rb +126 -18
- data/spec/mongoid/interceptable_spec.rb +1431 -573
- data/spec/mongoid/interceptable_spec_models.rb +244 -0
- data/spec/mongoid/loadable_spec.rb +86 -0
- data/spec/mongoid/loading_spec.rb +114 -0
- data/spec/mongoid/loggable_spec.rb +5 -5
- data/spec/mongoid/matcher/extract_attribute_data/numeric_keys.yml +104 -0
- data/spec/mongoid/matcher/extract_attribute_data/traversal.yml +239 -0
- data/spec/mongoid/matcher/extract_attribute_spec.rb +33 -0
- data/spec/mongoid/matcher/regexp_budget_spec.rb +570 -0
- data/spec/mongoid/model_resolver_spec.rb +167 -0
- data/spec/mongoid/mongoizable_spec.rb +277 -0
- data/spec/mongoid/monkey_patches_spec.rb +215 -0
- data/spec/mongoid/persistable/creatable_spec.rb +256 -209
- data/spec/mongoid/persistable/deletable_spec.rb +369 -80
- data/spec/mongoid/persistable/destroyable_spec.rb +439 -80
- data/spec/mongoid/persistable/incrementable_spec.rb +112 -77
- data/spec/mongoid/persistable/logical_spec.rb +92 -51
- data/spec/mongoid/persistable/maxable_spec.rb +148 -0
- data/spec/mongoid/persistable/minable_spec.rb +148 -0
- data/spec/mongoid/persistable/multipliable_spec.rb +212 -0
- data/spec/mongoid/persistable/poppable_spec.rb +87 -47
- data/spec/mongoid/persistable/pullable_spec.rb +161 -82
- data/spec/mongoid/persistable/pushable_spec.rb +188 -110
- data/spec/mongoid/persistable/renamable_spec.rb +91 -51
- data/spec/mongoid/persistable/savable_spec.rb +373 -155
- data/spec/mongoid/persistable/settable_spec.rb +164 -134
- data/spec/mongoid/persistable/unsettable_spec.rb +94 -58
- data/spec/mongoid/persistable/updatable_spec.rb +235 -267
- data/spec/mongoid/persistable/upsertable_spec.rb +150 -38
- data/spec/mongoid/persistable_spec.rb +198 -128
- data/spec/mongoid/persistence_context_spec.rb +133 -137
- data/spec/mongoid/positional_spec.rb +69 -78
- data/spec/mongoid/railties/bson_object_id_serializer_spec.rb +27 -0
- data/spec/mongoid/railties/console_sandbox_spec.rb +43 -0
- data/spec/mongoid/relations/proxy_spec.rb +8 -6
- data/spec/mongoid/reloadable_spec.rb +552 -123
- data/spec/mongoid/scopable_spec.rb +522 -455
- data/spec/mongoid/search_indexable_spec.rb +838 -0
- data/spec/mongoid/selectable_spec.rb +41 -53
- data/spec/mongoid/serializable_spec.rb +394 -319
- data/spec/mongoid/shardable_models.rb +76 -0
- data/spec/mongoid/shardable_spec.rb +245 -41
- data/spec/mongoid/stateful_spec.rb +170 -45
- data/spec/mongoid/tasks/database_rake_spec.rb +284 -133
- data/spec/mongoid/tasks/database_spec.rb +245 -51
- data/spec/mongoid/tasks/encryption_spec.rb +189 -0
- data/spec/mongoid/threaded_spec.rb +118 -85
- data/spec/mongoid/timestamps/created/short_spec.rb +15 -16
- data/spec/mongoid/timestamps/created_spec.rb +36 -14
- data/spec/mongoid/timestamps/timeless_spec.rb +230 -47
- data/spec/mongoid/timestamps/updated/short_spec.rb +26 -30
- data/spec/mongoid/timestamps/updated_spec.rb +23 -27
- data/spec/mongoid/timestamps_spec.rb +408 -34
- data/spec/mongoid/timestamps_spec_models.rb +67 -0
- data/spec/mongoid/touchable_spec.rb +1235 -136
- data/spec/mongoid/touchable_spec_models.rb +178 -0
- data/spec/mongoid/traversable_spec.rb +1188 -79
- data/spec/mongoid/validatable/associated_spec.rb +86 -89
- data/spec/mongoid/validatable/format_spec.rb +26 -33
- data/spec/mongoid/validatable/length_spec.rb +64 -85
- data/spec/mongoid/validatable/numericality_spec.rb +124 -14
- data/spec/mongoid/validatable/presence_spec.rb +168 -215
- data/spec/mongoid/validatable/uniqueness_spec.rb +702 -826
- data/spec/mongoid/validatable_spec.rb +76 -105
- data/spec/mongoid/warnings_spec.rb +32 -0
- data/spec/mongoid_spec.rb +67 -30
- data/spec/rails/controller_extension/controller_runtime_spec.rb +59 -60
- data/spec/rails/mongoid_spec.rb +38 -139
- data/spec/shared/CANDIDATE.md +28 -0
- data/spec/shared/bin/s3-copy +45 -0
- data/spec/shared/bin/s3-upload +69 -0
- data/spec/shared/lib/mrss/cluster_config.rb +16 -41
- data/spec/shared/lib/mrss/constraints.rb +44 -59
- data/spec/shared/lib/mrss/docker_runner.rb +42 -20
- data/spec/shared/lib/mrss/eg_config_utils.rb +20 -0
- data/spec/shared/lib/mrss/event_subscriber.rb +210 -0
- data/spec/shared/lib/mrss/lite_constraints.rb +48 -1
- data/spec/shared/lib/mrss/release/candidate.rb +287 -0
- data/spec/shared/lib/mrss/release/product_data.rb +144 -0
- data/spec/shared/lib/mrss/server_version_registry.rb +18 -24
- data/spec/shared/lib/mrss/session_registry.rb +69 -0
- data/spec/shared/lib/mrss/session_registry_legacy.rb +60 -0
- data/spec/shared/lib/mrss/spec_organizer.rb +48 -2
- data/spec/shared/lib/mrss/utils.rb +28 -6
- data/spec/shared/lib/tasks/candidate.rake +64 -0
- data/spec/shared/share/Dockerfile.erb +114 -96
- data/spec/shared/share/haproxy-1.conf +16 -0
- data/spec/shared/share/haproxy-2.conf +17 -0
- data/spec/shared/shlib/config.sh +27 -0
- data/spec/shared/shlib/distro.sh +12 -1
- data/spec/shared/shlib/server.sh +148 -65
- data/spec/shared/shlib/set_env.sh +56 -80
- data/spec/spec_helper.rb +104 -72
- data/spec/support/authorization.rb +2 -0
- data/spec/support/client_registry.rb +9 -0
- data/spec/support/constraints.rb +37 -22
- data/spec/support/crypt/models.rb +206 -0
- data/spec/support/crypt.rb +77 -0
- data/spec/support/expectations.rb +38 -17
- data/spec/support/feature_sandbox.rb +69 -0
- data/spec/support/helpers.rb +11 -0
- data/spec/support/immutable_ids.rb +118 -0
- data/spec/support/macros.rb +106 -1
- data/spec/support/models/account.rb +38 -0
- data/spec/support/models/acolyte.rb +20 -0
- data/spec/support/models/actor.rb +21 -0
- data/spec/support/models/actress.rb +4 -0
- data/spec/support/models/address.rb +88 -0
- data/spec/support/models/address_component.rb +8 -0
- data/spec/support/models/address_number.rb +9 -0
- data/spec/support/models/agency.rb +8 -0
- data/spec/support/models/agent.rb +19 -0
- data/spec/support/models/album.rb +20 -0
- data/spec/support/models/alert.rb +10 -0
- data/spec/support/models/animal.rb +28 -0
- data/spec/support/models/answer.rb +9 -0
- data/spec/support/models/appointment.rb +10 -0
- data/spec/support/models/armrest.rb +9 -0
- data/spec/support/models/article.rb +14 -0
- data/spec/support/models/artist.rb +95 -0
- data/spec/support/models/artwork.rb +7 -0
- data/spec/support/models/audible_sound.rb +3 -0
- data/spec/support/models/audio.rb +8 -0
- data/spec/support/models/augmentation.rb +25 -0
- data/spec/support/models/author.rb +9 -0
- data/spec/support/models/baby.rb +7 -0
- data/spec/support/models/band.rb +46 -0
- data/spec/support/models/bar.rb +13 -0
- data/spec/support/models/basic.rb +8 -0
- data/spec/support/models/bed.rb +3 -0
- data/spec/support/models/big_palette.rb +4 -0
- data/spec/support/models/birthday.rb +14 -0
- data/spec/support/models/bolt.rb +7 -0
- data/spec/support/models/bomb.rb +7 -0
- data/spec/support/models/book.rb +20 -0
- data/spec/support/models/breed.rb +7 -0
- data/spec/support/models/browser.rb +8 -0
- data/spec/support/models/building.rb +10 -0
- data/spec/support/models/building_address.rb +10 -0
- data/spec/support/models/bus.rb +10 -0
- data/spec/support/models/business.rb +8 -0
- data/spec/support/models/callback_test.rb +12 -0
- data/spec/support/models/canvas.rb +27 -0
- data/spec/support/models/car.rb +3 -0
- data/spec/support/models/cat.rb +9 -0
- data/spec/support/models/catalog.rb +24 -0
- data/spec/support/models/category.rb +11 -0
- data/spec/support/models/child.rb +7 -0
- data/spec/support/models/child_doc.rb +24 -0
- data/spec/support/models/church.rb +9 -0
- data/spec/support/models/circle.rb +5 -0
- data/spec/support/models/circuit.rb +7 -0
- data/spec/support/models/circus.rb +12 -0
- data/spec/support/models/code.rb +17 -0
- data/spec/support/models/coding/pull_request.rb +11 -0
- data/spec/support/models/coding.rb +3 -0
- data/spec/support/models/comment.rb +18 -0
- data/spec/support/models/company.rb +9 -0
- data/spec/support/models/consumption_period.rb +9 -0
- data/spec/support/models/contextable_item.rb +8 -0
- data/spec/support/models/contractor.rb +8 -0
- data/spec/support/models/cookie.rb +8 -0
- data/spec/support/models/country_code.rb +12 -0
- data/spec/support/models/courier_job.rb +7 -0
- data/spec/support/models/cover.rb +10 -0
- data/spec/support/models/crate.rb +12 -0
- data/spec/support/models/customer.rb +10 -0
- data/spec/support/models/customer_address.rb +11 -0
- data/spec/support/models/deed.rb +8 -0
- data/spec/support/models/definition.rb +12 -0
- data/spec/support/models/delegating_patient.rb +15 -0
- data/spec/support/models/description.rb +13 -0
- data/spec/support/models/dictionary.rb +19 -0
- data/spec/support/models/division.rb +13 -0
- data/spec/support/models/doctor.rb +14 -0
- data/spec/support/models/dog.rb +10 -0
- data/spec/support/models/dokument.rb +9 -0
- data/spec/support/models/draft.rb +11 -0
- data/spec/support/models/dragon.rb +7 -0
- data/spec/support/models/driver.rb +10 -0
- data/spec/support/models/drug.rb +9 -0
- data/spec/support/models/dungeon.rb +7 -0
- data/spec/support/models/edit.rb +8 -0
- data/spec/support/models/email.rb +9 -0
- data/spec/support/models/employer.rb +7 -0
- data/spec/support/models/entry.rb +9 -0
- data/spec/support/models/eraser.rb +3 -0
- data/spec/support/models/even.rb +10 -0
- data/spec/support/models/event.rb +24 -0
- data/spec/support/models/exhibition.rb +7 -0
- data/spec/support/models/exhibitor.rb +9 -0
- data/spec/support/models/explosion.rb +7 -0
- data/spec/support/models/eye.rb +11 -0
- data/spec/support/models/eye_bowl.rb +11 -0
- data/spec/support/models/face.rb +10 -0
- data/spec/support/models/fanatic.rb +9 -0
- data/spec/support/models/favorite.rb +15 -0
- data/spec/support/models/filesystem.rb +8 -0
- data/spec/support/models/fire_hydrant.rb +9 -0
- data/spec/support/models/firefox.rb +6 -0
- data/spec/support/models/fish.rb +9 -0
- data/spec/support/models/folder.rb +8 -0
- data/spec/support/models/folder_item.rb +10 -0
- data/spec/support/models/fruits.rb +41 -0
- data/spec/support/models/game.rb +21 -0
- data/spec/support/models/ghost.rb +9 -0
- data/spec/support/models/guitar.rb +4 -0
- data/spec/support/models/hole.rb +12 -0
- data/spec/support/models/home.rb +7 -0
- data/spec/support/models/house.rb +9 -0
- data/spec/support/models/html_writer.rb +5 -0
- data/spec/support/models/id_key.rb +9 -0
- data/spec/support/models/idnodef.rb +7 -0
- data/spec/support/models/image.rb +24 -0
- data/spec/support/models/implant.rb +27 -0
- data/spec/support/models/instrument.rb +8 -0
- data/spec/support/models/item.rb +11 -0
- data/spec/support/models/jar.rb +9 -0
- data/spec/support/models/kaleidoscope.rb +9 -0
- data/spec/support/models/kangaroo.rb +7 -0
- data/spec/support/models/label.rb +53 -0
- data/spec/support/models/language.rb +8 -0
- data/spec/support/models/lat_lng.rb +23 -0
- data/spec/support/models/league.rb +14 -0
- data/spec/support/models/learner.rb +4 -0
- data/spec/support/models/line_item.rb +9 -0
- data/spec/support/models/location.rb +11 -0
- data/spec/support/models/login.rb +10 -0
- data/spec/support/models/manufacturer.rb +9 -0
- data/spec/support/models/meat.rb +7 -0
- data/spec/support/models/membership.rb +8 -0
- data/spec/support/models/message.rb +13 -0
- data/spec/support/models/minim.rb +6 -0
- data/spec/support/models/mixed_drink.rb +7 -0
- data/spec/support/models/modifier_models.rb +25 -0
- data/spec/support/models/mop.rb +24 -0
- data/spec/support/models/movie.rb +16 -0
- data/spec/support/models/my_hash.rb +4 -0
- data/spec/support/models/name.rb +36 -0
- data/spec/support/models/name_only.rb +8 -0
- data/spec/support/models/node.rb +8 -0
- data/spec/support/models/note.rb +20 -0
- data/spec/support/models/nut.rb +7 -0
- data/spec/support/models/odd.rb +10 -0
- data/spec/support/models/order.rb +13 -0
- data/spec/support/models/ordered_post.rb +14 -0
- data/spec/support/models/ordered_preference.rb +9 -0
- data/spec/support/models/oscar.rb +17 -0
- data/spec/support/models/other_owner_object.rb +4 -0
- data/spec/support/models/override.rb +11 -0
- data/spec/support/models/ownable.rb +9 -0
- data/spec/support/models/owner.rb +11 -0
- data/spec/support/models/pack.rb +5 -0
- data/spec/support/models/page.rb +19 -0
- data/spec/support/models/page_question.rb +7 -0
- data/spec/support/models/palette.rb +10 -0
- data/spec/support/models/parent.rb +8 -0
- data/spec/support/models/parent_doc.rb +9 -0
- data/spec/support/models/passport.rb +23 -0
- data/spec/support/models/patient.rb +12 -0
- data/spec/support/models/pdf_writer.rb +5 -0
- data/spec/support/models/pencil.rb +3 -0
- data/spec/support/models/person.rb +237 -0
- data/spec/support/models/pet.rb +26 -0
- data/spec/support/models/pet_owner.rb +9 -0
- data/spec/support/models/phone.rb +13 -0
- data/spec/support/models/piano.rb +4 -0
- data/spec/support/models/pizza.rb +10 -0
- data/spec/support/models/player.rb +40 -0
- data/spec/support/models/post.rb +53 -0
- data/spec/support/models/post_genre.rb +9 -0
- data/spec/support/models/powerup.rb +25 -0
- data/spec/support/models/preference.rb +12 -0
- data/spec/support/models/princess.rb +13 -0
- data/spec/support/models/product.rb +23 -0
- data/spec/support/models/profile.rb +19 -0
- data/spec/support/models/pronunciation.rb +8 -0
- data/spec/support/models/pub.rb +9 -0
- data/spec/support/models/publication/encyclopedia.rb +11 -0
- data/spec/support/models/publication/review.rb +13 -0
- data/spec/support/models/publication.rb +4 -0
- data/spec/support/models/purchase.rb +7 -0
- data/spec/support/models/purchased_item.rb +11 -0
- data/spec/support/models/purse.rb +9 -0
- data/spec/support/models/question.rb +11 -0
- data/spec/support/models/quiz.rb +10 -0
- data/spec/support/models/rating.rb +11 -0
- data/spec/support/models/record.rb +54 -0
- data/spec/support/models/registry.rb +8 -0
- data/spec/support/models/role.rb +10 -0
- data/spec/support/models/root_category.rb +7 -0
- data/spec/support/models/sandbox/app/models/app_models_message.rb +3 -0
- data/spec/support/models/sandbox/lib/models/lib_models_message.rb +3 -0
- data/spec/support/models/sandbox/sandbox_message.rb +3 -0
- data/spec/support/models/sandbox/sandbox_user.rb +3 -0
- data/spec/support/models/sandbox/subdir/sandbox_comment.rb +3 -0
- data/spec/support/models/sandwich.rb +12 -0
- data/spec/support/models/scheduler.rb +9 -0
- data/spec/support/models/school.rb +14 -0
- data/spec/support/models/scribe.rb +8 -0
- data/spec/support/models/sealer.rb +7 -0
- data/spec/support/models/seat.rb +24 -0
- data/spec/support/models/seo.rb +12 -0
- data/spec/support/models/series.rb +8 -0
- data/spec/support/models/server.rb +16 -0
- data/spec/support/models/service.rb +25 -0
- data/spec/support/models/shape.rb +15 -0
- data/spec/support/models/shelf.rb +8 -0
- data/spec/support/models/shield.rb +19 -0
- data/spec/support/models/shipment_address.rb +5 -0
- data/spec/support/models/shipping_container.rb +8 -0
- data/spec/support/models/shipping_pack.rb +5 -0
- data/spec/support/models/shirt.rb +11 -0
- data/spec/support/models/shop.rb +9 -0
- data/spec/support/models/short_agent.rb +6 -0
- data/spec/support/models/short_quiz.rb +8 -0
- data/spec/support/models/simple.rb +8 -0
- data/spec/support/models/slave.rb +9 -0
- data/spec/support/models/song.rb +10 -0
- data/spec/support/models/sound.rb +8 -0
- data/spec/support/models/spacer.rb +7 -0
- data/spec/support/models/square.rb +6 -0
- data/spec/support/models/staff.rb +9 -0
- data/spec/support/models/store_as_dup_test1.rb +8 -0
- data/spec/support/models/store_as_dup_test2.rb +8 -0
- data/spec/support/models/store_as_dup_test3.rb +8 -0
- data/spec/support/models/store_as_dup_test4.rb +8 -0
- data/spec/support/models/strategy.rb +5 -0
- data/spec/support/models/student.rb +14 -0
- data/spec/support/models/sub_item.rb +5 -0
- data/spec/support/models/subscription.rb +8 -0
- data/spec/support/models/survey.rb +8 -0
- data/spec/support/models/symptom.rb +9 -0
- data/spec/support/models/system_role.rb +7 -0
- data/spec/support/models/tag.rb +11 -0
- data/spec/support/models/target.rb +8 -0
- data/spec/support/models/template.rb +8 -0
- data/spec/support/models/thing.rb +12 -0
- data/spec/support/models/threadlocker.rb +7 -0
- data/spec/support/models/title.rb +5 -0
- data/spec/support/models/tool.rb +11 -0
- data/spec/support/models/topping.rb +8 -0
- data/spec/support/models/toy.rb +9 -0
- data/spec/support/models/track.rb +41 -0
- data/spec/support/models/translation.rb +8 -0
- data/spec/support/models/tree.rb +11 -0
- data/spec/support/models/truck.rb +7 -0
- data/spec/support/models/user.rb +23 -0
- data/spec/support/models/user_account.rb +13 -0
- data/spec/support/models/validation_callback.rb +13 -0
- data/spec/support/models/vehicle.rb +18 -0
- data/spec/support/models/version.rb +8 -0
- data/spec/support/models/vertex.rb +8 -0
- data/spec/support/models/vet_visit.rb +8 -0
- data/spec/support/models/video.rb +16 -0
- data/spec/support/models/video_game.rb +3 -0
- data/spec/support/models/washer.rb +7 -0
- data/spec/support/models/weapon.rb +25 -0
- data/spec/support/models/wiki_page.rb +17 -0
- data/spec/support/models/word.rb +18 -0
- data/spec/support/models/word_origin.rb +13 -0
- data/spec/support/models/writer.rb +14 -0
- data/spec/support/rails_mock.rb +27 -0
- data/spec/support/schema_maps/schema_map_aws.json +17 -0
- data/spec/support/schema_maps/schema_map_aws_key_alt_names.json +12 -0
- data/spec/support/schema_maps/schema_map_azure.json +17 -0
- data/spec/support/schema_maps/schema_map_azure_key_alt_names.json +12 -0
- data/spec/support/schema_maps/schema_map_gcp.json +17 -0
- data/spec/support/schema_maps/schema_map_gcp_key_alt_names.json +12 -0
- data/spec/support/schema_maps/schema_map_kmip.json +17 -0
- data/spec/support/schema_maps/schema_map_kmip_key_alt_names.json +12 -0
- data/spec/support/schema_maps/schema_map_local.json +18 -0
- data/spec/support/schema_maps/schema_map_local_key_alt_names.json +12 -0
- data/spec/support/shared/time.rb +34 -0
- data/spec/support/sinatra_mock.rb +9 -0
- data/spec/support/spec_config.rb +45 -14
- metadata +1341 -780
- checksums.yaml.gz.sig +0 -0
- data/lib/mongoid/association/referenced/eager.rb +0 -162
- data/lib/mongoid/association/referenced/has_one/nested_builder.rb +0 -126
- data/lib/mongoid/contextual/geo_near.rb +0 -267
- data/lib/mongoid/criteria/queryable/forwardable.rb +0 -65
- data/lib/mongoid/errors/eager_load.rb +0 -22
- data/lib/mongoid/errors/invalid_session_use.rb +0 -24
- data/lib/mongoid/errors/invalid_storage_parent.rb +0 -26
- data/lib/mongoid/errors/invalid_value.rb +0 -16
- data/lib/mongoid/matchable/all.rb +0 -28
- data/lib/mongoid/matchable/and.rb +0 -30
- data/lib/mongoid/matchable/default.rb +0 -119
- data/lib/mongoid/matchable/elem_match.rb +0 -34
- data/lib/mongoid/matchable/eq.rb +0 -22
- data/lib/mongoid/matchable/exists.rb +0 -23
- data/lib/mongoid/matchable/gt.rb +0 -23
- data/lib/mongoid/matchable/gte.rb +0 -23
- data/lib/mongoid/matchable/in.rb +0 -24
- data/lib/mongoid/matchable/lt.rb +0 -23
- data/lib/mongoid/matchable/lte.rb +0 -23
- data/lib/mongoid/matchable/ne.rb +0 -21
- data/lib/mongoid/matchable/nin.rb +0 -22
- data/lib/mongoid/matchable/nor.rb +0 -37
- data/lib/mongoid/matchable/or.rb +0 -33
- data/lib/mongoid/matchable/regexp.rb +0 -27
- data/lib/mongoid/matchable/size.rb +0 -21
- data/lib/mongoid/query_cache.rb +0 -331
- data/lib/support/ruby_version.rb +0 -26
- data/spec/app/models/account.rb +0 -36
- data/spec/app/models/acolyte.rb +0 -17
- data/spec/app/models/actor.rb +0 -18
- data/spec/app/models/actress.rb +0 -2
- data/spec/app/models/address.rb +0 -79
- data/spec/app/models/address_component.rb +0 -5
- data/spec/app/models/address_number.rb +0 -6
- data/spec/app/models/agency.rb +0 -5
- data/spec/app/models/agent.rb +0 -16
- data/spec/app/models/album.rb +0 -18
- data/spec/app/models/alert.rb +0 -7
- data/spec/app/models/animal.rb +0 -26
- data/spec/app/models/answer.rb +0 -6
- data/spec/app/models/appointment.rb +0 -7
- data/spec/app/models/article.rb +0 -12
- data/spec/app/models/artist.rb +0 -87
- data/spec/app/models/artwork.rb +0 -4
- data/spec/app/models/audio.rb +0 -5
- data/spec/app/models/augmentation.rb +0 -11
- data/spec/app/models/author.rb +0 -6
- data/spec/app/models/baby.rb +0 -4
- data/spec/app/models/band.rb +0 -31
- data/spec/app/models/bar.rb +0 -10
- data/spec/app/models/basic.rb +0 -6
- data/spec/app/models/bed.rb +0 -1
- data/spec/app/models/big_palette.rb +0 -2
- data/spec/app/models/birthday.rb +0 -13
- data/spec/app/models/bomb.rb +0 -4
- data/spec/app/models/book.rb +0 -16
- data/spec/app/models/breed.rb +0 -4
- data/spec/app/models/browser.rb +0 -6
- data/spec/app/models/building.rb +0 -6
- data/spec/app/models/building_address.rb +0 -7
- data/spec/app/models/bus.rb +0 -7
- data/spec/app/models/business.rb +0 -5
- data/spec/app/models/callback_test.rb +0 -9
- data/spec/app/models/canvas.rb +0 -25
- data/spec/app/models/car.rb +0 -1
- data/spec/app/models/cat.rb +0 -8
- data/spec/app/models/category.rb +0 -8
- data/spec/app/models/child.rb +0 -4
- data/spec/app/models/child_doc.rb +0 -22
- data/spec/app/models/church.rb +0 -6
- data/spec/app/models/circle.rb +0 -3
- data/spec/app/models/circuit.rb +0 -4
- data/spec/app/models/circus.rb +0 -7
- data/spec/app/models/code.rb +0 -11
- data/spec/app/models/comment.rb +0 -16
- data/spec/app/models/company.rb +0 -5
- data/spec/app/models/consumption_period.rb +0 -7
- data/spec/app/models/contextable_item.rb +0 -5
- data/spec/app/models/contractor.rb +0 -5
- data/spec/app/models/cookie.rb +0 -6
- data/spec/app/models/country_code.rb +0 -8
- data/spec/app/models/courier_job.rb +0 -4
- data/spec/app/models/customer.rb +0 -11
- data/spec/app/models/customer_address.rb +0 -12
- data/spec/app/models/definition.rb +0 -9
- data/spec/app/models/delegating_patient.rb +0 -16
- data/spec/app/models/description.rb +0 -11
- data/spec/app/models/dictionary.rb +0 -10
- data/spec/app/models/division.rb +0 -10
- data/spec/app/models/doctor.rb +0 -12
- data/spec/app/models/dog.rb +0 -7
- data/spec/app/models/dokument.rb +0 -6
- data/spec/app/models/draft.rb +0 -9
- data/spec/app/models/dragon.rb +0 -4
- data/spec/app/models/driver.rb +0 -7
- data/spec/app/models/drug.rb +0 -6
- data/spec/app/models/dungeon.rb +0 -4
- data/spec/app/models/edit.rb +0 -5
- data/spec/app/models/email.rb +0 -6
- data/spec/app/models/employer.rb +0 -5
- data/spec/app/models/entry.rb +0 -6
- data/spec/app/models/eraser.rb +0 -1
- data/spec/app/models/even.rb +0 -7
- data/spec/app/models/event.rb +0 -22
- data/spec/app/models/exhibition.rb +0 -4
- data/spec/app/models/exhibitor.rb +0 -6
- data/spec/app/models/explosion.rb +0 -4
- data/spec/app/models/eye.rb +0 -9
- data/spec/app/models/eye_bowl.rb +0 -9
- data/spec/app/models/face.rb +0 -8
- data/spec/app/models/favorite.rb +0 -6
- data/spec/app/models/filesystem.rb +0 -5
- data/spec/app/models/fire_hydrant.rb +0 -6
- data/spec/app/models/firefox.rb +0 -4
- data/spec/app/models/fish.rb +0 -7
- data/spec/app/models/folder.rb +0 -7
- data/spec/app/models/folder_item.rb +0 -9
- data/spec/app/models/fruits.rb +0 -34
- data/spec/app/models/game.rb +0 -19
- data/spec/app/models/ghost.rb +0 -7
- data/spec/app/models/home.rb +0 -4
- data/spec/app/models/house.rb +0 -6
- data/spec/app/models/html_writer.rb +0 -3
- data/spec/app/models/id_key.rb +0 -6
- data/spec/app/models/image.rb +0 -22
- data/spec/app/models/implant.rb +0 -16
- data/spec/app/models/item.rb +0 -8
- data/spec/app/models/jar.rb +0 -7
- data/spec/app/models/kaleidoscope.rb +0 -6
- data/spec/app/models/kangaroo.rb +0 -4
- data/spec/app/models/label.rb +0 -47
- data/spec/app/models/language.rb +0 -5
- data/spec/app/models/lat_lng.rb +0 -15
- data/spec/app/models/league.rb +0 -11
- data/spec/app/models/learner.rb +0 -2
- data/spec/app/models/line_item.rb +0 -6
- data/spec/app/models/location.rb +0 -8
- data/spec/app/models/login.rb +0 -8
- data/spec/app/models/manufacturer.rb +0 -7
- data/spec/app/models/meat.rb +0 -4
- data/spec/app/models/membership.rb +0 -4
- data/spec/app/models/message.rb +0 -11
- data/spec/app/models/minim.rb +0 -7
- data/spec/app/models/mixed_drink.rb +0 -4
- data/spec/app/models/movie.rb +0 -13
- data/spec/app/models/my_hash.rb +0 -2
- data/spec/app/models/name.rb +0 -24
- data/spec/app/models/node.rb +0 -5
- data/spec/app/models/note.rb +0 -17
- data/spec/app/models/odd.rb +0 -7
- data/spec/app/models/ordered_post.rb +0 -11
- data/spec/app/models/ordered_preference.rb +0 -6
- data/spec/app/models/oscar.rb +0 -14
- data/spec/app/models/other_owner_object.rb +0 -2
- data/spec/app/models/override.rb +0 -16
- data/spec/app/models/ownable.rb +0 -6
- data/spec/app/models/owner.rb +0 -6
- data/spec/app/models/pack.rb +0 -3
- data/spec/app/models/page.rb +0 -16
- data/spec/app/models/page_question.rb +0 -4
- data/spec/app/models/palette.rb +0 -7
- data/spec/app/models/parent.rb +0 -5
- data/spec/app/models/parent_doc.rb +0 -6
- data/spec/app/models/passport.rb +0 -5
- data/spec/app/models/patient.rb +0 -9
- data/spec/app/models/pdf_writer.rb +0 -3
- data/spec/app/models/pencil.rb +0 -1
- data/spec/app/models/person.rb +0 -210
- data/spec/app/models/pet.rb +0 -23
- data/spec/app/models/pet_owner.rb +0 -6
- data/spec/app/models/phone.rb +0 -9
- data/spec/app/models/pizza.rb +0 -7
- data/spec/app/models/player.rb +0 -35
- data/spec/app/models/post.rb +0 -50
- data/spec/app/models/post_genre.rb +0 -6
- data/spec/app/models/powerup.rb +0 -11
- data/spec/app/models/preference.rb +0 -9
- data/spec/app/models/princess.rb +0 -10
- data/spec/app/models/product.rb +0 -17
- data/spec/app/models/profile.rb +0 -5
- data/spec/app/models/pronunciation.rb +0 -5
- data/spec/app/models/pub.rb +0 -6
- data/spec/app/models/purchase.rb +0 -4
- data/spec/app/models/question.rb +0 -8
- data/spec/app/models/quiz.rb +0 -7
- data/spec/app/models/rating.rb +0 -8
- data/spec/app/models/record.rb +0 -52
- data/spec/app/models/registry.rb +0 -4
- data/spec/app/models/role.rb +0 -7
- data/spec/app/models/root_category.rb +0 -4
- data/spec/app/models/sandwich.rb +0 -9
- data/spec/app/models/scheduler.rb +0 -7
- data/spec/app/models/seo.rb +0 -7
- data/spec/app/models/series.rb +0 -4
- data/spec/app/models/server.rb +0 -13
- data/spec/app/models/service.rb +0 -22
- data/spec/app/models/shape.rb +0 -12
- data/spec/app/models/shelf.rb +0 -5
- data/spec/app/models/shipment_address.rb +0 -3
- data/spec/app/models/shipping_container.rb +0 -5
- data/spec/app/models/shipping_pack.rb +0 -3
- data/spec/app/models/shop.rb +0 -6
- data/spec/app/models/short_agent.rb +0 -4
- data/spec/app/models/short_quiz.rb +0 -5
- data/spec/app/models/simple.rb +0 -5
- data/spec/app/models/slave.rb +0 -6
- data/spec/app/models/song.rb +0 -8
- data/spec/app/models/sound.rb +0 -5
- data/spec/app/models/square.rb +0 -4
- data/spec/app/models/staff.rb +0 -7
- data/spec/app/models/store_as_dup_test1.rb +0 -5
- data/spec/app/models/store_as_dup_test2.rb +0 -5
- data/spec/app/models/store_as_dup_test3.rb +0 -7
- data/spec/app/models/store_as_dup_test4.rb +0 -7
- data/spec/app/models/strategy.rb +0 -3
- data/spec/app/models/sub_item.rb +0 -3
- data/spec/app/models/subscription.rb +0 -5
- data/spec/app/models/survey.rb +0 -5
- data/spec/app/models/symptom.rb +0 -6
- data/spec/app/models/tag.rb +0 -8
- data/spec/app/models/target.rb +0 -5
- data/spec/app/models/template.rb +0 -5
- data/spec/app/models/thing.rb +0 -9
- data/spec/app/models/title.rb +0 -3
- data/spec/app/models/tool.rb +0 -8
- data/spec/app/models/topping.rb +0 -5
- data/spec/app/models/track.rb +0 -38
- data/spec/app/models/translation.rb +0 -5
- data/spec/app/models/tree.rb +0 -9
- data/spec/app/models/truck.rb +0 -3
- data/spec/app/models/user.rb +0 -21
- data/spec/app/models/user_account.rb +0 -10
- data/spec/app/models/validation_callback.rb +0 -10
- data/spec/app/models/vehicle.rb +0 -11
- data/spec/app/models/version.rb +0 -5
- data/spec/app/models/vertex.rb +0 -6
- data/spec/app/models/vet_visit.rb +0 -5
- data/spec/app/models/video.rb +0 -13
- data/spec/app/models/video_game.rb +0 -1
- data/spec/app/models/weapon.rb +0 -11
- data/spec/app/models/wiki_page.rb +0 -14
- data/spec/app/models/word.rb +0 -15
- data/spec/app/models/word_origin.rb +0 -11
- data/spec/app/models/writer.rb +0 -11
- data/spec/integration/matchable_spec.rb +0 -680
- data/spec/mongoid/contextual/geo_near_spec.rb +0 -469
- data/spec/mongoid/criteria/queryable/extensions/bignum_spec.rb +0 -58
- data/spec/mongoid/criteria/queryable/extensions/fixnum_spec.rb +0 -58
- data/spec/mongoid/criteria/queryable/forwardable_spec.rb +0 -87
- data/spec/mongoid/errors/eager_load_spec.rb +0 -29
- data/spec/mongoid/extensions_spec.rb +0 -29
- data/spec/mongoid/matchable/all_spec.rb +0 -31
- data/spec/mongoid/matchable/and_spec.rb +0 -187
- data/spec/mongoid/matchable/default_spec.rb +0 -137
- data/spec/mongoid/matchable/elem_match_spec.rb +0 -106
- data/spec/mongoid/matchable/eq_spec.rb +0 -48
- data/spec/mongoid/matchable/exists_spec.rb +0 -57
- data/spec/mongoid/matchable/gt_spec.rb +0 -86
- data/spec/mongoid/matchable/gte_spec.rb +0 -84
- data/spec/mongoid/matchable/in_spec.rb +0 -49
- data/spec/mongoid/matchable/lt_spec.rb +0 -85
- data/spec/mongoid/matchable/lte_spec.rb +0 -85
- data/spec/mongoid/matchable/ne_spec.rb +0 -46
- data/spec/mongoid/matchable/nin_spec.rb +0 -48
- data/spec/mongoid/matchable/nor_spec.rb +0 -209
- data/spec/mongoid/matchable/or_spec.rb +0 -131
- data/spec/mongoid/matchable/regexp_spec.rb +0 -59
- data/spec/mongoid/matchable/size_spec.rb +0 -25
- data/spec/mongoid/matchable_spec.rb +0 -853
- data/spec/mongoid/query_cache_spec.rb +0 -618
- data/spec/support/cluster_config.rb +0 -158
- data/spec/support/session_registry.rb +0 -50
- data.tar.gz.sig +0 -0
- metadata.gz.sig +0 -1
- /data/spec/{app → support}/models/array_field.rb +0 -0
- /data/spec/{app → support}/models/updatable.rb +0 -0
|
@@ -1,51 +1,56 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
require
|
|
4
|
-
require
|
|
5
|
-
require
|
|
6
|
-
require
|
|
7
|
-
require
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'mongoid/atomic_update_preparer'
|
|
4
|
+
require 'mongoid/pluckable'
|
|
5
|
+
require 'mongoid/contextual/mongo/documents_loader'
|
|
6
|
+
require 'mongoid/contextual/atomic'
|
|
7
|
+
require 'mongoid/contextual/aggregable/mongo'
|
|
8
|
+
require 'mongoid/contextual/command'
|
|
9
|
+
require 'mongoid/contextual/map_reduce'
|
|
10
|
+
require 'mongoid/association/eager_loadable'
|
|
8
11
|
|
|
9
12
|
module Mongoid
|
|
10
13
|
module Contextual
|
|
14
|
+
# Context object used for performing bulk query and persistence
|
|
15
|
+
# operations on documents which are persisted in the database and
|
|
16
|
+
# have not been loaded into application memory.
|
|
11
17
|
class Mongo
|
|
18
|
+
extend Forwardable
|
|
12
19
|
include Enumerable
|
|
13
20
|
include Aggregable::Mongo
|
|
14
21
|
include Atomic
|
|
15
22
|
include Association::EagerLoadable
|
|
16
23
|
include Queryable
|
|
24
|
+
include Pluckable
|
|
17
25
|
|
|
18
26
|
# Options constant.
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
:cursor_type,
|
|
32
|
-
:collation
|
|
33
|
-
].freeze
|
|
27
|
+
OPTIONS = %i[hint
|
|
28
|
+
limit
|
|
29
|
+
skip
|
|
30
|
+
sort
|
|
31
|
+
batch_size
|
|
32
|
+
max_scan
|
|
33
|
+
max_time_ms
|
|
34
|
+
snapshot
|
|
35
|
+
comment
|
|
36
|
+
read
|
|
37
|
+
cursor_type
|
|
38
|
+
collation].freeze
|
|
34
39
|
|
|
35
40
|
# @attribute [r] view The Mongo collection view.
|
|
36
41
|
attr_reader :view
|
|
37
42
|
|
|
38
|
-
#
|
|
43
|
+
# Run an explain on the criteria.
|
|
39
44
|
#
|
|
40
|
-
# @example
|
|
41
|
-
#
|
|
45
|
+
# @example Explain the criteria.
|
|
46
|
+
# Band.where(name: "Depeche Mode").explain
|
|
42
47
|
#
|
|
43
|
-
# @
|
|
48
|
+
# @param [ Hash ] options customizable options (See Mongo::Collection::View::Explainable)
|
|
44
49
|
#
|
|
45
|
-
# @
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
50
|
+
# @return [ Hash ] The explain result.
|
|
51
|
+
def_delegator :view, :explain
|
|
52
|
+
|
|
53
|
+
attr_reader :documents_loader
|
|
49
54
|
|
|
50
55
|
# Get the number of documents matching the query.
|
|
51
56
|
#
|
|
@@ -64,11 +69,38 @@ module Mongoid
|
|
|
64
69
|
# into the count.
|
|
65
70
|
#
|
|
66
71
|
# @return [ Integer ] The number of matches.
|
|
67
|
-
#
|
|
68
|
-
# @since 3.0.0
|
|
69
72
|
def count(options = {}, &block)
|
|
70
73
|
return super(&block) if block_given?
|
|
71
|
-
|
|
74
|
+
|
|
75
|
+
if valid_for_count_documents?
|
|
76
|
+
view.count_documents(options)
|
|
77
|
+
else
|
|
78
|
+
# TODO: Remove this when we remove the deprecated for_js API.
|
|
79
|
+
# https://jira.mongodb.org/browse/MONGOID-5681
|
|
80
|
+
view.count(options)
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# Get the estimated number of documents matching the query.
|
|
85
|
+
#
|
|
86
|
+
# Unlike count, estimated_count does not take a block because it is not
|
|
87
|
+
# traditionally defined (with a block) on Enumerable like count is.
|
|
88
|
+
#
|
|
89
|
+
# @example Get the estimated number of matching documents.
|
|
90
|
+
# context.estimated_count
|
|
91
|
+
#
|
|
92
|
+
# @param [ Hash ] options The options, such as maxTimeMS to be factored
|
|
93
|
+
# into the count.
|
|
94
|
+
#
|
|
95
|
+
# @return [ Integer ] The number of matches.
|
|
96
|
+
def estimated_count(options = {})
|
|
97
|
+
unless criteria.selector.empty?
|
|
98
|
+
raise Mongoid::Errors::InvalidEstimatedCountScoping.new(klass) if klass.default_scoping?
|
|
99
|
+
|
|
100
|
+
raise Mongoid::Errors::InvalidEstimatedCountCriteria.new(klass)
|
|
101
|
+
|
|
102
|
+
end
|
|
103
|
+
view.estimated_document_count(options)
|
|
72
104
|
end
|
|
73
105
|
|
|
74
106
|
# Delete all documents in the database that match the selector.
|
|
@@ -77,12 +109,10 @@ module Mongoid
|
|
|
77
109
|
# context.delete
|
|
78
110
|
#
|
|
79
111
|
# @return [ nil ] Nil.
|
|
80
|
-
#
|
|
81
|
-
# @since 3.0.0
|
|
82
112
|
def delete
|
|
83
113
|
view.delete_many.deleted_count
|
|
84
114
|
end
|
|
85
|
-
alias
|
|
115
|
+
alias delete_all delete
|
|
86
116
|
|
|
87
117
|
# Destroy all documents in the database that match the selector.
|
|
88
118
|
#
|
|
@@ -90,8 +120,6 @@ module Mongoid
|
|
|
90
120
|
# context.destroy
|
|
91
121
|
#
|
|
92
122
|
# @return [ nil ] Nil.
|
|
93
|
-
#
|
|
94
|
-
# @since 3.0.0
|
|
95
123
|
def destroy
|
|
96
124
|
each.inject(0) do |count, doc|
|
|
97
125
|
doc.destroy
|
|
@@ -99,21 +127,22 @@ module Mongoid
|
|
|
99
127
|
count
|
|
100
128
|
end
|
|
101
129
|
end
|
|
102
|
-
alias
|
|
130
|
+
alias destroy_all destroy
|
|
103
131
|
|
|
104
132
|
# Get the distinct values in the db for the provided field.
|
|
105
133
|
#
|
|
106
134
|
# @example Get the distinct values.
|
|
107
135
|
# context.distinct(:name)
|
|
108
136
|
#
|
|
109
|
-
# @param [ String
|
|
137
|
+
# @param [ String | Symbol ] field The name of the field.
|
|
110
138
|
#
|
|
111
139
|
# @return [ Array<Object> ] The distinct values for the field.
|
|
112
|
-
#
|
|
113
|
-
# @since 3.0.0
|
|
114
140
|
def distinct(field)
|
|
115
|
-
|
|
116
|
-
|
|
141
|
+
name = klass.cleanse_localized_field_names(field)
|
|
142
|
+
|
|
143
|
+
view.distinct(name).map do |value|
|
|
144
|
+
is_translation = "#{name}_translations" == field.to_s
|
|
145
|
+
recursive_demongoize(name, value, is_translation)
|
|
117
146
|
end
|
|
118
147
|
end
|
|
119
148
|
|
|
@@ -126,14 +155,11 @@ module Mongoid
|
|
|
126
155
|
# end
|
|
127
156
|
#
|
|
128
157
|
# @return [ Enumerator ] The enumerator.
|
|
129
|
-
#
|
|
130
|
-
# @since 3.0.0
|
|
131
158
|
def each(&block)
|
|
132
159
|
if block_given?
|
|
133
160
|
documents_for_iteration.each do |doc|
|
|
134
161
|
yield_document(doc, &block)
|
|
135
162
|
end
|
|
136
|
-
@cache_loaded = true
|
|
137
163
|
self
|
|
138
164
|
else
|
|
139
165
|
to_enum
|
|
@@ -145,34 +171,31 @@ module Mongoid
|
|
|
145
171
|
# @example Do any documents exist for the context.
|
|
146
172
|
# context.exists?
|
|
147
173
|
#
|
|
174
|
+
# @example Do any documents exist for given _id.
|
|
175
|
+
# context.exists?(BSON::ObjectId(...))
|
|
176
|
+
#
|
|
177
|
+
# @example Do any documents exist for given conditions.
|
|
178
|
+
# context.exists?(name: "...")
|
|
179
|
+
#
|
|
148
180
|
# @note We don't use count here since Mongo does not use counted
|
|
149
|
-
# b-tree indexes
|
|
150
|
-
# used to determine the value.
|
|
181
|
+
# b-tree indexes.
|
|
151
182
|
#
|
|
152
|
-
# @
|
|
183
|
+
# @param [ Hash | Object | false ] id_or_conditions an _id to
|
|
184
|
+
# search for, a hash of conditions, nil or false.
|
|
153
185
|
#
|
|
154
|
-
# @
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
return
|
|
186
|
+
# @return [ true | false ] If the count is more than zero.
|
|
187
|
+
# Always false if passed nil or false.
|
|
188
|
+
def exists?(id_or_conditions = :none)
|
|
189
|
+
return false if view.limit == 0
|
|
158
190
|
|
|
159
|
-
|
|
160
|
-
|
|
191
|
+
case id_or_conditions
|
|
192
|
+
when :none then !!view.projection(_id: 1).limit(1).first
|
|
193
|
+
when nil, false then false
|
|
194
|
+
when Hash then Mongo.new(criteria.where(id_or_conditions)).exists?
|
|
195
|
+
else Mongo.new(criteria.where(_id: id_or_conditions)).exists?
|
|
161
196
|
end
|
|
162
197
|
end
|
|
163
198
|
|
|
164
|
-
# Run an explain on the criteria.
|
|
165
|
-
#
|
|
166
|
-
# @example Explain the criteria.
|
|
167
|
-
# Band.where(name: "Depeche Mode").explain
|
|
168
|
-
#
|
|
169
|
-
# @return [ Hash ] The explain result.
|
|
170
|
-
#
|
|
171
|
-
# @since 3.0.0
|
|
172
|
-
def explain
|
|
173
|
-
view.explain
|
|
174
|
-
end
|
|
175
|
-
|
|
176
199
|
# Execute the find and modify command, used for MongoDB's
|
|
177
200
|
# $findAndModify.
|
|
178
201
|
#
|
|
@@ -182,17 +205,15 @@ module Mongoid
|
|
|
182
205
|
# @param [ Hash ] update The updates.
|
|
183
206
|
# @param [ Hash ] options The command options.
|
|
184
207
|
#
|
|
185
|
-
# @option options [ :before
|
|
208
|
+
# @option options [ :before | :after ] :return_document Return the updated document
|
|
186
209
|
# from before or after update.
|
|
187
|
-
# @option options [ true
|
|
210
|
+
# @option options [ true | false ] :upsert Create the document if it doesn't exist.
|
|
188
211
|
#
|
|
189
212
|
# @return [ Document ] The result of the command.
|
|
190
|
-
#
|
|
191
|
-
# @since 5.0.0
|
|
192
213
|
def find_one_and_update(update, options = {})
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
214
|
+
return unless doc = view.find_one_and_update(update, options)
|
|
215
|
+
|
|
216
|
+
Factory.from_db(klass, doc)
|
|
196
217
|
end
|
|
197
218
|
|
|
198
219
|
# Execute the find and modify command, used for MongoDB's
|
|
@@ -204,17 +225,15 @@ module Mongoid
|
|
|
204
225
|
# @param [ Hash ] replacement The replacement.
|
|
205
226
|
# @param [ Hash ] options The command options.
|
|
206
227
|
#
|
|
207
|
-
# @option options [ :before
|
|
228
|
+
# @option options [ :before | :after ] :return_document Return the updated document
|
|
208
229
|
# from before or after update.
|
|
209
|
-
# @option options [ true
|
|
230
|
+
# @option options [ true | false ] :upsert Create the document if it doesn't exist.
|
|
210
231
|
#
|
|
211
232
|
# @return [ Document ] The result of the command.
|
|
212
|
-
#
|
|
213
|
-
# @since 5.0.0
|
|
214
233
|
def find_one_and_replace(replacement, options = {})
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
234
|
+
return unless doc = view.find_one_and_replace(replacement, options)
|
|
235
|
+
|
|
236
|
+
Factory.from_db(klass, doc)
|
|
218
237
|
end
|
|
219
238
|
|
|
220
239
|
# Execute the find and modify command, used for MongoDB's
|
|
@@ -224,108 +243,20 @@ module Mongoid
|
|
|
224
243
|
# context.find_one_and_delete
|
|
225
244
|
#
|
|
226
245
|
# @return [ Document ] The result of the command.
|
|
227
|
-
#
|
|
228
|
-
# @since 5.0.0
|
|
229
246
|
def find_one_and_delete
|
|
230
|
-
|
|
231
|
-
Factory.from_db(klass, doc)
|
|
232
|
-
end
|
|
233
|
-
end
|
|
247
|
+
return unless doc = view.find_one_and_delete
|
|
234
248
|
|
|
235
|
-
|
|
236
|
-
#
|
|
237
|
-
# @example Get the first document.
|
|
238
|
-
# context.first
|
|
239
|
-
#
|
|
240
|
-
# @note Automatically adding a sort on _id when no other sort is
|
|
241
|
-
# defined on the criteria has the potential to cause bad performance issues.
|
|
242
|
-
# If you experience unexpected poor performance when using #first or #last
|
|
243
|
-
# and have no sort defined on the criteria, use the option { id_sort: :none }.
|
|
244
|
-
# Be aware that #first/#last won't guarantee order in this case.
|
|
245
|
-
#
|
|
246
|
-
# @param [ Hash ] opts The options for the query returning the first document.
|
|
247
|
-
#
|
|
248
|
-
# @option opts [ :none ] :id_sort Don't apply a sort on _id if no other sort
|
|
249
|
-
# is defined on the criteria.
|
|
250
|
-
#
|
|
251
|
-
# @return [ Document ] The first document.
|
|
252
|
-
#
|
|
253
|
-
# @since 3.0.0
|
|
254
|
-
def first(opts = {})
|
|
255
|
-
return documents.first if cached? && cache_loaded?
|
|
256
|
-
try_cache(:first) do
|
|
257
|
-
if sort = view.sort || ({ _id: 1 } unless opts[:id_sort] == :none)
|
|
258
|
-
if raw_doc = view.sort(sort).limit(-1).first
|
|
259
|
-
doc = Factory.from_db(klass, raw_doc, criteria)
|
|
260
|
-
eager_load([doc]).first
|
|
261
|
-
end
|
|
262
|
-
else
|
|
263
|
-
if raw_doc = view.limit(-1).first
|
|
264
|
-
doc = Factory.from_db(klass, raw_doc, criteria)
|
|
265
|
-
eager_load([doc]).first
|
|
266
|
-
end
|
|
267
|
-
end
|
|
268
|
-
end
|
|
249
|
+
Factory.from_db(klass, doc)
|
|
269
250
|
end
|
|
270
|
-
alias :one :first
|
|
271
251
|
|
|
272
252
|
# Return the first result without applying sort
|
|
273
253
|
#
|
|
274
254
|
# @api private
|
|
275
|
-
#
|
|
276
|
-
# @since 4.0.2
|
|
277
255
|
def find_first
|
|
278
|
-
return
|
|
279
|
-
if raw_doc = view.first
|
|
280
|
-
doc = Factory.from_db(klass, raw_doc, criteria)
|
|
281
|
-
eager_load([doc]).first
|
|
282
|
-
end
|
|
283
|
-
end
|
|
256
|
+
return unless raw_doc = view.first
|
|
284
257
|
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
# @example Find documents close to 10, 10.
|
|
288
|
-
# context.geo_near([ 10, 10 ])
|
|
289
|
-
#
|
|
290
|
-
# @example Find with spherical distance.
|
|
291
|
-
# context.geo_near([ 10, 10 ]).spherical
|
|
292
|
-
#
|
|
293
|
-
# @example Find with a max distance.
|
|
294
|
-
# context.geo_near([ 10, 10 ]).max_distance(0.5)
|
|
295
|
-
#
|
|
296
|
-
# @example Provide a distance multiplier.
|
|
297
|
-
# context.geo_near([ 10, 10 ]).distance_multiplier(1133)
|
|
298
|
-
#
|
|
299
|
-
# @param [ Array<Float> ] coordinates The coordinates.
|
|
300
|
-
#
|
|
301
|
-
# @return [ GeoNear ] The GeoNear command.
|
|
302
|
-
#
|
|
303
|
-
# @since 3.1.0
|
|
304
|
-
def geo_near(coordinates)
|
|
305
|
-
GeoNear.new(collection, criteria, coordinates)
|
|
306
|
-
end
|
|
307
|
-
|
|
308
|
-
# Invoke the block for each element of Contextual. Create a new array
|
|
309
|
-
# containing the values returned by the block.
|
|
310
|
-
#
|
|
311
|
-
# If the symbol field name is passed instead of the block, additional
|
|
312
|
-
# optimizations would be used.
|
|
313
|
-
#
|
|
314
|
-
# @example Map by some field.
|
|
315
|
-
# context.map(:field1)
|
|
316
|
-
#
|
|
317
|
-
# @example Map with block.
|
|
318
|
-
# context.map(&:field1)
|
|
319
|
-
#
|
|
320
|
-
# @param [ Symbol ] field The field name.
|
|
321
|
-
#
|
|
322
|
-
# @return [ Array ] The result of mapping.
|
|
323
|
-
def map(field = nil, &block)
|
|
324
|
-
if block_given?
|
|
325
|
-
super(&block)
|
|
326
|
-
else
|
|
327
|
-
criteria.pluck(field)
|
|
328
|
-
end
|
|
258
|
+
doc = Factory.from_db(klass, raw_doc, criteria)
|
|
259
|
+
eager_load([ doc ]).first
|
|
329
260
|
end
|
|
330
261
|
|
|
331
262
|
# Create the new Mongo context. This delegates operations to the
|
|
@@ -335,58 +266,27 @@ module Mongoid
|
|
|
335
266
|
# Mongo.new(criteria)
|
|
336
267
|
#
|
|
337
268
|
# @param [ Criteria ] criteria The criteria.
|
|
338
|
-
#
|
|
339
|
-
# @since 3.0.0
|
|
340
269
|
def initialize(criteria)
|
|
341
|
-
@criteria, @klass
|
|
270
|
+
@criteria, @klass = criteria, criteria.klass
|
|
342
271
|
@collection = @klass.collection
|
|
343
272
|
criteria.send(:merge_type_selection)
|
|
344
273
|
@view = collection.find(criteria.selector, session: _session)
|
|
345
274
|
apply_options
|
|
346
275
|
end
|
|
347
276
|
|
|
348
|
-
|
|
277
|
+
def_delegator :@klass, :database_field_name
|
|
349
278
|
|
|
350
|
-
#
|
|
351
|
-
#
|
|
352
|
-
# @example Get the last document.
|
|
353
|
-
# context.last
|
|
354
|
-
#
|
|
355
|
-
# @note Automatically adding a sort on _id when no other sort is
|
|
356
|
-
# defined on the criteria has the potential to cause bad performance issues.
|
|
357
|
-
# If you experience unexpected poor performance when using #first or #last
|
|
358
|
-
# and have no sort defined on the criteria, use the option { id_sort: :none }.
|
|
359
|
-
# Be aware that #first/#last won't guarantee order in this case.
|
|
360
|
-
#
|
|
361
|
-
# @param [ Hash ] opts The options for the query returning the first document.
|
|
362
|
-
#
|
|
363
|
-
# @option opts [ :none ] :id_sort Don't apply a sort on _id if no other sort
|
|
364
|
-
# is defined on the criteria.
|
|
365
|
-
#
|
|
366
|
-
# @since 3.0.0
|
|
367
|
-
def last(opts = {})
|
|
368
|
-
try_cache(:last) do
|
|
369
|
-
with_inverse_sorting(opts) do
|
|
370
|
-
if raw_doc = view.limit(-1).first
|
|
371
|
-
doc = Factory.from_db(klass, raw_doc, criteria)
|
|
372
|
-
eager_load([doc]).first
|
|
373
|
-
end
|
|
374
|
-
end
|
|
375
|
-
end
|
|
376
|
-
end
|
|
377
|
-
|
|
378
|
-
# Get's the number of documents matching the query selector.
|
|
279
|
+
# Returns the number of documents in the database matching
|
|
280
|
+
# the query selector.
|
|
379
281
|
#
|
|
380
282
|
# @example Get the length.
|
|
381
283
|
# context.length
|
|
382
284
|
#
|
|
383
285
|
# @return [ Integer ] The number of documents.
|
|
384
|
-
#
|
|
385
|
-
# @since 3.0.0
|
|
386
286
|
def length
|
|
387
|
-
|
|
287
|
+
count
|
|
388
288
|
end
|
|
389
|
-
alias
|
|
289
|
+
alias size length
|
|
390
290
|
|
|
391
291
|
# Limits the number of documents that are returned from the database.
|
|
392
292
|
#
|
|
@@ -396,8 +296,6 @@ module Mongoid
|
|
|
396
296
|
# @param [ Integer ] value The number of documents to return.
|
|
397
297
|
#
|
|
398
298
|
# @return [ Mongo ] The context.
|
|
399
|
-
#
|
|
400
|
-
# @since 3.0.0
|
|
401
299
|
def limit(value)
|
|
402
300
|
@view = view.limit(value) and self
|
|
403
301
|
end
|
|
@@ -411,37 +309,148 @@ module Mongoid
|
|
|
411
309
|
# @param [ String ] reduce The reduce js function.
|
|
412
310
|
#
|
|
413
311
|
# @return [ MapReduce ] The map/reduce lazy wrapper.
|
|
414
|
-
#
|
|
415
|
-
# @since 3.0.0
|
|
416
312
|
def map_reduce(map, reduce)
|
|
417
313
|
MapReduce.new(collection, criteria, map, reduce)
|
|
418
314
|
end
|
|
419
315
|
|
|
420
|
-
# Pluck the
|
|
421
|
-
#
|
|
316
|
+
# Pluck the field value(s) from the database. Returns one
|
|
317
|
+
# result for each document found in the database for
|
|
318
|
+
# the context. The results are normalized according to their
|
|
319
|
+
# Mongoid field types. Note that the results may include
|
|
320
|
+
# duplicates and nil values.
|
|
422
321
|
#
|
|
423
322
|
# @example Pluck a field.
|
|
424
323
|
# context.pluck(:_id)
|
|
425
324
|
#
|
|
426
|
-
# @
|
|
427
|
-
#
|
|
325
|
+
# @param [ [ String | Symbol ]... ] *fields Field(s) to pluck,
|
|
326
|
+
# which may include nested fields using dot-notation.
|
|
428
327
|
#
|
|
429
|
-
# @
|
|
328
|
+
# @return [ Array<Object> | Array<Array<Object>> ] The plucked values.
|
|
329
|
+
# If the *fields arg contains a single value, each result
|
|
330
|
+
# in the array will be a single value. Otherwise, each
|
|
331
|
+
# result in the array will be an array of values.
|
|
332
|
+
def pluck(*fields)
|
|
333
|
+
# Multiple fields can map to the same field name. For example,
|
|
334
|
+
# plucking a field and its _translations field map to the same
|
|
335
|
+
# field in the database. because of this, we need to prepare the
|
|
336
|
+
# projection specifically.
|
|
337
|
+
prep = prepare_pluck(fields, prepare_projection: true)
|
|
338
|
+
pluck_from_documents(view.projection(prep[:projection]), prep[:field_names])
|
|
339
|
+
end
|
|
340
|
+
|
|
341
|
+
# Pick the single field values from the database.
|
|
430
342
|
#
|
|
431
|
-
# @
|
|
343
|
+
# @example Pick a field.
|
|
344
|
+
# context.pick(:_id)
|
|
432
345
|
#
|
|
433
|
-
# @
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
346
|
+
# @param [ [ String | Symbol ]... ] *fields Field(s) to pick.
|
|
347
|
+
#
|
|
348
|
+
# @return [ Object | Array<Object> ] The picked values.
|
|
349
|
+
def pick(*fields)
|
|
350
|
+
limit(1).pluck(*fields).first
|
|
351
|
+
end
|
|
352
|
+
|
|
353
|
+
# Take the given number of documents from the database.
|
|
354
|
+
#
|
|
355
|
+
# @example Take 10 documents
|
|
356
|
+
# context.take(10)
|
|
357
|
+
#
|
|
358
|
+
# @param [ Integer | nil ] limit The number of documents to return or nil.
|
|
359
|
+
#
|
|
360
|
+
# @return [ Document | Array<Document> ] The list of documents, or one
|
|
361
|
+
# document if no value was given.
|
|
362
|
+
def take(limit = nil)
|
|
363
|
+
if limit
|
|
364
|
+
limit(limit).to_a
|
|
365
|
+
else
|
|
366
|
+
# Do to_a first so that the Mongo#first method is not used and the
|
|
367
|
+
# result is not sorted.
|
|
368
|
+
limit(1).to_a.first
|
|
438
369
|
end
|
|
370
|
+
end
|
|
439
371
|
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
372
|
+
# Take one document from the database and raise an error if there are none.
|
|
373
|
+
#
|
|
374
|
+
# @example Take a document
|
|
375
|
+
# context.take!
|
|
376
|
+
#
|
|
377
|
+
# @return [ Document ] The document.
|
|
378
|
+
#
|
|
379
|
+
# @raise [ Mongoid::Errors::DocumentNotFound ] raises when there are no
|
|
380
|
+
# documents to take.
|
|
381
|
+
def take!
|
|
382
|
+
# Do to_a first so that the Mongo#first method is not used and the
|
|
383
|
+
# result is not sorted.
|
|
384
|
+
raise Errors::DocumentNotFound.new(klass, nil, nil) unless fst = limit(1).to_a.first
|
|
385
|
+
|
|
386
|
+
fst
|
|
387
|
+
end
|
|
388
|
+
|
|
389
|
+
# Get a hash of counts for the values of a single field. For example,
|
|
390
|
+
# if the following documents were in the database:
|
|
391
|
+
#
|
|
392
|
+
# { _id: 1, age: 21 }
|
|
393
|
+
# { _id: 2, age: 21 }
|
|
394
|
+
# { _id: 3, age: 22 }
|
|
395
|
+
#
|
|
396
|
+
# Model.tally("age")
|
|
397
|
+
#
|
|
398
|
+
# would yield the following result:
|
|
399
|
+
#
|
|
400
|
+
# { 21 => 2, 22 => 1 }
|
|
401
|
+
#
|
|
402
|
+
# When tallying a field inside an array or embeds_many association:
|
|
403
|
+
#
|
|
404
|
+
# { _id: 1, array: [ { x: 1 }, { x: 2 } ] }
|
|
405
|
+
# { _id: 2, array: [ { x: 1 }, { x: 2 } ] }
|
|
406
|
+
# { _id: 3, array: [ { x: 1 }, { x: 3 } ] }
|
|
407
|
+
#
|
|
408
|
+
# Model.tally("array.x")
|
|
409
|
+
#
|
|
410
|
+
# The keys of the resulting hash are arrays:
|
|
411
|
+
#
|
|
412
|
+
# { [ 1, 2 ] => 2, [ 1, 3 ] => 1 }
|
|
413
|
+
#
|
|
414
|
+
# Note that if tallying an element in an array of hashes, and the key
|
|
415
|
+
# doesn't exist in some of the hashes, tally will not include those
|
|
416
|
+
# nil keys in the resulting hash:
|
|
417
|
+
#
|
|
418
|
+
# { _id: 1, array: [ { x: 1 }, { x: 2 }, { y: 3 } ] }
|
|
419
|
+
#
|
|
420
|
+
# Model.tally("array.x")
|
|
421
|
+
# # => { [ 1, 2 ] => 1 }
|
|
422
|
+
#
|
|
423
|
+
# @param [ String | Symbol ] field The field name.
|
|
424
|
+
#
|
|
425
|
+
# @return [ Hash ] The hash of counts.
|
|
426
|
+
def tally(field)
|
|
427
|
+
name = klass.cleanse_localized_field_names(field)
|
|
428
|
+
|
|
429
|
+
fld = klass.traverse_association_tree(name)
|
|
430
|
+
pipeline = [ { '$group' => { _id: "$#{name}", counts: { '$sum': 1 } } } ]
|
|
431
|
+
pipeline.unshift('$match' => view.filter) unless view.filter.blank?
|
|
432
|
+
|
|
433
|
+
collection.aggregate(pipeline).each_with_object({}) do |doc, tallies|
|
|
434
|
+
is_translation = "#{name}_translations" == field.to_s
|
|
435
|
+
val = doc['_id']
|
|
436
|
+
|
|
437
|
+
key = if val.is_a?(Array)
|
|
438
|
+
val.map do |v|
|
|
439
|
+
demongoize_with_field(fld, v, is_translation)
|
|
440
|
+
end
|
|
441
|
+
else
|
|
442
|
+
demongoize_with_field(fld, val, is_translation)
|
|
443
|
+
end
|
|
444
|
+
|
|
445
|
+
# The only time where a key will already exist in the tallies hash
|
|
446
|
+
# is when the values are stored differently in the database, but
|
|
447
|
+
# demongoize to the same value. A good example of when this happens
|
|
448
|
+
# is when using localized fields. While the server query won't group
|
|
449
|
+
# together hashes that have other values in different languages, the
|
|
450
|
+
# demongoized value is just the translation in the current locale,
|
|
451
|
+
# which can be the same across multiple of those unequal hashes.
|
|
452
|
+
tallies[key] ||= 0
|
|
453
|
+
tallies[key] += doc['counts']
|
|
445
454
|
end
|
|
446
455
|
end
|
|
447
456
|
|
|
@@ -453,8 +462,6 @@ module Mongoid
|
|
|
453
462
|
# @param [ Integer ] value The number of documents to skip.
|
|
454
463
|
#
|
|
455
464
|
# @return [ Mongo ] The context.
|
|
456
|
-
#
|
|
457
|
-
# @since 3.0.0
|
|
458
465
|
def skip(value)
|
|
459
466
|
@view = view.skip(value) and self
|
|
460
467
|
end
|
|
@@ -468,8 +475,6 @@ module Mongoid
|
|
|
468
475
|
# pairs.
|
|
469
476
|
#
|
|
470
477
|
# @return [ Mongo ] The context.
|
|
471
|
-
#
|
|
472
|
-
# @since 3.0.0
|
|
473
478
|
def sort(values = nil, &block)
|
|
474
479
|
if block_given?
|
|
475
480
|
super(&block)
|
|
@@ -492,9 +497,7 @@ module Mongoid
|
|
|
492
497
|
# @option opts [ Array ] :array_filters A set of filters specifying to which array elements
|
|
493
498
|
# an update should apply.
|
|
494
499
|
#
|
|
495
|
-
# @return [ nil
|
|
496
|
-
#
|
|
497
|
-
# @since 3.0.0
|
|
500
|
+
# @return [ nil | false ] False if no attributes were provided.
|
|
498
501
|
def update(attributes = nil, opts = {})
|
|
499
502
|
update_documents(attributes, :update_one, opts)
|
|
500
503
|
end
|
|
@@ -510,33 +513,257 @@ module Mongoid
|
|
|
510
513
|
# @option opts [ Array ] :array_filters A set of filters specifying to which array elements
|
|
511
514
|
# an update should apply.
|
|
512
515
|
#
|
|
513
|
-
# @return [ nil
|
|
514
|
-
#
|
|
515
|
-
# @since 3.0.0
|
|
516
|
+
# @return [ nil | false ] False if no attributes were provided.
|
|
516
517
|
def update_all(attributes = nil, opts = {})
|
|
517
518
|
update_documents(attributes, :update_many, opts)
|
|
518
519
|
end
|
|
519
520
|
|
|
520
|
-
|
|
521
|
+
# Get the first document in the database for the criteria's selector.
|
|
522
|
+
#
|
|
523
|
+
# @example Get the first document.
|
|
524
|
+
# context.first
|
|
525
|
+
#
|
|
526
|
+
# @note Automatically adding a sort on _id when no other sort is
|
|
527
|
+
# defined on the criteria has the potential to cause bad performance issues.
|
|
528
|
+
# If you experience unexpected poor performance when using #first or #last
|
|
529
|
+
# and have no sort defined on the criteria, use #take instead.
|
|
530
|
+
# Be aware that #take won't guarantee order.
|
|
531
|
+
#
|
|
532
|
+
# @param [ Integer ] limit The number of documents to return.
|
|
533
|
+
#
|
|
534
|
+
# @return [ Document | nil ] The first document or nil if none is found.
|
|
535
|
+
def first(limit = nil)
|
|
536
|
+
if limit.nil?
|
|
537
|
+
retrieve_nth(0)
|
|
538
|
+
else
|
|
539
|
+
retrieve_nth_with_limit(0, limit)
|
|
540
|
+
end
|
|
541
|
+
end
|
|
542
|
+
alias one first
|
|
521
543
|
|
|
522
|
-
#
|
|
544
|
+
# Get the first document in the database for the criteria's selector or
|
|
545
|
+
# raise an error if none is found.
|
|
546
|
+
#
|
|
547
|
+
# @example Get the first document.
|
|
548
|
+
# context.first!
|
|
549
|
+
#
|
|
550
|
+
# @note Automatically adding a sort on _id when no other sort is
|
|
551
|
+
# defined on the criteria has the potential to cause bad performance issues.
|
|
552
|
+
# If you experience unexpected poor performance when using #first! or #last!
|
|
553
|
+
# and have no sort defined on the criteria, use #take! instead.
|
|
554
|
+
# Be aware that #take! won't guarantee order.
|
|
555
|
+
#
|
|
556
|
+
# @return [ Document ] The first document.
|
|
557
|
+
#
|
|
558
|
+
# @raise [ Mongoid::Errors::DocumentNotFound ] raises when there are no
|
|
559
|
+
# documents available.
|
|
560
|
+
def first!
|
|
561
|
+
first || raise_document_not_found_error
|
|
562
|
+
end
|
|
563
|
+
|
|
564
|
+
# Get the last document in the database for the criteria's selector.
|
|
565
|
+
#
|
|
566
|
+
# @example Get the last document.
|
|
567
|
+
# context.last
|
|
523
568
|
#
|
|
524
|
-
# @
|
|
569
|
+
# @note Automatically adding a sort on _id when no other sort is
|
|
570
|
+
# defined on the criteria has the potential to cause bad performance issues.
|
|
571
|
+
# If you experience unexpected poor performance when using #first or #last
|
|
572
|
+
# and have no sort defined on the criteria, use #take instead.
|
|
573
|
+
# Be aware that #take won't guarantee order.
|
|
525
574
|
#
|
|
526
|
-
# @
|
|
575
|
+
# @param [ Integer ] limit The number of documents to return.
|
|
527
576
|
#
|
|
528
|
-
# @
|
|
529
|
-
def
|
|
530
|
-
|
|
531
|
-
|
|
577
|
+
# @return [ Document | nil ] The last document or nil if none is found.
|
|
578
|
+
def last(limit = nil)
|
|
579
|
+
if limit.nil?
|
|
580
|
+
retrieve_nth_to_last(0)
|
|
532
581
|
else
|
|
533
|
-
|
|
534
|
-
instance_variable_set("@#{key}", ret = yield)
|
|
535
|
-
end
|
|
536
|
-
ret
|
|
582
|
+
retrieve_nth_to_last_with_limit(0, limit)
|
|
537
583
|
end
|
|
538
584
|
end
|
|
539
585
|
|
|
586
|
+
# Get the last document in the database for the criteria's selector or
|
|
587
|
+
# raise an error if none is found.
|
|
588
|
+
#
|
|
589
|
+
# @example Get the last document.
|
|
590
|
+
# context.last!
|
|
591
|
+
#
|
|
592
|
+
# @note Automatically adding a sort on _id when no other sort is
|
|
593
|
+
# defined on the criteria has the potential to cause bad performance issues.
|
|
594
|
+
# If you experience unexpected poor performance when using #first! or #last!
|
|
595
|
+
# and have no sort defined on the criteria, use #take! instead.
|
|
596
|
+
# Be aware that #take! won't guarantee order.
|
|
597
|
+
#
|
|
598
|
+
# @return [ Document ] The last document.
|
|
599
|
+
#
|
|
600
|
+
# @raise [ Mongoid::Errors::DocumentNotFound ] raises when there are no
|
|
601
|
+
# documents available.
|
|
602
|
+
def last!
|
|
603
|
+
last || raise_document_not_found_error
|
|
604
|
+
end
|
|
605
|
+
|
|
606
|
+
# Get the second document in the database for the criteria's selector.
|
|
607
|
+
#
|
|
608
|
+
# @example Get the second document.
|
|
609
|
+
# context.second
|
|
610
|
+
#
|
|
611
|
+
# @return [ Document | nil ] The second document or nil if none is found.
|
|
612
|
+
def second
|
|
613
|
+
retrieve_nth(1)
|
|
614
|
+
end
|
|
615
|
+
|
|
616
|
+
# Get the second document in the database for the criteria's selector or
|
|
617
|
+
# raise an error if none is found.
|
|
618
|
+
#
|
|
619
|
+
# @example Get the second document.
|
|
620
|
+
# context.second!
|
|
621
|
+
#
|
|
622
|
+
# @return [ Document ] The second document.
|
|
623
|
+
#
|
|
624
|
+
# @raise [ Mongoid::Errors::DocumentNotFound ] raises when there are no
|
|
625
|
+
# documents available.
|
|
626
|
+
def second!
|
|
627
|
+
second || raise_document_not_found_error
|
|
628
|
+
end
|
|
629
|
+
|
|
630
|
+
# Get the third document in the database for the criteria's selector.
|
|
631
|
+
#
|
|
632
|
+
# @example Get the third document.
|
|
633
|
+
# context.third
|
|
634
|
+
#
|
|
635
|
+
# @return [ Document | nil ] The third document or nil if none is found.
|
|
636
|
+
def third
|
|
637
|
+
retrieve_nth(2)
|
|
638
|
+
end
|
|
639
|
+
|
|
640
|
+
# Get the third document in the database for the criteria's selector or
|
|
641
|
+
# raise an error if none is found.
|
|
642
|
+
#
|
|
643
|
+
# @example Get the third document.
|
|
644
|
+
# context.third!
|
|
645
|
+
#
|
|
646
|
+
# @return [ Document ] The third document.
|
|
647
|
+
#
|
|
648
|
+
# @raise [ Mongoid::Errors::DocumentNotFound ] raises when there are no
|
|
649
|
+
# documents available.
|
|
650
|
+
def third!
|
|
651
|
+
third || raise_document_not_found_error
|
|
652
|
+
end
|
|
653
|
+
|
|
654
|
+
# Get the fourth document in the database for the criteria's selector.
|
|
655
|
+
#
|
|
656
|
+
# @example Get the fourth document.
|
|
657
|
+
# context.fourth
|
|
658
|
+
#
|
|
659
|
+
# @return [ Document | nil ] The fourth document or nil if none is found.
|
|
660
|
+
def fourth
|
|
661
|
+
retrieve_nth(3)
|
|
662
|
+
end
|
|
663
|
+
|
|
664
|
+
# Get the fourth document in the database for the criteria's selector or
|
|
665
|
+
# raise an error if none is found.
|
|
666
|
+
#
|
|
667
|
+
# @example Get the fourth document.
|
|
668
|
+
# context.fourth!
|
|
669
|
+
#
|
|
670
|
+
# @return [ Document ] The fourth document.
|
|
671
|
+
#
|
|
672
|
+
# @raise [ Mongoid::Errors::DocumentNotFound ] raises when there are no
|
|
673
|
+
# documents available.
|
|
674
|
+
def fourth!
|
|
675
|
+
fourth || raise_document_not_found_error
|
|
676
|
+
end
|
|
677
|
+
|
|
678
|
+
# Get the fifth document in the database for the criteria's selector.
|
|
679
|
+
#
|
|
680
|
+
# @example Get the fifth document.
|
|
681
|
+
# context.fifth
|
|
682
|
+
#
|
|
683
|
+
# @return [ Document | nil ] The fifth document or nil if none is found.
|
|
684
|
+
def fifth
|
|
685
|
+
retrieve_nth(4)
|
|
686
|
+
end
|
|
687
|
+
|
|
688
|
+
# Get the fifth document in the database for the criteria's selector or
|
|
689
|
+
# raise an error if none is found.
|
|
690
|
+
#
|
|
691
|
+
# @example Get the fifth document.
|
|
692
|
+
# context.fifth!
|
|
693
|
+
#
|
|
694
|
+
# @return [ Document ] The fifth document.
|
|
695
|
+
#
|
|
696
|
+
# @raise [ Mongoid::Errors::DocumentNotFound ] raises when there are no
|
|
697
|
+
# documents available.
|
|
698
|
+
def fifth!
|
|
699
|
+
fifth || raise_document_not_found_error
|
|
700
|
+
end
|
|
701
|
+
|
|
702
|
+
# Get the second to last document in the database for the criteria's
|
|
703
|
+
# selector.
|
|
704
|
+
#
|
|
705
|
+
# @example Get the second to last document.
|
|
706
|
+
# context.second_to_last
|
|
707
|
+
#
|
|
708
|
+
# @return [ Document | nil ] The second to last document or nil if none
|
|
709
|
+
# is found.
|
|
710
|
+
def second_to_last
|
|
711
|
+
retrieve_nth_to_last(1)
|
|
712
|
+
end
|
|
713
|
+
|
|
714
|
+
# Get the second to last document in the database for the criteria's
|
|
715
|
+
# selector or raise an error if none is found.
|
|
716
|
+
#
|
|
717
|
+
# @example Get the second to last document.
|
|
718
|
+
# context.second_to_last!
|
|
719
|
+
#
|
|
720
|
+
# @return [ Document ] The second to last document.
|
|
721
|
+
#
|
|
722
|
+
# @raise [ Mongoid::Errors::DocumentNotFound ] raises when there are no
|
|
723
|
+
# documents available.
|
|
724
|
+
def second_to_last!
|
|
725
|
+
second_to_last || raise_document_not_found_error
|
|
726
|
+
end
|
|
727
|
+
|
|
728
|
+
# Get the third to last document in the database for the criteria's
|
|
729
|
+
# selector.
|
|
730
|
+
#
|
|
731
|
+
# @example Get the third to last document.
|
|
732
|
+
# context.third_to_last
|
|
733
|
+
#
|
|
734
|
+
# @return [ Document | nil ] The third to last document or nil if none
|
|
735
|
+
# is found.
|
|
736
|
+
def third_to_last
|
|
737
|
+
retrieve_nth_to_last(2)
|
|
738
|
+
end
|
|
739
|
+
|
|
740
|
+
# Get the third to last document in the database for the criteria's
|
|
741
|
+
# selector or raise an error if none is found.
|
|
742
|
+
#
|
|
743
|
+
# @example Get the third to last document.
|
|
744
|
+
# context.third_to_last!
|
|
745
|
+
#
|
|
746
|
+
# @return [ Document ] The third to last document.
|
|
747
|
+
#
|
|
748
|
+
# @raise [ Mongoid::Errors::DocumentNotFound ] raises when there are no
|
|
749
|
+
# documents available.
|
|
750
|
+
def third_to_last!
|
|
751
|
+
third_to_last || raise_document_not_found_error
|
|
752
|
+
end
|
|
753
|
+
|
|
754
|
+
# Schedule a task to load documents for the context.
|
|
755
|
+
#
|
|
756
|
+
# Depending on the Mongoid configuration, the scheduled task can be executed
|
|
757
|
+
# immediately on the caller's thread, or can be scheduled for an
|
|
758
|
+
# asynchronous execution.
|
|
759
|
+
#
|
|
760
|
+
# @api private
|
|
761
|
+
def load_async
|
|
762
|
+
@documents_loader ||= DocumentsLoader.new(view, klass, criteria)
|
|
763
|
+
end
|
|
764
|
+
|
|
765
|
+
private
|
|
766
|
+
|
|
540
767
|
# Update the documents for the provided method.
|
|
541
768
|
#
|
|
542
769
|
# @api private
|
|
@@ -547,13 +774,11 @@ module Mongoid
|
|
|
547
774
|
# @param [ Hash ] attributes The updates.
|
|
548
775
|
# @param [ Symbol ] method The method to use.
|
|
549
776
|
#
|
|
550
|
-
# @return [ true
|
|
551
|
-
#
|
|
552
|
-
# @since 3.0.4
|
|
777
|
+
# @return [ true | false ] If the update succeeded.
|
|
553
778
|
def update_documents(attributes, method = :update_one, opts = {})
|
|
554
779
|
return false unless attributes
|
|
555
|
-
|
|
556
|
-
view.send(method,
|
|
780
|
+
|
|
781
|
+
view.send(method, AtomicUpdatePreparer.prepare(attributes, klass), opts)
|
|
557
782
|
end
|
|
558
783
|
|
|
559
784
|
# Apply the field limitations.
|
|
@@ -562,12 +787,10 @@ module Mongoid
|
|
|
562
787
|
#
|
|
563
788
|
# @example Apply the field limitations.
|
|
564
789
|
# context.apply_fields
|
|
565
|
-
#
|
|
566
|
-
# @since 3.0.0
|
|
567
790
|
def apply_fields
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
791
|
+
return unless spec = criteria.options[:fields]
|
|
792
|
+
|
|
793
|
+
@view = view.projection(spec)
|
|
571
794
|
end
|
|
572
795
|
|
|
573
796
|
# Apply the options.
|
|
@@ -576,16 +799,14 @@ module Mongoid
|
|
|
576
799
|
#
|
|
577
800
|
# @example Apply all options.
|
|
578
801
|
# context.apply_options
|
|
579
|
-
#
|
|
580
|
-
# @since 3.1.0
|
|
581
802
|
def apply_options
|
|
582
803
|
apply_fields
|
|
583
804
|
OPTIONS.each do |name|
|
|
584
805
|
apply_option(name)
|
|
585
806
|
end
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
807
|
+
return unless criteria.options[:timeout] == false
|
|
808
|
+
|
|
809
|
+
@view = view.no_cursor_timeout
|
|
589
810
|
end
|
|
590
811
|
|
|
591
812
|
# Apply an option.
|
|
@@ -594,97 +815,53 @@ module Mongoid
|
|
|
594
815
|
#
|
|
595
816
|
# @example Apply the skip option.
|
|
596
817
|
# context.apply_option(:skip)
|
|
597
|
-
#
|
|
598
|
-
# @since 3.1.0
|
|
599
818
|
def apply_option(name)
|
|
600
|
-
|
|
601
|
-
@view = view.send(name, spec)
|
|
602
|
-
end
|
|
603
|
-
end
|
|
819
|
+
return unless spec = criteria.options[name]
|
|
604
820
|
|
|
605
|
-
|
|
606
|
-
#
|
|
607
|
-
# @api private
|
|
608
|
-
#
|
|
609
|
-
# @example Apply the inverse sorting params to the given block
|
|
610
|
-
# context.with_inverse_sorting
|
|
611
|
-
#
|
|
612
|
-
# @since 3.0.0
|
|
613
|
-
def with_inverse_sorting(opts = {})
|
|
614
|
-
begin
|
|
615
|
-
if sort = criteria.options[:sort] || ( { _id: 1 } unless opts[:id_sort] == :none )
|
|
616
|
-
@view = view.sort(Hash[sort.map{|k, v| [k, -1*v]}])
|
|
617
|
-
end
|
|
618
|
-
yield
|
|
619
|
-
ensure
|
|
620
|
-
apply_option(:sort)
|
|
621
|
-
end
|
|
821
|
+
@view = view.send(name, spec)
|
|
622
822
|
end
|
|
623
823
|
|
|
624
|
-
#
|
|
824
|
+
# Returns materialized documents for use in the cross-cluster $lookup
|
|
825
|
+
# fallback. Reads from the collection view and instantiates documents.
|
|
625
826
|
#
|
|
626
827
|
# @api private
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
# context.cacheable?
|
|
630
|
-
#
|
|
631
|
-
# @return [ true, false ] If caching, and the cache isn't loaded.
|
|
632
|
-
#
|
|
633
|
-
# @since 3.0.0
|
|
634
|
-
def cacheable?
|
|
635
|
-
cached? && !cache_loaded?
|
|
828
|
+
def docs_for_lookup_fallback
|
|
829
|
+
view.map { |doc| Factory.from_db(klass, doc, criteria) }
|
|
636
830
|
end
|
|
637
831
|
|
|
638
|
-
#
|
|
639
|
-
# iteration.
|
|
832
|
+
# Map the inverse sort symbols to the correct MongoDB values.
|
|
640
833
|
#
|
|
641
834
|
# @api private
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
#
|
|
646
|
-
# @return [ true, false ] If the cache is loaded.
|
|
647
|
-
#
|
|
648
|
-
# @since 3.0.0
|
|
649
|
-
def cache_loaded?
|
|
650
|
-
!!@cache_loaded
|
|
835
|
+
def inverse_sorting
|
|
836
|
+
sort = view.sort || { _id: 1 }
|
|
837
|
+
Hash[sort.map { |k, v| [ k, -1 * v ] }]
|
|
651
838
|
end
|
|
652
839
|
|
|
653
|
-
# Get the documents
|
|
654
|
-
#
|
|
655
|
-
# @api private
|
|
656
|
-
#
|
|
657
|
-
# @example Get the cached documents.
|
|
658
|
-
# context.documents
|
|
659
|
-
#
|
|
660
|
-
# @return [ Array<Document> ] The documents.
|
|
840
|
+
# Get the documents the context should iterate.
|
|
661
841
|
#
|
|
662
|
-
#
|
|
663
|
-
|
|
664
|
-
@documents ||= []
|
|
665
|
-
end
|
|
666
|
-
|
|
667
|
-
# Get the documents the context should iterate. This follows 3 rules:
|
|
842
|
+
# If the documents have been already preloaded by `Document::Loader`
|
|
843
|
+
# instance, they will be used.
|
|
668
844
|
#
|
|
669
|
-
#
|
|
670
|
-
# them.
|
|
671
|
-
# 2. If we are eager loading, then eager load the documents and use
|
|
672
|
-
# those.
|
|
673
|
-
# 3. Use the query.
|
|
845
|
+
# @return [ Array<Document> | Mongo::Collection::View ] The docs to iterate.
|
|
674
846
|
#
|
|
675
847
|
# @api private
|
|
676
|
-
#
|
|
677
|
-
# @example Get the documents for iteration.
|
|
678
|
-
# context.documents_for_iteration
|
|
679
|
-
#
|
|
680
|
-
# @return [ Array<Document>, Mongo::Collection::View ] The docs to iterate.
|
|
681
|
-
#
|
|
682
|
-
# @since 3.0.0
|
|
683
848
|
def documents_for_iteration
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
849
|
+
if @documents_loader
|
|
850
|
+
if @documents_loader.started?
|
|
851
|
+
@documents_loader.value!
|
|
852
|
+
else
|
|
853
|
+
@documents_loader.unschedule
|
|
854
|
+
@documents_loader.execute
|
|
855
|
+
end
|
|
856
|
+
else
|
|
857
|
+
return view unless eager_loadable?
|
|
858
|
+
return eager_load_with_lookup if criteria.use_lookup?
|
|
859
|
+
|
|
860
|
+
docs = view.map do |doc|
|
|
861
|
+
Factory.from_db(klass, doc, criteria)
|
|
862
|
+
end
|
|
863
|
+
eager_load(docs)
|
|
864
|
+
end
|
|
688
865
|
end
|
|
689
866
|
|
|
690
867
|
# Yield to the document.
|
|
@@ -697,17 +874,22 @@ module Mongoid
|
|
|
697
874
|
# end
|
|
698
875
|
#
|
|
699
876
|
# @param [ Document ] document The document to yield to.
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
877
|
+
def yield_document(document)
|
|
878
|
+
doc = if document.respond_to?(:_id)
|
|
879
|
+
document
|
|
880
|
+
elsif criteria.raw_results?
|
|
881
|
+
if criteria.typecast_results?
|
|
882
|
+
demongoize_hash(klass, document)
|
|
883
|
+
else
|
|
884
|
+
document
|
|
885
|
+
end
|
|
886
|
+
else
|
|
887
|
+
Factory.from_db(klass, document, criteria)
|
|
888
|
+
end
|
|
889
|
+
|
|
705
890
|
yield(doc)
|
|
706
|
-
documents.push(doc) if cacheable?
|
|
707
891
|
end
|
|
708
892
|
|
|
709
|
-
private
|
|
710
|
-
|
|
711
893
|
def _session
|
|
712
894
|
@criteria.send(:_session)
|
|
713
895
|
end
|
|
@@ -715,6 +897,180 @@ module Mongoid
|
|
|
715
897
|
def acknowledged_write?
|
|
716
898
|
collection.write_concern.nil? || collection.write_concern.acknowledged?
|
|
717
899
|
end
|
|
900
|
+
|
|
901
|
+
# Recursively demongoize the given value. This method recursively traverses
|
|
902
|
+
# the class tree to find the correct field to use to demongoize the value.
|
|
903
|
+
#
|
|
904
|
+
# @param [ String ] field_name The name of the field to demongoize.
|
|
905
|
+
# @param [ Object ] value The value to demongoize.
|
|
906
|
+
# @param [ true | false ] is_translation The field we are retrieving is an
|
|
907
|
+
# _translations field.
|
|
908
|
+
#
|
|
909
|
+
# @return [ Object ] The demongoized value.
|
|
910
|
+
def recursive_demongoize(field_name, value, is_translation)
|
|
911
|
+
field = klass.traverse_association_tree(field_name)
|
|
912
|
+
demongoize_with_field(field, value, is_translation)
|
|
913
|
+
end
|
|
914
|
+
|
|
915
|
+
# Demongoizes (converts from database to Ruby representation) the values
|
|
916
|
+
# of the given hash as if it were the raw representation of a document of
|
|
917
|
+
# the given klass.
|
|
918
|
+
#
|
|
919
|
+
# @note this method will modify the given hash, in-place, for performance
|
|
920
|
+
# reasons. If you wish to preserve the original hash, duplicate it before
|
|
921
|
+
# passing it to this method.
|
|
922
|
+
#
|
|
923
|
+
# @param [ Document ] klass the Document class that the given hash ought
|
|
924
|
+
# to represent
|
|
925
|
+
# @param [ Hash | nil ] hash the Hash instance containing the values to
|
|
926
|
+
# demongoize.
|
|
927
|
+
#
|
|
928
|
+
# @return [ Hash | nil ] the demongoized result (nil if the input Hash
|
|
929
|
+
# was nil)
|
|
930
|
+
#
|
|
931
|
+
# @api private
|
|
932
|
+
def demongoize_hash(klass, hash)
|
|
933
|
+
return nil unless hash
|
|
934
|
+
|
|
935
|
+
hash.each_key do |key|
|
|
936
|
+
value = hash[key]
|
|
937
|
+
|
|
938
|
+
# does the key represent a declared field on the document?
|
|
939
|
+
if (field = klass.fields[key])
|
|
940
|
+
hash[key] = field.demongoize(value)
|
|
941
|
+
next
|
|
942
|
+
end
|
|
943
|
+
|
|
944
|
+
# does the key represent an embedded relation on the document?
|
|
945
|
+
aliased_name = klass.aliased_associations[key] || key
|
|
946
|
+
next unless (assoc = klass.relations[aliased_name])
|
|
947
|
+
|
|
948
|
+
case value
|
|
949
|
+
when Array then value.each { |h| demongoize_hash(assoc.klass, h) }
|
|
950
|
+
when Hash then demongoize_hash(assoc.klass, value)
|
|
951
|
+
end
|
|
952
|
+
end
|
|
953
|
+
|
|
954
|
+
hash
|
|
955
|
+
end
|
|
956
|
+
|
|
957
|
+
# Demongoize the value for the given field. If the field is nil or the
|
|
958
|
+
# field is a translations field, the value is demongoized using its class.
|
|
959
|
+
#
|
|
960
|
+
# @param [ Field ] field The field to use to demongoize.
|
|
961
|
+
# @param [ Object ] value The value to demongoize.
|
|
962
|
+
# @param [ true | false ] is_translation The field we are retrieving is an
|
|
963
|
+
# _translations field.
|
|
964
|
+
#
|
|
965
|
+
# @return [ Object ] The demongoized value.
|
|
966
|
+
#
|
|
967
|
+
# @api private
|
|
968
|
+
def demongoize_with_field(field, value, is_translation)
|
|
969
|
+
if field
|
|
970
|
+
# If it's a localized field that's not a hash, don't demongoize
|
|
971
|
+
# again, we already have the translation. If it's an _translations
|
|
972
|
+
# field, don't demongoize, we want the full hash not just a
|
|
973
|
+
# specific translation.
|
|
974
|
+
# If it is a hash, and it's not a translations field, we need to
|
|
975
|
+
# demongoize to get the correct translation.
|
|
976
|
+
if field.localized? && (!value.is_a?(Hash) || is_translation)
|
|
977
|
+
value.class.demongoize(value)
|
|
978
|
+
else
|
|
979
|
+
field.demongoize(value)
|
|
980
|
+
end
|
|
981
|
+
else
|
|
982
|
+
value.class.demongoize(value)
|
|
983
|
+
end
|
|
984
|
+
end
|
|
985
|
+
|
|
986
|
+
# Process the raw documents retrieved for #first/#last.
|
|
987
|
+
#
|
|
988
|
+
# @return [ Array<Document> | Document ] The list of documents or a
|
|
989
|
+
# single document.
|
|
990
|
+
def process_raw_docs(raw_docs, limit)
|
|
991
|
+
docs = if criteria.raw_results?
|
|
992
|
+
if criteria.typecast_results?
|
|
993
|
+
raw_docs.map { |doc| demongoize_hash(klass, doc) }
|
|
994
|
+
else
|
|
995
|
+
raw_docs
|
|
996
|
+
end
|
|
997
|
+
else
|
|
998
|
+
mapped = raw_docs.map { |doc| Factory.from_db(klass, doc, criteria) }
|
|
999
|
+
if criteria.use_lookup?
|
|
1000
|
+
mapped
|
|
1001
|
+
else
|
|
1002
|
+
# $lookup already eager loads related documents
|
|
1003
|
+
eager_load(mapped)
|
|
1004
|
+
end
|
|
1005
|
+
end
|
|
1006
|
+
|
|
1007
|
+
limit ? docs : docs.first
|
|
1008
|
+
end
|
|
1009
|
+
|
|
1010
|
+
# Queries whether the current context is valid for use with
|
|
1011
|
+
# the #count_documents? predicate. A context is valid if it
|
|
1012
|
+
# does not include a `$where` operator.
|
|
1013
|
+
#
|
|
1014
|
+
# @return [ true | false ] whether or not the current context
|
|
1015
|
+
# excludes a `$where` operator.
|
|
1016
|
+
#
|
|
1017
|
+
# TODO: Remove this method when we remove the deprecated for_js API.
|
|
1018
|
+
# https://jira.mongodb.org/browse/MONGOID-5681
|
|
1019
|
+
def valid_for_count_documents?(hash = view.filter)
|
|
1020
|
+
# Note that `view.filter` is a BSON::Document, and all keys in a
|
|
1021
|
+
# BSON::Document are strings; we don't need to worry about symbol
|
|
1022
|
+
# representations of `$where`.
|
|
1023
|
+
hash.keys.each do |key|
|
|
1024
|
+
return false if key == '$where'
|
|
1025
|
+
return false if hash[key].is_a?(Hash) && !valid_for_count_documents?(hash[key])
|
|
1026
|
+
end
|
|
1027
|
+
|
|
1028
|
+
true
|
|
1029
|
+
end
|
|
1030
|
+
|
|
1031
|
+
def raise_document_not_found_error
|
|
1032
|
+
raise Errors::DocumentNotFound.new(klass, nil, nil)
|
|
1033
|
+
end
|
|
1034
|
+
|
|
1035
|
+
def retrieve_nth(n)
|
|
1036
|
+
retrieve_nth_with_limit(n, 1).first
|
|
1037
|
+
end
|
|
1038
|
+
|
|
1039
|
+
def retrieve_nth_with_limit(n, limit)
|
|
1040
|
+
sort = view.sort || { _id: 1 }
|
|
1041
|
+
v = view.sort(sort).limit(limit || 1)
|
|
1042
|
+
v = v.skip(n) if n > 0
|
|
1043
|
+
if criteria.use_lookup?
|
|
1044
|
+
# Update criteria and view with the sort, skip, and limit before eager loading
|
|
1045
|
+
@criteria = criteria.order_by(sort).limit(limit || 1)
|
|
1046
|
+
@criteria = @criteria.skip(n) if n > 0
|
|
1047
|
+
@view = collection.find(criteria.selector, session: _session).limit(limit || 1).sort(sort)
|
|
1048
|
+
@view = @view.skip(n) if n > 0
|
|
1049
|
+
eager_load_with_lookup
|
|
1050
|
+
elsif raw_docs = v.to_a
|
|
1051
|
+
process_raw_docs(raw_docs, limit)
|
|
1052
|
+
end
|
|
1053
|
+
end
|
|
1054
|
+
|
|
1055
|
+
def retrieve_nth_to_last(n)
|
|
1056
|
+
retrieve_nth_to_last_with_limit(n, 1).first
|
|
1057
|
+
end
|
|
1058
|
+
|
|
1059
|
+
def retrieve_nth_to_last_with_limit(n, limit)
|
|
1060
|
+
v = view.sort(inverse_sorting).limit(limit || 1)
|
|
1061
|
+
v = v.skip(n) if n > 0
|
|
1062
|
+
if criteria.use_lookup?
|
|
1063
|
+
# Update criteria and view with the inverse sort, skip, and limit before eager loading
|
|
1064
|
+
@criteria = criteria.order_by(inverse_sorting).limit(limit || 1)
|
|
1065
|
+
@criteria = @criteria.skip(n) if n > 0
|
|
1066
|
+
@view = collection.find(criteria.selector, session: _session).limit(limit || 1).sort(inverse_sorting)
|
|
1067
|
+
@view = @view.skip(n) if n > 0
|
|
1068
|
+
eager_load_with_lookup.reverse
|
|
1069
|
+
else
|
|
1070
|
+
raw_docs = v.to_a.reverse
|
|
1071
|
+
process_raw_docs(raw_docs, limit)
|
|
1072
|
+
end
|
|
1073
|
+
end
|
|
718
1074
|
end
|
|
719
1075
|
end
|
|
720
1076
|
end
|