mongoid 9.0.11 → 9.1.0
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 +8 -8
- data/README.md +5 -11
- data/Rakefile +35 -38
- data/lib/config/locales/en.yml +11 -0
- data/lib/mongoid/association/accessors.rb +19 -26
- data/lib/mongoid/association/bindable.rb +43 -45
- data/lib/mongoid/association/builders.rb +18 -15
- data/lib/mongoid/association/constrainable.rb +3 -5
- data/lib/mongoid/association/depending.rb +34 -38
- data/lib/mongoid/association/eager.rb +47 -13
- data/lib/mongoid/association/eager_loadable.rb +152 -24
- data/lib/mongoid/association/embedded/batchable.rb +28 -30
- data/lib/mongoid/association/embedded/cyclic.rb +8 -11
- data/lib/mongoid/association/embedded/eager.rb +1 -6
- data/lib/mongoid/association/embedded/embedded_in/binding.rb +6 -9
- data/lib/mongoid/association/embedded/embedded_in/buildable.rb +2 -3
- data/lib/mongoid/association/embedded/embedded_in/proxy.rb +2 -0
- data/lib/mongoid/association/embedded/embedded_in.rb +25 -22
- data/lib/mongoid/association/embedded/embeds_many/binding.rb +0 -2
- data/lib/mongoid/association/embedded/embeds_many/buildable.rb +2 -3
- data/lib/mongoid/association/embedded/embeds_many/proxy.rb +1 -1
- data/lib/mongoid/association/embedded/embeds_many.rb +24 -21
- data/lib/mongoid/association/embedded/embeds_one/binding.rb +0 -2
- data/lib/mongoid/association/embedded/embeds_one/buildable.rb +2 -6
- data/lib/mongoid/association/embedded/embeds_one.rb +20 -18
- data/lib/mongoid/association/embedded.rb +0 -1
- data/lib/mongoid/association/macros.rb +3 -7
- data/lib/mongoid/association/many.rb +75 -3
- data/lib/mongoid/association/marshalable.rb +0 -3
- data/lib/mongoid/association/nested/many.rb +18 -13
- data/lib/mongoid/association/nested/nested_buildable.rb +6 -9
- data/lib/mongoid/association/nested/one.rb +4 -7
- data/lib/mongoid/association/nested.rb +1 -3
- data/lib/mongoid/association/one.rb +0 -3
- data/lib/mongoid/association/options.rb +8 -7
- data/lib/mongoid/association/referenced/auto_save.rb +0 -2
- data/lib/mongoid/association/referenced/belongs_to/binding.rb +11 -15
- data/lib/mongoid/association/referenced/belongs_to/buildable.rb +23 -5
- data/lib/mongoid/association/referenced/belongs_to/eager.rb +1 -3
- data/lib/mongoid/association/referenced/belongs_to.rb +48 -43
- data/lib/mongoid/association/referenced/counter_cache.rb +5 -10
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/binding.rb +0 -2
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb +8 -4
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb +0 -3
- data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +52 -47
- data/lib/mongoid/association/referenced/has_many/binding.rb +0 -2
- data/lib/mongoid/association/referenced/has_many/buildable.rb +11 -5
- data/lib/mongoid/association/referenced/has_many/eager.rb +0 -3
- data/lib/mongoid/association/referenced/has_many/enumerable.rb +107 -38
- data/lib/mongoid/association/referenced/has_many/proxy.rb +10 -6
- data/lib/mongoid/association/referenced/has_many.rb +30 -24
- data/lib/mongoid/association/referenced/has_one/binding.rb +0 -2
- data/lib/mongoid/association/referenced/has_one/buildable.rb +11 -9
- data/lib/mongoid/association/referenced/has_one/eager.rb +0 -3
- data/lib/mongoid/association/referenced/has_one.rb +26 -21
- data/lib/mongoid/association/referenced/syncable.rb +30 -34
- data/lib/mongoid/association/referenced.rb +0 -1
- data/lib/mongoid/association/reflections.rb +0 -3
- data/lib/mongoid/association/relatable.rb +69 -66
- data/lib/mongoid/association.rb +12 -15
- data/lib/mongoid/atomic/modifiers.rb +65 -25
- data/lib/mongoid/atomic/paths/embedded/many.rb +4 -7
- data/lib/mongoid/atomic/paths/embedded/one.rb +2 -4
- data/lib/mongoid/atomic/paths/embedded.rb +3 -6
- data/lib/mongoid/atomic/paths/root.rb +1 -4
- data/lib/mongoid/atomic/paths.rb +2 -3
- data/lib/mongoid/atomic.rb +0 -3
- data/lib/mongoid/attributes/dynamic.rb +6 -7
- data/lib/mongoid/attributes/nested.rb +8 -10
- data/lib/mongoid/attributes/projector.rb +7 -20
- data/lib/mongoid/attributes/readonly.rb +6 -11
- data/lib/mongoid/attributes.rb +18 -34
- data/lib/mongoid/cacheable.rb +24 -6
- data/lib/mongoid/clients/factory.rb +12 -15
- data/lib/mongoid/clients/options.rb +2 -5
- data/lib/mongoid/clients/sessions.rb +50 -60
- data/lib/mongoid/clients/storage_options.rb +1 -4
- data/lib/mongoid/clients/validators/storage.rb +2 -3
- data/lib/mongoid/clients/validators.rb +1 -2
- data/lib/mongoid/clients.rb +8 -11
- data/lib/mongoid/collection_configurable.rb +10 -14
- data/lib/mongoid/composable.rb +30 -34
- data/lib/mongoid/config/defaults.rb +7 -4
- data/lib/mongoid/config/encryption.rb +1 -3
- data/lib/mongoid/config/environment.rb +7 -16
- data/lib/mongoid/config/introspection.rb +14 -16
- data/lib/mongoid/config/options.rb +16 -10
- data/lib/mongoid/config/validators/async_query_executor.rb +3 -6
- data/lib/mongoid/config/validators/client.rb +12 -15
- data/lib/mongoid/config/validators/option.rb +3 -5
- data/lib/mongoid/config/validators.rb +3 -4
- data/lib/mongoid/config.rb +132 -42
- data/lib/mongoid/contextual/aggregable/memory.rb +2 -4
- data/lib/mongoid/contextual/aggregable/mongo.rb +20 -22
- data/lib/mongoid/contextual/aggregable/none.rb +2 -4
- data/lib/mongoid/contextual/aggregable.rb +5 -7
- data/lib/mongoid/contextual/atomic.rb +20 -24
- data/lib/mongoid/contextual/command.rb +0 -3
- data/lib/mongoid/contextual/map_reduce.rb +12 -16
- data/lib/mongoid/contextual/memory.rb +72 -65
- data/lib/mongoid/contextual/mongo/documents_loader.rb +2 -3
- data/lib/mongoid/contextual/mongo.rb +169 -85
- data/lib/mongoid/contextual/none.rb +9 -9
- data/lib/mongoid/contextual/queryable.rb +1 -4
- data/lib/mongoid/contextual.rb +18 -7
- data/lib/mongoid/copyable.rb +7 -9
- data/lib/mongoid/criteria/findable.rb +5 -7
- data/lib/mongoid/criteria/includable.rb +37 -8
- data/lib/mongoid/criteria/inspectable.rb +2 -5
- data/lib/mongoid/criteria/marshalable.rb +4 -8
- data/lib/mongoid/criteria/modifiable.rb +3 -11
- data/lib/mongoid/criteria/options.rb +0 -3
- data/lib/mongoid/criteria/permission.rb +34 -37
- data/lib/mongoid/criteria/queryable/aggregable.rb +9 -10
- data/lib/mongoid/criteria/queryable/expandable.rb +4 -9
- data/lib/mongoid/criteria/queryable/extensions/array.rb +7 -10
- data/lib/mongoid/criteria/queryable/extensions/big_decimal.rb +3 -5
- data/lib/mongoid/criteria/queryable/extensions/boolean.rb +1 -4
- data/lib/mongoid/criteria/queryable/extensions/date.rb +2 -6
- data/lib/mongoid/criteria/queryable/extensions/date_time.rb +3 -7
- data/lib/mongoid/criteria/queryable/extensions/hash.rb +1 -4
- data/lib/mongoid/criteria/queryable/extensions/nil_class.rb +20 -11
- data/lib/mongoid/criteria/queryable/extensions/numeric.rb +15 -11
- data/lib/mongoid/criteria/queryable/extensions/object.rb +7 -8
- data/lib/mongoid/criteria/queryable/extensions/range.rb +6 -9
- data/lib/mongoid/criteria/queryable/extensions/regexp.rb +10 -12
- data/lib/mongoid/criteria/queryable/extensions/set.rb +4 -6
- data/lib/mongoid/criteria/queryable/extensions/string.rb +5 -9
- data/lib/mongoid/criteria/queryable/extensions/symbol.rb +3 -7
- data/lib/mongoid/criteria/queryable/extensions/time.rb +2 -6
- data/lib/mongoid/criteria/queryable/extensions/time_with_zone.rb +2 -12
- data/lib/mongoid/criteria/queryable/extensions.rb +18 -21
- data/lib/mongoid/criteria/queryable/key.rb +12 -18
- data/lib/mongoid/criteria/queryable/macroable.rb +0 -3
- data/lib/mongoid/criteria/queryable/mergeable.rb +31 -39
- data/lib/mongoid/criteria/queryable/optional.rb +17 -18
- data/lib/mongoid/criteria/queryable/options.rb +20 -9
- data/lib/mongoid/criteria/queryable/pipeline.rb +6 -9
- data/lib/mongoid/criteria/queryable/selectable.rb +131 -193
- data/lib/mongoid/criteria/queryable/selector.rb +20 -29
- data/lib/mongoid/criteria/queryable/smash.rb +1 -6
- data/lib/mongoid/criteria/queryable/storable.rb +20 -39
- data/lib/mongoid/criteria/queryable.rb +25 -15
- data/lib/mongoid/criteria/scopable.rb +5 -11
- data/lib/mongoid/criteria/translator.rb +5 -8
- data/lib/mongoid/criteria.rb +109 -54
- data/lib/mongoid/deprecable.rb +1 -4
- data/lib/mongoid/deprecation.rb +2 -6
- data/lib/mongoid/document.rb +1 -1
- data/lib/mongoid/encryptable.rb +1 -2
- data/lib/mongoid/equality.rb +4 -5
- data/lib/mongoid/errors/ambiguous_relationship.rb +2 -5
- data/lib/mongoid/errors/attribute_not_loaded.rb +1 -4
- data/lib/mongoid/errors/callback.rb +1 -4
- data/lib/mongoid/errors/create_collection_failure.rb +8 -11
- data/lib/mongoid/errors/criteria_argument_required.rb +2 -5
- data/lib/mongoid/errors/delete_restriction.rb +1 -4
- data/lib/mongoid/errors/document_not_destroyed.rb +1 -4
- data/lib/mongoid/errors/document_not_found.rb +16 -21
- data/lib/mongoid/errors/drop_collection_failure.rb +7 -10
- data/lib/mongoid/errors/empty_config_file.rb +1 -4
- data/lib/mongoid/errors/immutable_attribute.rb +1 -4
- data/lib/mongoid/errors/in_memory_collation_not_supported.rb +1 -4
- data/lib/mongoid/errors/invalid_async_query_executor.rb +2 -5
- data/lib/mongoid/errors/invalid_auto_encryption_configuration.rb +2 -5
- data/lib/mongoid/errors/invalid_collection.rb +1 -3
- data/lib/mongoid/errors/invalid_config_file.rb +1 -4
- data/lib/mongoid/errors/invalid_config_option.rb +2 -5
- data/lib/mongoid/errors/invalid_dependent_strategy.rb +6 -10
- data/lib/mongoid/errors/invalid_discriminator_key_target.rb +2 -4
- data/lib/mongoid/errors/invalid_dot_dollar_assignment.rb +1 -4
- data/lib/mongoid/errors/invalid_elem_match_operator.rb +5 -9
- data/lib/mongoid/errors/invalid_estimated_count_criteria.rb +1 -4
- data/lib/mongoid/errors/invalid_estimated_count_scoping.rb +1 -4
- data/lib/mongoid/errors/invalid_expression_operator.rb +3 -7
- data/lib/mongoid/errors/invalid_field.rb +2 -5
- data/lib/mongoid/errors/invalid_field_operator.rb +5 -9
- data/lib/mongoid/errors/invalid_field_option.rb +2 -5
- data/lib/mongoid/errors/invalid_field_type.rb +1 -4
- data/lib/mongoid/errors/invalid_find.rb +1 -4
- data/lib/mongoid/errors/invalid_global_executor_concurrency.rb +1 -4
- data/lib/mongoid/errors/invalid_includes.rb +3 -6
- data/lib/mongoid/errors/invalid_index.rb +1 -4
- data/lib/mongoid/errors/invalid_options.rb +1 -4
- data/lib/mongoid/errors/invalid_path.rb +1 -4
- data/lib/mongoid/errors/invalid_persistence_option.rb +4 -7
- data/lib/mongoid/errors/invalid_query.rb +6 -11
- data/lib/mongoid/errors/invalid_relation.rb +10 -13
- data/lib/mongoid/errors/invalid_relation_option.rb +4 -7
- data/lib/mongoid/errors/invalid_scope.rb +1 -4
- data/lib/mongoid/errors/invalid_session_nesting.rb +0 -3
- data/lib/mongoid/errors/invalid_set_polymorphic_relation.rb +2 -4
- data/lib/mongoid/errors/invalid_storage_options.rb +1 -4
- data/lib/mongoid/errors/invalid_time.rb +1 -4
- data/lib/mongoid/errors/invalid_transaction_nesting.rb +0 -3
- data/lib/mongoid/errors/inverse_not_found.rb +1 -4
- data/lib/mongoid/errors/mixed_client_configuration.rb +1 -4
- data/lib/mongoid/errors/mixed_relations.rb +1 -3
- data/lib/mongoid/errors/mongoid_error.rb +7 -11
- data/lib/mongoid/errors/nested_attributes_metadata_not_found.rb +1 -4
- data/lib/mongoid/errors/no_client_config.rb +1 -4
- data/lib/mongoid/errors/no_client_database.rb +1 -4
- data/lib/mongoid/errors/no_client_hosts.rb +1 -4
- data/lib/mongoid/errors/no_clients_config.rb +1 -4
- data/lib/mongoid/errors/no_default_client.rb +1 -4
- data/lib/mongoid/errors/no_environment.rb +1 -4
- data/lib/mongoid/errors/no_map_reduce_output.rb +1 -4
- data/lib/mongoid/errors/no_metadata.rb +1 -4
- data/lib/mongoid/errors/no_parent.rb +1 -4
- data/lib/mongoid/errors/readonly_attribute.rb +1 -4
- data/lib/mongoid/errors/readonly_document.rb +1 -4
- data/lib/mongoid/errors/rollback.rb +0 -2
- data/lib/mongoid/errors/scope_overwrite.rb +2 -4
- data/lib/mongoid/errors/sessions_not_supported.rb +0 -3
- data/lib/mongoid/errors/too_many_nested_attribute_records.rb +1 -3
- data/lib/mongoid/errors/transaction_error.rb +0 -3
- data/lib/mongoid/errors/transactions_not_supported.rb +0 -3
- data/lib/mongoid/errors/unknown_attribute.rb +1 -4
- data/lib/mongoid/errors/unknown_model.rb +1 -4
- data/lib/mongoid/errors/unregistered_class.rb +1 -1
- data/lib/mongoid/errors/unsaved_document.rb +1 -3
- data/lib/mongoid/errors/unsupported_isolation_level.rb +22 -0
- data/lib/mongoid/errors/unsupported_javascript.rb +1 -4
- data/lib/mongoid/errors/validations.rb +3 -5
- data/lib/mongoid/errors.rb +72 -72
- data/lib/mongoid/evolvable.rb +0 -3
- data/lib/mongoid/extensions/array.rb +5 -7
- data/lib/mongoid/extensions/big_decimal.rb +6 -7
- data/lib/mongoid/extensions/binary.rb +3 -6
- data/lib/mongoid/extensions/boolean.rb +4 -7
- data/lib/mongoid/extensions/bson_document.rb +28 -0
- data/lib/mongoid/extensions/date.rb +10 -14
- data/lib/mongoid/extensions/date_time.rb +2 -6
- data/lib/mongoid/extensions/decimal128.rb +1 -5
- data/lib/mongoid/extensions/false_class.rb +3 -5
- data/lib/mongoid/extensions/float.rb +5 -10
- data/lib/mongoid/extensions/hash.rb +5 -10
- data/lib/mongoid/extensions/integer.rb +5 -10
- data/lib/mongoid/extensions/module.rb +1 -4
- data/lib/mongoid/extensions/nil_class.rb +0 -1
- data/lib/mongoid/extensions/object.rb +9 -14
- data/lib/mongoid/extensions/object_id.rb +2 -6
- data/lib/mongoid/extensions/range.rb +14 -15
- data/lib/mongoid/extensions/raw_value.rb +0 -3
- data/lib/mongoid/extensions/regexp.rb +3 -6
- data/lib/mongoid/extensions/set.rb +3 -4
- data/lib/mongoid/extensions/string.rb +8 -12
- data/lib/mongoid/extensions/symbol.rb +3 -7
- data/lib/mongoid/extensions/time.rb +24 -26
- data/lib/mongoid/extensions/time_with_zone.rb +4 -7
- data/lib/mongoid/extensions/true_class.rb +3 -5
- data/lib/mongoid/extensions.rb +26 -26
- data/lib/mongoid/factory.rb +5 -9
- data/lib/mongoid/fields/encrypted.rb +0 -1
- data/lib/mongoid/fields/foreign_key.rb +9 -10
- data/lib/mongoid/fields/localized.rb +12 -12
- data/lib/mongoid/fields/standard.rb +5 -7
- data/lib/mongoid/fields/validators/macro.rb +23 -34
- data/lib/mongoid/fields/validators.rb +1 -2
- data/lib/mongoid/fields.rb +151 -58
- data/lib/mongoid/findable.rb +82 -53
- data/lib/mongoid/indexable/specification.rb +20 -6
- data/lib/mongoid/indexable/validators/options.rb +33 -40
- data/lib/mongoid/indexable.rb +26 -25
- data/lib/mongoid/inspectable.rb +4 -7
- data/lib/mongoid/interceptable.rb +41 -40
- data/lib/mongoid/loadable.rb +17 -18
- data/lib/mongoid/loggable.rb +4 -8
- data/lib/mongoid/matchable.rb +0 -2
- data/lib/mongoid/matcher/all.rb +2 -5
- data/lib/mongoid/matcher/and.rb +0 -3
- data/lib/mongoid/matcher/bits.rb +13 -8
- data/lib/mongoid/matcher/bits_all_clear.rb +1 -3
- data/lib/mongoid/matcher/bits_all_set.rb +1 -3
- data/lib/mongoid/matcher/bits_any_clear.rb +1 -3
- data/lib/mongoid/matcher/bits_any_set.rb +1 -3
- data/lib/mongoid/matcher/elem_match.rb +13 -15
- data/lib/mongoid/matcher/elem_match_expression.rb +0 -3
- data/lib/mongoid/matcher/eq.rb +0 -3
- data/lib/mongoid/matcher/eq_impl.rb +12 -17
- data/lib/mongoid/matcher/eq_impl_with_regexp.rb +4 -7
- data/lib/mongoid/matcher/exists.rb +1 -4
- data/lib/mongoid/matcher/expression.rb +5 -10
- data/lib/mongoid/matcher/expression_operator.rb +1 -3
- data/lib/mongoid/matcher/field_expression.rb +10 -13
- data/lib/mongoid/matcher/field_operator.rb +4 -6
- data/lib/mongoid/matcher/gt.rb +0 -3
- data/lib/mongoid/matcher/gte.rb +0 -3
- data/lib/mongoid/matcher/in.rb +9 -12
- data/lib/mongoid/matcher/lt.rb +0 -3
- data/lib/mongoid/matcher/lte.rb +0 -3
- data/lib/mongoid/matcher/mod.rb +4 -8
- data/lib/mongoid/matcher/ne.rb +0 -3
- data/lib/mongoid/matcher/nin.rb +0 -3
- data/lib/mongoid/matcher/nor.rb +1 -6
- data/lib/mongoid/matcher/not.rb +0 -3
- data/lib/mongoid/matcher/or.rb +0 -3
- data/lib/mongoid/matcher/regex.rb +11 -14
- data/lib/mongoid/matcher/size.rb +2 -5
- data/lib/mongoid/matcher/type.rb +32 -37
- data/lib/mongoid/matcher.rb +7 -15
- data/lib/mongoid/model_resolver.rb +1 -0
- data/lib/mongoid/persistable/creatable.rb +23 -7
- data/lib/mongoid/persistable/deletable.rb +6 -7
- data/lib/mongoid/persistable/destroyable.rb +2 -6
- data/lib/mongoid/persistable/incrementable.rb +1 -3
- data/lib/mongoid/persistable/logical.rb +3 -5
- data/lib/mongoid/persistable/maxable.rb +2 -4
- data/lib/mongoid/persistable/minable.rb +2 -4
- data/lib/mongoid/persistable/multipliable.rb +1 -3
- data/lib/mongoid/persistable/poppable.rb +2 -4
- data/lib/mongoid/persistable/pullable.rb +3 -5
- data/lib/mongoid/persistable/pushable.rb +5 -7
- data/lib/mongoid/persistable/renamable.rb +1 -3
- data/lib/mongoid/persistable/savable.rb +0 -3
- data/lib/mongoid/persistable/settable.rb +4 -13
- data/lib/mongoid/persistable/unsettable.rb +1 -3
- data/lib/mongoid/persistable/updatable.rb +16 -24
- data/lib/mongoid/persistable/upsertable.rb +8 -10
- data/lib/mongoid/persistable.rb +28 -27
- data/lib/mongoid/persistence_context.rb +34 -41
- data/lib/mongoid/pluckable.rb +0 -2
- data/lib/mongoid/positional.rb +5 -11
- data/lib/mongoid/railtie.rb +16 -22
- data/lib/mongoid/railties/console_sandbox.rb +6 -8
- data/lib/mongoid/railties/controller_runtime.rb +7 -11
- data/lib/mongoid/railties/database.rake +42 -44
- data/lib/mongoid/scopable.rb +23 -31
- data/lib/mongoid/search_indexable.rb +330 -0
- data/lib/mongoid/selectable.rb +1 -3
- data/lib/mongoid/serializable.rb +6 -8
- data/lib/mongoid/shardable.rb +8 -17
- data/lib/mongoid/stateful.rb +5 -10
- data/lib/mongoid/stringified_symbol.rb +5 -9
- data/lib/mongoid/tasks/database.rake +31 -32
- data/lib/mongoid/tasks/database.rb +28 -30
- data/lib/mongoid/tasks/encryption.rb +10 -15
- data/lib/mongoid/threaded/lifecycle.rb +6 -9
- data/lib/mongoid/threaded.rb +132 -32
- data/lib/mongoid/timestamps/created/short.rb +2 -3
- data/lib/mongoid/timestamps/created.rb +1 -2
- data/lib/mongoid/timestamps/short.rb +0 -1
- data/lib/mongoid/timestamps/timeless.rb +8 -13
- data/lib/mongoid/timestamps/updated/short.rb +2 -3
- data/lib/mongoid/timestamps/updated.rb +2 -5
- data/lib/mongoid/timestamps.rb +4 -6
- data/lib/mongoid/touchable.rb +19 -11
- data/lib/mongoid/traversable.rb +3 -2
- data/lib/mongoid/validatable/associated.rb +1 -5
- data/lib/mongoid/validatable/format.rb +0 -2
- data/lib/mongoid/validatable/length.rb +0 -2
- data/lib/mongoid/validatable/localizable.rb +0 -3
- data/lib/mongoid/validatable/macros.rb +0 -2
- data/lib/mongoid/validatable/numericality.rb +24 -3
- data/lib/mongoid/validatable/presence.rb +8 -10
- data/lib/mongoid/validatable/queryable.rb +0 -3
- data/lib/mongoid/validatable/uniqueness.rb +12 -14
- data/lib/mongoid/validatable.rb +15 -20
- data/lib/mongoid/version.rb +1 -1
- data/lib/mongoid/warnings.rb +10 -11
- data/lib/mongoid.rb +30 -34
- data/lib/rails/generators/mongoid/config/config_generator.rb +2 -4
- data/lib/rails/generators/mongoid/config/templates/mongoid.rb +4 -5
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +2 -2
- data/lib/rails/generators/mongoid/model/model_generator.rb +5 -8
- data/lib/rails/generators/mongoid_generator.rb +6 -10
- data/lib/rails/mongoid.rb +1 -3
- data/spec/integration/active_job_spec.rb +1 -2
- data/spec/integration/app_spec.rb +142 -178
- data/spec/integration/associations/belongs_to_spec.rb +30 -28
- data/spec/integration/associations/embedded_dirty_spec.rb +0 -1
- data/spec/integration/associations/embedded_spec.rb +41 -43
- data/spec/integration/associations/embeds_many_spec.rb +46 -29
- data/spec/integration/associations/embeds_one_spec.rb +2 -1
- data/spec/integration/associations/foreign_key_spec.rb +10 -11
- data/spec/integration/associations/foreign_key_spec_models.rb +8 -9
- data/spec/integration/associations/has_and_belongs_to_many_spec.rb +16 -13
- data/spec/integration/associations/has_many_spec.rb +17 -18
- data/spec/integration/associations/has_one_spec.rb +42 -45
- data/spec/integration/associations/nested_attributes_assignment_spec.rb +36 -39
- data/spec/integration/associations/reverse_population_spec.rb +4 -5
- data/spec/integration/associations/reverse_population_spec_models.rb +0 -1
- data/spec/integration/associations/scope_option_spec.rb +18 -20
- data/spec/integration/atomic/modifiers_spec.rb +72 -52
- data/spec/integration/bson_regexp_raw_spec.rb +2 -3
- data/spec/integration/caching_spec.rb +68 -0
- data/spec/integration/callbacks_models.rb +11 -9
- data/spec/integration/callbacks_spec.rb +78 -80
- data/spec/integration/contextual/empty_spec.rb +19 -20
- data/spec/integration/criteria/alias_query_spec.rb +12 -19
- data/spec/integration/criteria/date_field_spec.rb +1 -2
- data/spec/integration/criteria/default_scope_spec.rb +23 -18
- data/spec/integration/criteria/logical_spec.rb +18 -19
- data/spec/integration/criteria/range_spec.rb +77 -86
- data/spec/integration/criteria/raw_value_spec.rb +122 -109
- data/spec/integration/criteria/time_with_zone_spec.rb +33 -24
- data/spec/integration/discriminator_key_spec.rb +85 -85
- data/spec/integration/discriminator_value_spec.rb +70 -73
- data/spec/integration/document_spec.rb +9 -10
- data/spec/integration/dots_and_dollars_spec.rb +104 -119
- data/spec/integration/encryption_spec.rb +8 -11
- data/spec/integration/i18n_fallbacks_spec.rb +7 -8
- data/spec/integration/isolation_state_spec.rb +241 -0
- data/spec/integration/matcher_examples_spec.rb +145 -168
- data/spec/integration/matcher_operator_data/in.yml +6 -6
- data/spec/integration/matcher_operator_spec.rb +16 -26
- data/spec/integration/matcher_spec.rb +31 -32
- data/spec/integration/persistence/collection_options_spec.rb +0 -2
- data/spec/integration/persistence/range_field_spec.rb +24 -2
- data/spec/integration/server_query_spec.rb +43 -37
- data/spec/integration/shardable_spec.rb +10 -11
- data/spec/integration/stringified_symbol_field_spec.rb +28 -43
- data/spec/lite_spec_helper.rb +11 -12
- data/spec/mongoid/association/accessors_spec.rb +197 -285
- data/spec/mongoid/association/auto_save_spec.rb +62 -93
- data/spec/mongoid/association/builders_spec.rb +121 -74
- data/spec/mongoid/association/constrainable_spec.rb +9 -22
- data/spec/mongoid/association/counter_cache_spec.rb +71 -104
- data/spec/mongoid/association/depending_spec.rb +166 -208
- data/spec/mongoid/association/eager_spec.rb +416 -80
- data/spec/mongoid/association/embedded/cyclic_spec.rb +36 -48
- data/spec/mongoid/association/embedded/dirty_spec.rb +15 -20
- data/spec/mongoid/association/embedded/embedded_in/binding_spec.rb +36 -54
- data/spec/mongoid/association/embedded/embedded_in/buildable_spec.rb +8 -15
- data/spec/mongoid/association/embedded/embedded_in/proxy_spec.rb +135 -175
- data/spec/mongoid/association/embedded/embedded_in_spec.rb +25 -127
- data/spec/mongoid/association/embedded/embeds_many/binding_spec.rb +9 -14
- data/spec/mongoid/association/embedded/embeds_many/buildable_spec.rb +38 -51
- data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +1151 -1340
- data/spec/mongoid/association/embedded/embeds_many_models.rb +17 -17
- data/spec/mongoid/association/embedded/embeds_many_query_spec.rb +11 -11
- data/spec/mongoid/association/embedded/embeds_many_spec.rb +25 -138
- data/spec/mongoid/association/embedded/embeds_one/binding_spec.rb +15 -23
- data/spec/mongoid/association/embedded/embeds_one/buildable_spec.rb +18 -26
- data/spec/mongoid/association/embedded/embeds_one/proxy_spec.rb +204 -273
- data/spec/mongoid/association/embedded/embeds_one_dnl_models.rb +0 -1
- data/spec/mongoid/association/embedded/embeds_one_models.rb +0 -1
- data/spec/mongoid/association/embedded/embeds_one_query_spec.rb +5 -6
- data/spec/mongoid/association/embedded/embeds_one_spec.rb +28 -143
- data/spec/mongoid/association/macros_spec.rb +213 -300
- data/spec/mongoid/association/nested/many_spec.rb +51 -73
- data/spec/mongoid/association/nested/one_spec.rb +56 -80
- data/spec/mongoid/association/options_spec.rb +187 -310
- data/spec/mongoid/association/polymorphic_spec.rb +35 -53
- data/spec/mongoid/association/referenced/belongs_to/binding_spec.rb +55 -76
- data/spec/mongoid/association/referenced/belongs_to/buildable_spec.rb +36 -41
- data/spec/mongoid/association/referenced/belongs_to/eager_spec.rb +81 -101
- data/spec/mongoid/association/referenced/belongs_to/proxy_spec.rb +258 -345
- data/spec/mongoid/association/referenced/belongs_to_models.rb +4 -3
- data/spec/mongoid/association/referenced/belongs_to_query_spec.rb +3 -4
- data/spec/mongoid/association/referenced/belongs_to_spec.rb +91 -294
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/binding_spec.rb +42 -56
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/buildable_spec.rb +41 -32
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/eager_spec.rb +36 -42
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_persistence_spec.rb +8 -9
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +771 -975
- data/spec/mongoid/association/referenced/has_and_belongs_to_many_models.rb +9 -9
- data/spec/mongoid/association/referenced/has_and_belongs_to_many_query_spec.rb +5 -6
- data/spec/mongoid/association/referenced/has_and_belongs_to_many_spec.rb +59 -177
- data/spec/mongoid/association/referenced/has_many/binding_spec.rb +29 -43
- data/spec/mongoid/association/referenced/has_many/buildable_spec.rb +53 -53
- data/spec/mongoid/association/referenced/has_many/eager_spec.rb +104 -67
- data/spec/mongoid/association/referenced/has_many/enumerable_spec.rb +431 -675
- data/spec/mongoid/association/referenced/has_many/proxy_query_spec.rb +1 -2
- data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +173 -11
- data/spec/mongoid/association/referenced/has_many_models.rb +1 -2
- data/spec/mongoid/association/referenced/has_many_query_spec.rb +3 -4
- data/spec/mongoid/association/referenced/has_many_spec.rb +44 -188
- data/spec/mongoid/association/referenced/has_one/binding_spec.rb +26 -38
- data/spec/mongoid/association/referenced/has_one/buildable_spec.rb +28 -29
- data/spec/mongoid/association/referenced/has_one/eager_spec.rb +29 -42
- data/spec/mongoid/association/referenced/has_one/proxy_spec.rb +270 -332
- data/spec/mongoid/association/referenced/has_one_models.rb +1 -2
- data/spec/mongoid/association/referenced/has_one_query_spec.rb +3 -4
- data/spec/mongoid/association/referenced/has_one_spec.rb +40 -188
- data/spec/mongoid/association/reflections_spec.rb +14 -24
- data/spec/mongoid/association/syncable_spec.rb +102 -125
- data/spec/mongoid/association_spec.rb +47 -70
- data/spec/mongoid/atomic/modifiers_spec.rb +184 -197
- data/spec/mongoid/atomic/paths/embedded/many_spec.rb +39 -52
- data/spec/mongoid/atomic/paths/embedded/one_spec.rb +35 -49
- data/spec/mongoid/atomic/paths/root_spec.rb +9 -15
- data/spec/mongoid/atomic/paths_spec.rb +81 -117
- data/spec/mongoid/atomic_spec.rb +156 -171
- data/spec/mongoid/attributes/dynamic_spec.rb +5 -6
- data/spec/mongoid/attributes/nested_spec.rb +1219 -1692
- data/spec/mongoid/attributes/nested_spec_models.rb +8 -9
- data/spec/mongoid/attributes/projector_spec.rb +2 -6
- data/spec/mongoid/attributes/readonly_spec.rb +73 -97
- data/spec/mongoid/attributes_spec.rb +669 -826
- data/spec/mongoid/cacheable_spec.rb +74 -44
- data/spec/mongoid/changeable_spec.rb +591 -733
- data/spec/mongoid/clients/factory_spec.rb +122 -154
- data/spec/mongoid/clients/options_spec.rb +40 -59
- data/spec/mongoid/clients/sessions_spec.rb +25 -44
- data/spec/mongoid/clients/transactions_spec.rb +33 -50
- data/spec/mongoid/clients/transactions_spec_models.rb +3 -4
- data/spec/mongoid/clients_spec.rb +265 -355
- data/spec/mongoid/collection_configurable_spec.rb +9 -12
- data/spec/mongoid/composable_spec.rb +2 -9
- data/spec/mongoid/config/defaults_spec.rb +24 -31
- data/spec/mongoid/config/encryption_spec.rb +54 -57
- data/spec/mongoid/config/environment_spec.rb +30 -40
- data/spec/mongoid/config/introspection_spec.rb +42 -37
- data/spec/mongoid/config/options_spec.rb +14 -21
- data/spec/mongoid/config_spec.rb +145 -195
- data/spec/mongoid/contextual/aggregable/memory_spec.rb +98 -135
- data/spec/mongoid/contextual/aggregable/memory_table_spec.rb +4 -7
- data/spec/mongoid/contextual/aggregable/mongo_spec.rb +149 -187
- data/spec/mongoid/contextual/aggregable/none_spec.rb +14 -16
- data/spec/mongoid/contextual/atomic_spec.rb +223 -271
- data/spec/mongoid/contextual/map_reduce_spec.rb +110 -138
- data/spec/mongoid/contextual/memory_spec.rb +706 -854
- data/spec/mongoid/contextual/mongo/documents_loader_spec.rb +12 -13
- data/spec/mongoid/contextual/mongo_spec.rb +1008 -1213
- data/spec/mongoid/contextual/none_spec.rb +54 -59
- data/spec/mongoid/contextual/short_circuit_spec.rb +139 -0
- data/spec/mongoid/copyable_spec.rb +254 -300
- data/spec/mongoid/copyable_spec_models.rb +0 -1
- data/spec/mongoid/criteria/findable_spec.rb +322 -387
- data/spec/mongoid/criteria/includable_spec.rb +421 -372
- data/spec/mongoid/criteria/includable_spec_models.rb +16 -7
- data/spec/mongoid/criteria/inspectable_spec.rb +11 -14
- data/spec/mongoid/criteria/marshalable_spec.rb +11 -14
- data/spec/mongoid/criteria/modifiable_spec.rb +366 -512
- data/spec/mongoid/criteria/options_spec.rb +2 -7
- data/spec/mongoid/criteria/queryable/aggregable_spec.rb +112 -141
- data/spec/mongoid/criteria/queryable/expandable_spec.rb +8 -11
- data/spec/mongoid/criteria/queryable/extensions/array_spec.rb +127 -184
- data/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb +123 -60
- data/spec/mongoid/criteria/queryable/extensions/boolean_spec.rb +84 -117
- data/spec/mongoid/criteria/queryable/extensions/date_spec.rb +44 -65
- data/spec/mongoid/criteria/queryable/extensions/date_time_spec.rb +86 -108
- data/spec/mongoid/criteria/queryable/extensions/float_spec.rb +94 -33
- data/spec/mongoid/criteria/queryable/extensions/hash_spec.rb +103 -138
- data/spec/mongoid/criteria/queryable/extensions/integer_spec.rb +95 -34
- data/spec/mongoid/criteria/queryable/extensions/nil_class_spec.rb +25 -38
- data/spec/mongoid/criteria/queryable/extensions/numeric_spec.rb +26 -28
- data/spec/mongoid/criteria/queryable/extensions/object_spec.rb +32 -54
- data/spec/mongoid/criteria/queryable/extensions/range_spec.rb +136 -130
- data/spec/mongoid/criteria/queryable/extensions/regexp_raw_spec.rb +16 -24
- data/spec/mongoid/criteria/queryable/extensions/regexp_spec.rb +15 -24
- data/spec/mongoid/criteria/queryable/extensions/set_spec.rb +12 -19
- data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +46 -63
- data/spec/mongoid/criteria/queryable/extensions/symbol_spec.rb +28 -42
- data/spec/mongoid/criteria/queryable/extensions/time_spec.rb +73 -96
- data/spec/mongoid/criteria/queryable/extensions/time_with_zone_spec.rb +71 -92
- data/spec/mongoid/criteria/queryable/key_spec.rb +32 -36
- data/spec/mongoid/criteria/queryable/mergeable_spec.rb +44 -49
- data/spec/mongoid/criteria/queryable/optional_spec.rb +373 -486
- data/spec/mongoid/criteria/queryable/options_spec.rb +172 -123
- data/spec/mongoid/criteria/queryable/pipeline_spec.rb +65 -87
- data/spec/mongoid/criteria/queryable/queryable_spec.rb +41 -53
- data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +576 -680
- data/spec/mongoid/criteria/queryable/selectable_shared_examples.rb +7 -11
- data/spec/mongoid/criteria/queryable/selectable_spec.rb +807 -998
- data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +154 -191
- data/spec/mongoid/criteria/queryable/selector_spec.rb +294 -390
- data/spec/mongoid/criteria/queryable/smash_spec.rb +6 -11
- data/spec/mongoid/criteria/queryable/storable_spec.rb +54 -81
- data/spec/mongoid/criteria/queryable_mql_spec.rb +92 -0
- data/spec/mongoid/criteria/scopable_spec.rb +102 -138
- data/spec/mongoid/criteria/translator_spec.rb +59 -60
- data/spec/mongoid/criteria_projection_spec.rb +81 -99
- data/spec/mongoid/criteria_spec.rb +935 -950
- data/spec/mongoid/document_fields_spec.rb +28 -30
- data/spec/mongoid/document_persistence_context_spec.rb +13 -15
- data/spec/mongoid/document_query_spec.rb +9 -12
- data/spec/mongoid/document_spec.rb +317 -409
- data/spec/mongoid/equality_spec.rb +69 -102
- data/spec/mongoid/errors/ambiguous_relationship_spec.rb +8 -11
- data/spec/mongoid/errors/attribute_not_loaded_spec.rb +7 -10
- data/spec/mongoid/errors/callback_spec.rb +8 -11
- data/spec/mongoid/errors/delete_restriction_spec.rb +5 -8
- data/spec/mongoid/errors/document_not_destroyed_spec.rb +7 -10
- data/spec/mongoid/errors/document_not_found_spec.rb +51 -61
- data/spec/mongoid/errors/invalid_collection_spec.rb +9 -13
- data/spec/mongoid/errors/invalid_config_file_spec.rb +8 -11
- data/spec/mongoid/errors/invalid_config_option_spec.rb +8 -11
- data/spec/mongoid/errors/invalid_field_option_spec.rb +7 -10
- data/spec/mongoid/errors/invalid_field_spec.rb +10 -11
- data/spec/mongoid/errors/invalid_field_type_spec.rb +9 -12
- data/spec/mongoid/errors/invalid_find_spec.rb +8 -11
- data/spec/mongoid/errors/invalid_includes_spec.rb +9 -13
- data/spec/mongoid/errors/invalid_index_spec.rb +8 -11
- data/spec/mongoid/errors/invalid_options_spec.rb +8 -11
- data/spec/mongoid/errors/invalid_path_spec.rb +7 -10
- data/spec/mongoid/errors/invalid_relation_spec.rb +15 -16
- data/spec/mongoid/errors/invalid_scope_spec.rb +8 -11
- data/spec/mongoid/errors/invalid_set_polymorphic_relation_spec.rb +3 -6
- data/spec/mongoid/errors/invalid_storage_options_spec.rb +8 -11
- data/spec/mongoid/errors/invalid_time_spec.rb +8 -11
- data/spec/mongoid/errors/inverse_not_found_spec.rb +9 -12
- data/spec/mongoid/errors/mixed_client_configuration_spec.rb +9 -12
- data/spec/mongoid/errors/mixed_relations_spec.rb +8 -11
- data/spec/mongoid/errors/mongoid_error_spec.rb +21 -24
- data/spec/mongoid/errors/nested_attributes_metadata_not_found_spec.rb +6 -9
- data/spec/mongoid/errors/no_client_config_spec.rb +7 -10
- data/spec/mongoid/errors/no_client_database_spec.rb +9 -12
- data/spec/mongoid/errors/no_client_hosts_spec.rb +9 -12
- data/spec/mongoid/errors/no_clients_config_spec.rb +7 -10
- data/spec/mongoid/errors/no_environment_spec.rb +8 -11
- data/spec/mongoid/errors/no_map_reduce_output_spec.rb +8 -11
- data/spec/mongoid/errors/no_metadata_spec.rb +7 -10
- data/spec/mongoid/errors/no_parent_spec.rb +7 -10
- data/spec/mongoid/errors/readonly_attribute_spec.rb +7 -10
- data/spec/mongoid/errors/readonly_document_spec.rb +6 -9
- data/spec/mongoid/errors/scope_overwrite_spec.rb +9 -12
- data/spec/mongoid/errors/too_many_nested_attribute_records_spec.rb +9 -12
- data/spec/mongoid/errors/unknown_attribute_spec.rb +5 -8
- data/spec/mongoid/errors/unsaved_document_spec.rb +8 -11
- data/spec/mongoid/errors/unsupported_javascript_spec.rb +9 -12
- data/spec/mongoid/errors/validations_spec.rb +11 -14
- data/spec/mongoid/extensions/array_spec.rb +70 -92
- data/spec/mongoid/extensions/big_decimal_spec.rb +233 -313
- data/spec/mongoid/extensions/binary_spec.rb +21 -32
- data/spec/mongoid/extensions/boolean_spec.rb +52 -75
- data/spec/mongoid/extensions/date_class_mongoize_spec.rb +43 -54
- data/spec/mongoid/extensions/date_spec.rb +26 -38
- data/spec/mongoid/extensions/date_time_spec.rb +22 -32
- data/spec/mongoid/extensions/decimal128_spec.rb +12 -18
- data/spec/mongoid/extensions/false_class_spec.rb +10 -17
- data/spec/mongoid/extensions/float_spec.rb +40 -61
- data/spec/mongoid/extensions/hash_spec.rb +46 -64
- data/spec/mongoid/extensions/integer_spec.rb +40 -61
- data/spec/mongoid/extensions/module_spec.rb +13 -18
- data/spec/mongoid/extensions/nil_class_spec.rb +2 -5
- data/spec/mongoid/extensions/object_id_spec.rb +159 -247
- data/spec/mongoid/extensions/object_spec.rb +31 -47
- data/spec/mongoid/extensions/range_spec.rb +112 -115
- data/spec/mongoid/extensions/raw_value_spec.rb +10 -12
- data/spec/mongoid/extensions/regexp_spec.rb +23 -36
- data/spec/mongoid/extensions/set_spec.rb +38 -49
- data/spec/mongoid/extensions/string_spec.rb +118 -167
- data/spec/mongoid/extensions/stringified_symbol_spec.rb +29 -43
- data/spec/mongoid/extensions/symbol_spec.rb +12 -20
- data/spec/mongoid/extensions/time_spec.rb +160 -202
- data/spec/mongoid/extensions/time_with_zone_spec.rb +79 -105
- data/spec/mongoid/extensions/true_class_spec.rb +10 -17
- data/spec/mongoid/factory_spec.rb +125 -148
- data/spec/mongoid/fields/foreign_key_spec.rb +124 -169
- data/spec/mongoid/fields/localized_spec.rb +140 -185
- data/spec/mongoid/fields/standard_spec.rb +43 -59
- data/spec/mongoid/fields_spec.rb +751 -620
- data/spec/mongoid/findable_spec.rb +260 -228
- data/spec/mongoid/indexable/specification_spec.rb +42 -58
- data/spec/mongoid/indexable_spec.rb +260 -264
- data/spec/mongoid/inspectable_spec.rb +45 -55
- data/spec/mongoid/interceptable_spec.rb +784 -922
- data/spec/mongoid/interceptable_spec_models.rb +35 -35
- data/spec/mongoid/loading_spec.rb +47 -43
- data/spec/mongoid/loggable_spec.rb +3 -6
- data/spec/mongoid/matcher/extract_attribute_spec.rb +2 -6
- data/spec/mongoid/mongoizable_spec.rb +81 -90
- data/spec/mongoid/monkey_patches_spec.rb +4 -0
- data/spec/mongoid/persistable/creatable_spec.rb +181 -220
- data/spec/mongoid/persistable/deletable_spec.rb +139 -179
- data/spec/mongoid/persistable/destroyable_spec.rb +135 -164
- data/spec/mongoid/persistable/incrementable_spec.rb +65 -84
- data/spec/mongoid/persistable/logical_spec.rb +48 -61
- data/spec/mongoid/persistable/maxable_spec.rb +45 -44
- data/spec/mongoid/persistable/minable_spec.rb +45 -44
- data/spec/mongoid/persistable/multipliable_spec.rb +54 -69
- data/spec/mongoid/persistable/poppable_spec.rb +47 -60
- data/spec/mongoid/persistable/pullable_spec.rb +79 -103
- data/spec/mongoid/persistable/pushable_spec.rb +106 -131
- data/spec/mongoid/persistable/renamable_spec.rb +52 -65
- data/spec/mongoid/persistable/savable_spec.rb +152 -189
- data/spec/mongoid/persistable/settable_spec.rb +121 -145
- data/spec/mongoid/persistable/unsettable_spec.rb +56 -73
- data/spec/mongoid/persistable/updatable_spec.rb +185 -232
- data/spec/mongoid/persistable/upsertable_spec.rb +66 -77
- data/spec/mongoid/persistable_spec.rb +132 -139
- data/spec/mongoid/persistence_context_spec.rb +31 -89
- data/spec/mongoid/positional_spec.rb +67 -79
- data/spec/mongoid/railties/bson_object_id_serializer_spec.rb +0 -3
- data/spec/mongoid/railties/console_sandbox_spec.rb +0 -1
- data/spec/mongoid/relations/proxy_spec.rb +0 -1
- data/spec/mongoid/reloadable_spec.rb +250 -145
- data/spec/mongoid/scopable_spec.rb +319 -398
- data/spec/mongoid/search_indexable_spec.rb +446 -41
- data/spec/mongoid/selectable_spec.rb +36 -51
- data/spec/mongoid/serializable_spec.rb +269 -312
- data/spec/mongoid/shardable_models.rb +5 -5
- data/spec/mongoid/shardable_spec.rb +44 -46
- data/spec/mongoid/stateful_spec.rb +55 -75
- data/spec/mongoid/tasks/database_rake_spec.rb +138 -141
- data/spec/mongoid/tasks/database_spec.rb +80 -80
- data/spec/mongoid/tasks/encryption_spec.rb +22 -20
- data/spec/mongoid/threaded_spec.rb +57 -83
- data/spec/mongoid/timestamps/created/short_spec.rb +12 -16
- data/spec/mongoid/timestamps/created_spec.rb +16 -20
- data/spec/mongoid/timestamps/timeless_spec.rb +31 -43
- data/spec/mongoid/timestamps/updated/short_spec.rb +21 -28
- data/spec/mongoid/timestamps/updated_spec.rb +18 -25
- data/spec/mongoid/timestamps_spec.rb +171 -188
- data/spec/mongoid/timestamps_spec_models.rb +12 -13
- data/spec/mongoid/touchable_spec.rb +330 -294
- data/spec/mongoid/touchable_spec_models.rb +35 -37
- data/spec/mongoid/traversable_spec.rb +435 -471
- data/spec/mongoid/validatable/associated_spec.rb +42 -60
- data/spec/mongoid/validatable/format_spec.rb +24 -34
- data/spec/mongoid/validatable/length_spec.rb +62 -86
- data/spec/mongoid/validatable/numericality_spec.rb +114 -23
- data/spec/mongoid/validatable/presence_spec.rb +149 -203
- data/spec/mongoid/validatable/uniqueness_spec.rb +632 -830
- data/spec/mongoid/validatable_spec.rb +71 -103
- data/spec/mongoid/warnings_spec.rb +5 -9
- data/spec/mongoid_spec.rb +25 -41
- data/spec/rails/controller_extension/controller_runtime_spec.rb +45 -49
- data/spec/rails/mongoid_spec.rb +15 -15
- data/spec/shared/lib/mrss/cluster_config.rb +5 -40
- data/spec/shared/lib/mrss/constraints.rb +7 -46
- data/spec/shared/lib/mrss/docker_runner.rb +10 -21
- data/spec/shared/lib/mrss/eg_config_utils.rb +0 -31
- data/spec/shared/lib/mrss/release/candidate.rb +18 -12
- data/spec/shared/lib/mrss/server_version_registry.rb +1 -5
- data/spec/shared/share/Dockerfile.erb +6 -8
- data/spec/shared/shlib/server.sh +0 -50
- data/spec/shared/shlib/set_env.sh +4 -10
- data/spec/spec_helper.rb +54 -31
- data/spec/support/authorization.rb +0 -1
- data/spec/support/client_registry.rb +1 -2
- data/spec/support/constraints.rb +8 -25
- data/spec/support/crypt/models.rb +7 -8
- data/spec/support/crypt.rb +6 -9
- data/spec/support/expectations.rb +21 -7
- data/spec/support/feature_sandbox.rb +1 -4
- data/spec/support/helpers.rb +0 -1
- data/spec/support/immutable_ids.rb +20 -21
- data/spec/support/macros.rb +1 -3
- data/spec/support/models/account.rb +4 -5
- data/spec/support/models/acolyte.rb +5 -5
- data/spec/support/models/actor.rb +3 -3
- data/spec/support/models/actress.rb +0 -1
- data/spec/support/models/address.rb +11 -11
- data/spec/support/models/address_component.rb +1 -1
- data/spec/support/models/address_number.rb +1 -1
- data/spec/support/models/agency.rb +1 -1
- data/spec/support/models/agent.rb +1 -1
- data/spec/support/models/album.rb +1 -2
- data/spec/support/models/alert.rb +1 -1
- data/spec/support/models/animal.rb +4 -5
- data/spec/support/models/answer.rb +1 -1
- data/spec/support/models/appointment.rb +2 -2
- data/spec/support/models/armrest.rb +0 -1
- data/spec/support/models/array_field.rb +0 -1
- data/spec/support/models/article.rb +0 -1
- data/spec/support/models/artist.rb +16 -12
- data/spec/support/models/artwork.rb +1 -1
- data/spec/support/models/audible_sound.rb +0 -1
- data/spec/support/models/audio.rb +2 -2
- data/spec/support/models/augmentation.rb +1 -2
- data/spec/support/models/author.rb +1 -1
- data/spec/support/models/baby.rb +1 -1
- data/spec/support/models/band.rb +0 -1
- data/spec/support/models/bar.rb +2 -2
- data/spec/support/models/basic.rb +0 -1
- data/spec/support/models/bed.rb +0 -1
- data/spec/support/models/big_palette.rb +0 -1
- data/spec/support/models/birthday.rb +3 -5
- data/spec/support/models/bolt.rb +0 -1
- data/spec/support/models/bomb.rb +1 -1
- data/spec/support/models/book.rb +1 -1
- data/spec/support/models/breed.rb +1 -1
- data/spec/support/models/browser.rb +1 -2
- data/spec/support/models/building.rb +0 -1
- data/spec/support/models/building_address.rb +1 -1
- data/spec/support/models/bus.rb +1 -1
- data/spec/support/models/business.rb +2 -2
- data/spec/support/models/callback_test.rb +1 -1
- data/spec/support/models/canvas.rb +5 -6
- data/spec/support/models/car.rb +0 -1
- data/spec/support/models/cat.rb +0 -2
- data/spec/support/models/catalog.rb +0 -1
- data/spec/support/models/category.rb +1 -1
- data/spec/support/models/child.rb +1 -1
- data/spec/support/models/child_doc.rb +5 -6
- data/spec/support/models/church.rb +1 -1
- data/spec/support/models/circle.rb +0 -1
- data/spec/support/models/circuit.rb +2 -2
- data/spec/support/models/circus.rb +0 -1
- data/spec/support/models/code.rb +2 -1
- data/spec/support/models/coding/pull_request.rb +0 -1
- data/spec/support/models/coding.rb +0 -1
- data/spec/support/models/comment.rb +0 -1
- data/spec/support/models/company.rb +0 -1
- data/spec/support/models/consumption_period.rb +0 -1
- data/spec/support/models/contextable_item.rb +1 -1
- data/spec/support/models/contractor.rb +1 -1
- data/spec/support/models/cookie.rb +0 -1
- data/spec/support/models/country_code.rb +2 -3
- data/spec/support/models/courier_job.rb +2 -2
- data/spec/support/models/cover.rb +0 -1
- data/spec/support/models/crate.rb +0 -1
- data/spec/support/models/customer.rb +0 -1
- data/spec/support/models/customer_address.rb +0 -1
- data/spec/support/models/deed.rb +1 -1
- data/spec/support/models/definition.rb +1 -1
- data/spec/support/models/delegating_patient.rb +0 -1
- data/spec/support/models/description.rb +0 -1
- data/spec/support/models/dictionary.rb +1 -1
- data/spec/support/models/division.rb +2 -2
- data/spec/support/models/doctor.rb +0 -1
- data/spec/support/models/dog.rb +2 -2
- data/spec/support/models/dokument.rb +1 -1
- data/spec/support/models/draft.rb +0 -1
- data/spec/support/models/dragon.rb +1 -1
- data/spec/support/models/driver.rb +2 -2
- data/spec/support/models/drug.rb +1 -1
- data/spec/support/models/dungeon.rb +1 -1
- data/spec/support/models/edit.rb +1 -1
- data/spec/support/models/email.rb +1 -1
- data/spec/support/models/employer.rb +1 -2
- data/spec/support/models/entry.rb +1 -1
- data/spec/support/models/eraser.rb +0 -1
- data/spec/support/models/even.rb +1 -1
- data/spec/support/models/event.rb +3 -4
- data/spec/support/models/exhibition.rb +1 -1
- data/spec/support/models/exhibitor.rb +1 -1
- data/spec/support/models/explosion.rb +1 -1
- data/spec/support/models/eye.rb +0 -1
- data/spec/support/models/eye_bowl.rb +2 -3
- data/spec/support/models/face.rb +2 -3
- data/spec/support/models/fanatic.rb +1 -1
- data/spec/support/models/favorite.rb +1 -1
- data/spec/support/models/filesystem.rb +1 -1
- data/spec/support/models/fire_hydrant.rb +1 -1
- data/spec/support/models/firefox.rb +0 -1
- data/spec/support/models/fish.rb +0 -1
- data/spec/support/models/folder.rb +0 -2
- data/spec/support/models/folder_item.rb +1 -3
- data/spec/support/models/fruits.rb +9 -5
- data/spec/support/models/game.rb +3 -4
- data/spec/support/models/ghost.rb +0 -1
- data/spec/support/models/guitar.rb +0 -1
- data/spec/support/models/hole.rb +0 -1
- data/spec/support/models/home.rb +1 -1
- data/spec/support/models/house.rb +2 -2
- data/spec/support/models/html_writer.rb +0 -1
- data/spec/support/models/id_key.rb +3 -3
- data/spec/support/models/idnodef.rb +0 -1
- data/spec/support/models/image.rb +0 -1
- data/spec/support/models/implant.rb +1 -2
- data/spec/support/models/instrument.rb +2 -3
- data/spec/support/models/item.rb +2 -2
- data/spec/support/models/jar.rb +1 -2
- data/spec/support/models/kaleidoscope.rb +1 -1
- data/spec/support/models/kangaroo.rb +1 -1
- data/spec/support/models/label.rb +2 -2
- data/spec/support/models/language.rb +1 -1
- data/spec/support/models/lat_lng.rb +0 -1
- data/spec/support/models/league.rb +1 -1
- data/spec/support/models/learner.rb +0 -1
- data/spec/support/models/line_item.rb +1 -1
- data/spec/support/models/location.rb +1 -1
- data/spec/support/models/login.rb +1 -2
- data/spec/support/models/manufacturer.rb +0 -1
- data/spec/support/models/meat.rb +1 -1
- data/spec/support/models/membership.rb +1 -1
- data/spec/support/models/message.rb +1 -2
- data/spec/support/models/minim.rb +0 -1
- data/spec/support/models/mixed_drink.rb +1 -1
- data/spec/support/models/modifier_models.rb +25 -0
- data/spec/support/models/mop.rb +0 -1
- data/spec/support/models/movie.rb +1 -1
- data/spec/support/models/my_hash.rb +1 -2
- data/spec/support/models/name.rb +4 -5
- data/spec/support/models/name_only.rb +0 -1
- data/spec/support/models/node.rb +1 -1
- data/spec/support/models/note.rb +3 -3
- data/spec/support/models/nut.rb +0 -1
- data/spec/support/models/odd.rb +1 -1
- data/spec/support/models/order.rb +1 -1
- data/spec/support/models/ordered_post.rb +1 -1
- data/spec/support/models/ordered_preference.rb +1 -1
- data/spec/support/models/oscar.rb +1 -1
- data/spec/support/models/other_owner_object.rb +0 -1
- data/spec/support/models/override.rb +3 -11
- data/spec/support/models/ownable.rb +1 -1
- data/spec/support/models/owner.rb +1 -1
- data/spec/support/models/pack.rb +0 -1
- data/spec/support/models/page.rb +3 -3
- data/spec/support/models/page_question.rb +1 -1
- data/spec/support/models/palette.rb +2 -2
- data/spec/support/models/parent.rb +2 -2
- data/spec/support/models/parent_doc.rb +1 -1
- data/spec/support/models/passport.rb +0 -1
- data/spec/support/models/patient.rb +2 -2
- data/spec/support/models/pdf_writer.rb +0 -1
- data/spec/support/models/pencil.rb +0 -1
- data/spec/support/models/person.rb +32 -28
- data/spec/support/models/pet.rb +1 -1
- data/spec/support/models/pet_owner.rb +1 -1
- data/spec/support/models/phone.rb +1 -2
- data/spec/support/models/piano.rb +0 -1
- data/spec/support/models/pizza.rb +1 -1
- data/spec/support/models/player.rb +5 -5
- data/spec/support/models/post.rb +12 -12
- data/spec/support/models/post_genre.rb +1 -1
- data/spec/support/models/powerup.rb +1 -2
- data/spec/support/models/preference.rb +2 -2
- data/spec/support/models/princess.rb +1 -1
- data/spec/support/models/product.rb +3 -3
- data/spec/support/models/profile.rb +2 -1
- data/spec/support/models/pronunciation.rb +1 -1
- data/spec/support/models/pub.rb +2 -2
- data/spec/support/models/publication/encyclopedia.rb +0 -1
- data/spec/support/models/publication/review.rb +0 -1
- data/spec/support/models/publication.rb +0 -1
- data/spec/support/models/purchase.rb +1 -1
- data/spec/support/models/purchased_item.rb +1 -1
- data/spec/support/models/question.rb +2 -2
- data/spec/support/models/quiz.rb +1 -1
- data/spec/support/models/rating.rb +1 -1
- data/spec/support/models/record.rb +2 -3
- data/spec/support/models/registry.rb +1 -1
- data/spec/support/models/role.rb +1 -1
- data/spec/support/models/root_category.rb +1 -1
- data/spec/support/models/sandbox/app/models/app_models_message.rb +0 -1
- data/spec/support/models/sandbox/lib/models/lib_models_message.rb +0 -1
- data/spec/support/models/sandbox/sandbox_message.rb +0 -1
- data/spec/support/models/sandbox/sandbox_user.rb +0 -1
- data/spec/support/models/sandbox/subdir/sandbox_comment.rb +0 -1
- data/spec/support/models/sandwich.rb +1 -1
- data/spec/support/models/scheduler.rb +0 -1
- data/spec/support/models/school.rb +0 -1
- data/spec/support/models/scribe.rb +1 -1
- data/spec/support/models/sealer.rb +0 -1
- data/spec/support/models/seat.rb +0 -1
- data/spec/support/models/seo.rb +1 -1
- data/spec/support/models/series.rb +1 -1
- data/spec/support/models/server.rb +1 -1
- data/spec/support/models/service.rb +2 -2
- data/spec/support/models/shape.rb +3 -3
- data/spec/support/models/shelf.rb +1 -1
- data/spec/support/models/shield.rb +2 -2
- data/spec/support/models/shipment_address.rb +0 -1
- data/spec/support/models/shipping_container.rb +1 -1
- data/spec/support/models/shipping_pack.rb +0 -1
- data/spec/support/models/shirt.rb +0 -1
- data/spec/support/models/shop.rb +2 -2
- data/spec/support/models/short_agent.rb +0 -1
- data/spec/support/models/short_quiz.rb +1 -1
- data/spec/support/models/simple.rb +1 -1
- data/spec/support/models/slave.rb +1 -1
- data/spec/support/models/song.rb +1 -2
- data/spec/support/models/sound.rb +2 -2
- data/spec/support/models/spacer.rb +0 -1
- data/spec/support/models/square.rb +0 -1
- data/spec/support/models/staff.rb +0 -1
- data/spec/support/models/store_as_dup_test1.rb +2 -2
- data/spec/support/models/store_as_dup_test2.rb +1 -1
- data/spec/support/models/store_as_dup_test3.rb +2 -2
- data/spec/support/models/store_as_dup_test4.rb +1 -1
- data/spec/support/models/strategy.rb +0 -1
- data/spec/support/models/student.rb +1 -2
- data/spec/support/models/sub_item.rb +0 -1
- data/spec/support/models/subscription.rb +2 -2
- data/spec/support/models/survey.rb +2 -2
- data/spec/support/models/symptom.rb +2 -2
- data/spec/support/models/system_role.rb +2 -3
- data/spec/support/models/tag.rb +2 -2
- data/spec/support/models/target.rb +1 -1
- data/spec/support/models/template.rb +1 -1
- data/spec/support/models/thing.rb +1 -1
- data/spec/support/models/threadlocker.rb +0 -1
- data/spec/support/models/title.rb +0 -1
- data/spec/support/models/tool.rb +3 -3
- data/spec/support/models/topping.rb +1 -1
- data/spec/support/models/toy.rb +0 -1
- data/spec/support/models/track.rb +1 -1
- data/spec/support/models/translation.rb +1 -1
- data/spec/support/models/tree.rb +2 -3
- data/spec/support/models/truck.rb +0 -1
- data/spec/support/models/updatable.rb +0 -1
- data/spec/support/models/user.rb +3 -4
- data/spec/support/models/user_account.rb +3 -3
- data/spec/support/models/validation_callback.rb +4 -4
- data/spec/support/models/vehicle.rb +2 -3
- data/spec/support/models/version.rb +1 -1
- data/spec/support/models/vertex.rb +0 -1
- data/spec/support/models/vet_visit.rb +1 -1
- data/spec/support/models/video.rb +2 -2
- data/spec/support/models/video_game.rb +0 -1
- data/spec/support/models/washer.rb +0 -1
- data/spec/support/models/weapon.rb +1 -2
- data/spec/support/models/wiki_page.rb +2 -3
- data/spec/support/models/word.rb +2 -2
- data/spec/support/models/word_origin.rb +1 -2
- data/spec/support/models/writer.rb +3 -3
- data/spec/support/rails_mock.rb +7 -12
- data/spec/support/shared/time.rb +0 -1
- data/spec/support/sinatra_mock.rb +4 -2
- data/spec/support/spec_config.rb +19 -18
- metadata +14 -2
|
@@ -1,139 +1,123 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
|
-
# rubocop:todo all
|
|
3
2
|
|
|
4
|
-
require
|
|
3
|
+
require 'spec_helper'
|
|
5
4
|
|
|
6
5
|
describe Mongoid::Criteria do
|
|
7
|
-
|
|
8
|
-
describe "#==" do
|
|
9
|
-
|
|
6
|
+
describe '#==' do
|
|
10
7
|
let(:criteria) do
|
|
11
|
-
Band.where(name:
|
|
8
|
+
Band.where(name: 'Depeche Mode')
|
|
12
9
|
end
|
|
13
10
|
|
|
14
|
-
context
|
|
15
|
-
|
|
16
|
-
context "when the criteria are the same" do
|
|
17
|
-
|
|
11
|
+
context 'when the other is a criteria' do
|
|
12
|
+
context 'when the criteria are the same' do
|
|
18
13
|
let(:other) do
|
|
19
|
-
Band.where(name:
|
|
14
|
+
Band.where(name: 'Depeche Mode')
|
|
20
15
|
end
|
|
21
16
|
|
|
22
|
-
it
|
|
17
|
+
it 'returns true' do
|
|
23
18
|
expect(criteria).to eq(other)
|
|
24
19
|
end
|
|
25
20
|
end
|
|
26
21
|
|
|
27
|
-
context
|
|
28
|
-
|
|
22
|
+
context 'when the criteria differ' do
|
|
29
23
|
let(:other) do
|
|
30
|
-
Band.where(name:
|
|
24
|
+
Band.where(name: 'Tool')
|
|
31
25
|
end
|
|
32
26
|
|
|
33
|
-
it
|
|
34
|
-
expect(criteria).
|
|
27
|
+
it 'returns false' do
|
|
28
|
+
expect(criteria).not_to eq(other)
|
|
35
29
|
end
|
|
36
30
|
end
|
|
37
31
|
end
|
|
38
32
|
|
|
39
|
-
context
|
|
40
|
-
|
|
41
|
-
context "when the entries are the same" do
|
|
42
|
-
|
|
33
|
+
context 'when the other is an enumerable' do
|
|
34
|
+
context 'when the entries are the same' do
|
|
43
35
|
let!(:band) do
|
|
44
|
-
Band.create!(name:
|
|
36
|
+
Band.create!(name: 'Depeche Mode')
|
|
45
37
|
end
|
|
46
38
|
|
|
47
39
|
let(:other) do
|
|
48
40
|
[ band ]
|
|
49
41
|
end
|
|
50
42
|
|
|
51
|
-
it
|
|
43
|
+
it 'returns true' do
|
|
52
44
|
expect(criteria).to eq(other)
|
|
53
45
|
end
|
|
54
46
|
end
|
|
55
47
|
|
|
56
|
-
context
|
|
57
|
-
|
|
48
|
+
context 'when the entries are not the same' do
|
|
58
49
|
let!(:band) do
|
|
59
|
-
Band.create!(name:
|
|
50
|
+
Band.create!(name: 'Depeche Mode')
|
|
60
51
|
end
|
|
61
52
|
|
|
62
53
|
let!(:other_band) do
|
|
63
|
-
Band.create!(name:
|
|
54
|
+
Band.create!(name: 'Tool')
|
|
64
55
|
end
|
|
65
56
|
|
|
66
57
|
let(:other) do
|
|
67
58
|
[ other_band ]
|
|
68
59
|
end
|
|
69
60
|
|
|
70
|
-
it
|
|
71
|
-
expect(criteria).
|
|
61
|
+
it 'returns false' do
|
|
62
|
+
expect(criteria).not_to eq(other)
|
|
72
63
|
end
|
|
73
64
|
end
|
|
74
65
|
end
|
|
75
66
|
|
|
76
|
-
context
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
expect(criteria).to_not eq("test")
|
|
67
|
+
context 'when the other is neither a criteria or enumerable' do
|
|
68
|
+
it 'returns false' do
|
|
69
|
+
expect(criteria).not_to eq('test')
|
|
80
70
|
end
|
|
81
71
|
end
|
|
82
72
|
end
|
|
83
73
|
|
|
84
|
-
describe
|
|
85
|
-
|
|
86
|
-
context "when the other is a criteria" do
|
|
87
|
-
|
|
74
|
+
describe '#===' do
|
|
75
|
+
context 'when the other is a criteria' do
|
|
88
76
|
let(:other) do
|
|
89
|
-
Band.where(name:
|
|
77
|
+
Band.where(name: 'Depeche Mode')
|
|
90
78
|
end
|
|
91
79
|
|
|
92
|
-
it
|
|
93
|
-
expect(described_class === other).to be true
|
|
80
|
+
it 'returns true' do
|
|
81
|
+
expect(described_class === other).to be true # rubocop:disable Style/CaseEquality
|
|
94
82
|
end
|
|
95
83
|
end
|
|
96
84
|
|
|
97
|
-
context
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
expect(described_class === []).to be false
|
|
85
|
+
context 'when the other is not a criteria' do
|
|
86
|
+
it 'returns false' do
|
|
87
|
+
expect(described_class === []).to be false # rubocop:disable Style/CaseEquality
|
|
101
88
|
end
|
|
102
89
|
end
|
|
103
90
|
end
|
|
104
91
|
|
|
105
|
-
describe
|
|
106
|
-
|
|
92
|
+
describe '#asc' do
|
|
107
93
|
let(:person) do
|
|
108
94
|
Person.create!
|
|
109
95
|
end
|
|
110
96
|
|
|
111
|
-
context
|
|
112
|
-
|
|
97
|
+
context 'when the documents are embedded' do
|
|
113
98
|
let!(:hobrecht) do
|
|
114
|
-
person.addresses.create!(street:
|
|
99
|
+
person.addresses.create!(street: 'hobrecht', name: 'hobrecht')
|
|
115
100
|
end
|
|
116
101
|
|
|
117
102
|
let!(:friedel) do
|
|
118
|
-
person.addresses.create!(street:
|
|
103
|
+
person.addresses.create!(street: 'friedel', name: 'friedel')
|
|
119
104
|
end
|
|
120
105
|
|
|
121
106
|
let!(:pfluger) do
|
|
122
|
-
person.addresses.create!(street:
|
|
107
|
+
person.addresses.create!(street: 'pfluger', name: 'pfluger')
|
|
123
108
|
end
|
|
124
109
|
|
|
125
110
|
let(:criteria) do
|
|
126
111
|
person.addresses.asc(:name)
|
|
127
112
|
end
|
|
128
113
|
|
|
129
|
-
it
|
|
114
|
+
it 'returns the sorted documents' do
|
|
130
115
|
expect(criteria).to eq([ friedel, hobrecht, pfluger ])
|
|
131
116
|
end
|
|
132
117
|
end
|
|
133
118
|
end
|
|
134
119
|
|
|
135
|
-
describe
|
|
136
|
-
|
|
120
|
+
describe '#batch_size' do
|
|
137
121
|
let(:person) do
|
|
138
122
|
Person.create!
|
|
139
123
|
end
|
|
@@ -142,17 +126,16 @@ describe Mongoid::Criteria do
|
|
|
142
126
|
Person.batch_size(1000)
|
|
143
127
|
end
|
|
144
128
|
|
|
145
|
-
it
|
|
129
|
+
it 'adds the batch size option' do
|
|
146
130
|
expect(criteria.options[:batch_size]).to eq(1000)
|
|
147
131
|
end
|
|
148
132
|
|
|
149
|
-
it
|
|
133
|
+
it 'returns the correct documents' do
|
|
150
134
|
expect(criteria).to eq([ person ])
|
|
151
135
|
end
|
|
152
136
|
end
|
|
153
137
|
|
|
154
|
-
describe
|
|
155
|
-
|
|
138
|
+
describe '#read' do
|
|
156
139
|
let(:person) do
|
|
157
140
|
Person.create!
|
|
158
141
|
end
|
|
@@ -161,21 +144,19 @@ describe Mongoid::Criteria do
|
|
|
161
144
|
Person.read(mode: :secondary)
|
|
162
145
|
end
|
|
163
146
|
|
|
164
|
-
it
|
|
147
|
+
it 'adds the read option' do
|
|
165
148
|
expect(criteria.options[:read]).to eq(mode: :secondary)
|
|
166
149
|
end
|
|
167
150
|
end
|
|
168
151
|
|
|
169
|
-
describe
|
|
170
|
-
|
|
171
|
-
context "when provided a single field" do
|
|
172
|
-
|
|
152
|
+
describe '#aggregates' do
|
|
153
|
+
context 'when provided a single field' do
|
|
173
154
|
let!(:depeche) do
|
|
174
|
-
Band.create!(name:
|
|
155
|
+
Band.create!(name: 'Depeche Mode', likes: 1000)
|
|
175
156
|
end
|
|
176
157
|
|
|
177
158
|
let!(:tool) do
|
|
178
|
-
Band.create!(name:
|
|
159
|
+
Band.create!(name: 'Tool', likes: 500)
|
|
179
160
|
end
|
|
180
161
|
|
|
181
162
|
let(:criteria) do
|
|
@@ -186,38 +167,36 @@ describe Mongoid::Criteria do
|
|
|
186
167
|
criteria.aggregates(:likes)
|
|
187
168
|
end
|
|
188
169
|
|
|
189
|
-
it
|
|
190
|
-
expect(aggregates[
|
|
170
|
+
it 'returns an avg' do
|
|
171
|
+
expect(aggregates['avg']).to eq(750)
|
|
191
172
|
end
|
|
192
173
|
|
|
193
|
-
it
|
|
194
|
-
expect(aggregates[
|
|
174
|
+
it 'returns a count' do
|
|
175
|
+
expect(aggregates['count']).to eq(2)
|
|
195
176
|
end
|
|
196
177
|
|
|
197
|
-
it
|
|
198
|
-
expect(aggregates[
|
|
178
|
+
it 'returns a max' do
|
|
179
|
+
expect(aggregates['max']).to eq(1000)
|
|
199
180
|
end
|
|
200
181
|
|
|
201
|
-
it
|
|
202
|
-
expect(aggregates[
|
|
182
|
+
it 'returns a min' do
|
|
183
|
+
expect(aggregates['min']).to eq(500)
|
|
203
184
|
end
|
|
204
185
|
|
|
205
|
-
it
|
|
206
|
-
expect(aggregates[
|
|
186
|
+
it 'returns a sum' do
|
|
187
|
+
expect(aggregates['sum']).to eq(1500)
|
|
207
188
|
end
|
|
208
189
|
end
|
|
209
190
|
end
|
|
210
191
|
|
|
211
|
-
describe
|
|
212
|
-
|
|
213
|
-
context "when provided a single field" do
|
|
214
|
-
|
|
192
|
+
describe '#avg' do
|
|
193
|
+
context 'when provided a single field' do
|
|
215
194
|
let!(:depeche) do
|
|
216
|
-
Band.create!(name:
|
|
195
|
+
Band.create!(name: 'Depeche Mode', likes: 1000)
|
|
217
196
|
end
|
|
218
197
|
|
|
219
198
|
let!(:tool) do
|
|
220
|
-
Band.create!(name:
|
|
199
|
+
Band.create!(name: 'Tool', likes: 500)
|
|
221
200
|
end
|
|
222
201
|
|
|
223
202
|
let(:criteria) do
|
|
@@ -228,73 +207,67 @@ describe Mongoid::Criteria do
|
|
|
228
207
|
criteria.avg(:likes)
|
|
229
208
|
end
|
|
230
209
|
|
|
231
|
-
it
|
|
210
|
+
it 'returns the avg of the provided field' do
|
|
232
211
|
expect(avg).to eq(750)
|
|
233
212
|
end
|
|
234
213
|
end
|
|
235
214
|
end
|
|
236
215
|
|
|
237
|
-
[
|
|
238
|
-
|
|
239
|
-
describe "\##{method}" do
|
|
240
|
-
|
|
216
|
+
%i[all all_in].each do |method|
|
|
217
|
+
describe "##{method}" do
|
|
241
218
|
let!(:match) do
|
|
242
|
-
Band.create!(genres: [
|
|
219
|
+
Band.create!(genres: %w[electro dub])
|
|
243
220
|
end
|
|
244
221
|
|
|
245
222
|
let!(:non_match) do
|
|
246
|
-
Band.create!(genres: [
|
|
223
|
+
Band.create!(genres: [ 'house' ])
|
|
247
224
|
end
|
|
248
225
|
|
|
249
226
|
let(:criteria) do
|
|
250
|
-
Band.send(method, genres: [
|
|
227
|
+
Band.send(method, genres: %w[electro dub])
|
|
251
228
|
end
|
|
252
229
|
|
|
253
|
-
it
|
|
230
|
+
it 'returns the matching documents' do
|
|
254
231
|
expect(criteria).to eq([ match ])
|
|
255
232
|
end
|
|
256
233
|
end
|
|
257
234
|
end
|
|
258
235
|
|
|
259
|
-
[
|
|
260
|
-
|
|
261
|
-
describe "\##{method}" do
|
|
262
|
-
|
|
236
|
+
%i[and all_of].each do |method|
|
|
237
|
+
describe "##{method}" do
|
|
263
238
|
let!(:match) do
|
|
264
|
-
Band.create!(name:
|
|
239
|
+
Band.create!(name: 'Depeche Mode', genres: [ 'electro' ])
|
|
265
240
|
end
|
|
266
241
|
|
|
267
242
|
let!(:non_match) do
|
|
268
|
-
Band.create!(genres: [
|
|
243
|
+
Band.create!(genres: [ 'house' ])
|
|
269
244
|
end
|
|
270
245
|
|
|
271
246
|
let(:criteria) do
|
|
272
|
-
Band.send(method, { genres:
|
|
247
|
+
Band.send(method, { genres: 'electro' }, { name: 'Depeche Mode' })
|
|
273
248
|
end
|
|
274
249
|
|
|
275
|
-
it
|
|
250
|
+
it 'returns the matching documents' do
|
|
276
251
|
expect(criteria).to eq([ match ])
|
|
277
252
|
end
|
|
278
253
|
end
|
|
279
254
|
end
|
|
280
255
|
|
|
281
|
-
describe
|
|
282
|
-
|
|
256
|
+
describe '#as_json' do
|
|
283
257
|
let!(:band) do
|
|
284
|
-
Band.create!(name:
|
|
258
|
+
Band.create!(name: 'Depeche Mode')
|
|
285
259
|
end
|
|
286
260
|
|
|
287
261
|
let(:criteria) do
|
|
288
|
-
Band.where(name:
|
|
262
|
+
Band.where(name: 'Depeche Mode')
|
|
289
263
|
end
|
|
290
264
|
|
|
291
|
-
it
|
|
265
|
+
it 'returns the criteria as a json hash' do
|
|
292
266
|
expect(criteria.as_json).to eq([ band.serializable_hash.as_json ])
|
|
293
267
|
end
|
|
294
268
|
end
|
|
295
269
|
|
|
296
|
-
describe
|
|
297
|
-
|
|
270
|
+
describe '#between' do
|
|
298
271
|
let!(:match) do
|
|
299
272
|
Band.create!(member_count: 3)
|
|
300
273
|
end
|
|
@@ -307,69 +280,65 @@ describe Mongoid::Criteria do
|
|
|
307
280
|
Band.between(member_count: 1..5)
|
|
308
281
|
end
|
|
309
282
|
|
|
310
|
-
it
|
|
283
|
+
it 'returns the matching documents' do
|
|
311
284
|
expect(criteria).to eq([ match ])
|
|
312
285
|
end
|
|
313
286
|
end
|
|
314
287
|
|
|
315
|
-
[
|
|
316
|
-
|
|
288
|
+
%i[build new].each do |method|
|
|
317
289
|
describe "##{method}" do
|
|
318
|
-
|
|
319
290
|
let(:criteria) do
|
|
320
|
-
Band.where(name:
|
|
291
|
+
Band.where(name: 'Depeche Mode')
|
|
321
292
|
end
|
|
322
293
|
|
|
323
|
-
context
|
|
324
|
-
|
|
294
|
+
context 'when provided valid attributes' do
|
|
325
295
|
let(:band) do
|
|
326
|
-
criteria.send(method, genres: [
|
|
296
|
+
criteria.send(method, genres: [ 'electro' ])
|
|
327
297
|
end
|
|
328
298
|
|
|
329
|
-
it
|
|
299
|
+
it 'returns the new document' do
|
|
330
300
|
expect(band).to be_new_record
|
|
331
301
|
end
|
|
332
302
|
|
|
333
|
-
it
|
|
334
|
-
expect(band.name).to eq(
|
|
303
|
+
it 'sets the criteria attributes' do
|
|
304
|
+
expect(band.name).to eq('Depeche Mode')
|
|
335
305
|
end
|
|
336
306
|
|
|
337
|
-
it
|
|
338
|
-
expect(band.genres).to eq([
|
|
307
|
+
it 'sets the attributes passed to build' do
|
|
308
|
+
expect(band.genres).to eq([ 'electro' ])
|
|
339
309
|
end
|
|
340
310
|
end
|
|
341
311
|
|
|
342
|
-
context
|
|
343
|
-
context
|
|
312
|
+
context 'when provided a block' do
|
|
313
|
+
context 'when provided valid attributes' do
|
|
344
314
|
let(:band) do
|
|
345
315
|
criteria.send(method) do |c|
|
|
346
|
-
c.genres = [
|
|
316
|
+
c.genres = [ 'electro' ]
|
|
347
317
|
end
|
|
348
318
|
end
|
|
349
319
|
|
|
350
|
-
it
|
|
320
|
+
it 'returns the new document' do
|
|
351
321
|
expect(band).to be_new_record
|
|
352
322
|
end
|
|
353
323
|
|
|
354
|
-
it
|
|
355
|
-
expect(band.name).to eq(
|
|
324
|
+
it 'sets the criteria attributes' do
|
|
325
|
+
expect(band.name).to eq('Depeche Mode')
|
|
356
326
|
end
|
|
357
327
|
|
|
358
|
-
it
|
|
359
|
-
expect(band.genres).to eq([
|
|
328
|
+
it 'sets the attributes passed to build' do
|
|
329
|
+
expect(band.genres).to eq([ 'electro' ])
|
|
360
330
|
end
|
|
361
331
|
end
|
|
362
332
|
end
|
|
363
333
|
end
|
|
364
334
|
end
|
|
365
335
|
|
|
366
|
-
describe
|
|
367
|
-
|
|
336
|
+
describe '#cache' do
|
|
368
337
|
let!(:person) do
|
|
369
338
|
Person.create!
|
|
370
339
|
end
|
|
371
340
|
|
|
372
|
-
context
|
|
341
|
+
context 'when the query cache is enabled' do
|
|
373
342
|
query_cache_enabled
|
|
374
343
|
|
|
375
344
|
let(:criteria) do
|
|
@@ -377,19 +346,17 @@ describe Mongoid::Criteria do
|
|
|
377
346
|
end
|
|
378
347
|
|
|
379
348
|
before do
|
|
380
|
-
criteria.
|
|
349
|
+
criteria.to_a
|
|
381
350
|
end
|
|
382
351
|
|
|
383
|
-
it
|
|
352
|
+
it 'does not hit the database after first iteration' do
|
|
384
353
|
expect_no_queries do
|
|
385
|
-
criteria.
|
|
386
|
-
expect(doc).to eq(person)
|
|
387
|
-
end
|
|
354
|
+
expect(criteria).to all(eq(person))
|
|
388
355
|
end
|
|
389
356
|
end
|
|
390
357
|
end
|
|
391
358
|
|
|
392
|
-
context
|
|
359
|
+
context 'when the criteria is eager loading' do
|
|
393
360
|
query_cache_enabled
|
|
394
361
|
|
|
395
362
|
let(:criteria) do
|
|
@@ -397,29 +364,28 @@ describe Mongoid::Criteria do
|
|
|
397
364
|
end
|
|
398
365
|
|
|
399
366
|
before do
|
|
400
|
-
criteria.
|
|
367
|
+
criteria.to_a
|
|
401
368
|
end
|
|
402
369
|
|
|
403
|
-
it
|
|
370
|
+
it 'does not hit the database after first iteration' do
|
|
404
371
|
expect_no_queries do
|
|
405
|
-
criteria.
|
|
406
|
-
expect(doc).to eq(person)
|
|
407
|
-
end
|
|
372
|
+
expect(criteria).to all(eq(person))
|
|
408
373
|
end
|
|
409
374
|
end
|
|
410
375
|
end
|
|
411
376
|
end
|
|
412
377
|
|
|
413
|
-
[
|
|
414
|
-
|
|
415
|
-
describe "\##{method}" do
|
|
416
|
-
|
|
378
|
+
%i[clone dup].each do |method|
|
|
379
|
+
describe "##{method}" do
|
|
417
380
|
let(:band) do
|
|
418
381
|
Band.new
|
|
419
382
|
end
|
|
383
|
+
let(:clone) do
|
|
384
|
+
criteria.send(method)
|
|
385
|
+
end
|
|
420
386
|
|
|
421
387
|
let(:criteria) do
|
|
422
|
-
Band.where(name:
|
|
388
|
+
Band.where(name: 'Depeche Mode').asc(:name).includes(:records).read(mode: :secondary)
|
|
423
389
|
end
|
|
424
390
|
|
|
425
391
|
before do
|
|
@@ -427,51 +393,47 @@ describe Mongoid::Criteria do
|
|
|
427
393
|
criteria.context
|
|
428
394
|
end
|
|
429
395
|
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
end
|
|
433
|
-
|
|
434
|
-
it "contains an equal selector" do
|
|
435
|
-
expect(clone.selector).to eq({ "name" => "Depeche Mode" })
|
|
396
|
+
it 'contains an equal selector' do
|
|
397
|
+
expect(clone.selector).to eq({ 'name' => 'Depeche Mode' })
|
|
436
398
|
end
|
|
437
399
|
|
|
438
|
-
it
|
|
439
|
-
expect(clone.selector).
|
|
400
|
+
it 'clones the selector' do
|
|
401
|
+
expect(clone.selector).not_to equal(criteria.selector)
|
|
440
402
|
end
|
|
441
403
|
|
|
442
|
-
it
|
|
443
|
-
expect(clone.options).to eq({ sort: {
|
|
404
|
+
it 'contains equal options' do
|
|
405
|
+
expect(clone.options).to eq({ sort: { 'name' => 1 }, read: { mode: :secondary } })
|
|
444
406
|
end
|
|
445
407
|
|
|
446
|
-
it
|
|
447
|
-
expect(clone.options).
|
|
408
|
+
it 'clones the options' do
|
|
409
|
+
expect(clone.options).not_to equal(criteria.options)
|
|
448
410
|
end
|
|
449
411
|
|
|
450
|
-
it
|
|
451
|
-
expect(clone.inclusions).to eq([ Band.relations[
|
|
412
|
+
it 'contains equal inclusions' do
|
|
413
|
+
expect(clone.inclusions).to eq([ Band.relations['records'] ])
|
|
452
414
|
end
|
|
453
415
|
|
|
454
|
-
it
|
|
455
|
-
expect(clone.inclusions).
|
|
416
|
+
it 'clones the inclusions' do
|
|
417
|
+
expect(clone.inclusions).not_to equal(criteria.inclusions)
|
|
456
418
|
end
|
|
457
419
|
|
|
458
|
-
it
|
|
420
|
+
it 'contains equal documents' do
|
|
459
421
|
expect(clone.documents).to eq([ band ])
|
|
460
422
|
end
|
|
461
423
|
|
|
462
|
-
it
|
|
463
|
-
expect(clone.documents).
|
|
424
|
+
it 'clones the documents' do
|
|
425
|
+
expect(clone.documents).not_to equal(criteria.documents)
|
|
464
426
|
end
|
|
465
427
|
|
|
466
|
-
it
|
|
428
|
+
it 'contains equal scoping options' do
|
|
467
429
|
expect(clone.scoping_options).to eq([ nil, nil ])
|
|
468
430
|
end
|
|
469
431
|
|
|
470
|
-
it
|
|
471
|
-
expect(clone.scoping_options).
|
|
432
|
+
it 'clones the scoping options' do
|
|
433
|
+
expect(clone.scoping_options).not_to equal(criteria.scoping_options)
|
|
472
434
|
end
|
|
473
435
|
|
|
474
|
-
it
|
|
436
|
+
it 'sets the context to nil' do
|
|
475
437
|
expect(clone.instance_variable_get(:@context)).to be_nil
|
|
476
438
|
end
|
|
477
439
|
|
|
@@ -481,57 +443,51 @@ describe Mongoid::Criteria do
|
|
|
481
443
|
end
|
|
482
444
|
end
|
|
483
445
|
|
|
484
|
-
describe
|
|
485
|
-
|
|
486
|
-
context "when the model is embedded" do
|
|
487
|
-
|
|
446
|
+
describe '#context' do
|
|
447
|
+
context 'when the model is embedded' do
|
|
488
448
|
let(:criteria) do
|
|
489
449
|
described_class.new(Record) do |criteria|
|
|
490
450
|
criteria.embedded = true
|
|
491
451
|
end
|
|
492
452
|
end
|
|
493
453
|
|
|
494
|
-
it
|
|
454
|
+
it 'returns the embedded context' do
|
|
495
455
|
expect(criteria.context).to be_a(Mongoid::Contextual::Memory)
|
|
496
456
|
end
|
|
497
457
|
end
|
|
498
458
|
|
|
499
|
-
context
|
|
500
|
-
|
|
459
|
+
context 'when the model is not embedded' do
|
|
501
460
|
let(:criteria) do
|
|
502
461
|
described_class.new(Band)
|
|
503
462
|
end
|
|
504
463
|
|
|
505
|
-
it
|
|
464
|
+
it 'returns the mongo context' do
|
|
506
465
|
expect(criteria.context).to be_a(Mongoid::Contextual::Mongo)
|
|
507
466
|
end
|
|
508
467
|
end
|
|
509
468
|
end
|
|
510
469
|
|
|
511
|
-
describe
|
|
512
|
-
|
|
470
|
+
describe '#delete' do
|
|
513
471
|
let(:depeche) do
|
|
514
|
-
Band.create!(name:
|
|
472
|
+
Band.create!(name: 'Depeche Mode')
|
|
515
473
|
end
|
|
516
474
|
|
|
517
475
|
let(:tool) do
|
|
518
|
-
Band.create!(name:
|
|
476
|
+
Band.create!(name: 'Tool')
|
|
519
477
|
end
|
|
520
478
|
|
|
521
|
-
context
|
|
522
|
-
|
|
479
|
+
context 'when no selector is provided' do
|
|
523
480
|
before do
|
|
524
481
|
Band.all.delete
|
|
525
482
|
end
|
|
526
483
|
|
|
527
|
-
it
|
|
484
|
+
it 'deletes all the documents from the database' do
|
|
528
485
|
expect(Band.count).to eq(0)
|
|
529
486
|
end
|
|
530
487
|
end
|
|
531
488
|
end
|
|
532
489
|
|
|
533
|
-
describe
|
|
534
|
-
|
|
490
|
+
describe '#documents' do
|
|
535
491
|
let(:band) do
|
|
536
492
|
Band.new
|
|
537
493
|
end
|
|
@@ -542,13 +498,12 @@ describe Mongoid::Criteria do
|
|
|
542
498
|
end
|
|
543
499
|
end
|
|
544
500
|
|
|
545
|
-
it
|
|
501
|
+
it 'returns the documents' do
|
|
546
502
|
expect(criteria.documents).to eq([ band ])
|
|
547
503
|
end
|
|
548
504
|
end
|
|
549
505
|
|
|
550
|
-
describe
|
|
551
|
-
|
|
506
|
+
describe '#documents=' do
|
|
552
507
|
let(:band) do
|
|
553
508
|
Band.new
|
|
554
509
|
end
|
|
@@ -561,119 +516,107 @@ describe Mongoid::Criteria do
|
|
|
561
516
|
criteria.documents = [ band ]
|
|
562
517
|
end
|
|
563
518
|
|
|
564
|
-
it
|
|
519
|
+
it 'sets the documents' do
|
|
565
520
|
expect(criteria.documents).to eq([ band ])
|
|
566
521
|
end
|
|
567
522
|
end
|
|
568
523
|
|
|
569
|
-
describe
|
|
570
|
-
|
|
524
|
+
describe '#each' do
|
|
571
525
|
let!(:band) do
|
|
572
|
-
Band.create!(name:
|
|
526
|
+
Band.create!(name: 'Depeche Mode')
|
|
573
527
|
end
|
|
574
528
|
|
|
575
|
-
context
|
|
576
|
-
|
|
529
|
+
context 'when provided a block' do
|
|
577
530
|
let(:criteria) do
|
|
578
|
-
Band.where(name:
|
|
531
|
+
Band.where(name: 'Depeche Mode')
|
|
579
532
|
end
|
|
580
533
|
|
|
581
|
-
it
|
|
582
|
-
criteria.
|
|
583
|
-
expect(doc).to eq(band)
|
|
584
|
-
end
|
|
534
|
+
it 'iterates over the matching documents' do
|
|
535
|
+
expect(criteria).to all(eq(band))
|
|
585
536
|
end
|
|
586
537
|
end
|
|
587
538
|
end
|
|
588
539
|
|
|
589
|
-
describe
|
|
590
|
-
|
|
540
|
+
describe '#elem_match' do
|
|
591
541
|
let!(:match) do
|
|
592
|
-
Band.create!(name:
|
|
542
|
+
Band.create!(name: 'Depeche Mode').tap do |band|
|
|
593
543
|
r = band.records
|
|
594
|
-
r.create!(name:
|
|
544
|
+
r.create!(name: '101')
|
|
595
545
|
end
|
|
596
546
|
end
|
|
597
547
|
|
|
598
548
|
let!(:non_match) do
|
|
599
|
-
Band.create!(genres: [
|
|
549
|
+
Band.create!(genres: [ 'house' ])
|
|
600
550
|
end
|
|
601
551
|
|
|
602
552
|
let(:criteria) do
|
|
603
|
-
Band.elem_match(records: { name:
|
|
553
|
+
Band.elem_match(records: { name: '101' })
|
|
604
554
|
end
|
|
605
555
|
|
|
606
|
-
it
|
|
556
|
+
it 'returns the matching documents' do
|
|
607
557
|
expect(criteria).to eq([ match ])
|
|
608
558
|
end
|
|
609
559
|
end
|
|
610
560
|
|
|
611
|
-
describe
|
|
612
|
-
|
|
561
|
+
describe '#embedded?' do
|
|
613
562
|
let(:person) do
|
|
614
563
|
Person.new
|
|
615
564
|
end
|
|
616
565
|
|
|
617
|
-
context
|
|
618
|
-
|
|
566
|
+
context 'when the criteria is embedded' do
|
|
619
567
|
let(:criteria) do
|
|
620
|
-
person.addresses.where(street:
|
|
568
|
+
person.addresses.where(street: 'hobrecht')
|
|
621
569
|
end
|
|
622
570
|
|
|
623
|
-
it
|
|
571
|
+
it 'returns true' do
|
|
624
572
|
expect(criteria).to be_embedded
|
|
625
573
|
end
|
|
626
574
|
end
|
|
627
575
|
|
|
628
|
-
context
|
|
629
|
-
|
|
576
|
+
context 'when the criteria is not embedded' do
|
|
630
577
|
let(:criteria) do
|
|
631
578
|
Person.where(active: true)
|
|
632
579
|
end
|
|
633
580
|
|
|
634
|
-
it
|
|
635
|
-
expect(criteria).
|
|
581
|
+
it 'returns false' do
|
|
582
|
+
expect(criteria).not_to be_embedded
|
|
636
583
|
end
|
|
637
584
|
end
|
|
638
585
|
end
|
|
639
586
|
|
|
640
|
-
describe
|
|
641
|
-
|
|
642
|
-
context "when matching documents exist" do
|
|
643
|
-
|
|
587
|
+
describe '#empty?' do
|
|
588
|
+
context 'when matching documents exist' do
|
|
644
589
|
let!(:match) do
|
|
645
|
-
Band.create!(name:
|
|
590
|
+
Band.create!(name: 'Depeche Mode')
|
|
646
591
|
end
|
|
647
592
|
|
|
648
593
|
let(:criteria) do
|
|
649
|
-
Band.where(name:
|
|
594
|
+
Band.where(name: 'Depeche Mode')
|
|
650
595
|
end
|
|
651
596
|
|
|
652
|
-
it
|
|
653
|
-
expect(criteria).
|
|
597
|
+
it 'returns false' do
|
|
598
|
+
expect(criteria).not_to be_empty
|
|
654
599
|
end
|
|
655
600
|
end
|
|
656
601
|
|
|
657
|
-
context
|
|
658
|
-
|
|
602
|
+
context 'when no matching documents exist' do
|
|
659
603
|
let!(:nonmatch) do
|
|
660
|
-
Band.create!(name:
|
|
604
|
+
Band.create!(name: 'New Order')
|
|
661
605
|
end
|
|
662
606
|
|
|
663
607
|
let(:criteria) do
|
|
664
|
-
Band.where(name:
|
|
608
|
+
Band.where(name: 'Depeche Mode')
|
|
665
609
|
end
|
|
666
610
|
|
|
667
|
-
it
|
|
611
|
+
it 'returns true' do
|
|
668
612
|
expect(criteria).to be_empty
|
|
669
613
|
end
|
|
670
614
|
end
|
|
671
615
|
end
|
|
672
616
|
|
|
673
|
-
describe
|
|
674
|
-
|
|
617
|
+
describe '#exists' do
|
|
675
618
|
let!(:match) do
|
|
676
|
-
Band.create!(name:
|
|
619
|
+
Band.create!(name: 'Depeche Mode')
|
|
677
620
|
end
|
|
678
621
|
|
|
679
622
|
let!(:non_match) do
|
|
@@ -684,99 +627,91 @@ describe Mongoid::Criteria do
|
|
|
684
627
|
Band.exists(name: true)
|
|
685
628
|
end
|
|
686
629
|
|
|
687
|
-
it
|
|
630
|
+
it 'returns the matching documents' do
|
|
688
631
|
expect(criteria).to eq([ match ])
|
|
689
632
|
end
|
|
690
633
|
end
|
|
691
634
|
|
|
692
|
-
describe
|
|
693
|
-
|
|
694
|
-
context "when matching documents exist" do
|
|
695
|
-
|
|
635
|
+
describe '#exists?' do
|
|
636
|
+
context 'when matching documents exist' do
|
|
696
637
|
let!(:match) do
|
|
697
|
-
Band.create!(name:
|
|
638
|
+
Band.create!(name: 'Depeche Mode')
|
|
698
639
|
end
|
|
699
640
|
|
|
700
641
|
let(:criteria) do
|
|
701
|
-
Band.where(name:
|
|
642
|
+
Band.where(name: 'Depeche Mode')
|
|
702
643
|
end
|
|
703
644
|
|
|
704
|
-
it
|
|
645
|
+
it 'returns true' do
|
|
705
646
|
expect(criteria.exists?).to be true
|
|
706
647
|
end
|
|
707
648
|
end
|
|
708
649
|
|
|
709
|
-
context
|
|
710
|
-
|
|
650
|
+
context 'when no matching documents exist' do
|
|
711
651
|
let(:criteria) do
|
|
712
|
-
Band.where(name:
|
|
652
|
+
Band.where(name: 'Depeche Mode')
|
|
713
653
|
end
|
|
714
654
|
|
|
715
|
-
it
|
|
655
|
+
it 'returns false' do
|
|
716
656
|
expect(criteria.exists?).to be false
|
|
717
657
|
end
|
|
718
658
|
end
|
|
719
659
|
end
|
|
720
660
|
|
|
721
|
-
describe
|
|
722
|
-
|
|
661
|
+
describe '#explain' do
|
|
723
662
|
let(:criteria) do
|
|
724
|
-
Band.where(name:
|
|
663
|
+
Band.where(name: 'Depeche Mode')
|
|
725
664
|
end
|
|
726
665
|
|
|
727
|
-
it
|
|
728
|
-
expect(criteria.explain).
|
|
666
|
+
it 'returns the criteria explain path' do
|
|
667
|
+
expect(criteria.explain).not_to be_empty
|
|
729
668
|
end
|
|
730
669
|
end
|
|
731
670
|
|
|
732
|
-
describe
|
|
733
|
-
|
|
671
|
+
describe '#extract_id' do
|
|
734
672
|
let(:id) do
|
|
735
673
|
BSON::ObjectId.new
|
|
736
674
|
end
|
|
737
675
|
|
|
738
|
-
context
|
|
739
|
-
|
|
676
|
+
context 'when an id exists' do
|
|
740
677
|
let(:criteria) do
|
|
741
678
|
described_class.new(Band) do |criteria|
|
|
742
679
|
criteria.selector[:id] = id
|
|
743
680
|
end
|
|
744
681
|
end
|
|
745
682
|
|
|
746
|
-
it
|
|
683
|
+
it 'returns the id' do
|
|
747
684
|
expect(criteria.extract_id).to eq(id)
|
|
748
685
|
end
|
|
749
686
|
end
|
|
750
687
|
|
|
751
|
-
context
|
|
752
|
-
|
|
688
|
+
context 'when an _id exists' do
|
|
753
689
|
let(:criteria) do
|
|
754
690
|
described_class.new(Band) do |criteria|
|
|
755
691
|
criteria.selector[:_id] = id
|
|
756
692
|
end
|
|
757
693
|
end
|
|
758
694
|
|
|
759
|
-
it
|
|
695
|
+
it 'returns the _id' do
|
|
760
696
|
expect(criteria.extract_id).to eq(id)
|
|
761
697
|
end
|
|
762
698
|
end
|
|
763
699
|
end
|
|
764
700
|
|
|
765
|
-
describe
|
|
766
|
-
|
|
767
|
-
context "when using the default discriminator key" do
|
|
701
|
+
describe '#field_list' do
|
|
702
|
+
context 'when using the default discriminator key' do
|
|
768
703
|
let(:criteria) do
|
|
769
704
|
Doctor.only(:_id)
|
|
770
705
|
end
|
|
771
706
|
|
|
772
|
-
it
|
|
773
|
-
expect(criteria.field_list).to eq([
|
|
707
|
+
it 'returns the fields with required _id minus type' do
|
|
708
|
+
expect(criteria.field_list).to eq([ '_id' ])
|
|
774
709
|
end
|
|
775
710
|
end
|
|
776
711
|
|
|
777
|
-
context
|
|
712
|
+
context 'when using a custom discriminator key' do
|
|
778
713
|
before do
|
|
779
|
-
Person.discriminator_key =
|
|
714
|
+
Person.discriminator_key = 'dkey'
|
|
780
715
|
end
|
|
781
716
|
|
|
782
717
|
after do
|
|
@@ -787,228 +722,215 @@ describe Mongoid::Criteria do
|
|
|
787
722
|
Doctor.only(:_id, :_type)
|
|
788
723
|
end
|
|
789
724
|
|
|
790
|
-
it
|
|
791
|
-
expect(criteria.field_list).to eq([
|
|
725
|
+
it 'returns the fields with type without dkey' do
|
|
726
|
+
expect(criteria.field_list).to eq(%w[_id _type])
|
|
792
727
|
end
|
|
793
728
|
end
|
|
794
729
|
end
|
|
795
730
|
|
|
796
|
-
describe
|
|
731
|
+
describe '#find' do
|
|
797
732
|
let!(:depeche) do
|
|
798
|
-
Band.create!(name:
|
|
733
|
+
Band.create!(name: 'Depeche Mode')
|
|
799
734
|
end
|
|
800
735
|
|
|
801
736
|
let(:criteria) do
|
|
802
|
-
Band.where(name:
|
|
737
|
+
Band.where(name: 'Depeche Mode')
|
|
803
738
|
end
|
|
804
739
|
|
|
805
|
-
context
|
|
806
|
-
it
|
|
807
|
-
result = criteria.find { |c| c.name ==
|
|
740
|
+
context 'when given a block' do
|
|
741
|
+
it 'behaves as Enumerable' do
|
|
742
|
+
result = criteria.find { |c| c.name == 'Depeche Mode' }
|
|
808
743
|
expect(result).to eq(depeche)
|
|
809
744
|
end
|
|
810
745
|
end
|
|
811
746
|
|
|
812
|
-
context
|
|
813
|
-
it
|
|
814
|
-
result = criteria.find(-> {
|
|
815
|
-
expect(result).to eq(
|
|
747
|
+
context 'when given a Proc and a block' do
|
|
748
|
+
it 'behaves as Enumerable' do
|
|
749
|
+
result = criteria.find(-> { 'default' }) { |c| c.name == 'Not Depeche Mode' }
|
|
750
|
+
expect(result).to eq('default')
|
|
816
751
|
end
|
|
817
752
|
end
|
|
818
753
|
|
|
819
|
-
context
|
|
820
|
-
it
|
|
754
|
+
context 'when given a Proc without a block' do
|
|
755
|
+
it 'raises an error' do
|
|
821
756
|
lambda do
|
|
822
|
-
criteria.find(-> {
|
|
823
|
-
|
|
757
|
+
criteria.find(-> { 'default' })
|
|
758
|
+
# Proc is not serializable to a BSON type
|
|
824
759
|
end.should raise_error(BSON::Error::UnserializableClass)
|
|
825
760
|
end
|
|
826
761
|
end
|
|
827
762
|
|
|
828
|
-
context
|
|
829
|
-
it
|
|
763
|
+
context 'when given an id' do
|
|
764
|
+
it 'behaves as Findable' do
|
|
830
765
|
result = criteria.find(depeche.id)
|
|
831
766
|
expect(result).to eq(depeche)
|
|
832
767
|
end
|
|
833
768
|
end
|
|
834
769
|
end
|
|
835
770
|
|
|
836
|
-
describe
|
|
837
|
-
|
|
771
|
+
describe '#find_one_and_update' do
|
|
838
772
|
let!(:depeche) do
|
|
839
|
-
Band.create!(name:
|
|
773
|
+
Band.create!(name: 'Depeche Mode')
|
|
840
774
|
end
|
|
841
775
|
|
|
842
776
|
let!(:tool) do
|
|
843
|
-
Band.create!(name:
|
|
777
|
+
Band.create!(name: 'Tool')
|
|
844
778
|
end
|
|
845
779
|
|
|
846
|
-
context
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
context "when returning the updated document" do
|
|
851
|
-
|
|
780
|
+
context 'when the selector matches' do
|
|
781
|
+
context 'when the identity map is enabled' do
|
|
782
|
+
context 'when returning the updated document' do
|
|
852
783
|
let(:criteria) do
|
|
853
|
-
Band.where(name:
|
|
784
|
+
Band.where(name: 'Depeche Mode')
|
|
854
785
|
end
|
|
855
786
|
|
|
856
787
|
let(:result) do
|
|
857
|
-
criteria.find_one_and_update({
|
|
788
|
+
criteria.find_one_and_update({ '$inc' => { likes: 1 } }, return_document: :after)
|
|
858
789
|
end
|
|
859
790
|
|
|
860
|
-
it
|
|
791
|
+
it 'returns the first matching document' do
|
|
861
792
|
expect(result).to eq(depeche)
|
|
862
793
|
end
|
|
863
794
|
end
|
|
864
795
|
|
|
865
|
-
context
|
|
866
|
-
|
|
796
|
+
context 'when not returning the updated document' do
|
|
867
797
|
let(:criteria) do
|
|
868
|
-
Band.where(name:
|
|
798
|
+
Band.where(name: 'Depeche Mode')
|
|
869
799
|
end
|
|
870
800
|
|
|
871
801
|
let!(:result) do
|
|
872
|
-
criteria.find_one_and_update(
|
|
802
|
+
criteria.find_one_and_update('$inc' => { likes: 1 })
|
|
873
803
|
end
|
|
874
804
|
|
|
875
805
|
before do
|
|
876
806
|
depeche.reload
|
|
877
807
|
end
|
|
878
808
|
|
|
879
|
-
it
|
|
809
|
+
it 'returns the first matching document' do
|
|
880
810
|
expect(result).to eq(depeche)
|
|
881
811
|
end
|
|
882
812
|
end
|
|
883
813
|
end
|
|
884
814
|
|
|
885
|
-
context
|
|
886
|
-
|
|
815
|
+
context 'when not providing options' do
|
|
887
816
|
let(:criteria) do
|
|
888
|
-
Band.where(name:
|
|
817
|
+
Band.where(name: 'Depeche Mode')
|
|
889
818
|
end
|
|
890
819
|
|
|
891
820
|
let!(:result) do
|
|
892
|
-
criteria.find_one_and_update(
|
|
821
|
+
criteria.find_one_and_update('$inc' => { likes: 1 })
|
|
893
822
|
end
|
|
894
823
|
|
|
895
|
-
it
|
|
824
|
+
it 'returns the first matching document' do
|
|
896
825
|
expect(result).to eq(depeche)
|
|
897
826
|
end
|
|
898
827
|
|
|
899
|
-
it
|
|
828
|
+
it 'updates the document in the database' do
|
|
900
829
|
expect(depeche.reload.likes).to eq(1)
|
|
901
830
|
end
|
|
902
831
|
end
|
|
903
832
|
|
|
904
|
-
context
|
|
905
|
-
|
|
833
|
+
context 'when sorting' do
|
|
906
834
|
let(:criteria) do
|
|
907
835
|
Band.desc(:name)
|
|
908
836
|
end
|
|
909
837
|
|
|
910
838
|
let!(:result) do
|
|
911
|
-
criteria.find_one_and_update(
|
|
839
|
+
criteria.find_one_and_update('$inc' => { likes: 1 })
|
|
912
840
|
end
|
|
913
841
|
|
|
914
|
-
it
|
|
842
|
+
it 'returns the first matching document' do
|
|
915
843
|
expect(result).to eq(tool)
|
|
916
844
|
end
|
|
917
845
|
|
|
918
|
-
it
|
|
846
|
+
it 'updates the document in the database' do
|
|
919
847
|
expect(tool.reload.likes).to eq(1)
|
|
920
848
|
end
|
|
921
849
|
end
|
|
922
850
|
|
|
923
|
-
context
|
|
924
|
-
|
|
851
|
+
context 'when limiting fields' do
|
|
925
852
|
let(:criteria) do
|
|
926
853
|
Band.only(:_id)
|
|
927
854
|
end
|
|
928
855
|
|
|
929
856
|
let!(:result) do
|
|
930
|
-
criteria.find_one_and_update(
|
|
857
|
+
criteria.find_one_and_update('$inc' => { likes: 1 })
|
|
931
858
|
end
|
|
932
859
|
|
|
933
|
-
it
|
|
860
|
+
it 'returns the first matching document' do
|
|
934
861
|
expect(result).to eq(depeche)
|
|
935
862
|
end
|
|
936
863
|
|
|
937
|
-
it
|
|
864
|
+
it 'limits the returned fields' do
|
|
938
865
|
expect(result.name).to be_nil
|
|
939
866
|
end
|
|
940
867
|
|
|
941
|
-
it
|
|
868
|
+
it 'updates the document in the database' do
|
|
942
869
|
expect(depeche.reload.likes).to eq(1)
|
|
943
870
|
end
|
|
944
871
|
end
|
|
945
872
|
|
|
946
|
-
context
|
|
947
|
-
|
|
873
|
+
context 'when returning new' do
|
|
948
874
|
let(:criteria) do
|
|
949
|
-
Band.where(name:
|
|
875
|
+
Band.where(name: 'Depeche Mode')
|
|
950
876
|
end
|
|
951
877
|
|
|
952
878
|
let!(:result) do
|
|
953
|
-
criteria.find_one_and_update({
|
|
879
|
+
criteria.find_one_and_update({ '$inc' => { likes: 1 } }, return_document: :after)
|
|
954
880
|
end
|
|
955
881
|
|
|
956
|
-
it
|
|
882
|
+
it 'returns the first matching document' do
|
|
957
883
|
expect(result).to eq(depeche)
|
|
958
884
|
end
|
|
959
885
|
|
|
960
|
-
it
|
|
886
|
+
it 'returns the updated document' do
|
|
961
887
|
expect(result.likes).to eq(1)
|
|
962
888
|
end
|
|
963
889
|
end
|
|
964
890
|
|
|
965
|
-
context
|
|
966
|
-
|
|
891
|
+
context 'when removing' do
|
|
967
892
|
let(:criteria) do
|
|
968
|
-
Band.where(name:
|
|
893
|
+
Band.where(name: 'Depeche Mode')
|
|
969
894
|
end
|
|
970
895
|
|
|
971
896
|
let!(:result) do
|
|
972
897
|
criteria.find_one_and_delete
|
|
973
898
|
end
|
|
974
899
|
|
|
975
|
-
it
|
|
900
|
+
it 'returns the first matching document' do
|
|
976
901
|
expect(result).to eq(depeche)
|
|
977
902
|
end
|
|
978
903
|
|
|
979
|
-
it
|
|
980
|
-
expect
|
|
904
|
+
it 'deletes the document from the database' do
|
|
905
|
+
expect do
|
|
981
906
|
depeche.reload
|
|
982
|
-
|
|
907
|
+
end.to raise_error(Mongoid::Errors::DocumentNotFound, /Document\(s\) not found for class Band with id\(s\)/)
|
|
983
908
|
end
|
|
984
909
|
end
|
|
985
910
|
end
|
|
986
911
|
|
|
987
|
-
context
|
|
988
|
-
|
|
912
|
+
context 'when the selector does not match' do
|
|
989
913
|
let(:criteria) do
|
|
990
|
-
Band.where(name:
|
|
914
|
+
Band.where(name: 'Placebo')
|
|
991
915
|
end
|
|
992
916
|
|
|
993
917
|
let(:result) do
|
|
994
|
-
criteria.find_one_and_update(
|
|
918
|
+
criteria.find_one_and_update('$inc' => { likes: 1 })
|
|
995
919
|
end
|
|
996
920
|
|
|
997
|
-
context
|
|
998
|
-
|
|
921
|
+
context 'without upsert' do
|
|
999
922
|
let(:result) do
|
|
1000
|
-
criteria.find_one_and_update(
|
|
923
|
+
criteria.find_one_and_update('$inc' => { likes: 1 })
|
|
1001
924
|
end
|
|
1002
925
|
|
|
1003
|
-
it
|
|
926
|
+
it 'returns nil' do
|
|
1004
927
|
expect(result).to be_nil
|
|
1005
928
|
end
|
|
1006
929
|
end
|
|
1007
930
|
end
|
|
1008
931
|
end
|
|
1009
932
|
|
|
1010
|
-
describe
|
|
1011
|
-
|
|
933
|
+
describe '#freeze' do
|
|
1012
934
|
let(:criteria) do
|
|
1013
935
|
Band.all
|
|
1014
936
|
end
|
|
@@ -1017,21 +939,20 @@ describe Mongoid::Criteria do
|
|
|
1017
939
|
criteria.freeze
|
|
1018
940
|
end
|
|
1019
941
|
|
|
1020
|
-
it
|
|
942
|
+
it 'freezes the criteria' do
|
|
1021
943
|
expect(criteria).to be_frozen
|
|
1022
944
|
end
|
|
1023
945
|
|
|
1024
|
-
it
|
|
946
|
+
it 'initializes inclusions' do
|
|
1025
947
|
expect(criteria.inclusions).to be_empty
|
|
1026
948
|
end
|
|
1027
949
|
|
|
1028
|
-
it
|
|
1029
|
-
expect(criteria.context).
|
|
950
|
+
it 'initializes the context' do
|
|
951
|
+
expect(criteria.context).not_to be_nil
|
|
1030
952
|
end
|
|
1031
953
|
end
|
|
1032
954
|
|
|
1033
|
-
describe
|
|
1034
|
-
|
|
955
|
+
describe '#eq' do
|
|
1035
956
|
let!(:match) do
|
|
1036
957
|
Band.create(member_count: 5)
|
|
1037
958
|
end
|
|
@@ -1044,13 +965,12 @@ describe Mongoid::Criteria do
|
|
|
1044
965
|
Band.eq(member_count: 5)
|
|
1045
966
|
end
|
|
1046
967
|
|
|
1047
|
-
it
|
|
968
|
+
it 'returns the matching documents' do
|
|
1048
969
|
expect(criteria).to eq([ match ])
|
|
1049
970
|
end
|
|
1050
971
|
end
|
|
1051
972
|
|
|
1052
|
-
describe
|
|
1053
|
-
|
|
973
|
+
describe '#gt' do
|
|
1054
974
|
let!(:match) do
|
|
1055
975
|
Band.create!(member_count: 5)
|
|
1056
976
|
end
|
|
@@ -1063,13 +983,12 @@ describe Mongoid::Criteria do
|
|
|
1063
983
|
Band.gt(member_count: 4)
|
|
1064
984
|
end
|
|
1065
985
|
|
|
1066
|
-
it
|
|
986
|
+
it 'returns the matching documents' do
|
|
1067
987
|
expect(criteria).to eq([ match ])
|
|
1068
988
|
end
|
|
1069
989
|
end
|
|
1070
990
|
|
|
1071
|
-
describe
|
|
1072
|
-
|
|
991
|
+
describe '#gte' do
|
|
1073
992
|
let!(:match) do
|
|
1074
993
|
Band.create!(member_count: 5)
|
|
1075
994
|
end
|
|
@@ -1082,36 +1001,32 @@ describe Mongoid::Criteria do
|
|
|
1082
1001
|
Band.gte(member_count: 5)
|
|
1083
1002
|
end
|
|
1084
1003
|
|
|
1085
|
-
it
|
|
1004
|
+
it 'returns the matching documents' do
|
|
1086
1005
|
expect(criteria).to eq([ match ])
|
|
1087
1006
|
end
|
|
1088
1007
|
end
|
|
1089
1008
|
|
|
1090
|
-
[
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
context "when querying on a normal field" do
|
|
1095
|
-
|
|
1009
|
+
%i[in any_in].each do |method|
|
|
1010
|
+
describe "##{method}" do
|
|
1011
|
+
context 'when querying on a normal field' do
|
|
1096
1012
|
let!(:match) do
|
|
1097
|
-
Band.create!(genres: [
|
|
1013
|
+
Band.create!(genres: %w[electro dub])
|
|
1098
1014
|
end
|
|
1099
1015
|
|
|
1100
1016
|
let!(:non_match) do
|
|
1101
|
-
Band.create!(genres: [
|
|
1017
|
+
Band.create!(genres: [ 'house' ])
|
|
1102
1018
|
end
|
|
1103
1019
|
|
|
1104
1020
|
let(:criteria) do
|
|
1105
|
-
Band.send(method, genres: [
|
|
1021
|
+
Band.send(method, genres: [ 'dub' ])
|
|
1106
1022
|
end
|
|
1107
1023
|
|
|
1108
|
-
it
|
|
1024
|
+
it 'returns the matching documents' do
|
|
1109
1025
|
expect(criteria).to eq([ match ])
|
|
1110
1026
|
end
|
|
1111
1027
|
end
|
|
1112
1028
|
|
|
1113
|
-
context
|
|
1114
|
-
|
|
1029
|
+
context 'when querying on a foreign key' do
|
|
1115
1030
|
let(:id) do
|
|
1116
1031
|
BSON::ObjectId.new
|
|
1117
1032
|
end
|
|
@@ -1120,43 +1035,38 @@ describe Mongoid::Criteria do
|
|
|
1120
1035
|
Person.create!(preference_ids: [ id ])
|
|
1121
1036
|
end
|
|
1122
1037
|
|
|
1123
|
-
context
|
|
1124
|
-
|
|
1038
|
+
context 'when providing valid ids' do
|
|
1125
1039
|
let(:criteria) do
|
|
1126
1040
|
Person.send(method, preference_ids: [ id ])
|
|
1127
1041
|
end
|
|
1128
1042
|
|
|
1129
|
-
it
|
|
1043
|
+
it 'returns the matching documents' do
|
|
1130
1044
|
expect(criteria).to eq([ match_one ])
|
|
1131
1045
|
end
|
|
1132
1046
|
end
|
|
1133
1047
|
|
|
1134
|
-
context
|
|
1135
|
-
|
|
1048
|
+
context 'when providing empty strings' do
|
|
1136
1049
|
let(:criteria) do
|
|
1137
|
-
Person.send(method, preference_ids: [ id,
|
|
1050
|
+
Person.send(method, preference_ids: [ id, '' ])
|
|
1138
1051
|
end
|
|
1139
1052
|
|
|
1140
|
-
it
|
|
1053
|
+
it 'returns the matching documents' do
|
|
1141
1054
|
expect(criteria).to eq([ match_one ])
|
|
1142
1055
|
end
|
|
1143
1056
|
end
|
|
1144
1057
|
|
|
1145
|
-
context
|
|
1146
|
-
|
|
1147
|
-
context "when the relation is a many to many" do
|
|
1148
|
-
|
|
1058
|
+
context 'when providing nils' do
|
|
1059
|
+
context 'when the relation is a many to many' do
|
|
1149
1060
|
let(:criteria) do
|
|
1150
1061
|
Person.send(method, preference_ids: [ id, nil ])
|
|
1151
1062
|
end
|
|
1152
1063
|
|
|
1153
|
-
it
|
|
1064
|
+
it 'returns the matching documents' do
|
|
1154
1065
|
expect(criteria).to eq([ match_one ])
|
|
1155
1066
|
end
|
|
1156
1067
|
end
|
|
1157
1068
|
|
|
1158
|
-
context
|
|
1159
|
-
|
|
1069
|
+
context 'when the relation is a one to one' do
|
|
1160
1070
|
let!(:game) do
|
|
1161
1071
|
Game.create!
|
|
1162
1072
|
end
|
|
@@ -1165,7 +1075,7 @@ describe Mongoid::Criteria do
|
|
|
1165
1075
|
Game.send(method, person_id: [ nil ])
|
|
1166
1076
|
end
|
|
1167
1077
|
|
|
1168
|
-
it
|
|
1078
|
+
it 'returns the matching documents' do
|
|
1169
1079
|
expect(criteria).to eq([ game ])
|
|
1170
1080
|
end
|
|
1171
1081
|
end
|
|
@@ -1174,27 +1084,25 @@ describe Mongoid::Criteria do
|
|
|
1174
1084
|
end
|
|
1175
1085
|
end
|
|
1176
1086
|
|
|
1177
|
-
describe
|
|
1178
|
-
|
|
1087
|
+
describe '#initialize' do
|
|
1179
1088
|
let(:criteria) do
|
|
1180
1089
|
described_class.new(Band)
|
|
1181
1090
|
end
|
|
1182
1091
|
|
|
1183
|
-
it
|
|
1092
|
+
it 'sets the class' do
|
|
1184
1093
|
expect(criteria.klass).to eq(Band)
|
|
1185
1094
|
end
|
|
1186
1095
|
|
|
1187
|
-
it
|
|
1096
|
+
it 'sets the aliased fields' do
|
|
1188
1097
|
expect(criteria.aliased_fields).to eq(Band.aliased_fields)
|
|
1189
1098
|
end
|
|
1190
1099
|
|
|
1191
|
-
it
|
|
1100
|
+
it 'sets the serializers' do
|
|
1192
1101
|
expect(criteria.serializers).to eq(Band.fields)
|
|
1193
1102
|
end
|
|
1194
1103
|
end
|
|
1195
1104
|
|
|
1196
|
-
describe
|
|
1197
|
-
|
|
1105
|
+
describe '#lt' do
|
|
1198
1106
|
let!(:match) do
|
|
1199
1107
|
Band.create!(member_count: 1)
|
|
1200
1108
|
end
|
|
@@ -1207,13 +1115,12 @@ describe Mongoid::Criteria do
|
|
|
1207
1115
|
Band.lt(member_count: 4)
|
|
1208
1116
|
end
|
|
1209
1117
|
|
|
1210
|
-
it
|
|
1118
|
+
it 'returns the matching documents' do
|
|
1211
1119
|
expect(criteria).to eq([ match ])
|
|
1212
1120
|
end
|
|
1213
1121
|
end
|
|
1214
1122
|
|
|
1215
|
-
describe
|
|
1216
|
-
|
|
1123
|
+
describe '#lte' do
|
|
1217
1124
|
let!(:match) do
|
|
1218
1125
|
Band.create!(member_count: 4)
|
|
1219
1126
|
end
|
|
@@ -1226,22 +1133,21 @@ describe Mongoid::Criteria do
|
|
|
1226
1133
|
Band.lte(member_count: 4)
|
|
1227
1134
|
end
|
|
1228
1135
|
|
|
1229
|
-
it
|
|
1136
|
+
it 'returns the matching documents' do
|
|
1230
1137
|
expect(criteria).to eq([ match ])
|
|
1231
1138
|
end
|
|
1232
1139
|
end
|
|
1233
1140
|
|
|
1234
|
-
describe
|
|
1235
|
-
|
|
1141
|
+
describe '#map_reduce' do
|
|
1236
1142
|
let(:map) do
|
|
1237
|
-
%
|
|
1143
|
+
%{
|
|
1238
1144
|
function() {
|
|
1239
1145
|
emit(this.name, { likes: this.likes });
|
|
1240
1146
|
}}
|
|
1241
1147
|
end
|
|
1242
1148
|
|
|
1243
1149
|
let(:reduce) do
|
|
1244
|
-
%
|
|
1150
|
+
%{
|
|
1245
1151
|
function(key, values) {
|
|
1246
1152
|
var result = { likes: 0 };
|
|
1247
1153
|
values.forEach(function(value) {
|
|
@@ -1252,72 +1158,64 @@ describe Mongoid::Criteria do
|
|
|
1252
1158
|
end
|
|
1253
1159
|
|
|
1254
1160
|
let!(:depeche_mode) do
|
|
1255
|
-
Band.create!(name:
|
|
1161
|
+
Band.create!(name: 'Depeche Mode', likes: 200)
|
|
1256
1162
|
end
|
|
1257
1163
|
|
|
1258
1164
|
let!(:tool) do
|
|
1259
|
-
Band.create!(name:
|
|
1165
|
+
Band.create!(name: 'Tool', likes: 100)
|
|
1260
1166
|
end
|
|
1261
1167
|
|
|
1262
|
-
context
|
|
1263
|
-
|
|
1168
|
+
context 'when no timeout options are provided' do
|
|
1264
1169
|
let(:map_reduce) do
|
|
1265
1170
|
Band.limit(2).map_reduce(map, reduce).out(inline: 1)
|
|
1266
1171
|
end
|
|
1267
1172
|
|
|
1268
|
-
it
|
|
1173
|
+
it 'returns the map/reduce results' do
|
|
1269
1174
|
expect(map_reduce.sort_by { |doc| doc['_id'] }).to eq([
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1175
|
+
{ '_id' => 'Depeche Mode', 'value' => { 'likes' => 200 } },
|
|
1176
|
+
{ '_id' => 'Tool', 'value' => { 'likes' => 100 } }
|
|
1177
|
+
])
|
|
1273
1178
|
end
|
|
1274
1179
|
end
|
|
1275
1180
|
end
|
|
1276
1181
|
|
|
1277
|
-
describe
|
|
1278
|
-
|
|
1279
|
-
context "when provided a single field" do
|
|
1280
|
-
|
|
1182
|
+
describe '#max' do
|
|
1183
|
+
context 'when provided a single field' do
|
|
1281
1184
|
let!(:depeche) do
|
|
1282
|
-
Band.create!(name:
|
|
1185
|
+
Band.create!(name: 'Depeche Mode', likes: 1000)
|
|
1283
1186
|
end
|
|
1284
1187
|
|
|
1285
1188
|
let!(:tool) do
|
|
1286
|
-
Band.create!(name:
|
|
1189
|
+
Band.create!(name: 'Tool', likes: 500)
|
|
1287
1190
|
end
|
|
1288
1191
|
|
|
1289
1192
|
let(:criteria) do
|
|
1290
1193
|
Band.all
|
|
1291
1194
|
end
|
|
1292
1195
|
|
|
1293
|
-
context
|
|
1294
|
-
|
|
1196
|
+
context 'when provided a symbol' do
|
|
1295
1197
|
let(:max) do
|
|
1296
1198
|
criteria.max(:likes)
|
|
1297
1199
|
end
|
|
1298
1200
|
|
|
1299
|
-
it
|
|
1201
|
+
it 'returns the max of the provided field' do
|
|
1300
1202
|
expect(max).to eq(1000)
|
|
1301
1203
|
end
|
|
1302
1204
|
end
|
|
1303
1205
|
|
|
1304
|
-
context
|
|
1305
|
-
|
|
1206
|
+
context 'when provided a block' do
|
|
1306
1207
|
let(:max) do
|
|
1307
|
-
criteria.
|
|
1308
|
-
a.likes <=> b.likes
|
|
1309
|
-
end
|
|
1208
|
+
criteria.max_by(&:likes)
|
|
1310
1209
|
end
|
|
1311
1210
|
|
|
1312
|
-
it
|
|
1211
|
+
it 'returns the document with the max value for the field' do
|
|
1313
1212
|
expect(max).to eq(depeche)
|
|
1314
1213
|
end
|
|
1315
1214
|
end
|
|
1316
1215
|
end
|
|
1317
1216
|
end
|
|
1318
1217
|
|
|
1319
|
-
describe
|
|
1320
|
-
|
|
1218
|
+
describe '#max_distance' do
|
|
1321
1219
|
before do
|
|
1322
1220
|
Bar.create_indexes
|
|
1323
1221
|
end
|
|
@@ -1334,23 +1232,21 @@ describe Mongoid::Criteria do
|
|
|
1334
1232
|
Bar.near(location: [ 52, 13 ]).max_distance(location: 5)
|
|
1335
1233
|
end
|
|
1336
1234
|
|
|
1337
|
-
it
|
|
1235
|
+
it 'returns the matching documents' do
|
|
1338
1236
|
expect(criteria).to eq([ match ])
|
|
1339
1237
|
end
|
|
1340
1238
|
end
|
|
1341
1239
|
|
|
1342
|
-
describe
|
|
1343
|
-
|
|
1240
|
+
describe '#merge' do
|
|
1344
1241
|
let(:band) do
|
|
1345
1242
|
Band.new
|
|
1346
1243
|
end
|
|
1347
1244
|
|
|
1348
1245
|
let(:criteria) do
|
|
1349
|
-
Band.scoped.where(name:
|
|
1246
|
+
Band.scoped.where(name: 'Depeche Mode').asc(:name)
|
|
1350
1247
|
end
|
|
1351
1248
|
|
|
1352
|
-
context
|
|
1353
|
-
|
|
1249
|
+
context 'when merging with another criteria' do
|
|
1354
1250
|
let(:mergeable) do
|
|
1355
1251
|
Band.includes(:records).tap do |crit|
|
|
1356
1252
|
crit.documents = [ band ]
|
|
@@ -1358,83 +1254,83 @@ describe Mongoid::Criteria do
|
|
|
1358
1254
|
end
|
|
1359
1255
|
|
|
1360
1256
|
let(:association) do
|
|
1361
|
-
Band.relations[
|
|
1257
|
+
Band.relations['records']
|
|
1362
1258
|
end
|
|
1363
1259
|
|
|
1364
1260
|
let(:merged) do
|
|
1365
1261
|
criteria.merge(mergeable)
|
|
1366
1262
|
end
|
|
1367
1263
|
|
|
1368
|
-
it
|
|
1369
|
-
expect(merged.selector).to eq({
|
|
1264
|
+
it 'merges the selector' do
|
|
1265
|
+
expect(merged.selector).to eq({ 'name' => 'Depeche Mode' })
|
|
1370
1266
|
end
|
|
1371
1267
|
|
|
1372
|
-
it
|
|
1373
|
-
expect(merged.options).to eq({ sort: {
|
|
1268
|
+
it 'merges the options' do
|
|
1269
|
+
expect(merged.options).to eq({ sort: { 'name' => 1 } })
|
|
1374
1270
|
end
|
|
1375
1271
|
|
|
1376
|
-
it
|
|
1272
|
+
it 'merges the documents' do
|
|
1377
1273
|
expect(merged.documents).to eq([ band ])
|
|
1378
1274
|
end
|
|
1379
1275
|
|
|
1380
|
-
it
|
|
1276
|
+
it 'merges the scoping options' do
|
|
1381
1277
|
expect(merged.scoping_options).to eq([ nil, nil ])
|
|
1382
1278
|
end
|
|
1383
1279
|
|
|
1384
|
-
it
|
|
1280
|
+
it 'merges the inclusions' do
|
|
1385
1281
|
expect(merged.inclusions).to eq([ association ])
|
|
1386
1282
|
end
|
|
1387
1283
|
|
|
1388
|
-
it
|
|
1389
|
-
expect(merged).
|
|
1284
|
+
it 'returns a new criteria' do
|
|
1285
|
+
expect(merged).not_to equal(criteria)
|
|
1390
1286
|
end
|
|
1391
1287
|
end
|
|
1392
1288
|
|
|
1393
|
-
context
|
|
1394
|
-
|
|
1289
|
+
context 'when merging with a hash' do
|
|
1395
1290
|
let(:mergeable) do
|
|
1396
1291
|
{ klass: Band, includes: [ :records ] }
|
|
1397
1292
|
end
|
|
1398
1293
|
|
|
1399
1294
|
let(:association) do
|
|
1400
|
-
Band.relations[
|
|
1295
|
+
Band.relations['records']
|
|
1401
1296
|
end
|
|
1402
1297
|
|
|
1403
1298
|
let(:merged) do
|
|
1404
1299
|
criteria.merge(mergeable)
|
|
1405
1300
|
end
|
|
1406
1301
|
|
|
1407
|
-
it
|
|
1408
|
-
expect(merged.selector).to eq({
|
|
1302
|
+
it 'merges the selector' do
|
|
1303
|
+
expect(merged.selector).to eq({ 'name' => 'Depeche Mode' })
|
|
1409
1304
|
end
|
|
1410
1305
|
|
|
1411
|
-
it
|
|
1412
|
-
expect(merged.options).to eq({ sort: {
|
|
1306
|
+
it 'merges the options' do
|
|
1307
|
+
expect(merged.options).to eq({ sort: { 'name' => 1 } })
|
|
1413
1308
|
end
|
|
1414
1309
|
|
|
1415
|
-
it
|
|
1310
|
+
it 'merges the scoping options' do
|
|
1416
1311
|
expect(merged.scoping_options).to eq([ nil, nil ])
|
|
1417
1312
|
end
|
|
1418
1313
|
|
|
1419
|
-
it
|
|
1314
|
+
it 'merges the inclusions' do
|
|
1420
1315
|
expect(merged.inclusions).to eq([ association ])
|
|
1421
1316
|
end
|
|
1422
1317
|
|
|
1423
|
-
it
|
|
1424
|
-
expect(merged).
|
|
1318
|
+
it 'returns a new criteria' do
|
|
1319
|
+
expect(merged).not_to equal(criteria)
|
|
1425
1320
|
end
|
|
1426
1321
|
end
|
|
1427
1322
|
end
|
|
1428
1323
|
|
|
1429
1324
|
describe '#merge!' do
|
|
1325
|
+
subject(:merged) { criteria.merge!(other) }
|
|
1326
|
+
|
|
1430
1327
|
let(:band) { Band.new }
|
|
1431
1328
|
let(:criteria) { Band.scoped.where(name: 'Depeche Mode').asc(:name) }
|
|
1432
1329
|
let(:association) { Band.relations['records'] }
|
|
1433
|
-
subject(:merged) { criteria.merge!(other) }
|
|
1434
1330
|
|
|
1435
1331
|
context 'when merging a Criteria' do
|
|
1436
1332
|
let(:other) do
|
|
1437
|
-
{ klass: Band, includes: [:records] }
|
|
1333
|
+
{ klass: Band, includes: [ :records ] }
|
|
1438
1334
|
end
|
|
1439
1335
|
|
|
1440
1336
|
it 'merges the selector' do
|
|
@@ -1442,7 +1338,7 @@ describe Mongoid::Criteria do
|
|
|
1442
1338
|
end
|
|
1443
1339
|
|
|
1444
1340
|
it 'merges the options' do
|
|
1445
|
-
expect(merged.options).to eq({ sort: { 'name' => 1 }})
|
|
1341
|
+
expect(merged.options).to eq({ sort: { 'name' => 1 } })
|
|
1446
1342
|
end
|
|
1447
1343
|
|
|
1448
1344
|
it 'merges the scoping options' do
|
|
@@ -1470,7 +1366,7 @@ describe Mongoid::Criteria do
|
|
|
1470
1366
|
end
|
|
1471
1367
|
|
|
1472
1368
|
it 'merges the options' do
|
|
1473
|
-
expect(merged.options).to eq({ sort: { 'name' => 1 }})
|
|
1369
|
+
expect(merged.options).to eq({ sort: { 'name' => 1 } })
|
|
1474
1370
|
end
|
|
1475
1371
|
|
|
1476
1372
|
it 'merges the documents' do
|
|
@@ -1491,50 +1387,43 @@ describe Mongoid::Criteria do
|
|
|
1491
1387
|
end
|
|
1492
1388
|
end
|
|
1493
1389
|
|
|
1494
|
-
describe
|
|
1495
|
-
|
|
1496
|
-
context "when provided a single field" do
|
|
1497
|
-
|
|
1390
|
+
describe '#min' do
|
|
1391
|
+
context 'when provided a single field' do
|
|
1498
1392
|
let!(:depeche) do
|
|
1499
|
-
Band.create!(name:
|
|
1393
|
+
Band.create!(name: 'Depeche Mode', likes: 1000)
|
|
1500
1394
|
end
|
|
1501
1395
|
|
|
1502
1396
|
let!(:tool) do
|
|
1503
|
-
Band.create!(name:
|
|
1397
|
+
Band.create!(name: 'Tool', likes: 500)
|
|
1504
1398
|
end
|
|
1505
1399
|
|
|
1506
1400
|
let(:criteria) do
|
|
1507
1401
|
Band.all
|
|
1508
1402
|
end
|
|
1509
1403
|
|
|
1510
|
-
context
|
|
1511
|
-
|
|
1404
|
+
context 'when provided a symbol' do
|
|
1512
1405
|
let(:min) do
|
|
1513
1406
|
criteria.min(:likes)
|
|
1514
1407
|
end
|
|
1515
1408
|
|
|
1516
|
-
it
|
|
1409
|
+
it 'returns the min of the provided field' do
|
|
1517
1410
|
expect(min).to eq(500)
|
|
1518
1411
|
end
|
|
1519
1412
|
end
|
|
1520
1413
|
|
|
1521
|
-
context
|
|
1522
|
-
|
|
1414
|
+
context 'when provided a block' do
|
|
1523
1415
|
let(:min) do
|
|
1524
|
-
criteria.
|
|
1525
|
-
a.likes <=> b.likes
|
|
1526
|
-
end
|
|
1416
|
+
criteria.min_by(&:likes)
|
|
1527
1417
|
end
|
|
1528
1418
|
|
|
1529
|
-
it
|
|
1419
|
+
it 'returns the document with the min value for the field' do
|
|
1530
1420
|
expect(min).to eq(tool)
|
|
1531
1421
|
end
|
|
1532
1422
|
end
|
|
1533
1423
|
end
|
|
1534
1424
|
end
|
|
1535
1425
|
|
|
1536
|
-
describe
|
|
1537
|
-
|
|
1426
|
+
describe '#mod' do
|
|
1538
1427
|
let!(:match) do
|
|
1539
1428
|
Band.create!(member_count: 5)
|
|
1540
1429
|
end
|
|
@@ -1547,32 +1436,30 @@ describe Mongoid::Criteria do
|
|
|
1547
1436
|
Band.mod(member_count: [ 4, 1 ])
|
|
1548
1437
|
end
|
|
1549
1438
|
|
|
1550
|
-
it
|
|
1439
|
+
it 'returns the matching documents' do
|
|
1551
1440
|
expect(criteria).to eq([ match ])
|
|
1552
1441
|
end
|
|
1553
1442
|
end
|
|
1554
1443
|
|
|
1555
|
-
describe
|
|
1556
|
-
|
|
1444
|
+
describe '#ne' do
|
|
1557
1445
|
let!(:match) do
|
|
1558
|
-
Band.create!(name:
|
|
1446
|
+
Band.create!(name: 'Depeche Mode')
|
|
1559
1447
|
end
|
|
1560
1448
|
|
|
1561
1449
|
let!(:non_match) do
|
|
1562
|
-
Band.create!(name:
|
|
1450
|
+
Band.create!(name: 'Tool')
|
|
1563
1451
|
end
|
|
1564
1452
|
|
|
1565
1453
|
let(:criteria) do
|
|
1566
|
-
Band.ne(name:
|
|
1454
|
+
Band.ne(name: 'Tool')
|
|
1567
1455
|
end
|
|
1568
1456
|
|
|
1569
|
-
it
|
|
1457
|
+
it 'returns the matching documents' do
|
|
1570
1458
|
expect(criteria).to eq([ match ])
|
|
1571
1459
|
end
|
|
1572
1460
|
end
|
|
1573
1461
|
|
|
1574
|
-
describe
|
|
1575
|
-
|
|
1462
|
+
describe '#near' do
|
|
1576
1463
|
before do
|
|
1577
1464
|
Bar.create_indexes
|
|
1578
1465
|
end
|
|
@@ -1585,13 +1472,12 @@ describe Mongoid::Criteria do
|
|
|
1585
1472
|
Bar.near(location: [ 52, 13 ])
|
|
1586
1473
|
end
|
|
1587
1474
|
|
|
1588
|
-
it
|
|
1475
|
+
it 'returns the matching documents' do
|
|
1589
1476
|
expect(criteria).to eq([ match ])
|
|
1590
1477
|
end
|
|
1591
1478
|
end
|
|
1592
1479
|
|
|
1593
|
-
describe
|
|
1594
|
-
|
|
1480
|
+
describe '#near_sphere' do
|
|
1595
1481
|
before do
|
|
1596
1482
|
Bar.create_indexes
|
|
1597
1483
|
end
|
|
@@ -1604,129 +1490,118 @@ describe Mongoid::Criteria do
|
|
|
1604
1490
|
Bar.near_sphere(location: [ 52, 13 ])
|
|
1605
1491
|
end
|
|
1606
1492
|
|
|
1607
|
-
it
|
|
1493
|
+
it 'returns the matching documents' do
|
|
1608
1494
|
expect(criteria).to eq([ match ])
|
|
1609
1495
|
end
|
|
1610
1496
|
end
|
|
1611
1497
|
|
|
1612
|
-
describe
|
|
1613
|
-
|
|
1498
|
+
describe '#nin' do
|
|
1614
1499
|
let!(:match) do
|
|
1615
|
-
Band.create!(name:
|
|
1500
|
+
Band.create!(name: 'Depeche Mode')
|
|
1616
1501
|
end
|
|
1617
1502
|
|
|
1618
1503
|
let!(:non_match) do
|
|
1619
|
-
Band.create!(name:
|
|
1504
|
+
Band.create!(name: 'Tool')
|
|
1620
1505
|
end
|
|
1621
1506
|
|
|
1622
1507
|
let(:criteria) do
|
|
1623
|
-
Band.nin(name: [
|
|
1508
|
+
Band.nin(name: [ 'Tool' ])
|
|
1624
1509
|
end
|
|
1625
1510
|
|
|
1626
|
-
it
|
|
1511
|
+
it 'returns the matching documents' do
|
|
1627
1512
|
expect(criteria).to eq([ match ])
|
|
1628
1513
|
end
|
|
1629
1514
|
end
|
|
1630
1515
|
|
|
1631
|
-
describe
|
|
1632
|
-
|
|
1516
|
+
describe '#nor' do
|
|
1633
1517
|
let!(:match) do
|
|
1634
|
-
Band.create!(name:
|
|
1518
|
+
Band.create!(name: 'Depeche Mode')
|
|
1635
1519
|
end
|
|
1636
1520
|
|
|
1637
1521
|
let!(:non_match) do
|
|
1638
|
-
Band.create!(name:
|
|
1522
|
+
Band.create!(name: 'Tool')
|
|
1639
1523
|
end
|
|
1640
1524
|
|
|
1641
1525
|
let(:criteria) do
|
|
1642
|
-
Band.nor({ name:
|
|
1526
|
+
Band.nor({ name: 'Tool' }, { name: 'New Order' })
|
|
1643
1527
|
end
|
|
1644
1528
|
|
|
1645
|
-
it
|
|
1529
|
+
it 'returns the matching documents' do
|
|
1646
1530
|
expect(criteria).to eq([ match ])
|
|
1647
1531
|
end
|
|
1648
1532
|
end
|
|
1649
1533
|
|
|
1650
|
-
[
|
|
1651
|
-
|
|
1652
|
-
describe "\##{method}" do
|
|
1653
|
-
|
|
1534
|
+
%i[or any_of].each do |method|
|
|
1535
|
+
describe "##{method}" do
|
|
1654
1536
|
let!(:match) do
|
|
1655
|
-
Band.create!(name:
|
|
1537
|
+
Band.create!(name: 'Depeche Mode')
|
|
1656
1538
|
end
|
|
1657
1539
|
|
|
1658
1540
|
let!(:non_match) do
|
|
1659
|
-
Band.create!(name:
|
|
1541
|
+
Band.create!(name: 'Tool')
|
|
1660
1542
|
end
|
|
1661
1543
|
|
|
1662
|
-
context
|
|
1663
|
-
|
|
1544
|
+
context 'when sending a normal $or criterion' do
|
|
1664
1545
|
let(:criteria) do
|
|
1665
|
-
Band.send(method, { name:
|
|
1546
|
+
Band.send(method, { name: 'Depeche Mode' }, { name: 'New Order' })
|
|
1666
1547
|
end
|
|
1667
1548
|
|
|
1668
|
-
it
|
|
1549
|
+
it 'returns the matching documents' do
|
|
1669
1550
|
expect(criteria).to eq([ match ])
|
|
1670
1551
|
end
|
|
1671
1552
|
end
|
|
1672
1553
|
|
|
1673
|
-
context
|
|
1674
|
-
|
|
1554
|
+
context 'when matching against an id or other parameter' do
|
|
1675
1555
|
let(:criteria) do
|
|
1676
|
-
Band.send(method, { id: match.id }, { name:
|
|
1556
|
+
Band.send(method, { id: match.id }, { name: 'New Order' })
|
|
1677
1557
|
end
|
|
1678
1558
|
|
|
1679
|
-
it
|
|
1559
|
+
it 'returns the matching documents' do
|
|
1680
1560
|
expect(criteria).to eq([ match ])
|
|
1681
1561
|
end
|
|
1682
1562
|
end
|
|
1683
1563
|
end
|
|
1684
1564
|
end
|
|
1685
1565
|
|
|
1686
|
-
describe
|
|
1687
|
-
|
|
1566
|
+
describe '#pluck' do
|
|
1688
1567
|
let!(:depeche) do
|
|
1689
|
-
Band.create!(name:
|
|
1568
|
+
Band.create!(name: 'Depeche Mode', likes: 3)
|
|
1690
1569
|
end
|
|
1691
1570
|
|
|
1692
1571
|
let!(:tool) do
|
|
1693
|
-
Band.create!(name:
|
|
1572
|
+
Band.create!(name: 'Tool', likes: 3)
|
|
1694
1573
|
end
|
|
1695
1574
|
|
|
1696
1575
|
let!(:photek) do
|
|
1697
|
-
Band.create!(name:
|
|
1576
|
+
Band.create!(name: 'Photek', likes: 1)
|
|
1698
1577
|
end
|
|
1699
1578
|
|
|
1700
1579
|
let(:maniacs) do
|
|
1701
|
-
Band.create!(name:
|
|
1580
|
+
Band.create!(name: '10,000 Maniacs', likes: 1, sales: '1E2')
|
|
1702
1581
|
end
|
|
1703
1582
|
|
|
1704
|
-
context
|
|
1705
|
-
|
|
1583
|
+
context 'when the field is aliased' do
|
|
1706
1584
|
let!(:expensive) do
|
|
1707
|
-
Product.create!(price:
|
|
1585
|
+
Product.create!(price: 100_000)
|
|
1708
1586
|
end
|
|
1709
1587
|
|
|
1710
1588
|
let!(:cheap) do
|
|
1711
1589
|
Product.create!(price: 1)
|
|
1712
1590
|
end
|
|
1713
1591
|
|
|
1714
|
-
context
|
|
1715
|
-
|
|
1592
|
+
context 'when using alias_attribute' do
|
|
1716
1593
|
let(:plucked) do
|
|
1717
1594
|
Product.pluck(:price)
|
|
1718
1595
|
end
|
|
1719
1596
|
|
|
1720
|
-
it
|
|
1721
|
-
expect(plucked).to eq([
|
|
1597
|
+
it 'uses the aliases' do
|
|
1598
|
+
expect(plucked).to eq([ 100_000, 1 ])
|
|
1722
1599
|
end
|
|
1723
1600
|
end
|
|
1724
1601
|
end
|
|
1725
1602
|
|
|
1726
|
-
context
|
|
1727
|
-
|
|
1728
|
-
context "when there are no duplicate values" do
|
|
1729
|
-
|
|
1603
|
+
context 'when the criteria matches' do
|
|
1604
|
+
context 'when there are no duplicate values' do
|
|
1730
1605
|
let(:criteria) do
|
|
1731
1606
|
Band.where(:name.exists => true)
|
|
1732
1607
|
end
|
|
@@ -1735,18 +1610,17 @@ describe Mongoid::Criteria do
|
|
|
1735
1610
|
criteria.pluck(:name)
|
|
1736
1611
|
end
|
|
1737
1612
|
|
|
1738
|
-
it
|
|
1739
|
-
expect(plucked).to contain_exactly(
|
|
1613
|
+
it 'returns the values' do
|
|
1614
|
+
expect(plucked).to contain_exactly('Depeche Mode', 'Tool', 'Photek')
|
|
1740
1615
|
end
|
|
1741
1616
|
|
|
1742
|
-
context
|
|
1743
|
-
it
|
|
1617
|
+
context 'when subsequently executing the criteria without a pluck' do
|
|
1618
|
+
it 'does not limit the fields' do
|
|
1744
1619
|
expect(criteria.first.likes).to eq(3)
|
|
1745
1620
|
end
|
|
1746
1621
|
end
|
|
1747
1622
|
|
|
1748
1623
|
context 'when the field is a subdocument' do
|
|
1749
|
-
|
|
1750
1624
|
let(:criteria) do
|
|
1751
1625
|
Band.where(name: 'FKA Twigs')
|
|
1752
1626
|
end
|
|
@@ -1763,8 +1637,8 @@ describe Mongoid::Criteria do
|
|
|
1763
1637
|
|
|
1764
1638
|
let(:expected) do
|
|
1765
1639
|
[
|
|
1766
|
-
[
|
|
1767
|
-
['FKA Twigs', [
|
|
1640
|
+
[ 'FKA Twigs', nil ],
|
|
1641
|
+
[ 'FKA Twigs', [ 'LP1' ] ]
|
|
1768
1642
|
]
|
|
1769
1643
|
end
|
|
1770
1644
|
|
|
@@ -1774,7 +1648,6 @@ describe Mongoid::Criteria do
|
|
|
1774
1648
|
end
|
|
1775
1649
|
|
|
1776
1650
|
context 'when only a subdocument field is plucked' do
|
|
1777
|
-
|
|
1778
1651
|
before do
|
|
1779
1652
|
Band.create!(name: 'FKA Twigs')
|
|
1780
1653
|
Band.create!(name: 'FKA Twigs', records: [ Record.new(name: 'LP1') ])
|
|
@@ -1787,7 +1660,7 @@ describe Mongoid::Criteria do
|
|
|
1787
1660
|
let(:expected) do
|
|
1788
1661
|
[
|
|
1789
1662
|
nil,
|
|
1790
|
-
[
|
|
1663
|
+
[ 'LP1' ]
|
|
1791
1664
|
]
|
|
1792
1665
|
end
|
|
1793
1666
|
|
|
@@ -1798,83 +1671,75 @@ describe Mongoid::Criteria do
|
|
|
1798
1671
|
end
|
|
1799
1672
|
end
|
|
1800
1673
|
|
|
1801
|
-
context
|
|
1802
|
-
|
|
1674
|
+
context 'when plucking multi-fields' do
|
|
1803
1675
|
let(:plucked) do
|
|
1804
1676
|
Band.where(:name.exists => true).pluck(:name, :likes)
|
|
1805
1677
|
end
|
|
1806
1678
|
|
|
1807
|
-
it
|
|
1808
|
-
expect(plucked).to contain_exactly([
|
|
1679
|
+
it 'returns the values' do
|
|
1680
|
+
expect(plucked).to contain_exactly([ 'Depeche Mode', 3 ], [ 'Tool', 3 ], [ 'Photek', 1 ])
|
|
1809
1681
|
end
|
|
1810
1682
|
end
|
|
1811
1683
|
|
|
1812
|
-
context
|
|
1813
|
-
|
|
1684
|
+
context 'when there are duplicate values' do
|
|
1814
1685
|
let(:plucked) do
|
|
1815
1686
|
Band.where(:name.exists => true).pluck(:likes)
|
|
1816
1687
|
end
|
|
1817
1688
|
|
|
1818
|
-
it
|
|
1689
|
+
it 'returns the duplicates' do
|
|
1819
1690
|
expect(plucked).to contain_exactly(3, 3, 1)
|
|
1820
1691
|
end
|
|
1821
1692
|
end
|
|
1822
1693
|
end
|
|
1823
1694
|
|
|
1824
|
-
context
|
|
1825
|
-
|
|
1695
|
+
context 'when the criteria does not match' do
|
|
1826
1696
|
let(:plucked) do
|
|
1827
|
-
Band.where(name:
|
|
1697
|
+
Band.where(name: 'New Order').pluck(:_id)
|
|
1828
1698
|
end
|
|
1829
1699
|
|
|
1830
|
-
it
|
|
1700
|
+
it 'returns an empty array' do
|
|
1831
1701
|
expect(plucked).to be_empty
|
|
1832
1702
|
end
|
|
1833
1703
|
end
|
|
1834
1704
|
|
|
1835
|
-
context
|
|
1836
|
-
|
|
1705
|
+
context 'when plucking an aliased field' do
|
|
1837
1706
|
let(:plucked) do
|
|
1838
1707
|
Band.all.pluck(:id)
|
|
1839
1708
|
end
|
|
1840
1709
|
|
|
1841
|
-
it
|
|
1710
|
+
it 'returns the field values' do
|
|
1842
1711
|
expect(plucked).to eq([ depeche.id, tool.id, photek.id ])
|
|
1843
1712
|
end
|
|
1844
1713
|
end
|
|
1845
1714
|
|
|
1846
|
-
context
|
|
1847
|
-
|
|
1715
|
+
context 'when plucking existent and non-existent fields' do
|
|
1848
1716
|
let(:plucked) do
|
|
1849
1717
|
Band.all.pluck(:id, :fooz)
|
|
1850
1718
|
end
|
|
1851
1719
|
|
|
1852
|
-
it
|
|
1853
|
-
expect(plucked).to eq([[depeche.id, nil], [tool.id, nil], [photek.id, nil] ])
|
|
1720
|
+
it 'returns nil for the field that doesnt exist' do
|
|
1721
|
+
expect(plucked).to eq([ [ depeche.id, nil ], [ tool.id, nil ], [ photek.id, nil ] ])
|
|
1854
1722
|
end
|
|
1855
1723
|
end
|
|
1856
1724
|
|
|
1857
|
-
context
|
|
1858
|
-
|
|
1859
|
-
context "when pluck one field" do
|
|
1860
|
-
|
|
1725
|
+
context 'when plucking a field that doesnt exist' do
|
|
1726
|
+
context 'when pluck one field' do
|
|
1861
1727
|
let(:plucked) do
|
|
1862
1728
|
Band.all.pluck(:foo)
|
|
1863
1729
|
end
|
|
1864
1730
|
|
|
1865
|
-
it
|
|
1866
|
-
expect(plucked).to eq([nil, nil, nil])
|
|
1731
|
+
it 'returns an array with nil values' do
|
|
1732
|
+
expect(plucked).to eq([ nil, nil, nil ])
|
|
1867
1733
|
end
|
|
1868
1734
|
end
|
|
1869
1735
|
|
|
1870
|
-
context
|
|
1871
|
-
|
|
1736
|
+
context 'when pluck multiple fields' do
|
|
1872
1737
|
let(:plucked) do
|
|
1873
1738
|
Band.all.pluck(:foo, :bar)
|
|
1874
1739
|
end
|
|
1875
1740
|
|
|
1876
|
-
it
|
|
1877
|
-
expect(plucked).to eq([[nil, nil], [nil, nil], [nil, nil]])
|
|
1741
|
+
it 'returns an array of arrays with nil values' do
|
|
1742
|
+
expect(plucked).to eq([ [ nil, nil ], [ nil, nil ], [ nil, nil ] ])
|
|
1878
1743
|
end
|
|
1879
1744
|
end
|
|
1880
1745
|
end
|
|
@@ -1908,16 +1773,15 @@ describe Mongoid::Criteria do
|
|
|
1908
1773
|
end
|
|
1909
1774
|
|
|
1910
1775
|
it 'returns the full translations hash to _translations' do
|
|
1911
|
-
expect(plucked_translations.first).to eq({
|
|
1776
|
+
expect(plucked_translations.first).to eq({ 'de' => 'deutsch-text', 'en' => 'english-text' })
|
|
1912
1777
|
end
|
|
1913
1778
|
|
|
1914
1779
|
it 'returns both' do
|
|
1915
|
-
expect(plucked_translations_both.first).to eq([{
|
|
1780
|
+
expect(plucked_translations_both.first).to eq([ { 'de' => 'deutsch-text', 'en' => 'english-text' }, 'deutsch-text' ])
|
|
1916
1781
|
end
|
|
1917
1782
|
end
|
|
1918
1783
|
|
|
1919
1784
|
context 'when plucking a specific locale' do
|
|
1920
|
-
|
|
1921
1785
|
let(:plucked) do
|
|
1922
1786
|
Dictionary.all.pluck(:'description.de')
|
|
1923
1787
|
end
|
|
@@ -1928,7 +1792,6 @@ describe Mongoid::Criteria do
|
|
|
1928
1792
|
end
|
|
1929
1793
|
|
|
1930
1794
|
context 'when plucking a specific locale from _translations field' do
|
|
1931
|
-
|
|
1932
1795
|
let(:plucked) do
|
|
1933
1796
|
Dictionary.all.pluck(:'description_translations.de')
|
|
1934
1797
|
end
|
|
@@ -1949,55 +1812,54 @@ describe Mongoid::Criteria do
|
|
|
1949
1812
|
Dictionary.all.pluck(:description).first
|
|
1950
1813
|
end
|
|
1951
1814
|
|
|
1952
|
-
it
|
|
1815
|
+
it 'correctly uses the fallback' do
|
|
1953
1816
|
I18n.locale = :en
|
|
1954
|
-
|
|
1817
|
+
Dictionary.create!(description: 'english-text')
|
|
1955
1818
|
I18n.locale = :he
|
|
1956
|
-
plucked.should
|
|
1819
|
+
plucked.should eq 'english-text'
|
|
1957
1820
|
end
|
|
1958
1821
|
end
|
|
1959
1822
|
|
|
1960
|
-
context
|
|
1823
|
+
context 'when the localized field is embedded' do
|
|
1961
1824
|
with_default_i18n_configs
|
|
1962
1825
|
|
|
1963
1826
|
before do
|
|
1964
1827
|
p = Passport.new
|
|
1965
1828
|
I18n.locale = :en
|
|
1966
|
-
p.name =
|
|
1829
|
+
p.name = 'Neil'
|
|
1967
1830
|
I18n.locale = :he
|
|
1968
|
-
p.name =
|
|
1831
|
+
p.name = 'Nissim'
|
|
1969
1832
|
|
|
1970
|
-
Person.create!(passport: p, employer_id:
|
|
1833
|
+
Person.create!(passport: p, employer_id: 12_345)
|
|
1971
1834
|
end
|
|
1972
1835
|
|
|
1973
1836
|
let(:plucked) do
|
|
1974
|
-
Person.where(employer_id:
|
|
1837
|
+
Person.where(employer_id: 12_345).pluck('pass.name').first
|
|
1975
1838
|
end
|
|
1976
1839
|
|
|
1977
1840
|
let(:plucked_translations) do
|
|
1978
|
-
Person.where(employer_id:
|
|
1841
|
+
Person.where(employer_id: 12_345).pluck('pass.name_translations').first
|
|
1979
1842
|
end
|
|
1980
1843
|
|
|
1981
1844
|
let(:plucked_translations_field) do
|
|
1982
|
-
Person.where(employer_id:
|
|
1845
|
+
Person.where(employer_id: 12_345).pluck('pass.name_translations.en').first
|
|
1983
1846
|
end
|
|
1984
1847
|
|
|
1985
|
-
it
|
|
1986
|
-
expect(plucked).to eq(
|
|
1848
|
+
it 'returns the translation for the current locale' do
|
|
1849
|
+
expect(plucked).to eq('Nissim')
|
|
1987
1850
|
end
|
|
1988
1851
|
|
|
1989
|
-
it
|
|
1990
|
-
expect(plucked_translations).to eq({
|
|
1852
|
+
it 'returns the full _translation hash' do
|
|
1853
|
+
expect(plucked_translations).to eq({ 'en' => 'Neil', 'he' => 'Nissim' })
|
|
1991
1854
|
end
|
|
1992
1855
|
|
|
1993
|
-
it
|
|
1994
|
-
expect(plucked_translations_field).to eq(
|
|
1856
|
+
it 'returns the translation for the requested locale' do
|
|
1857
|
+
expect(plucked_translations_field).to eq('Neil')
|
|
1995
1858
|
end
|
|
1996
1859
|
end
|
|
1997
1860
|
end
|
|
1998
1861
|
|
|
1999
1862
|
context 'when plucking a field to be demongoized' do
|
|
2000
|
-
|
|
2001
1863
|
let(:plucked) do
|
|
2002
1864
|
Band.where(name: maniacs.name).pluck(:sales)
|
|
2003
1865
|
end
|
|
@@ -2005,87 +1867,87 @@ describe Mongoid::Criteria do
|
|
|
2005
1867
|
context 'when value is stored as string' do
|
|
2006
1868
|
config_override :map_big_decimal_to_decimal128, false
|
|
2007
1869
|
|
|
2008
|
-
it
|
|
1870
|
+
it 'demongoizes the field' do
|
|
2009
1871
|
expect(plucked.first).to be_a(BigDecimal)
|
|
2010
|
-
expect(plucked.first).to eq(BigDecimal(
|
|
1872
|
+
expect(plucked.first).to eq(BigDecimal('1E2'))
|
|
2011
1873
|
end
|
|
2012
1874
|
end
|
|
2013
1875
|
|
|
2014
1876
|
context 'when value is stored as decimal128' do
|
|
2015
1877
|
config_override :map_big_decimal_to_decimal128, true
|
|
2016
1878
|
|
|
2017
|
-
it
|
|
1879
|
+
it 'demongoizes the field' do
|
|
2018
1880
|
expect(plucked.first).to be_a(BigDecimal)
|
|
2019
|
-
expect(plucked.first).to eq(BigDecimal(
|
|
1881
|
+
expect(plucked.first).to eq(BigDecimal('1E2'))
|
|
2020
1882
|
end
|
|
2021
1883
|
end
|
|
2022
1884
|
end
|
|
2023
1885
|
|
|
2024
|
-
context
|
|
2025
|
-
let(:label) { Label.new(sales:
|
|
1886
|
+
context 'when plucking an embedded field' do
|
|
1887
|
+
let(:label) { Label.new(sales: '1E2') }
|
|
2026
1888
|
let!(:band) { Band.create!(label: label) }
|
|
2027
1889
|
|
|
2028
|
-
let(:plucked) { Band.where(_id: band.id).pluck(
|
|
1890
|
+
let(:plucked) { Band.where(_id: band.id).pluck('label.sales') }
|
|
2029
1891
|
|
|
2030
|
-
it
|
|
2031
|
-
expect(plucked).to eq([ BigDecimal(
|
|
1892
|
+
it 'demongoizes the field' do
|
|
1893
|
+
expect(plucked).to eq([ BigDecimal('1E2') ])
|
|
2032
1894
|
end
|
|
2033
1895
|
end
|
|
2034
1896
|
|
|
2035
|
-
context
|
|
2036
|
-
let(:label) { Label.new(sales:
|
|
2037
|
-
let!(:band) { Band.create!(labels: [label]) }
|
|
1897
|
+
context 'when plucking an embeds_many field' do
|
|
1898
|
+
let(:label) { Label.new(sales: '1E2') }
|
|
1899
|
+
let!(:band) { Band.create!(labels: [ label ]) }
|
|
2038
1900
|
|
|
2039
|
-
let(:plucked) { Band.where(_id: band.id).pluck(
|
|
1901
|
+
let(:plucked) { Band.where(_id: band.id).pluck('labels.sales') }
|
|
2040
1902
|
|
|
2041
|
-
it
|
|
2042
|
-
expect(plucked.first).to eq([ BigDecimal(
|
|
1903
|
+
it 'demongoizes the field' do
|
|
1904
|
+
expect(plucked.first).to eq([ BigDecimal('1E2') ])
|
|
2043
1905
|
end
|
|
2044
1906
|
end
|
|
2045
1907
|
|
|
2046
|
-
context
|
|
2047
|
-
let(:label) { Label.new(sales:
|
|
1908
|
+
context 'when plucking a nonexistent embedded field' do
|
|
1909
|
+
let(:label) { Label.new(sales: '1E2') }
|
|
2048
1910
|
let!(:band) { Band.create!(label: label) }
|
|
2049
1911
|
|
|
2050
|
-
let(:plucked) { Band.where(_id: band.id).pluck(
|
|
1912
|
+
let(:plucked) { Band.where(_id: band.id).pluck('label.qwerty') }
|
|
2051
1913
|
|
|
2052
|
-
it
|
|
1914
|
+
it 'returns nil' do
|
|
2053
1915
|
expect(plucked.first).to eq(nil)
|
|
2054
1916
|
end
|
|
2055
1917
|
end
|
|
2056
1918
|
|
|
2057
|
-
context
|
|
2058
|
-
|
|
1919
|
+
context 'when tallying deeply nested arrays/embedded associations' do
|
|
2059
1920
|
before do
|
|
2060
|
-
Person.create!(addresses: [ Address.new(code: Code.new(deepest: Deepest.new(array: [ { y: { z: 1 } },
|
|
2061
|
-
|
|
2062
|
-
Person.create!(addresses: [ Address.new(code: Code.new(deepest: Deepest.new(array: [ { y: { z: 1 } },
|
|
1921
|
+
Person.create!(addresses: [ Address.new(code: Code.new(deepest: Deepest.new(array: [ { y: { z: 1 } },
|
|
1922
|
+
{ y: { z: 2 } } ]))) ])
|
|
1923
|
+
Person.create!(addresses: [ Address.new(code: Code.new(deepest: Deepest.new(array: [ { y: { z: 1 } },
|
|
1924
|
+
{ y: { z: 2 } } ]))) ])
|
|
1925
|
+
Person.create!(addresses: [ Address.new(code: Code.new(deepest: Deepest.new(array: [ { y: { z: 1 } },
|
|
1926
|
+
{ y: { z: 3 } } ]))) ])
|
|
2063
1927
|
end
|
|
2064
1928
|
|
|
2065
1929
|
let(:plucked) do
|
|
2066
|
-
Person.pluck(
|
|
1930
|
+
Person.pluck('addresses.code.deepest.array.y.z')
|
|
2067
1931
|
end
|
|
2068
1932
|
|
|
2069
|
-
it
|
|
1933
|
+
it 'returns the correct hash' do
|
|
2070
1934
|
expect(plucked).to eq([
|
|
2071
|
-
|
|
2072
|
-
|
|
1935
|
+
[ [ 1, 2 ] ], [ [ 1, 2 ] ], [ [ 1, 3 ] ]
|
|
1936
|
+
])
|
|
2073
1937
|
end
|
|
2074
1938
|
end
|
|
2075
1939
|
end
|
|
2076
1940
|
|
|
2077
|
-
describe
|
|
2078
|
-
|
|
1941
|
+
describe '#pick' do
|
|
2079
1942
|
let!(:depeche) do
|
|
2080
|
-
Band.create!(name:
|
|
1943
|
+
Band.create!(name: 'Depeche Mode', likes: 3)
|
|
2081
1944
|
end
|
|
2082
1945
|
|
|
2083
1946
|
let!(:tool) do
|
|
2084
|
-
Band.create!(name:
|
|
1947
|
+
Band.create!(name: 'Tool', likes: 3)
|
|
2085
1948
|
end
|
|
2086
1949
|
|
|
2087
|
-
context
|
|
2088
|
-
|
|
1950
|
+
context 'when picking a field' do
|
|
2089
1951
|
let(:criteria) do
|
|
2090
1952
|
Band.all
|
|
2091
1953
|
end
|
|
@@ -2094,13 +1956,12 @@ describe Mongoid::Criteria do
|
|
|
2094
1956
|
criteria.pick(:name)
|
|
2095
1957
|
end
|
|
2096
1958
|
|
|
2097
|
-
it
|
|
2098
|
-
expect(picked).to eq(
|
|
1959
|
+
it 'returns one element' do
|
|
1960
|
+
expect(picked).to eq('Depeche Mode')
|
|
2099
1961
|
end
|
|
2100
1962
|
end
|
|
2101
1963
|
|
|
2102
|
-
context
|
|
2103
|
-
|
|
1964
|
+
context 'when picking multiple fields' do
|
|
2104
1965
|
let(:criteria) do
|
|
2105
1966
|
Band.all
|
|
2106
1967
|
end
|
|
@@ -2109,98 +1970,87 @@ describe Mongoid::Criteria do
|
|
|
2109
1970
|
criteria.pick(:name, :likes)
|
|
2110
1971
|
end
|
|
2111
1972
|
|
|
2112
|
-
it
|
|
2113
|
-
expect(picked).to eq([
|
|
1973
|
+
it 'returns an array' do
|
|
1974
|
+
expect(picked).to eq([ 'Depeche Mode', 3 ])
|
|
2114
1975
|
end
|
|
2115
1976
|
end
|
|
2116
1977
|
end
|
|
2117
1978
|
|
|
2118
|
-
describe
|
|
2119
|
-
|
|
1979
|
+
describe '#respond_to?' do
|
|
2120
1980
|
let(:criteria) do
|
|
2121
1981
|
described_class.new(Person)
|
|
2122
1982
|
end
|
|
2123
1983
|
|
|
2124
1984
|
before do
|
|
2125
1985
|
class Person
|
|
2126
|
-
def self.ages
|
|
1986
|
+
def self.ages
|
|
1987
|
+
self
|
|
1988
|
+
end
|
|
2127
1989
|
end
|
|
2128
1990
|
end
|
|
2129
1991
|
|
|
2130
|
-
context
|
|
2131
|
-
|
|
2132
|
-
it "returns true" do
|
|
1992
|
+
context 'when asking about a model public class method' do
|
|
1993
|
+
it 'returns true' do
|
|
2133
1994
|
expect(criteria).to respond_to(:ages)
|
|
2134
1995
|
end
|
|
2135
1996
|
end
|
|
2136
1997
|
|
|
2137
|
-
context
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
it "returns true" do
|
|
1998
|
+
context 'when asking about a model private class method' do
|
|
1999
|
+
context 'when including private methods' do
|
|
2000
|
+
it 'returns true' do
|
|
2142
2001
|
expect(criteria.respond_to?(:for_ids, true)).to be true
|
|
2143
2002
|
end
|
|
2144
2003
|
end
|
|
2145
2004
|
end
|
|
2146
2005
|
|
|
2147
|
-
context
|
|
2148
|
-
|
|
2149
|
-
it "returns true" do
|
|
2006
|
+
context 'when asking about a model class public instance method' do
|
|
2007
|
+
it 'returns true' do
|
|
2150
2008
|
expect(criteria.respond_to?(:join)).to be true
|
|
2151
2009
|
end
|
|
2152
2010
|
end
|
|
2153
2011
|
|
|
2154
|
-
context
|
|
2155
|
-
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
it "returns false" do
|
|
2159
|
-
expect(criteria).to_not respond_to(:initialize_copy)
|
|
2012
|
+
context 'when asking about a model private instance method' do
|
|
2013
|
+
context 'when not including private methods' do
|
|
2014
|
+
it 'returns false' do
|
|
2015
|
+
expect(criteria).not_to respond_to(:initialize_copy)
|
|
2160
2016
|
end
|
|
2161
2017
|
end
|
|
2162
2018
|
|
|
2163
|
-
context
|
|
2164
|
-
|
|
2165
|
-
it "returns true" do
|
|
2019
|
+
context 'when including private methods' do
|
|
2020
|
+
it 'returns true' do
|
|
2166
2021
|
expect(criteria.respond_to?(:initialize_copy, true)).to be true
|
|
2167
2022
|
end
|
|
2168
2023
|
end
|
|
2169
2024
|
end
|
|
2170
2025
|
|
|
2171
|
-
context
|
|
2172
|
-
|
|
2173
|
-
it "returns true" do
|
|
2026
|
+
context 'when asking about a criteria instance method' do
|
|
2027
|
+
it 'returns true' do
|
|
2174
2028
|
expect(criteria).to respond_to(:context)
|
|
2175
2029
|
end
|
|
2176
2030
|
end
|
|
2177
2031
|
|
|
2178
|
-
context
|
|
2179
|
-
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
it "returns false" do
|
|
2183
|
-
expect(criteria).to_not respond_to(:puts)
|
|
2032
|
+
context 'when asking about a private criteria instance method' do
|
|
2033
|
+
context 'when not including private methods' do
|
|
2034
|
+
it 'returns false' do
|
|
2035
|
+
expect(criteria).not_to respond_to(:puts)
|
|
2184
2036
|
end
|
|
2185
2037
|
end
|
|
2186
2038
|
|
|
2187
|
-
context
|
|
2188
|
-
|
|
2189
|
-
it "returns true" do
|
|
2039
|
+
context 'when including private methods' do
|
|
2040
|
+
it 'returns true' do
|
|
2190
2041
|
expect(criteria.respond_to?(:puts, true)).to be true
|
|
2191
2042
|
end
|
|
2192
2043
|
end
|
|
2193
2044
|
end
|
|
2194
2045
|
end
|
|
2195
2046
|
|
|
2196
|
-
describe
|
|
2197
|
-
|
|
2047
|
+
describe '#sort' do
|
|
2198
2048
|
let!(:depeche) do
|
|
2199
|
-
Band.create!(name:
|
|
2049
|
+
Band.create!(name: 'Depeche Mode', likes: 1000)
|
|
2200
2050
|
end
|
|
2201
2051
|
|
|
2202
2052
|
let!(:tool) do
|
|
2203
|
-
Band.create!(name:
|
|
2053
|
+
Band.create!(name: 'Tool', likes: 500)
|
|
2204
2054
|
end
|
|
2205
2055
|
|
|
2206
2056
|
let(:sorted) do
|
|
@@ -2209,195 +2059,355 @@ describe Mongoid::Criteria do
|
|
|
2209
2059
|
end
|
|
2210
2060
|
end
|
|
2211
2061
|
|
|
2212
|
-
it
|
|
2062
|
+
it 'sorts the results in memory' do
|
|
2213
2063
|
expect(sorted).to eq([ tool, depeche ])
|
|
2214
2064
|
end
|
|
2215
2065
|
end
|
|
2216
2066
|
|
|
2217
|
-
describe
|
|
2218
|
-
|
|
2219
|
-
context "when provided a single field" do
|
|
2220
|
-
|
|
2067
|
+
describe '#sum' do
|
|
2068
|
+
context 'when provided a single field' do
|
|
2221
2069
|
let!(:depeche) do
|
|
2222
|
-
Band.create!(name:
|
|
2070
|
+
Band.create!(name: 'Depeche Mode', likes: 1000)
|
|
2223
2071
|
end
|
|
2224
2072
|
|
|
2225
2073
|
let!(:tool) do
|
|
2226
|
-
Band.create!(name:
|
|
2074
|
+
Band.create!(name: 'Tool', likes: 500)
|
|
2227
2075
|
end
|
|
2228
2076
|
|
|
2229
2077
|
let(:criteria) do
|
|
2230
2078
|
Band.all
|
|
2231
2079
|
end
|
|
2232
2080
|
|
|
2233
|
-
context
|
|
2234
|
-
|
|
2081
|
+
context 'when provided a symbol' do
|
|
2235
2082
|
let(:sum) do
|
|
2236
2083
|
criteria.sum(:likes)
|
|
2237
2084
|
end
|
|
2238
2085
|
|
|
2239
|
-
it
|
|
2086
|
+
it 'returns the sum of the provided field' do
|
|
2240
2087
|
expect(sum).to eq(1500)
|
|
2241
2088
|
end
|
|
2242
2089
|
end
|
|
2243
2090
|
|
|
2244
|
-
context
|
|
2245
|
-
|
|
2091
|
+
context 'when provided a block' do
|
|
2246
2092
|
let(:sum) do
|
|
2247
2093
|
criteria.sum(&:likes)
|
|
2248
2094
|
end
|
|
2249
2095
|
|
|
2250
|
-
it
|
|
2096
|
+
it 'returns the sum for the provided block' do
|
|
2251
2097
|
expect(sum).to eq(1500)
|
|
2252
2098
|
end
|
|
2253
2099
|
end
|
|
2254
2100
|
end
|
|
2255
2101
|
end
|
|
2256
2102
|
|
|
2257
|
-
describe
|
|
2258
|
-
|
|
2103
|
+
describe '#to_ary' do
|
|
2259
2104
|
let!(:band) do
|
|
2260
|
-
Band.create!(name:
|
|
2105
|
+
Band.create!(name: 'Depeche Mode')
|
|
2261
2106
|
end
|
|
2262
2107
|
|
|
2263
2108
|
let(:criteria) do
|
|
2264
|
-
Band.where(name:
|
|
2109
|
+
Band.where(name: 'Depeche Mode')
|
|
2265
2110
|
end
|
|
2266
2111
|
|
|
2267
|
-
it
|
|
2112
|
+
it 'returns the executed criteria' do
|
|
2268
2113
|
expect(criteria.to_ary).to eq([ band ])
|
|
2269
2114
|
end
|
|
2270
2115
|
end
|
|
2271
2116
|
|
|
2272
|
-
describe
|
|
2117
|
+
describe '#raw' do
|
|
2118
|
+
let(:result) { results[0] }
|
|
2119
|
+
|
|
2120
|
+
context 'when the parameters are inconsistent' do
|
|
2121
|
+
let(:results) { criteria.raw(false, typed: false).to_a }
|
|
2122
|
+
let(:criteria) { Person }
|
|
2123
|
+
|
|
2124
|
+
it 'raises an ArgumentError' do
|
|
2125
|
+
expect { result }.to raise_error(ArgumentError)
|
|
2126
|
+
end
|
|
2127
|
+
end
|
|
2128
|
+
|
|
2129
|
+
context 'when returning untyped results' do
|
|
2130
|
+
let(:results) { criteria.raw.to_a }
|
|
2131
|
+
|
|
2132
|
+
context 'without associations' do
|
|
2133
|
+
before do
|
|
2134
|
+
Band.create(name: 'the band',
|
|
2135
|
+
active: true,
|
|
2136
|
+
genres: %w[ abc def ],
|
|
2137
|
+
member_count: 112,
|
|
2138
|
+
rating: 4.2,
|
|
2139
|
+
created: Time.now,
|
|
2140
|
+
updated: Time.now,
|
|
2141
|
+
sales: 1_234_567.89,
|
|
2142
|
+
decimal: 9_876_543.21,
|
|
2143
|
+
decibels: 140..170,
|
|
2144
|
+
deleted: false,
|
|
2145
|
+
mojo: Math::PI,
|
|
2146
|
+
tags: { 'one' => 1, 'two' => 2 },
|
|
2147
|
+
location: LatLng.new(41.74, -111.83))
|
|
2148
|
+
end
|
|
2149
|
+
|
|
2150
|
+
let(:criteria) { Band.where(name: 'the band') }
|
|
2151
|
+
|
|
2152
|
+
it 'returns a hash' do
|
|
2153
|
+
expect(result).to be_a(Hash)
|
|
2154
|
+
end
|
|
2155
|
+
|
|
2156
|
+
it 'does not demongoize the result' do
|
|
2157
|
+
expect(result['genres']).to be_a(Array)
|
|
2158
|
+
expect(result['decibels']).to eq({ 'min' => 140, 'max' => 170 })
|
|
2159
|
+
expect(result['location']).to eq [ -111.83, 41.74 ]
|
|
2160
|
+
end
|
|
2161
|
+
end
|
|
2162
|
+
|
|
2163
|
+
context 'with associations' do
|
|
2164
|
+
before do
|
|
2165
|
+
Person.create({
|
|
2166
|
+
addresses: [ Address.new(end_date: 2.months.from_now) ],
|
|
2167
|
+
passport: Passport.new(exp: 1.year.from_now)
|
|
2168
|
+
})
|
|
2169
|
+
end
|
|
2170
|
+
|
|
2171
|
+
let(:criteria) { Person }
|
|
2172
|
+
|
|
2173
|
+
it 'demongoizes the embedded relation' do
|
|
2174
|
+
expect(result['addresses']).to be_a(Array)
|
|
2175
|
+
expect(result['addresses'][0]['end_date']).to be_a(Time)
|
|
2176
|
+
|
|
2177
|
+
# `pass` is how it is stored, `passport` is how it is aliased
|
|
2178
|
+
expect(result['pass']).to be_a(Hash)
|
|
2179
|
+
expect(result['pass']['exp']).to be_a(Time)
|
|
2180
|
+
end
|
|
2181
|
+
end
|
|
2182
|
+
|
|
2183
|
+
context 'with projections' do
|
|
2184
|
+
before { Person.create(title: 'sir', dob: Date.new(1980, 1, 1)) }
|
|
2185
|
+
|
|
2186
|
+
context 'using #only' do
|
|
2187
|
+
let(:criteria) { Person.only(:dob) }
|
|
2188
|
+
|
|
2189
|
+
it 'produces a hash with only the _id and the requested key' do
|
|
2190
|
+
expect(result).to be_a(Hash)
|
|
2191
|
+
expect(result.keys).to eq %w[ _id dob ]
|
|
2192
|
+
expect(result['dob']).to eq Date.new(1980, 1, 1)
|
|
2193
|
+
end
|
|
2194
|
+
end
|
|
2195
|
+
|
|
2196
|
+
context 'using #without' do
|
|
2197
|
+
let(:criteria) { Person.without(:dob) }
|
|
2198
|
+
|
|
2199
|
+
it 'produces a hash that excludes requested key' do
|
|
2200
|
+
expect(result).to be_a(Hash)
|
|
2201
|
+
expect(result.keys).not_to include('dob')
|
|
2202
|
+
expect(result.keys).to be_present
|
|
2203
|
+
end
|
|
2204
|
+
end
|
|
2205
|
+
end
|
|
2206
|
+
end
|
|
2207
|
+
|
|
2208
|
+
context 'when returning typed results' do
|
|
2209
|
+
let(:results) { criteria.raw(typed: true).to_a }
|
|
2210
|
+
|
|
2211
|
+
context 'without associations' do
|
|
2212
|
+
before do
|
|
2213
|
+
Band.create(name: 'the band',
|
|
2214
|
+
active: true,
|
|
2215
|
+
genres: %w[ abc def ],
|
|
2216
|
+
member_count: 112,
|
|
2217
|
+
rating: 4.2,
|
|
2218
|
+
created: Time.now,
|
|
2219
|
+
updated: Time.now,
|
|
2220
|
+
sales: 1_234_567.89,
|
|
2221
|
+
decimal: 9_876_543.21,
|
|
2222
|
+
decibels: 140..170,
|
|
2223
|
+
deleted: false,
|
|
2224
|
+
mojo: Math::PI,
|
|
2225
|
+
tags: { 'one' => 1, 'two' => 2 },
|
|
2226
|
+
location: LatLng.new(41.74, -111.83))
|
|
2227
|
+
end
|
|
2228
|
+
|
|
2229
|
+
let(:criteria) { Band.where(name: 'the band') }
|
|
2230
|
+
|
|
2231
|
+
it 'returns a hash' do
|
|
2232
|
+
expect(result).to be_a(Hash)
|
|
2233
|
+
end
|
|
2234
|
+
|
|
2235
|
+
it 'demongoizes the result' do
|
|
2236
|
+
expect(result['genres']).to be_a(Array)
|
|
2237
|
+
expect(result['decibels']).to be_a(Range)
|
|
2238
|
+
expect(result['location']).to be_a(LatLng)
|
|
2239
|
+
end
|
|
2240
|
+
end
|
|
2241
|
+
|
|
2242
|
+
context 'with associations' do
|
|
2243
|
+
before do
|
|
2244
|
+
Person.create({
|
|
2245
|
+
addresses: [ Address.new(end_date: 2.months.from_now) ],
|
|
2246
|
+
passport: Passport.new(exp: 1.year.from_now)
|
|
2247
|
+
})
|
|
2248
|
+
end
|
|
2249
|
+
|
|
2250
|
+
let(:criteria) { Person }
|
|
2251
|
+
|
|
2252
|
+
it 'demongoizes the embedded relation' do
|
|
2253
|
+
expect(result['addresses']).to be_a(Array)
|
|
2254
|
+
expect(result['addresses'][0]['end_date']).to be_a(Date)
|
|
2255
|
+
|
|
2256
|
+
# `pass` is how it is stored, `passport` is how it is aliased
|
|
2257
|
+
expect(result['pass']).to be_a(Hash)
|
|
2258
|
+
expect(result['pass']['exp']).to be_a(Date)
|
|
2259
|
+
end
|
|
2260
|
+
end
|
|
2261
|
+
|
|
2262
|
+
context 'with projections' do
|
|
2263
|
+
before { Person.create(title: 'sir', dob: Date.new(1980, 1, 1)) }
|
|
2264
|
+
|
|
2265
|
+
context 'using #only' do
|
|
2266
|
+
let(:criteria) { Person.only(:dob) }
|
|
2267
|
+
|
|
2268
|
+
it 'produces a hash with only the _id and the requested key' do
|
|
2269
|
+
expect(result).to be_a(Hash)
|
|
2270
|
+
expect(result.keys).to eq %w[ _id dob ]
|
|
2271
|
+
expect(result['dob']).to eq Date.new(1980, 1, 1)
|
|
2272
|
+
end
|
|
2273
|
+
end
|
|
2274
|
+
|
|
2275
|
+
context 'using #without' do
|
|
2276
|
+
let(:criteria) { Person.without(:dob) }
|
|
2277
|
+
|
|
2278
|
+
it 'produces a hash that excludes requested key' do
|
|
2279
|
+
expect(result).to be_a(Hash)
|
|
2280
|
+
expect(result.keys).not_to include('dob')
|
|
2281
|
+
expect(result.keys).to be_present
|
|
2282
|
+
end
|
|
2283
|
+
end
|
|
2284
|
+
end
|
|
2285
|
+
end
|
|
2286
|
+
end
|
|
2287
|
+
|
|
2288
|
+
describe '#max_scan' do
|
|
2273
2289
|
max_server_version '4.0'
|
|
2274
2290
|
|
|
2275
2291
|
let!(:band) do
|
|
2276
|
-
Band.create!(name:
|
|
2292
|
+
Band.create!(name: 'Depeche Mode')
|
|
2277
2293
|
end
|
|
2278
2294
|
|
|
2279
2295
|
let!(:band2) do
|
|
2280
|
-
Band.create!(name:
|
|
2296
|
+
Band.create!(name: 'Tool')
|
|
2281
2297
|
end
|
|
2282
2298
|
|
|
2283
2299
|
let(:criteria) do
|
|
2284
2300
|
Band.where({}).max_scan(1)
|
|
2285
2301
|
end
|
|
2286
2302
|
|
|
2287
|
-
it
|
|
2288
|
-
expect(criteria.to_ary).to eq [band]
|
|
2303
|
+
it 'executes the criteria while properly giving the max scan to Mongo' do
|
|
2304
|
+
expect(criteria.to_ary).to eq [ band ]
|
|
2289
2305
|
end
|
|
2290
2306
|
end
|
|
2291
2307
|
|
|
2292
|
-
describe
|
|
2293
|
-
|
|
2308
|
+
describe '#to_proc' do
|
|
2294
2309
|
let(:criteria) do
|
|
2295
2310
|
Band.all
|
|
2296
2311
|
end
|
|
2297
2312
|
|
|
2298
|
-
it
|
|
2313
|
+
it 'returns a proc' do
|
|
2299
2314
|
expect(criteria.to_proc).to be_a(Proc)
|
|
2300
2315
|
end
|
|
2301
2316
|
|
|
2302
|
-
it
|
|
2317
|
+
it 'wraps the criteria in the proc' do
|
|
2303
2318
|
expect(criteria.to_proc[]).to eq(criteria)
|
|
2304
2319
|
end
|
|
2305
2320
|
end
|
|
2306
2321
|
|
|
2307
|
-
describe
|
|
2308
|
-
context
|
|
2309
|
-
context
|
|
2310
|
-
|
|
2322
|
+
describe '#type' do
|
|
2323
|
+
context 'when using the default discriminator_key' do
|
|
2324
|
+
context 'when the type is a string' do
|
|
2311
2325
|
let!(:browser) do
|
|
2312
2326
|
Browser.create!
|
|
2313
2327
|
end
|
|
2314
2328
|
|
|
2315
2329
|
let(:criteria) do
|
|
2316
|
-
Canvas.all.type(
|
|
2330
|
+
Canvas.all.type('Browser')
|
|
2317
2331
|
end
|
|
2318
2332
|
|
|
2319
|
-
it
|
|
2333
|
+
it 'returns documents with the provided type' do
|
|
2320
2334
|
expect(criteria).to eq([ browser ])
|
|
2321
2335
|
end
|
|
2322
2336
|
end
|
|
2323
2337
|
|
|
2324
|
-
context
|
|
2325
|
-
|
|
2338
|
+
context 'when the type is an Array of type' do
|
|
2326
2339
|
let!(:browser) do
|
|
2327
2340
|
Firefox.create!
|
|
2328
2341
|
end
|
|
2329
2342
|
|
|
2330
2343
|
let(:criteria) do
|
|
2331
|
-
Canvas.all.type([
|
|
2344
|
+
Canvas.all.type(%w[Browser Firefox])
|
|
2332
2345
|
end
|
|
2333
2346
|
|
|
2334
|
-
it
|
|
2347
|
+
it 'returns documents with the provided types' do
|
|
2335
2348
|
expect(criteria).to eq([ browser ])
|
|
2336
2349
|
end
|
|
2337
2350
|
end
|
|
2338
2351
|
end
|
|
2339
2352
|
|
|
2340
|
-
context
|
|
2353
|
+
context 'when using a custom discriminator_key' do
|
|
2341
2354
|
before do
|
|
2342
|
-
Canvas.discriminator_key =
|
|
2355
|
+
Canvas.discriminator_key = 'dkey'
|
|
2343
2356
|
end
|
|
2344
2357
|
|
|
2345
2358
|
after do
|
|
2346
2359
|
Canvas.discriminator_key = nil
|
|
2347
2360
|
end
|
|
2348
2361
|
|
|
2349
|
-
context
|
|
2350
|
-
|
|
2362
|
+
context 'when the type is a string' do
|
|
2351
2363
|
let!(:browser) do
|
|
2352
2364
|
Browser.create!
|
|
2353
2365
|
end
|
|
2354
2366
|
|
|
2355
2367
|
let(:criteria) do
|
|
2356
|
-
Canvas.all.type(
|
|
2368
|
+
Canvas.all.type('Browser')
|
|
2357
2369
|
end
|
|
2358
2370
|
|
|
2359
|
-
it
|
|
2371
|
+
it 'returns documents with the provided type' do
|
|
2360
2372
|
expect(criteria).to eq([ browser ])
|
|
2361
2373
|
end
|
|
2362
2374
|
end
|
|
2363
2375
|
|
|
2364
|
-
context
|
|
2365
|
-
|
|
2376
|
+
context 'when the type is an Array of type' do
|
|
2366
2377
|
let!(:browser) do
|
|
2367
2378
|
Firefox.create!
|
|
2368
2379
|
end
|
|
2369
2380
|
|
|
2370
2381
|
let(:criteria) do
|
|
2371
|
-
Canvas.all.type([
|
|
2382
|
+
Canvas.all.type(%w[Browser Firefox])
|
|
2372
2383
|
end
|
|
2373
2384
|
|
|
2374
|
-
it
|
|
2385
|
+
it 'returns documents with the provided types' do
|
|
2375
2386
|
expect(criteria).to eq([ browser ])
|
|
2376
2387
|
end
|
|
2377
2388
|
end
|
|
2378
2389
|
end
|
|
2379
2390
|
end
|
|
2380
2391
|
|
|
2381
|
-
describe
|
|
2382
|
-
|
|
2392
|
+
describe '#where' do
|
|
2383
2393
|
let!(:match) do
|
|
2384
|
-
Band.create!(name:
|
|
2394
|
+
Band.create!(name: 'Depeche Mode')
|
|
2385
2395
|
end
|
|
2386
2396
|
|
|
2387
2397
|
let!(:non_match) do
|
|
2388
|
-
Band.create!(name:
|
|
2398
|
+
Band.create!(name: 'Tool')
|
|
2389
2399
|
end
|
|
2390
2400
|
|
|
2391
2401
|
context 'when provided no arguments' do
|
|
2392
2402
|
context 'on a model class' do
|
|
2393
2403
|
it 'returns an empty criteria' do
|
|
2394
|
-
Band.where.selector.should
|
|
2404
|
+
Band.where.selector.should eq({})
|
|
2395
2405
|
end
|
|
2396
2406
|
end
|
|
2397
2407
|
|
|
2398
2408
|
context 'on an association' do
|
|
2399
2409
|
it 'returns an empty criteria' do
|
|
2400
|
-
match.records.where.selector.should
|
|
2410
|
+
match.records.where.selector.should eq({})
|
|
2401
2411
|
end
|
|
2402
2412
|
end
|
|
2403
2413
|
end
|
|
@@ -2406,7 +2416,7 @@ describe Mongoid::Criteria do
|
|
|
2406
2416
|
context 'on a model class' do
|
|
2407
2417
|
it 'raises ArgumentError' do
|
|
2408
2418
|
lambda do
|
|
2409
|
-
Band.where({foo: 1}, {bar: 2})
|
|
2419
|
+
Band.where({ foo: 1 }, { bar: 2 })
|
|
2410
2420
|
end.should raise_error(ArgumentError, /where requires zero or one arguments/)
|
|
2411
2421
|
end
|
|
2412
2422
|
end
|
|
@@ -2414,50 +2424,44 @@ describe Mongoid::Criteria do
|
|
|
2414
2424
|
context 'on an association' do
|
|
2415
2425
|
it 'raises ArgumentError' do
|
|
2416
2426
|
lambda do
|
|
2417
|
-
match.records.where({foo: 1}, {bar: 2})
|
|
2427
|
+
match.records.where({ foo: 1 }, { bar: 2 })
|
|
2418
2428
|
end.should raise_error(ArgumentError, /where requires zero or one arguments/)
|
|
2419
2429
|
end
|
|
2420
2430
|
end
|
|
2421
2431
|
end
|
|
2422
2432
|
|
|
2423
|
-
context
|
|
2424
|
-
|
|
2425
|
-
|
|
2426
|
-
|
|
2427
|
-
|
|
2428
|
-
|
|
2429
|
-
match.records.where("this.name == null")
|
|
2430
|
-
}.to raise_error(Mongoid::Errors::UnsupportedJavascript)
|
|
2433
|
+
context 'when provided a string' do
|
|
2434
|
+
context 'when the criteria is embedded' do
|
|
2435
|
+
it 'raises an error' do
|
|
2436
|
+
expect do
|
|
2437
|
+
match.records.where('this.name == null')
|
|
2438
|
+
end.to raise_error(Mongoid::Errors::UnsupportedJavascript)
|
|
2431
2439
|
end
|
|
2432
2440
|
end
|
|
2433
2441
|
|
|
2434
|
-
context
|
|
2435
|
-
|
|
2442
|
+
context 'when the criteria is not embedded' do
|
|
2436
2443
|
let(:criteria) do
|
|
2437
2444
|
Band.where("this.name == 'Depeche Mode'")
|
|
2438
2445
|
end
|
|
2439
2446
|
|
|
2440
|
-
it
|
|
2447
|
+
it 'returns the matching documents' do
|
|
2441
2448
|
expect(criteria).to eq([ match ])
|
|
2442
2449
|
end
|
|
2443
2450
|
end
|
|
2444
2451
|
end
|
|
2445
2452
|
|
|
2446
|
-
context
|
|
2447
|
-
|
|
2448
|
-
context "when the criteria is standard" do
|
|
2449
|
-
|
|
2453
|
+
context 'when provided criterion' do
|
|
2454
|
+
context 'when the criteria is standard' do
|
|
2450
2455
|
let(:criteria) do
|
|
2451
|
-
Band.where(name:
|
|
2456
|
+
Band.where(name: 'Depeche Mode')
|
|
2452
2457
|
end
|
|
2453
2458
|
|
|
2454
|
-
it
|
|
2459
|
+
it 'returns the matching documents' do
|
|
2455
2460
|
expect(criteria).to eq([ match ])
|
|
2456
2461
|
end
|
|
2457
2462
|
end
|
|
2458
2463
|
|
|
2459
|
-
context
|
|
2460
|
-
|
|
2464
|
+
context 'when the criteria is an exact fk array match' do
|
|
2461
2465
|
let(:id_one) do
|
|
2462
2466
|
BSON::ObjectId.new
|
|
2463
2467
|
end
|
|
@@ -2470,13 +2474,12 @@ describe Mongoid::Criteria do
|
|
|
2470
2474
|
Account.where(agent_ids: [ id_one, id_two ])
|
|
2471
2475
|
end
|
|
2472
2476
|
|
|
2473
|
-
it
|
|
2474
|
-
expect(criteria.selector).to eq({
|
|
2477
|
+
it 'does not wrap the array in another array' do
|
|
2478
|
+
expect(criteria.selector).to eq({ 'agent_ids' => [ id_one, id_two ] })
|
|
2475
2479
|
end
|
|
2476
2480
|
end
|
|
2477
2481
|
|
|
2478
|
-
context
|
|
2479
|
-
|
|
2482
|
+
context 'when querying on a big decimal' do
|
|
2480
2483
|
context 'when map_big_decimal_to_decimal128 is false' do
|
|
2481
2484
|
config_override :map_big_decimal_to_decimal128, false
|
|
2482
2485
|
|
|
@@ -2485,14 +2488,14 @@ describe Mongoid::Criteria do
|
|
|
2485
2488
|
end
|
|
2486
2489
|
|
|
2487
2490
|
let!(:band) do
|
|
2488
|
-
Band.create!(name:
|
|
2491
|
+
Band.create!(name: 'Boards of Canada', sales: sales)
|
|
2489
2492
|
end
|
|
2490
2493
|
|
|
2491
2494
|
let(:from_db) do
|
|
2492
2495
|
Band.where(sales: sales).first
|
|
2493
2496
|
end
|
|
2494
2497
|
|
|
2495
|
-
it
|
|
2498
|
+
it 'finds the document by the big decimal value' do
|
|
2496
2499
|
expect(from_db).to eq(band)
|
|
2497
2500
|
end
|
|
2498
2501
|
end
|
|
@@ -2505,14 +2508,14 @@ describe Mongoid::Criteria do
|
|
|
2505
2508
|
end
|
|
2506
2509
|
|
|
2507
2510
|
let!(:band) do
|
|
2508
|
-
Band.create!(name:
|
|
2511
|
+
Band.create!(name: 'Boards of Canada', sales: sales)
|
|
2509
2512
|
end
|
|
2510
2513
|
|
|
2511
2514
|
let(:from_db) do
|
|
2512
2515
|
Band.where(sales: sales).first
|
|
2513
2516
|
end
|
|
2514
2517
|
|
|
2515
|
-
it
|
|
2518
|
+
it 'finds the document by the big decimal value' do
|
|
2516
2519
|
expect(from_db).to eq(band)
|
|
2517
2520
|
end
|
|
2518
2521
|
end
|
|
@@ -2526,33 +2529,33 @@ describe Mongoid::Criteria do
|
|
|
2526
2529
|
|
|
2527
2530
|
let!(:band) do
|
|
2528
2531
|
Mongoid.map_big_decimal_to_decimal128 = false
|
|
2529
|
-
Band.create!(name:
|
|
2532
|
+
Band.create!(name: 'Boards of Canada', sales: sales)
|
|
2530
2533
|
end
|
|
2531
2534
|
|
|
2532
|
-
it
|
|
2535
|
+
it 'cannot find values when querying using a BigDecimal value' do
|
|
2533
2536
|
Mongoid.map_big_decimal_to_decimal128 = true
|
|
2534
2537
|
from_db = Band.where(sales: sales).first
|
|
2535
2538
|
expect(from_db).to eq(nil)
|
|
2536
2539
|
end
|
|
2537
2540
|
|
|
2538
|
-
it
|
|
2541
|
+
it 'cannot find values when querying using a string value' do
|
|
2539
2542
|
Mongoid.map_big_decimal_to_decimal128 = true
|
|
2540
2543
|
from_db = Band.where(sales: sales.to_s).first
|
|
2541
2544
|
expect(from_db).to eq(nil)
|
|
2542
2545
|
end
|
|
2543
2546
|
|
|
2544
|
-
context
|
|
2547
|
+
context 'after converting value' do
|
|
2545
2548
|
before do
|
|
2546
2549
|
Mongoid.map_big_decimal_to_decimal128 = true
|
|
2547
2550
|
band.set(sales: band.sales)
|
|
2548
2551
|
end
|
|
2549
2552
|
|
|
2550
|
-
it
|
|
2553
|
+
it 'can find values when querying using a BigDecimal value' do
|
|
2551
2554
|
from_db = Band.where(sales: sales).first
|
|
2552
2555
|
expect(from_db).to eq(band)
|
|
2553
2556
|
end
|
|
2554
2557
|
|
|
2555
|
-
it
|
|
2558
|
+
it 'can find values when querying using a string value' do
|
|
2556
2559
|
from_db = Band.where(sales: sales.to_s).first
|
|
2557
2560
|
expect(from_db).to eq(band)
|
|
2558
2561
|
end
|
|
@@ -2560,8 +2563,7 @@ describe Mongoid::Criteria do
|
|
|
2560
2563
|
end
|
|
2561
2564
|
end
|
|
2562
2565
|
|
|
2563
|
-
context
|
|
2564
|
-
|
|
2566
|
+
context 'when querying on a big decimal from a dynamic field' do
|
|
2565
2567
|
context 'when map_big_decimal_to_decimal128 is false' do
|
|
2566
2568
|
config_override :map_big_decimal_to_decimal128, false
|
|
2567
2569
|
|
|
@@ -2570,14 +2572,14 @@ describe Mongoid::Criteria do
|
|
|
2570
2572
|
end
|
|
2571
2573
|
|
|
2572
2574
|
let!(:band) do
|
|
2573
|
-
Band.create!(name:
|
|
2575
|
+
Band.create!(name: 'Boards of Canada', fans: fans)
|
|
2574
2576
|
end
|
|
2575
2577
|
|
|
2576
2578
|
let(:from_db) do
|
|
2577
2579
|
Band.where(fans: fans.to_s).first
|
|
2578
2580
|
end
|
|
2579
2581
|
|
|
2580
|
-
it
|
|
2582
|
+
it 'finds the document by the big decimal value' do
|
|
2581
2583
|
expect(from_db).to eq(band)
|
|
2582
2584
|
end
|
|
2583
2585
|
end
|
|
@@ -2590,40 +2592,38 @@ describe Mongoid::Criteria do
|
|
|
2590
2592
|
end
|
|
2591
2593
|
|
|
2592
2594
|
let!(:band) do
|
|
2593
|
-
Band.create!(name:
|
|
2595
|
+
Band.create!(name: 'Boards of Canada', fans: fans)
|
|
2594
2596
|
end
|
|
2595
2597
|
|
|
2596
2598
|
let(:from_db) do
|
|
2597
2599
|
Band.where(fans: fans).first
|
|
2598
2600
|
end
|
|
2599
2601
|
|
|
2600
|
-
it
|
|
2602
|
+
it 'only finds the document by the string value' do
|
|
2601
2603
|
expect(from_db).to eq(band)
|
|
2602
2604
|
end
|
|
2603
2605
|
end
|
|
2604
2606
|
end
|
|
2605
2607
|
|
|
2606
|
-
context
|
|
2607
|
-
|
|
2608
|
+
context 'when querying on a BSON::Decimal128' do
|
|
2608
2609
|
let(:decimal) do
|
|
2609
|
-
BSON::Decimal128.new(
|
|
2610
|
+
BSON::Decimal128.new('0.0005')
|
|
2610
2611
|
end
|
|
2611
2612
|
|
|
2612
2613
|
let!(:band) do
|
|
2613
|
-
Band.create!(name:
|
|
2614
|
+
Band.create!(name: 'Boards of Canada', decimal: decimal)
|
|
2614
2615
|
end
|
|
2615
2616
|
|
|
2616
2617
|
let(:from_db) do
|
|
2617
2618
|
Band.where(decimal: decimal).first
|
|
2618
2619
|
end
|
|
2619
2620
|
|
|
2620
|
-
it
|
|
2621
|
+
it 'finds the document by the big decimal value' do
|
|
2621
2622
|
expect(from_db).to eq(band)
|
|
2622
2623
|
end
|
|
2623
2624
|
end
|
|
2624
2625
|
|
|
2625
2626
|
context 'when querying on a polymorphic relation' do
|
|
2626
|
-
|
|
2627
2627
|
let(:movie) do
|
|
2628
2628
|
Movie.create!
|
|
2629
2629
|
end
|
|
@@ -2637,14 +2637,13 @@ describe Mongoid::Criteria do
|
|
|
2637
2637
|
end
|
|
2638
2638
|
end
|
|
2639
2639
|
|
|
2640
|
-
context
|
|
2641
|
-
|
|
2640
|
+
context 'when querying an embedded document with aliases' do
|
|
2642
2641
|
let(:criteria) do
|
|
2643
|
-
Person.where(
|
|
2642
|
+
Person.where('phones.extension' => '123')
|
|
2644
2643
|
end
|
|
2645
2644
|
|
|
2646
|
-
it
|
|
2647
|
-
expect(criteria.selector).to eq(
|
|
2645
|
+
it 'expands the aliases' do
|
|
2646
|
+
expect(criteria.selector).to eq('mobile_phones.ext' => '123')
|
|
2648
2647
|
end
|
|
2649
2648
|
end
|
|
2650
2649
|
end
|
|
@@ -2661,7 +2660,7 @@ describe Mongoid::Criteria do
|
|
|
2661
2660
|
let(:criteria) { Band.where(foo: 1).where(foo: 2) }
|
|
2662
2661
|
|
|
2663
2662
|
it 'combines criteria' do
|
|
2664
|
-
expect(criteria.selector).to eq('foo' => 1, '$and' => [{'foo' => 2}])
|
|
2663
|
+
expect(criteria.selector).to eq('foo' => 1, '$and' => [ { 'foo' => 2 } ])
|
|
2665
2664
|
end
|
|
2666
2665
|
end
|
|
2667
2666
|
|
|
@@ -2670,7 +2669,8 @@ describe Mongoid::Criteria do
|
|
|
2670
2669
|
|
|
2671
2670
|
it 'combines criteria' do
|
|
2672
2671
|
expect(criteria.selector).to eq(
|
|
2673
|
-
'foo' => 1, '$and' => [{'foo' => 2}], 'bar' => 3
|
|
2672
|
+
'foo' => 1, '$and' => [ { 'foo' => 2 } ], 'bar' => 3
|
|
2673
|
+
)
|
|
2674
2674
|
end
|
|
2675
2675
|
end
|
|
2676
2676
|
|
|
@@ -2679,11 +2679,12 @@ describe Mongoid::Criteria do
|
|
|
2679
2679
|
|
|
2680
2680
|
it 'combines criteria' do
|
|
2681
2681
|
expect(criteria.selector).to eq(
|
|
2682
|
-
'foo' => 1, '$and' => [{'foo' => 2}], 'bar' => 3
|
|
2682
|
+
'foo' => 1, '$and' => [ { 'foo' => 2 } ], 'bar' => 3
|
|
2683
|
+
)
|
|
2683
2684
|
end
|
|
2684
2685
|
end
|
|
2685
2686
|
|
|
2686
|
-
context
|
|
2687
|
+
context 'when duplicating where conditions' do
|
|
2687
2688
|
let(:criteria) { Sound.where(active: true).where(active: true) }
|
|
2688
2689
|
|
|
2689
2690
|
it 'does not duplicate criteria' do
|
|
@@ -2691,12 +2692,13 @@ describe Mongoid::Criteria do
|
|
|
2691
2692
|
end
|
|
2692
2693
|
end
|
|
2693
2694
|
|
|
2694
|
-
context
|
|
2695
|
+
context 'when duplicating where conditions with different values' do
|
|
2695
2696
|
let(:criteria) { Sound.where(active: true).where(active: false).where(active: true).where(active: false) }
|
|
2696
2697
|
|
|
2697
2698
|
it 'does not duplicate criteria' do
|
|
2698
2699
|
expect(criteria.selector).to eq(
|
|
2699
|
-
'active' => true, '$and' => [{'active' => false}]
|
|
2700
|
+
'active' => true, '$and' => [ { 'active' => false } ]
|
|
2701
|
+
)
|
|
2700
2702
|
end
|
|
2701
2703
|
end
|
|
2702
2704
|
|
|
@@ -2705,171 +2707,170 @@ describe Mongoid::Criteria do
|
|
|
2705
2707
|
# it is difficult to analyze the criteria used. For this reason, I have
|
|
2706
2708
|
# extracted the crux of the issue, adding an _id to the the criteria twice,
|
|
2707
2709
|
# and used that for the test case.
|
|
2708
|
-
context
|
|
2710
|
+
context 'when searching by _id twice' do
|
|
2709
2711
|
let(:_id) { BSON::ObjectId.new }
|
|
2710
2712
|
let(:criteria) { Band.where(_id: _id) }
|
|
2711
|
-
let(:dup_criteria) { criteria.where(_id: _id)}
|
|
2713
|
+
let(:dup_criteria) { criteria.where(_id: _id) }
|
|
2712
2714
|
|
|
2713
|
-
it
|
|
2714
|
-
expect(dup_criteria.selector).to eq({
|
|
2715
|
+
it 'does not duplicate the criteria' do
|
|
2716
|
+
expect(dup_criteria.selector).to eq({ '_id' => _id })
|
|
2715
2717
|
end
|
|
2716
2718
|
end
|
|
2717
2719
|
|
|
2718
|
-
context
|
|
2719
|
-
let(:criteria) { Band.where(
|
|
2720
|
+
context 'when querying an embedded field' do
|
|
2721
|
+
let(:criteria) { Band.where('label.name': 12_345) }
|
|
2720
2722
|
|
|
2721
|
-
it
|
|
2722
|
-
expect(criteria.selector).to eq(
|
|
2723
|
+
it 'mongoizes the embedded field in the selector' do
|
|
2724
|
+
expect(criteria.selector).to eq('label.name' => '12345')
|
|
2723
2725
|
end
|
|
2724
2726
|
end
|
|
2725
2727
|
|
|
2726
|
-
context
|
|
2727
|
-
|
|
2728
|
-
context "when querying an embeds_many association" do
|
|
2728
|
+
context 'when querying with a range' do
|
|
2729
|
+
context 'when querying an embeds_many association' do
|
|
2729
2730
|
let(:criteria) do
|
|
2730
|
-
Band.where(
|
|
2731
|
+
Band.where('labels' => 10..15)
|
|
2731
2732
|
end
|
|
2732
2733
|
|
|
2733
|
-
it
|
|
2734
|
+
it 'correctly uses elemMatch without an inner key' do
|
|
2734
2735
|
expect(criteria.selector).to eq(
|
|
2735
|
-
|
|
2736
|
-
|
|
2736
|
+
'labels' => {
|
|
2737
|
+
'$elemMatch' => { '$gte' => 10, '$lte' => 15 }
|
|
2737
2738
|
}
|
|
2738
2739
|
)
|
|
2739
2740
|
end
|
|
2740
2741
|
end
|
|
2741
2742
|
|
|
2742
|
-
context
|
|
2743
|
+
context 'when querying an element in an embeds_many association' do
|
|
2743
2744
|
let(:criteria) do
|
|
2744
|
-
Band.where(
|
|
2745
|
+
Band.where('labels.age' => 10..15)
|
|
2745
2746
|
end
|
|
2746
2747
|
|
|
2747
|
-
it
|
|
2748
|
+
it 'correctly uses elemMatch' do
|
|
2748
2749
|
expect(criteria.selector).to eq(
|
|
2749
|
-
|
|
2750
|
-
|
|
2751
|
-
|
|
2750
|
+
'labels' => {
|
|
2751
|
+
'$elemMatch' => {
|
|
2752
|
+
'age' => { '$gte' => 10, '$lte' => 15 }
|
|
2752
2753
|
}
|
|
2753
2754
|
}
|
|
2754
2755
|
)
|
|
2755
2756
|
end
|
|
2756
2757
|
end
|
|
2757
2758
|
|
|
2758
|
-
context
|
|
2759
|
+
context 'when querying a field of type array' do
|
|
2759
2760
|
let(:criteria) do
|
|
2760
|
-
Band.where(
|
|
2761
|
+
Band.where('genres' => 10..15)
|
|
2761
2762
|
end
|
|
2762
2763
|
|
|
2763
|
-
it
|
|
2764
|
+
it 'correctly uses elemMatch without an inner key' do
|
|
2764
2765
|
expect(criteria.selector).to eq(
|
|
2765
|
-
|
|
2766
|
-
|
|
2766
|
+
'genres' => {
|
|
2767
|
+
'$elemMatch' => { '$gte' => 10, '$lte' => 15 }
|
|
2767
2768
|
}
|
|
2768
2769
|
)
|
|
2769
2770
|
end
|
|
2770
2771
|
end
|
|
2771
2772
|
|
|
2772
|
-
context
|
|
2773
|
+
context 'when querying an aliased field of type array' do
|
|
2773
2774
|
let(:criteria) do
|
|
2774
|
-
Person.where(
|
|
2775
|
+
Person.where('array' => 10..15)
|
|
2775
2776
|
end
|
|
2776
2777
|
|
|
2777
|
-
it
|
|
2778
|
+
it 'correctly uses the aliased field and elemMatch' do
|
|
2778
2779
|
expect(criteria.selector).to eq(
|
|
2779
|
-
|
|
2780
|
-
|
|
2780
|
+
'a' => {
|
|
2781
|
+
'$elemMatch' => { '$gte' => 10, '$lte' => 15 }
|
|
2781
2782
|
}
|
|
2782
2783
|
)
|
|
2783
2784
|
end
|
|
2784
2785
|
end
|
|
2785
2786
|
|
|
2786
|
-
context
|
|
2787
|
+
context 'when querying a field inside an array' do
|
|
2787
2788
|
let(:criteria) do
|
|
2788
|
-
Band.where(
|
|
2789
|
+
Band.where('genres.age' => 10..15)
|
|
2789
2790
|
end
|
|
2790
2791
|
|
|
2791
|
-
it
|
|
2792
|
+
it 'correctly uses elemMatch' do
|
|
2792
2793
|
expect(criteria.selector).to eq(
|
|
2793
|
-
|
|
2794
|
-
|
|
2795
|
-
|
|
2794
|
+
'genres' => {
|
|
2795
|
+
'$elemMatch' => {
|
|
2796
|
+
'age' => { '$gte' => 10, '$lte' => 15 }
|
|
2796
2797
|
}
|
|
2797
2798
|
}
|
|
2798
2799
|
)
|
|
2799
2800
|
end
|
|
2800
2801
|
end
|
|
2801
2802
|
|
|
2802
|
-
context
|
|
2803
|
+
context 'when there are no embeds_manys or Arrays' do
|
|
2803
2804
|
let(:criteria) do
|
|
2804
|
-
Band.where(
|
|
2805
|
+
Band.where('fans.info.age' => 10..15)
|
|
2805
2806
|
end
|
|
2806
2807
|
|
|
2807
|
-
it
|
|
2808
|
+
it 'does not use elemMatch' do
|
|
2808
2809
|
expect(criteria.selector).to eq(
|
|
2809
|
-
|
|
2810
|
+
'fans.info.age' => { '$gte' => 10, '$lte' => 15 }
|
|
2810
2811
|
)
|
|
2811
2812
|
end
|
|
2812
2813
|
end
|
|
2813
2814
|
|
|
2814
|
-
context
|
|
2815
|
+
context 'when querying a nested element in an embeds_many association' do
|
|
2815
2816
|
let(:criteria) do
|
|
2816
|
-
Band.where(
|
|
2817
|
+
Band.where('labels.age.number' => 10..15)
|
|
2817
2818
|
end
|
|
2818
2819
|
|
|
2819
|
-
it
|
|
2820
|
+
it 'correctly uses elemMatch' do
|
|
2820
2821
|
expect(criteria.selector).to eq(
|
|
2821
|
-
|
|
2822
|
-
|
|
2823
|
-
|
|
2822
|
+
'labels' => {
|
|
2823
|
+
'$elemMatch' => {
|
|
2824
|
+
'age.number' => { '$gte' => 10, '$lte' => 15 }
|
|
2824
2825
|
}
|
|
2825
2826
|
}
|
|
2826
2827
|
)
|
|
2827
2828
|
end
|
|
2828
2829
|
end
|
|
2829
2830
|
|
|
2830
|
-
context
|
|
2831
|
+
context 'when querying a nested element in an Array' do
|
|
2831
2832
|
let(:criteria) do
|
|
2832
|
-
Band.where(
|
|
2833
|
+
Band.where('genres.name.length' => 10..15)
|
|
2833
2834
|
end
|
|
2834
2835
|
|
|
2835
|
-
it
|
|
2836
|
+
it 'correctly uses elemMatch' do
|
|
2836
2837
|
expect(criteria.selector).to eq(
|
|
2837
|
-
|
|
2838
|
-
|
|
2839
|
-
|
|
2838
|
+
'genres' => {
|
|
2839
|
+
'$elemMatch' => {
|
|
2840
|
+
'name.length' => { '$gte' => 10, '$lte' => 15 }
|
|
2840
2841
|
}
|
|
2841
2842
|
}
|
|
2842
2843
|
)
|
|
2843
2844
|
end
|
|
2844
2845
|
end
|
|
2845
2846
|
|
|
2846
|
-
context
|
|
2847
|
-
context
|
|
2847
|
+
context 'when querying a nested element in a nested embeds_many association' do
|
|
2848
|
+
context 'when the outer association is an embeds_many' do
|
|
2848
2849
|
let(:criteria) do
|
|
2849
|
-
Band.where(
|
|
2850
|
+
Band.where('records.tracks.name.length' => 10..15)
|
|
2850
2851
|
end
|
|
2851
2852
|
|
|
2852
|
-
it
|
|
2853
|
+
it 'correctly uses elemMatch' do
|
|
2853
2854
|
expect(criteria.selector).to eq(
|
|
2854
|
-
|
|
2855
|
-
|
|
2856
|
-
|
|
2855
|
+
'records.tracks' => {
|
|
2856
|
+
'$elemMatch' => {
|
|
2857
|
+
'name.length' => { '$gte' => 10, '$lte' => 15 }
|
|
2857
2858
|
}
|
|
2858
2859
|
}
|
|
2859
2860
|
)
|
|
2860
2861
|
end
|
|
2861
2862
|
end
|
|
2862
2863
|
|
|
2863
|
-
context
|
|
2864
|
+
context 'when the outer association is an embeds_one' do
|
|
2864
2865
|
let(:criteria) do
|
|
2865
|
-
Person.where(
|
|
2866
|
+
Person.where('name.translations.language.length' => 10..15)
|
|
2866
2867
|
end
|
|
2867
2868
|
|
|
2868
|
-
it
|
|
2869
|
+
it 'correctly uses elemMatch' do
|
|
2869
2870
|
expect(criteria.selector).to eq(
|
|
2870
|
-
|
|
2871
|
-
|
|
2872
|
-
|
|
2871
|
+
'name.translations' => {
|
|
2872
|
+
'$elemMatch' => {
|
|
2873
|
+
'language.length' => { '$gte' => 10, '$lte' => 15 }
|
|
2873
2874
|
}
|
|
2874
2875
|
}
|
|
2875
2876
|
)
|
|
@@ -2877,48 +2878,49 @@ describe Mongoid::Criteria do
|
|
|
2877
2878
|
end
|
|
2878
2879
|
end
|
|
2879
2880
|
|
|
2880
|
-
context
|
|
2881
|
+
context 'when querying a deeply nested array' do
|
|
2881
2882
|
let(:criteria) do
|
|
2882
|
-
Person.where(
|
|
2883
|
+
Person.where('addresses.code.deepest.array.element.item' => 10..15)
|
|
2883
2884
|
end
|
|
2884
2885
|
|
|
2885
|
-
it
|
|
2886
|
+
it 'correctly uses elemMatch' do
|
|
2886
2887
|
expect(criteria.selector).to eq(
|
|
2887
|
-
|
|
2888
|
-
|
|
2889
|
-
|
|
2888
|
+
'addresses.code.deepest.array' => {
|
|
2889
|
+
'$elemMatch' => {
|
|
2890
|
+
'element.item' => { '$gte' => 10, '$lte' => 15 }
|
|
2890
2891
|
}
|
|
2891
2892
|
}
|
|
2892
2893
|
)
|
|
2893
2894
|
end
|
|
2894
2895
|
end
|
|
2895
2896
|
|
|
2896
|
-
context
|
|
2897
|
+
context 'when there are multiple conditions' do
|
|
2897
2898
|
let(:criteria) do
|
|
2898
|
-
Band.where(
|
|
2899
|
+
Band.where('$or' => [ { 'labels.age' => 10..15 }, { labels: 8 } ])
|
|
2899
2900
|
end
|
|
2900
2901
|
|
|
2901
|
-
it
|
|
2902
|
-
expect(criteria.selector).to eq(
|
|
2903
|
-
|
|
2904
|
-
|
|
2905
|
-
|
|
2906
|
-
|
|
2907
|
-
|
|
2908
|
-
|
|
2902
|
+
it 'correctly combines the conditions' do
|
|
2903
|
+
expect(criteria.selector).to eq('$or' => [
|
|
2904
|
+
{ 'labels' => {
|
|
2905
|
+
'$elemMatch' => {
|
|
2906
|
+
'age' => { '$gte' => 10, '$lte' => 15 }
|
|
2907
|
+
}
|
|
2908
|
+
} },
|
|
2909
|
+
{ 'labels' => 8 }
|
|
2910
|
+
])
|
|
2909
2911
|
end
|
|
2910
2912
|
end
|
|
2911
2913
|
|
|
2912
|
-
context
|
|
2914
|
+
context 'when the association is aliased' do
|
|
2913
2915
|
let(:criteria) do
|
|
2914
|
-
Person.where(
|
|
2916
|
+
Person.where('passport.passport_pages.num_stamps' => 10..18)
|
|
2915
2917
|
end
|
|
2916
2918
|
|
|
2917
|
-
it
|
|
2919
|
+
it 'correctly uses the aliased association' do
|
|
2918
2920
|
expect(criteria.selector).to eq(
|
|
2919
|
-
|
|
2920
|
-
|
|
2921
|
-
|
|
2921
|
+
'pass.passport_pages' => {
|
|
2922
|
+
'$elemMatch' => {
|
|
2923
|
+
'num_stamps' => { '$gte' => 10, '$lte' => 18 }
|
|
2922
2924
|
}
|
|
2923
2925
|
}
|
|
2924
2926
|
)
|
|
@@ -2926,24 +2928,23 @@ describe Mongoid::Criteria do
|
|
|
2926
2928
|
end
|
|
2927
2929
|
end
|
|
2928
2930
|
|
|
2929
|
-
context
|
|
2930
|
-
let!(:person) { Person.create!(species: :hello)}
|
|
2931
|
+
context 'when searching for a regex on a symbol field' do
|
|
2932
|
+
let!(:person) { Person.create!(species: :hello) }
|
|
2931
2933
|
let(:criteria) { Person.where(species: /ell/) }
|
|
2932
2934
|
|
|
2933
|
-
it
|
|
2934
|
-
expect(criteria.selector).to eq({
|
|
2935
|
+
it 'creates the correct criteria' do
|
|
2936
|
+
expect(criteria.selector).to eq({ 'species' => /ell/ })
|
|
2935
2937
|
end
|
|
2936
2938
|
|
|
2937
|
-
it
|
|
2939
|
+
it 'finds the document' do
|
|
2938
2940
|
expect(criteria.first).to eq(person)
|
|
2939
2941
|
end
|
|
2940
2942
|
end
|
|
2941
2943
|
end
|
|
2942
2944
|
|
|
2943
|
-
describe
|
|
2944
|
-
|
|
2945
|
+
describe '#for_js' do
|
|
2945
2946
|
let!(:match) do
|
|
2946
|
-
Band.create!(name:
|
|
2947
|
+
Band.create!(name: 'Depeche Mode')
|
|
2947
2948
|
end
|
|
2948
2949
|
|
|
2949
2950
|
it 'is deprecated' do
|
|
@@ -2952,44 +2953,41 @@ describe Mongoid::Criteria do
|
|
|
2952
2953
|
Band.for_js("this.name == 'Depeche Mode'")
|
|
2953
2954
|
end
|
|
2954
2955
|
|
|
2955
|
-
context
|
|
2956
|
-
|
|
2956
|
+
context 'when the code has no scope' do
|
|
2957
2957
|
let(:criteria) do
|
|
2958
2958
|
Band.for_js("this.name == 'Depeche Mode'")
|
|
2959
2959
|
end
|
|
2960
2960
|
|
|
2961
|
-
it
|
|
2961
|
+
it 'returns the matching documents' do
|
|
2962
2962
|
expect(criteria).to eq([ match ])
|
|
2963
2963
|
end
|
|
2964
2964
|
end
|
|
2965
2965
|
|
|
2966
|
-
context
|
|
2966
|
+
context 'when the code has scope' do
|
|
2967
2967
|
max_server_version '4.2'
|
|
2968
2968
|
|
|
2969
2969
|
let(:criteria) do
|
|
2970
|
-
Band.for_js(
|
|
2970
|
+
Band.for_js('this.name == param', param: 'Depeche Mode')
|
|
2971
2971
|
end
|
|
2972
2972
|
|
|
2973
|
-
it
|
|
2973
|
+
it 'returns the matching documents' do
|
|
2974
2974
|
expect(criteria).to eq([ match ])
|
|
2975
2975
|
end
|
|
2976
2976
|
end
|
|
2977
2977
|
end
|
|
2978
2978
|
|
|
2979
|
-
describe
|
|
2980
|
-
|
|
2979
|
+
describe '#method_missing' do
|
|
2981
2980
|
let(:criteria) do
|
|
2982
2981
|
Person.all
|
|
2983
2982
|
end
|
|
2984
2983
|
|
|
2985
|
-
context
|
|
2986
|
-
|
|
2984
|
+
context 'when the method exists on the class' do
|
|
2987
2985
|
before do
|
|
2988
2986
|
expect(Person).to receive(:minor).and_call_original
|
|
2989
2987
|
expect(Person).to receive(:older_than).and_call_original
|
|
2990
2988
|
end
|
|
2991
2989
|
|
|
2992
|
-
it
|
|
2990
|
+
it 'calls the method on the class' do
|
|
2993
2991
|
expect(criteria.minor).to be_empty
|
|
2994
2992
|
expect do
|
|
2995
2993
|
criteria.older_than(age: 25)
|
|
@@ -2997,66 +2995,61 @@ describe Mongoid::Criteria do
|
|
|
2997
2995
|
end
|
|
2998
2996
|
end
|
|
2999
2997
|
|
|
3000
|
-
context
|
|
3001
|
-
|
|
2998
|
+
context 'when the method exists on the criteria' do
|
|
3002
2999
|
before do
|
|
3003
3000
|
expect(criteria).to receive(:only).and_call_original
|
|
3004
3001
|
end
|
|
3005
3002
|
|
|
3006
|
-
it
|
|
3003
|
+
it 'calls the method on the criteria' do
|
|
3007
3004
|
expect(criteria.only).to eq(criteria)
|
|
3008
3005
|
end
|
|
3009
3006
|
end
|
|
3010
3007
|
|
|
3011
|
-
context
|
|
3012
|
-
|
|
3008
|
+
context 'when the method exists on array' do
|
|
3013
3009
|
before do
|
|
3014
3010
|
expect(criteria).to receive(:entries).and_call_original
|
|
3015
3011
|
end
|
|
3016
3012
|
|
|
3017
|
-
it
|
|
3013
|
+
it 'calls the method on the criteria' do
|
|
3018
3014
|
expect(criteria.at(0)).to be_nil
|
|
3019
3015
|
end
|
|
3020
3016
|
end
|
|
3021
3017
|
|
|
3022
|
-
context
|
|
3023
|
-
|
|
3018
|
+
context 'when the method does not exist' do
|
|
3024
3019
|
before do
|
|
3025
|
-
expect(criteria).
|
|
3020
|
+
expect(criteria).not_to receive(:entries)
|
|
3026
3021
|
end
|
|
3027
3022
|
|
|
3028
|
-
it
|
|
3029
|
-
expect
|
|
3023
|
+
it 'raises an error' do
|
|
3024
|
+
expect do
|
|
3030
3025
|
criteria.to_hash
|
|
3031
|
-
|
|
3026
|
+
end.to raise_error(NoMethodError)
|
|
3032
3027
|
end
|
|
3033
3028
|
end
|
|
3034
3029
|
end
|
|
3035
3030
|
|
|
3036
|
-
describe
|
|
3037
|
-
|
|
3031
|
+
describe '#uniq' do
|
|
3038
3032
|
let!(:band_one) do
|
|
3039
|
-
Band.create!(name:
|
|
3033
|
+
Band.create!(name: 'New Order')
|
|
3040
3034
|
end
|
|
3041
3035
|
|
|
3042
3036
|
let!(:band_two) do
|
|
3043
|
-
Band.create!(name:
|
|
3037
|
+
Band.create!(name: 'New Order')
|
|
3044
3038
|
end
|
|
3045
3039
|
|
|
3046
3040
|
let(:criteria) do
|
|
3047
3041
|
Band.all
|
|
3048
3042
|
end
|
|
3049
3043
|
|
|
3050
|
-
it
|
|
3044
|
+
it 'passes the block through method_missing' do
|
|
3051
3045
|
expect(criteria.uniq(&:name)).to eq([ band_one ])
|
|
3052
3046
|
end
|
|
3053
3047
|
end
|
|
3054
3048
|
|
|
3055
|
-
describe
|
|
3056
|
-
|
|
3049
|
+
describe '#with' do
|
|
3057
3050
|
let!(:criteria_and_collection) do
|
|
3058
3051
|
collection = nil
|
|
3059
|
-
criteria = Band.where(name:
|
|
3052
|
+
criteria = Band.where(name: 'Depeche Mode').with(collection: 'artists') do |crit|
|
|
3060
3053
|
collection = crit.collection
|
|
3061
3054
|
crit
|
|
3062
3055
|
end
|
|
@@ -3071,19 +3064,17 @@ describe Mongoid::Criteria do
|
|
|
3071
3064
|
criteria_and_collection[1]
|
|
3072
3065
|
end
|
|
3073
3066
|
|
|
3074
|
-
it
|
|
3075
|
-
expect(criteria.selector).to eq(
|
|
3067
|
+
it 'retains the criteria selection' do
|
|
3068
|
+
expect(criteria.selector).to eq('name' => 'Depeche Mode')
|
|
3076
3069
|
end
|
|
3077
3070
|
|
|
3078
|
-
it
|
|
3079
|
-
expect(collection.name).to eq(
|
|
3071
|
+
it 'sets the persistence options' do
|
|
3072
|
+
expect(collection.name).to eq('artists')
|
|
3080
3073
|
end
|
|
3081
3074
|
end
|
|
3082
3075
|
|
|
3083
|
-
describe
|
|
3084
|
-
|
|
3085
|
-
context "when checking within a polygon" do
|
|
3086
|
-
|
|
3076
|
+
describe '#geo_spatial' do
|
|
3077
|
+
context 'when checking within a polygon' do
|
|
3087
3078
|
before do
|
|
3088
3079
|
Bar.create_indexes
|
|
3089
3080
|
end
|
|
@@ -3094,54 +3085,51 @@ describe Mongoid::Criteria do
|
|
|
3094
3085
|
|
|
3095
3086
|
let(:criteria) do
|
|
3096
3087
|
Bar.geo_spatial(
|
|
3097
|
-
:location.within_polygon => [[[ 50, 10 ], [ 50, 20 ], [ 60, 20 ], [ 60, 10 ], [ 50, 10 ]]]
|
|
3088
|
+
:location.within_polygon => [ [ [ 50, 10 ], [ 50, 20 ], [ 60, 20 ], [ 60, 10 ], [ 50, 10 ] ] ]
|
|
3098
3089
|
)
|
|
3099
3090
|
end
|
|
3100
3091
|
|
|
3101
|
-
it
|
|
3092
|
+
it 'returns the matching documents' do
|
|
3102
3093
|
expect(criteria).to eq([ match ])
|
|
3103
3094
|
end
|
|
3104
3095
|
end
|
|
3105
3096
|
end
|
|
3106
3097
|
|
|
3107
|
-
describe
|
|
3108
|
-
|
|
3098
|
+
describe '#with_size' do
|
|
3109
3099
|
let!(:match) do
|
|
3110
|
-
Band.create!(genres: [
|
|
3100
|
+
Band.create!(genres: %w[electro dub])
|
|
3111
3101
|
end
|
|
3112
3102
|
|
|
3113
3103
|
let!(:non_match) do
|
|
3114
|
-
Band.create!(genres: [
|
|
3104
|
+
Band.create!(genres: [ 'house' ])
|
|
3115
3105
|
end
|
|
3116
3106
|
|
|
3117
3107
|
let(:criteria) do
|
|
3118
3108
|
Band.with_size(genres: 2)
|
|
3119
3109
|
end
|
|
3120
3110
|
|
|
3121
|
-
it
|
|
3111
|
+
it 'returns the matching documents' do
|
|
3122
3112
|
expect(criteria).to eq([ match ])
|
|
3123
3113
|
end
|
|
3124
3114
|
end
|
|
3125
3115
|
|
|
3126
|
-
describe
|
|
3127
|
-
|
|
3116
|
+
describe '#with_type' do
|
|
3128
3117
|
let!(:match) do
|
|
3129
|
-
Band.create!(name:
|
|
3118
|
+
Band.create!(name: 'Depeche Mode')
|
|
3130
3119
|
end
|
|
3131
3120
|
|
|
3132
3121
|
let(:criteria) do
|
|
3133
3122
|
Band.with_type(name: 2)
|
|
3134
3123
|
end
|
|
3135
3124
|
|
|
3136
|
-
it
|
|
3125
|
+
it 'returns the matching documents' do
|
|
3137
3126
|
expect(criteria).to eq([ match ])
|
|
3138
3127
|
end
|
|
3139
3128
|
end
|
|
3140
3129
|
|
|
3141
|
-
describe
|
|
3142
|
-
context
|
|
3143
|
-
context
|
|
3144
|
-
|
|
3130
|
+
describe '#type_selection' do
|
|
3131
|
+
context 'when using the default discriminator_key' do
|
|
3132
|
+
context 'when only one subclass exists' do
|
|
3145
3133
|
let(:criteria) do
|
|
3146
3134
|
described_class.new(Firefox)
|
|
3147
3135
|
end
|
|
@@ -3150,13 +3138,12 @@ describe Mongoid::Criteria do
|
|
|
3150
3138
|
criteria.send(:type_selection)
|
|
3151
3139
|
end
|
|
3152
3140
|
|
|
3153
|
-
it
|
|
3154
|
-
expect(selection).to eq({ _type:
|
|
3141
|
+
it 'does not use an $in query' do
|
|
3142
|
+
expect(selection).to eq({ _type: 'Firefox' })
|
|
3155
3143
|
end
|
|
3156
3144
|
end
|
|
3157
3145
|
|
|
3158
|
-
context
|
|
3159
|
-
|
|
3146
|
+
context 'when more than one subclass exists' do
|
|
3160
3147
|
let(:criteria) do
|
|
3161
3148
|
described_class.new(Browser)
|
|
3162
3149
|
end
|
|
@@ -3165,23 +3152,22 @@ describe Mongoid::Criteria do
|
|
|
3165
3152
|
criteria.send(:type_selection)
|
|
3166
3153
|
end
|
|
3167
3154
|
|
|
3168
|
-
it
|
|
3169
|
-
expect(selection).to eq({ _type: {
|
|
3155
|
+
it 'does not use an $in query' do
|
|
3156
|
+
expect(selection).to eq({ _type: { '$in' => %w[Firefox Browser] } })
|
|
3170
3157
|
end
|
|
3171
3158
|
end
|
|
3172
3159
|
end
|
|
3173
3160
|
|
|
3174
|
-
context
|
|
3161
|
+
context 'when using a custom discriminator_key' do
|
|
3175
3162
|
before do
|
|
3176
|
-
Canvas.discriminator_key =
|
|
3163
|
+
Canvas.discriminator_key = 'dkey'
|
|
3177
3164
|
end
|
|
3178
3165
|
|
|
3179
3166
|
after do
|
|
3180
3167
|
Canvas.discriminator_key = nil
|
|
3181
3168
|
end
|
|
3182
3169
|
|
|
3183
|
-
context
|
|
3184
|
-
|
|
3170
|
+
context 'when only one subclass exists' do
|
|
3185
3171
|
let(:criteria) do
|
|
3186
3172
|
described_class.new(Firefox)
|
|
3187
3173
|
end
|
|
@@ -3190,13 +3176,12 @@ describe Mongoid::Criteria do
|
|
|
3190
3176
|
criteria.send(:type_selection)
|
|
3191
3177
|
end
|
|
3192
3178
|
|
|
3193
|
-
it
|
|
3194
|
-
expect(selection).to eq({ dkey:
|
|
3179
|
+
it 'does not use an $in query' do
|
|
3180
|
+
expect(selection).to eq({ dkey: 'Firefox' })
|
|
3195
3181
|
end
|
|
3196
3182
|
end
|
|
3197
3183
|
|
|
3198
|
-
context
|
|
3199
|
-
|
|
3184
|
+
context 'when more than one subclass exists' do
|
|
3200
3185
|
let(:criteria) do
|
|
3201
3186
|
described_class.new(Browser)
|
|
3202
3187
|
end
|
|
@@ -3205,8 +3190,8 @@ describe Mongoid::Criteria do
|
|
|
3205
3190
|
criteria.send(:type_selection)
|
|
3206
3191
|
end
|
|
3207
3192
|
|
|
3208
|
-
it
|
|
3209
|
-
expect(selection).to eq({ dkey: {
|
|
3193
|
+
it 'does not use an $in query' do
|
|
3194
|
+
expect(selection).to eq({ dkey: { '$in' => %w[Firefox Browser] } })
|
|
3210
3195
|
end
|
|
3211
3196
|
end
|
|
3212
3197
|
end
|
|
@@ -3276,13 +3261,13 @@ describe Mongoid::Criteria do
|
|
|
3276
3261
|
{
|
|
3277
3262
|
klass: Band,
|
|
3278
3263
|
gt: { members: 2 },
|
|
3279
|
-
in: { genre: [
|
|
3264
|
+
in: { genre: %w[rock metal] }
|
|
3280
3265
|
}
|
|
3281
3266
|
end
|
|
3282
3267
|
|
|
3283
3268
|
it 'applies selector methods' do
|
|
3284
3269
|
expect(criteria.selector['members']).to eq({ '$gt' => 2 })
|
|
3285
|
-
expect(criteria.selector['genre']).to eq({ '$in' => [
|
|
3270
|
+
expect(criteria.selector['genre']).to eq({ '$in' => %w[rock metal] })
|
|
3286
3271
|
end
|
|
3287
3272
|
end
|
|
3288
3273
|
|
|
@@ -3313,7 +3298,7 @@ describe Mongoid::Criteria do
|
|
|
3313
3298
|
|
|
3314
3299
|
context 'when attempting to call create!' do
|
|
3315
3300
|
let(:hash) do
|
|
3316
|
-
{ klass: Band,
|
|
3301
|
+
{ klass: Band, create!: { name: 'Malicious' } }
|
|
3317
3302
|
end
|
|
3318
3303
|
|
|
3319
3304
|
it 'raises ArgumentError' do
|
|
@@ -3343,7 +3328,7 @@ describe Mongoid::Criteria do
|
|
|
3343
3328
|
|
|
3344
3329
|
context 'when attempting to call execute_or_raise' do
|
|
3345
3330
|
let(:hash) do
|
|
3346
|
-
{ klass: Band, execute_or_raise: [
|
|
3331
|
+
{ klass: Band, execute_or_raise: %w[id1 id2] }
|
|
3347
3332
|
end
|
|
3348
3333
|
|
|
3349
3334
|
it 'raises ArgumentError' do
|
|
@@ -3429,20 +3414,20 @@ describe Mongoid::Criteria do
|
|
|
3429
3414
|
it 'allows all whitelisted methods' do
|
|
3430
3415
|
# Sample of allowed methods from each category
|
|
3431
3416
|
allowed_sample = {
|
|
3432
|
-
where: { name: 'Test' },
|
|
3417
|
+
where: { name: 'Test' }, # Query selector
|
|
3433
3418
|
limit: 10, # Query option
|
|
3434
3419
|
skip: 5, # Query option
|
|
3435
|
-
gt: { age: 18 },
|
|
3436
|
-
in: { status: ['active'] },
|
|
3437
|
-
ascending: :name,
|
|
3438
|
-
includes: :notes,
|
|
3439
|
-
merge: { klass: Band }
|
|
3420
|
+
gt: { age: 18 }, # Query selector
|
|
3421
|
+
in: { status: [ 'active' ] }, # Query selector
|
|
3422
|
+
ascending: :name, # Sorting
|
|
3423
|
+
includes: :notes, # Eager loading
|
|
3424
|
+
merge: { klass: Band } # Merge
|
|
3440
3425
|
}
|
|
3441
3426
|
|
|
3442
3427
|
allowed_sample.each do |method, args|
|
|
3443
3428
|
hash = { klass: Band, method => args }
|
|
3444
|
-
expect { described_class.from_hash(hash) }.not_to raise_error
|
|
3445
|
-
"Expected method '#{method}' to be allowed but it was blocked"
|
|
3429
|
+
expect { described_class.from_hash(hash) }.not_to raise_error
|
|
3430
|
+
# "Expected method '#{method}' to be allowed but it was blocked"
|
|
3446
3431
|
end
|
|
3447
3432
|
end
|
|
3448
3433
|
end
|