mongoid 3.1.6 → 4.0.2
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 +479 -8
- data/README.md +10 -7
- data/lib/config/locales/en.yml +34 -20
- data/lib/mongoid.rb +10 -76
- data/lib/mongoid/atomic.rb +3 -14
- data/lib/mongoid/atomic/paths/embedded.rb +0 -30
- data/lib/mongoid/atomic/paths/embedded/many.rb +1 -1
- data/lib/mongoid/atomic/paths/embedded/one.rb +1 -1
- data/lib/mongoid/atomic/paths/root.rb +0 -13
- data/lib/mongoid/attributes.rb +65 -101
- data/lib/mongoid/attributes/dynamic.rb +154 -0
- data/lib/mongoid/attributes/nested.rb +82 -0
- data/lib/mongoid/attributes/processing.rb +13 -66
- data/lib/mongoid/{dirty.rb → changeable.rb} +32 -2
- data/lib/mongoid/composable.rb +105 -0
- data/lib/mongoid/config.rb +3 -9
- data/lib/mongoid/config/options.rb +1 -1
- data/lib/mongoid/contextual.rb +2 -0
- data/lib/mongoid/contextual/aggregable/memory.rb +2 -2
- data/lib/mongoid/contextual/aggregable/mongo.rb +7 -9
- data/lib/mongoid/contextual/atomic.rb +53 -53
- data/lib/mongoid/contextual/geo_near.rb +1 -1
- data/lib/mongoid/contextual/map_reduce.rb +4 -2
- data/lib/mongoid/contextual/memory.rb +23 -11
- data/lib/mongoid/contextual/mongo.rb +75 -57
- data/lib/mongoid/contextual/none.rb +90 -0
- data/lib/mongoid/contextual/text_search.rb +178 -0
- data/lib/mongoid/copyable.rb +2 -3
- data/lib/mongoid/criteria.rb +39 -34
- data/lib/mongoid/criteria/#findable.rb# +141 -0
- data/lib/mongoid/{criterion → criteria}/findable.rb +7 -47
- data/lib/mongoid/{criterion/inspection.rb → criteria/inspectable.rb} +2 -2
- data/lib/mongoid/{criterion → criteria}/marshalable.rb +3 -1
- data/lib/mongoid/{criterion → criteria}/modifiable.rb +28 -8
- data/lib/mongoid/criteria/permission.rb +70 -0
- data/lib/mongoid/{criterion/scoping.rb → criteria/scopable.rb} +2 -2
- data/lib/mongoid/document.rb +39 -24
- data/lib/mongoid/errors.rb +4 -1
- data/lib/mongoid/errors/document_not_destroyed.rb +25 -0
- data/lib/mongoid/errors/document_not_found.rb +2 -1
- data/lib/mongoid/errors/invalid_storage_options.rb +1 -1
- data/lib/mongoid/errors/invalid_storage_parent.rb +26 -0
- data/lib/mongoid/errors/invalid_value.rb +16 -0
- data/lib/mongoid/errors/mongoid_error.rb +1 -1
- data/lib/mongoid/errors/readonly_document.rb +24 -0
- data/lib/mongoid/extensions.rb +15 -7
- data/lib/mongoid/extensions/array.rb +3 -3
- data/lib/mongoid/extensions/big_decimal.rb +1 -1
- data/lib/mongoid/extensions/boolean.rb +15 -17
- data/lib/mongoid/extensions/date.rb +1 -1
- data/lib/mongoid/extensions/date_time.rb +3 -3
- data/lib/mongoid/extensions/float.rb +2 -1
- data/lib/mongoid/extensions/hash.rb +3 -3
- data/lib/mongoid/extensions/integer.rb +1 -1
- data/lib/mongoid/extensions/object.rb +7 -7
- data/lib/mongoid/extensions/object_id.rb +5 -5
- data/lib/mongoid/extensions/range.rb +9 -3
- data/lib/mongoid/extensions/regexp.rb +1 -1
- data/lib/mongoid/extensions/set.rb +1 -1
- data/lib/mongoid/extensions/string.rb +6 -18
- data/lib/mongoid/extensions/symbol.rb +1 -1
- data/lib/mongoid/extensions/time.rb +3 -3
- data/lib/mongoid/extensions/time_with_zone.rb +1 -1
- data/lib/mongoid/factory.rb +8 -6
- data/lib/mongoid/fields.rb +41 -8
- data/lib/mongoid/fields/foreign_key.rb +3 -3
- data/lib/mongoid/fields/localized.rb +1 -1
- data/lib/mongoid/fields/standard.rb +5 -17
- data/lib/mongoid/fields/validators/macro.rb +15 -5
- data/lib/mongoid/{finders.rb → findable.rb} +35 -7
- data/lib/mongoid/{indexes.rb → indexable.rb} +50 -55
- data/lib/mongoid/indexable/specification.rb +104 -0
- data/lib/mongoid/{indexes → indexable}/validators/options.rb +2 -1
- data/lib/mongoid/{inspection.rb → inspectable.rb} +15 -11
- data/lib/mongoid/{callbacks.rb → interceptable.rb} +30 -62
- data/lib/mongoid/log_subscriber.rb +55 -0
- data/lib/mongoid/matchable.rb +152 -0
- data/lib/mongoid/{matchers → matchable}/all.rb +1 -1
- data/lib/mongoid/{matchers → matchable}/and.rb +2 -2
- data/lib/mongoid/{matchers → matchable}/default.rb +1 -1
- data/lib/mongoid/{matchers → matchable}/exists.rb +1 -1
- data/lib/mongoid/{matchers → matchable}/gt.rb +1 -1
- data/lib/mongoid/{matchers → matchable}/gte.rb +1 -1
- data/lib/mongoid/{matchers → matchable}/in.rb +2 -2
- data/lib/mongoid/{matchers → matchable}/lt.rb +1 -1
- data/lib/mongoid/{matchers → matchable}/lte.rb +1 -1
- data/lib/mongoid/{matchers → matchable}/ne.rb +2 -2
- data/lib/mongoid/{matchers → matchable}/nin.rb +3 -2
- data/lib/mongoid/{matchers → matchable}/or.rb +2 -2
- data/lib/mongoid/{matchers → matchable}/size.rb +1 -1
- data/lib/mongoid/persistable.rb +216 -0
- data/lib/mongoid/persistable/creatable.rb +189 -0
- data/lib/mongoid/persistable/deletable.rb +149 -0
- data/lib/mongoid/persistable/destroyable.rb +60 -0
- data/lib/mongoid/persistable/incrementable.rb +36 -0
- data/lib/mongoid/persistable/logical.rb +38 -0
- data/lib/mongoid/persistable/poppable.rb +39 -0
- data/lib/mongoid/persistable/pullable.rb +55 -0
- data/lib/mongoid/persistable/pushable.rb +62 -0
- data/lib/mongoid/persistable/renamable.rb +35 -0
- data/lib/mongoid/persistable/savable.rb +52 -0
- data/lib/mongoid/persistable/settable.rb +33 -0
- data/lib/mongoid/persistable/unsettable.rb +36 -0
- data/lib/mongoid/persistable/updatable.rb +153 -0
- data/lib/mongoid/persistable/upsertable.rb +55 -0
- data/lib/mongoid/positional.rb +71 -0
- data/lib/mongoid/query_cache.rb +255 -0
- data/lib/mongoid/railtie.rb +34 -60
- data/lib/mongoid/railties/database.rake +9 -25
- data/lib/mongoid/relations.rb +11 -25
- data/lib/mongoid/relations/accessors.rb +15 -51
- data/lib/mongoid/relations/auto_save.rb +15 -36
- data/lib/mongoid/relations/binding.rb +2 -25
- data/lib/mongoid/relations/bindings/embedded/in.rb +1 -1
- data/lib/mongoid/relations/bindings/embedded/many.rb +1 -3
- data/lib/mongoid/relations/bindings/referenced/in.rb +3 -8
- data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +4 -2
- data/lib/mongoid/relations/builder.rb +1 -1
- data/lib/mongoid/relations/builders.rb +2 -2
- data/lib/mongoid/relations/builders/embedded/one.rb +1 -1
- data/lib/mongoid/relations/builders/nested_attributes/many.rb +10 -13
- data/lib/mongoid/relations/builders/nested_attributes/one.rb +5 -6
- data/lib/mongoid/relations/builders/referenced/in.rb +1 -1
- data/lib/mongoid/relations/builders/referenced/many.rb +1 -2
- data/lib/mongoid/relations/builders/referenced/many_to_many.rb +1 -2
- data/lib/mongoid/relations/builders/referenced/one.rb +1 -1
- data/lib/mongoid/relations/cascading.rb +3 -5
- data/lib/mongoid/relations/constraint.rb +1 -1
- data/lib/mongoid/relations/conversions.rb +1 -1
- data/lib/mongoid/relations/counter_cache.rb +39 -15
- data/lib/mongoid/relations/eager.rb +46 -0
- data/lib/mongoid/relations/eager/base.rb +149 -0
- data/lib/mongoid/relations/eager/belongs_to.rb +31 -0
- data/lib/mongoid/relations/eager/has_and_belongs_to_many.rb +47 -0
- data/lib/mongoid/relations/eager/has_many.rb +38 -0
- data/lib/mongoid/relations/eager/has_one.rb +30 -0
- data/lib/mongoid/relations/embedded/batchable.rb +4 -5
- data/lib/mongoid/relations/embedded/in.rb +4 -4
- data/lib/mongoid/relations/embedded/many.rb +14 -32
- data/lib/mongoid/relations/embedded/one.rb +1 -1
- data/lib/mongoid/relations/macros.rb +3 -4
- data/lib/mongoid/relations/many.rb +51 -31
- data/lib/mongoid/relations/marshalable.rb +3 -3
- data/lib/mongoid/relations/metadata.rb +14 -79
- data/lib/mongoid/relations/nested_builder.rb +2 -2
- data/lib/mongoid/relations/one.rb +1 -1
- data/lib/mongoid/relations/options.rb +1 -0
- data/lib/mongoid/relations/polymorphic.rb +0 -1
- data/lib/mongoid/relations/proxy.rb +21 -41
- data/lib/mongoid/relations/referenced/in.rb +4 -20
- data/lib/mongoid/relations/referenced/many.rb +22 -35
- data/lib/mongoid/relations/referenced/many_to_many.rb +19 -42
- data/lib/mongoid/relations/referenced/one.rb +6 -25
- data/lib/mongoid/relations/synchronization.rb +4 -4
- data/lib/mongoid/relations/targets/enumerable.rb +10 -10
- data/lib/mongoid/relations/touchable.rb +34 -1
- data/lib/mongoid/{reloading.rb → reloadable.rb} +7 -5
- data/lib/mongoid/{scoping.rb → scopable.rb} +36 -36
- data/lib/mongoid/selectable.rb +59 -0
- data/lib/mongoid/{serialization.rb → serializable.rb} +11 -2
- data/lib/mongoid/sessions.rb +37 -345
- data/lib/mongoid/sessions/factory.rb +2 -0
- data/lib/mongoid/sessions/options.rb +185 -0
- data/lib/mongoid/sessions/storage_options.rb +140 -0
- data/lib/mongoid/sessions/thread_options.rb +19 -0
- data/lib/mongoid/sessions/validators/storage.rb +16 -3
- data/lib/mongoid/{sharding.rb → shardable.rb} +5 -1
- data/lib/mongoid/{state.rb → stateful.rb} +13 -1
- data/lib/mongoid/support/query_counter.rb +23 -0
- data/lib/mongoid/tasks/database.rake +31 -0
- data/lib/mongoid/tasks/database.rb +107 -0
- data/lib/mongoid/threaded.rb +26 -172
- data/lib/mongoid/threaded/lifecycle.rb +0 -28
- data/lib/mongoid/timestamps/updated/short.rb +2 -2
- data/lib/mongoid/{hierarchy.rb → traversable.rb} +15 -7
- data/lib/mongoid/{validations.rb → validatable.rb} +23 -10
- data/lib/mongoid/{validations → validatable}/associated.rb +4 -2
- data/lib/mongoid/{validations → validatable}/format.rb +1 -1
- data/lib/mongoid/{validations → validatable}/length.rb +1 -1
- data/lib/mongoid/{validations → validatable}/localizable.rb +1 -1
- data/lib/mongoid/{validations → validatable}/macros.rb +4 -3
- data/lib/mongoid/{validations → validatable}/presence.rb +1 -1
- data/lib/mongoid/{validations → validatable}/queryable.rb +1 -2
- data/lib/mongoid/{validations → validatable}/uniqueness.rb +25 -21
- data/lib/mongoid/version.rb +1 -1
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +46 -25
- data/lib/rails/generators/mongoid/model/model_generator.rb +0 -1
- data/lib/rails/generators/mongoid/model/templates/model.rb.tt +0 -3
- data/lib/rails/generators/mongoid_generator.rb +3 -40
- data/lib/rails/mongoid.rb +0 -122
- data/lib/support/ruby_version.rb +1 -1
- data/spec/app/models/account.rb +2 -6
- data/spec/app/models/acolyte.rb +2 -1
- data/spec/app/models/actor.rb +0 -1
- data/spec/app/models/address.rb +8 -3
- data/spec/app/models/animal.rb +1 -1
- data/spec/app/models/appointment.rb +3 -3
- data/spec/app/models/article.rb +1 -4
- data/spec/app/models/audio.rb +5 -0
- data/spec/app/models/author.rb +0 -2
- data/spec/app/models/band.rb +3 -2
- data/spec/app/models/bar.rb +1 -0
- data/spec/app/models/book.rb +1 -0
- data/spec/app/models/building.rb +0 -2
- data/spec/app/models/building_address.rb +0 -2
- data/spec/app/models/bus.rb +1 -1
- data/spec/app/models/canvas.rb +1 -1
- data/spec/app/models/contextable_item.rb +5 -0
- data/spec/app/models/contractor.rb +0 -2
- data/spec/app/models/country_code.rb +1 -1
- data/spec/app/models/definition.rb +2 -2
- data/spec/app/models/dog.rb +1 -1
- data/spec/app/models/draft.rb +9 -0
- data/spec/app/models/dragon.rb +4 -0
- data/spec/app/models/drug.rb +1 -3
- data/spec/app/models/dungeon.rb +4 -0
- data/spec/app/models/edit.rb +5 -0
- data/spec/app/models/even.rb +7 -0
- data/spec/app/models/event.rb +1 -1
- data/spec/app/models/filesystem.rb +1 -0
- data/spec/app/models/fish.rb +0 -1
- data/spec/app/models/fruits.rb +6 -0
- data/spec/app/models/game.rb +0 -2
- data/spec/app/models/house.rb +1 -3
- data/spec/app/models/id_key.rb +6 -0
- data/spec/app/models/item.rb +1 -5
- data/spec/app/models/jar.rb +1 -1
- data/spec/app/models/label.rb +4 -4
- data/spec/app/models/line_item.rb +1 -1
- data/spec/app/models/login.rb +1 -1
- data/spec/app/models/message.rb +8 -0
- data/spec/app/models/movie.rb +1 -0
- data/spec/app/models/name.rb +3 -3
- data/spec/app/models/note.rb +3 -1
- data/spec/app/models/odd.rb +7 -0
- data/spec/app/models/oscar.rb +1 -1
- data/spec/app/models/parent_doc.rb +1 -1
- data/spec/app/models/person.rb +16 -10
- data/spec/app/models/phone.rb +1 -3
- data/spec/app/models/player.rb +3 -3
- data/spec/app/models/post.rb +5 -3
- data/spec/app/models/preference.rb +1 -1
- data/spec/app/models/quiz.rb +0 -3
- data/spec/app/models/record.rb +10 -5
- data/spec/app/models/registry.rb +1 -1
- data/spec/app/models/server.rb +1 -1
- data/spec/app/models/service.rb +3 -3
- data/spec/app/models/sound.rb +5 -0
- data/spec/app/models/symptom.rb +1 -1
- data/spec/app/models/template.rb +1 -1
- data/spec/app/models/title.rb +0 -1
- data/spec/app/models/track.rb +5 -5
- data/spec/app/models/tree.rb +3 -3
- data/spec/app/models/video.rb +1 -5
- data/spec/app/models/wiki_page.rb +2 -5
- data/spec/app/models/word.rb +3 -0
- data/spec/app/models/word_origin.rb +1 -1
- data/spec/config/mongoid.yml +7 -8
- data/spec/helpers.rb +18 -0
- data/spec/mongoid/#atomic_spec.rb# +365 -0
- data/spec/mongoid/atomic/modifiers_spec.rb +22 -22
- data/spec/mongoid/atomic/paths/embedded/many_spec.rb +8 -49
- data/spec/mongoid/atomic/paths/embedded/one_spec.rb +7 -49
- data/spec/mongoid/atomic/paths/root_spec.rb +3 -61
- data/spec/mongoid/atomic/paths_spec.rb +19 -19
- data/spec/mongoid/atomic_spec.rb +19 -19
- data/spec/mongoid/{nested_attributes_spec.rb → attributes/nested_spec.rb} +474 -592
- data/spec/mongoid/attributes/readonly_spec.rb +58 -18
- data/spec/mongoid/attributes_spec.rb +349 -434
- data/spec/mongoid/{dirty_spec.rb → changeable_spec.rb} +203 -153
- data/spec/mongoid/{components_spec.rb → composable_spec.rb} +3 -3
- data/spec/mongoid/config/environment_spec.rb +3 -3
- data/spec/mongoid/config/options_spec.rb +6 -6
- data/spec/mongoid/config_spec.rb +24 -48
- data/spec/mongoid/contextual/aggregable/memory_spec.rb +12 -12
- data/spec/mongoid/contextual/aggregable/mongo_spec.rb +78 -38
- data/spec/mongoid/contextual/atomic_spec.rb +60 -56
- data/spec/mongoid/contextual/find_and_modify_spec.rb +14 -14
- data/spec/mongoid/contextual/geo_near_spec.rb +22 -22
- data/spec/mongoid/contextual/map_reduce_spec.rb +27 -27
- data/spec/mongoid/contextual/memory_spec.rb +156 -94
- data/spec/mongoid/contextual/mongo_spec.rb +238 -150
- data/spec/mongoid/contextual/none_spec.rb +127 -0
- data/spec/mongoid/contextual/text_search_spec.rb +209 -0
- data/spec/mongoid/copyable_spec.rb +56 -68
- data/spec/mongoid/criteria/findable_spec.rb +991 -0
- data/spec/mongoid/{criterion/inspection_spec.rb → criteria/inspectable_spec.rb} +5 -5
- data/spec/mongoid/{criterion → criteria}/marshalable_spec.rb +2 -2
- data/spec/mongoid/criteria/modifiable_spec.rb +1252 -0
- data/spec/mongoid/{criterion/scoping_spec.rb → criteria/scopable_spec.rb} +37 -37
- data/spec/mongoid/criteria_spec.rb +715 -2421
- data/spec/mongoid/document_spec.rb +171 -121
- data/spec/mongoid/equality_spec.rb +22 -22
- data/spec/mongoid/errors/ambiguous_relationship_spec.rb +3 -3
- data/spec/mongoid/errors/callback_spec.rb +3 -3
- data/spec/mongoid/errors/delete_restriction_spec.rb +3 -3
- data/spec/mongoid/errors/document_not_destroyed_spec.rb +33 -0
- data/spec/mongoid/errors/document_not_found_spec.rb +11 -11
- data/spec/mongoid/errors/eager_load_spec.rb +3 -3
- data/spec/mongoid/errors/invalid_collection_spec.rb +3 -3
- data/spec/mongoid/errors/invalid_config_option_spec.rb +3 -3
- data/spec/mongoid/errors/invalid_field_option_spec.rb +3 -3
- data/spec/mongoid/errors/invalid_field_spec.rb +3 -3
- data/spec/mongoid/errors/invalid_find_spec.rb +3 -3
- data/spec/mongoid/errors/invalid_includes_spec.rb +3 -3
- data/spec/mongoid/errors/invalid_index_spec.rb +3 -3
- data/spec/mongoid/errors/invalid_options_spec.rb +3 -3
- data/spec/mongoid/errors/invalid_path_spec.rb +3 -3
- data/spec/mongoid/errors/invalid_scope_spec.rb +5 -5
- data/spec/mongoid/errors/invalid_set_polymorphic_relation_spec.rb +3 -3
- data/spec/mongoid/errors/invalid_storage_options_spec.rb +3 -3
- data/spec/mongoid/errors/invalid_time_spec.rb +3 -3
- data/spec/mongoid/errors/inverse_not_found_spec.rb +3 -3
- data/spec/mongoid/errors/mixed_relations_spec.rb +3 -3
- data/spec/mongoid/errors/mixed_session_configuration_spec.rb +3 -3
- data/spec/mongoid/errors/mongoid_error_spec.rb +8 -8
- data/spec/mongoid/errors/nested_attributes_metadata_not_found_spec.rb +3 -3
- data/spec/mongoid/errors/no_environment_spec.rb +3 -3
- data/spec/mongoid/errors/no_map_reduce_output_spec.rb +3 -3
- data/spec/mongoid/errors/no_metadata_spec.rb +3 -3
- data/spec/mongoid/errors/no_parent_spec.rb +3 -3
- data/spec/mongoid/errors/no_session_config_spec.rb +3 -3
- data/spec/mongoid/errors/no_session_database_spec.rb +3 -3
- data/spec/mongoid/errors/no_session_hosts_spec.rb +3 -3
- data/spec/mongoid/errors/no_sessions_config_spec.rb +3 -3
- data/spec/mongoid/errors/readonly_attribute_spec.rb +3 -3
- data/spec/mongoid/errors/readonly_document_spec.rb +29 -0
- data/spec/mongoid/errors/scope_overwrite_spec.rb +3 -3
- data/spec/mongoid/errors/too_many_nested_attribute_records_spec.rb +3 -3
- data/spec/mongoid/errors/unknown_attribute_spec.rb +5 -5
- data/spec/mongoid/errors/unsaved_document_spec.rb +3 -3
- data/spec/mongoid/errors/unsupported_javascript_spec.rb +3 -3
- data/spec/mongoid/errors/validations_spec.rb +7 -7
- data/spec/mongoid/extensions/array_spec.rb +61 -61
- data/spec/mongoid/extensions/big_decimal_spec.rb +11 -11
- data/spec/mongoid/extensions/binary_spec.rb +12 -12
- data/spec/mongoid/extensions/boolean_spec.rb +18 -18
- data/spec/mongoid/extensions/date_spec.rb +13 -13
- data/spec/mongoid/extensions/date_time_spec.rb +8 -8
- data/spec/mongoid/extensions/false_class_spec.rb +5 -5
- data/spec/mongoid/extensions/float_spec.rb +29 -15
- data/spec/mongoid/extensions/hash_spec.rb +29 -29
- data/spec/mongoid/extensions/integer_spec.rb +15 -15
- data/spec/mongoid/extensions/module_spec.rb +2 -2
- data/spec/mongoid/extensions/nil_class_spec.rb +1 -1
- data/spec/mongoid/extensions/object_id_spec.rb +134 -134
- data/spec/mongoid/extensions/object_spec.rb +26 -26
- data/spec/mongoid/extensions/range_spec.rb +61 -12
- data/spec/mongoid/extensions/regexp_spec.rb +4 -4
- data/spec/mongoid/extensions/set_spec.rb +4 -4
- data/spec/mongoid/extensions/string_spec.rb +35 -46
- data/spec/mongoid/extensions/symbol_spec.rb +9 -9
- data/spec/mongoid/extensions/time_spec.rb +49 -49
- data/spec/mongoid/extensions/time_with_zone_spec.rb +34 -34
- data/spec/mongoid/extensions/true_class_spec.rb +5 -5
- data/spec/mongoid/extensions_spec.rb +29 -0
- data/spec/mongoid/factory_spec.rb +15 -15
- data/spec/mongoid/fields/foreign_key_spec.rb +73 -66
- data/spec/mongoid/fields/internal/foreign_keys/array_spec.rb +15 -15
- data/spec/mongoid/fields/internal/foreign_keys/object_spec.rb +22 -22
- data/spec/mongoid/fields/localized_spec.rb +38 -23
- data/spec/mongoid/fields/standard_spec.rb +12 -12
- data/spec/mongoid/fields_spec.rb +263 -217
- data/spec/mongoid/findable_spec.rb +509 -0
- data/spec/mongoid/indexable/specification_spec.rb +102 -0
- data/spec/mongoid/{indexes_spec.rb → indexable_spec.rb} +171 -51
- data/spec/mongoid/{inspection_spec.rb → inspectable_spec.rb} +6 -14
- data/spec/mongoid/{callbacks_spec.rb → interceptable_spec.rb} +117 -116
- data/spec/mongoid/log_subscriber_spec.rb +75 -0
- data/spec/mongoid/loggable_spec.rb +1 -1
- data/spec/mongoid/{matchers → matchable}/all_spec.rb +4 -4
- data/spec/mongoid/{matchers → matchable}/and_spec.rb +11 -11
- data/spec/mongoid/{matchers → matchable}/default_spec.rb +15 -15
- data/spec/mongoid/{matchers → matchable}/exists_spec.rb +5 -5
- data/spec/mongoid/{matchers → matchable}/gt_spec.rb +9 -8
- data/spec/mongoid/{matchers → matchable}/gte_spec.rb +7 -7
- data/spec/mongoid/matchable/in_spec.rb +49 -0
- data/spec/mongoid/{matchers → matchable}/lt_spec.rb +7 -7
- data/spec/mongoid/{matchers → matchable}/lte_spec.rb +7 -7
- data/spec/mongoid/matchable/ne_spec.rb +46 -0
- data/spec/mongoid/matchable/nin_spec.rb +48 -0
- data/spec/mongoid/{matchers → matchable}/or_spec.rb +6 -6
- data/spec/mongoid/{matchers → matchable}/size_spec.rb +3 -3
- data/spec/mongoid/{matchers_spec.rb → matchable_spec.rb} +41 -41
- data/spec/mongoid/persistable/creatable_spec.rb +553 -0
- data/spec/mongoid/persistable/deletable_spec.rb +218 -0
- data/spec/mongoid/persistable/destroyable_spec.rb +192 -0
- data/spec/mongoid/persistable/incrementable_spec.rb +173 -0
- data/spec/mongoid/persistable/logical_spec.rb +143 -0
- data/spec/mongoid/persistable/poppable_spec.rb +115 -0
- data/spec/mongoid/persistable/pullable_spec.rb +228 -0
- data/spec/mongoid/persistable/pushable_spec.rb +258 -0
- data/spec/mongoid/persistable/renamable_spec.rb +135 -0
- data/spec/mongoid/persistable/savable_spec.rb +498 -0
- data/spec/mongoid/persistable/settable_spec.rb +162 -0
- data/spec/mongoid/persistable/unsettable_spec.rb +155 -0
- data/spec/mongoid/persistable/updatable_spec.rb +558 -0
- data/spec/mongoid/persistable/upsertable_spec.rb +106 -0
- data/spec/mongoid/persistable_spec.rb +206 -0
- data/spec/mongoid/{atomic/positionable_spec.rb → positional_spec.rb} +14 -19
- data/spec/mongoid/query_cache_spec.rb +263 -0
- data/spec/mongoid/railties/document_spec.rb +2 -2
- data/spec/mongoid/relations/accessors_spec.rb +53 -145
- data/spec/mongoid/relations/auto_save_spec.rb +37 -17
- data/spec/mongoid/relations/bindings/embedded/in_spec.rb +12 -12
- data/spec/mongoid/relations/bindings/embedded/many_spec.rb +3 -3
- data/spec/mongoid/relations/bindings/embedded/one_spec.rb +5 -5
- data/spec/mongoid/relations/bindings/referenced/in_spec.rb +26 -26
- data/spec/mongoid/relations/bindings/referenced/many_spec.rb +10 -10
- data/spec/mongoid/relations/bindings/referenced/many_to_many_spec.rb +19 -19
- data/spec/mongoid/relations/bindings/referenced/one_spec.rb +12 -12
- data/spec/mongoid/relations/builders/embedded/in_spec.rb +4 -4
- data/spec/mongoid/relations/builders/embedded/many_spec.rb +18 -18
- data/spec/mongoid/relations/builders/embedded/one_spec.rb +11 -11
- data/spec/mongoid/relations/builders/nested_attributes/many_spec.rb +12 -13
- data/spec/mongoid/relations/builders/nested_attributes/one_spec.rb +14 -14
- data/spec/mongoid/relations/builders/referenced/in_spec.rb +24 -35
- data/spec/mongoid/relations/builders/referenced/many_spec.rb +15 -15
- data/spec/mongoid/relations/builders/referenced/many_to_many_spec.rb +18 -18
- data/spec/mongoid/relations/builders/referenced/one_spec.rb +12 -25
- data/spec/mongoid/relations/builders_spec.rb +21 -21
- data/spec/mongoid/relations/cascading/delete_spec.rb +9 -9
- data/spec/mongoid/relations/cascading/destroy_spec.rb +6 -6
- data/spec/mongoid/relations/cascading/nullify_spec.rb +4 -4
- data/spec/mongoid/relations/cascading/restrict_spec.rb +7 -7
- data/spec/mongoid/relations/cascading_spec.rb +16 -16
- data/spec/mongoid/relations/constraint_spec.rb +8 -7
- data/spec/mongoid/relations/conversions_spec.rb +16 -14
- data/spec/mongoid/relations/counter_cache_spec.rb +85 -0
- data/spec/mongoid/relations/cyclic_spec.rb +18 -18
- data/spec/mongoid/relations/eager/belongs_to_spec.rb +154 -0
- data/spec/mongoid/relations/eager/has_and_belongs_to_many_spec.rb +117 -0
- data/spec/mongoid/relations/eager/has_many_spec.rb +207 -0
- data/spec/mongoid/relations/eager/has_one_spec.rb +163 -0
- data/spec/mongoid/relations/eager_spec.rb +228 -0
- data/spec/mongoid/relations/embedded/dirty_spec.rb +8 -8
- data/spec/mongoid/relations/embedded/in_spec.rb +56 -57
- data/spec/mongoid/relations/embedded/many_spec.rb +467 -372
- data/spec/mongoid/relations/embedded/one_spec.rb +108 -141
- data/spec/mongoid/relations/macros_spec.rb +108 -102
- data/spec/mongoid/relations/metadata_spec.rb +180 -255
- data/spec/mongoid/relations/options_spec.rb +1 -1
- data/spec/mongoid/relations/polymorphic_spec.rb +13 -17
- data/spec/mongoid/relations/proxy_spec.rb +2 -2
- data/spec/mongoid/relations/referenced/in_spec.rb +128 -303
- data/spec/mongoid/relations/referenced/many_spec.rb +587 -527
- data/spec/mongoid/relations/referenced/many_to_many_spec.rb +401 -424
- data/spec/mongoid/relations/referenced/one_spec.rb +149 -264
- data/spec/mongoid/relations/reflections_spec.rb +6 -6
- data/spec/mongoid/relations/synchronization_spec.rb +46 -50
- data/spec/mongoid/relations/targets/enumerable_spec.rb +118 -118
- data/spec/mongoid/relations/touchable_spec.rb +333 -0
- data/spec/mongoid/relations_spec.rb +16 -15
- data/spec/mongoid/{reloading_spec.rb → reloadable_spec.rb} +38 -55
- data/spec/mongoid/{scoping_spec.rb → scopable_spec.rb} +89 -164
- data/spec/mongoid/selectable_spec.rb +134 -0
- data/spec/mongoid/{serialization_spec.rb → serializable_spec.rb} +129 -82
- data/spec/mongoid/sessions/factory_spec.rb +49 -28
- data/spec/mongoid/sessions/mongo_uri_spec.rb +7 -7
- data/spec/mongoid/sessions/options_spec.rb +108 -0
- data/spec/mongoid/sessions_spec.rb +319 -178
- data/spec/mongoid/{sharding_spec.rb → shardable_spec.rb} +8 -8
- data/spec/mongoid/{state_spec.rb → stateful_spec.rb} +36 -11
- data/spec/mongoid/tasks/database_rake_spec.rb +285 -0
- data/spec/mongoid/tasks/database_spec.rb +160 -0
- data/spec/mongoid/threaded_spec.rb +17 -70
- data/spec/mongoid/timestamps/created/short_spec.rb +6 -6
- data/spec/mongoid/timestamps/created_spec.rb +5 -5
- data/spec/mongoid/timestamps/timeless_spec.rb +6 -6
- data/spec/mongoid/timestamps/updated/short_spec.rb +11 -11
- data/spec/mongoid/timestamps/updated_spec.rb +9 -13
- data/spec/mongoid/timestamps_spec.rb +9 -13
- data/spec/mongoid/{hierarchy_spec.rb → traversable_spec.rb} +19 -19
- data/spec/mongoid/{validations → validatable}/associated_spec.rb +45 -22
- data/spec/mongoid/{validations → validatable}/format_spec.rb +6 -6
- data/spec/mongoid/{validations → validatable}/length_spec.rb +14 -14
- data/spec/mongoid/{validations → validatable}/numericality_spec.rb +1 -1
- data/spec/mongoid/{validations → validatable}/presence_spec.rb +37 -44
- data/spec/mongoid/{validations → validatable}/uniqueness_spec.rb +184 -192
- data/spec/mongoid/{validations_spec.rb → validatable_spec.rb} +28 -28
- data/spec/mongoid_spec.rb +8 -8
- data/spec/rails/mongoid_spec.rb +19 -335
- data/spec/spec_helper.rb +31 -13
- metadata +303 -283
- data/lib/mongoid/atomic/positionable.rb +0 -73
- data/lib/mongoid/components.rb +0 -92
- data/lib/mongoid/config/inflections.rb +0 -6
- data/lib/mongoid/contextual/eager.rb +0 -158
- data/lib/mongoid/errors/versioning_not_on_root.rb +0 -23
- data/lib/mongoid/identity_map.rb +0 -163
- data/lib/mongoid/json.rb +0 -16
- data/lib/mongoid/matchers.rb +0 -32
- data/lib/mongoid/matchers/strategies.rb +0 -97
- data/lib/mongoid/multi_parameter_attributes.rb +0 -105
- data/lib/mongoid/nested_attributes.rb +0 -78
- data/lib/mongoid/observer.rb +0 -192
- data/lib/mongoid/paranoia.rb +0 -136
- data/lib/mongoid/persistence.rb +0 -357
- data/lib/mongoid/persistence/atomic.rb +0 -231
- data/lib/mongoid/persistence/atomic/add_to_set.rb +0 -47
- data/lib/mongoid/persistence/atomic/bit.rb +0 -35
- data/lib/mongoid/persistence/atomic/inc.rb +0 -45
- data/lib/mongoid/persistence/atomic/operation.rb +0 -154
- data/lib/mongoid/persistence/atomic/pop.rb +0 -32
- data/lib/mongoid/persistence/atomic/pull.rb +0 -32
- data/lib/mongoid/persistence/atomic/pull_all.rb +0 -32
- data/lib/mongoid/persistence/atomic/push.rb +0 -25
- data/lib/mongoid/persistence/atomic/push_all.rb +0 -25
- data/lib/mongoid/persistence/atomic/rename.rb +0 -30
- data/lib/mongoid/persistence/atomic/sets.rb +0 -28
- data/lib/mongoid/persistence/atomic/unset.rb +0 -27
- data/lib/mongoid/persistence/deletion.rb +0 -31
- data/lib/mongoid/persistence/insertion.rb +0 -38
- data/lib/mongoid/persistence/modification.rb +0 -35
- data/lib/mongoid/persistence/operations.rb +0 -214
- data/lib/mongoid/persistence/operations/embedded/insert.rb +0 -46
- data/lib/mongoid/persistence/operations/embedded/remove.rb +0 -43
- data/lib/mongoid/persistence/operations/insert.rb +0 -34
- data/lib/mongoid/persistence/operations/remove.rb +0 -33
- data/lib/mongoid/persistence/operations/update.rb +0 -59
- data/lib/mongoid/persistence/operations/upsert.rb +0 -28
- data/lib/mongoid/persistence/upsertion.rb +0 -31
- data/lib/mongoid/unit_of_work.rb +0 -61
- data/lib/mongoid/versioning.rb +0 -217
- data/lib/rack/mongoid.rb +0 -2
- data/lib/rack/mongoid/middleware/identity_map.rb +0 -39
- data/lib/rails/generators/mongoid/observer/observer_generator.rb +0 -17
- data/lib/rails/generators/mongoid/observer/templates/observer.rb.tt +0 -4
- data/spec/app/models/actor_observer.rb +0 -15
- data/spec/app/models/callback_recorder.rb +0 -25
- data/spec/app/models/paranoid_phone.rb +0 -25
- data/spec/app/models/paranoid_post.rb +0 -36
- data/spec/app/models/phone_observer.rb +0 -6
- data/spec/mongoid/attributes/processing_spec.rb +0 -149
- data/spec/mongoid/criterion/destructive_spec.rb +0 -101
- data/spec/mongoid/criterion/modifiable_spec.rb +0 -409
- data/spec/mongoid/criterion/modification_spec.rb +0 -402
- data/spec/mongoid/errors/versioning_not_on_root_spec.rb +0 -29
- data/spec/mongoid/finders_spec.rb +0 -321
- data/spec/mongoid/identity_map_spec.rb +0 -564
- data/spec/mongoid/json_spec.rb +0 -33
- data/spec/mongoid/matchers/in_spec.rb +0 -25
- data/spec/mongoid/matchers/ne_spec.rb +0 -25
- data/spec/mongoid/matchers/nin_spec.rb +0 -25
- data/spec/mongoid/multi_parameter_attributes_spec.rb +0 -128
- data/spec/mongoid/observer_spec.rb +0 -290
- data/spec/mongoid/paranoia_spec.rb +0 -759
- data/spec/mongoid/persistence/atomic/add_to_set_spec.rb +0 -262
- data/spec/mongoid/persistence/atomic/bit_spec.rb +0 -88
- data/spec/mongoid/persistence/atomic/inc_spec.rb +0 -133
- data/spec/mongoid/persistence/atomic/pop_spec.rb +0 -111
- data/spec/mongoid/persistence/atomic/pull_all_spec.rb +0 -77
- data/spec/mongoid/persistence/atomic/pull_spec.rb +0 -80
- data/spec/mongoid/persistence/atomic/push_all_spec.rb +0 -77
- data/spec/mongoid/persistence/atomic/push_spec.rb +0 -77
- data/spec/mongoid/persistence/atomic/rename_spec.rb +0 -42
- data/spec/mongoid/persistence/atomic/sets_spec.rb +0 -154
- data/spec/mongoid/persistence/atomic/unset_spec.rb +0 -65
- data/spec/mongoid/persistence/atomic_spec.rb +0 -216
- data/spec/mongoid/persistence/operations/embedded/insert_spec.rb +0 -191
- data/spec/mongoid/persistence/operations/embedded/remove_spec.rb +0 -8
- data/spec/mongoid/persistence/operations/insert_spec.rb +0 -149
- data/spec/mongoid/persistence/operations/remove_spec.rb +0 -113
- data/spec/mongoid/persistence/operations/update_spec.rb +0 -141
- data/spec/mongoid/persistence/operations/upsert_spec.rb +0 -59
- data/spec/mongoid/persistence/operations_spec.rb +0 -313
- data/spec/mongoid/persistence_spec.rb +0 -2279
- data/spec/mongoid/unit_of_work_spec.rb +0 -196
- data/spec/mongoid/versioning_spec.rb +0 -540
- data/spec/rack/mongoid/middleware/identity_map_spec.rb +0 -72
@@ -1,6 +1,6 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
describe Mongoid::
|
3
|
+
describe Mongoid::Criteria::Scopable do
|
4
4
|
|
5
5
|
describe "#apply_default_scope" do
|
6
6
|
|
@@ -11,7 +11,7 @@ describe Mongoid::Criterion::Scoping do
|
|
11
11
|
end
|
12
12
|
|
13
13
|
before do
|
14
|
-
Band.default_scope scope
|
14
|
+
Band.default_scope ->{ scope }
|
15
15
|
end
|
16
16
|
|
17
17
|
after do
|
@@ -25,11 +25,11 @@ describe Mongoid::Criterion::Scoping do
|
|
25
25
|
end
|
26
26
|
|
27
27
|
it "merges in the options" do
|
28
|
-
scoped.options.
|
28
|
+
expect(scoped.options).to eq({ skip: 20 })
|
29
29
|
end
|
30
30
|
|
31
31
|
it "sets scoped to true" do
|
32
|
-
scoped.
|
32
|
+
expect(scoped).to be_scoped
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
@@ -40,7 +40,7 @@ describe Mongoid::Criterion::Scoping do
|
|
40
40
|
end
|
41
41
|
|
42
42
|
before do
|
43
|
-
Band.default_scope scope
|
43
|
+
Band.default_scope ->{ scope }
|
44
44
|
end
|
45
45
|
|
46
46
|
after do
|
@@ -54,11 +54,11 @@ describe Mongoid::Criterion::Scoping do
|
|
54
54
|
end
|
55
55
|
|
56
56
|
it "merges in the options" do
|
57
|
-
scoped.selector.
|
57
|
+
expect(scoped.selector).to eq({ "name" => "Depeche Mode" })
|
58
58
|
end
|
59
59
|
|
60
60
|
it "sets scoped to true" do
|
61
|
-
scoped.
|
61
|
+
expect(scoped).to be_scoped
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
@@ -69,7 +69,7 @@ describe Mongoid::Criterion::Scoping do
|
|
69
69
|
end
|
70
70
|
|
71
71
|
before do
|
72
|
-
Band.default_scope scope
|
72
|
+
Band.default_scope ->{ scope }
|
73
73
|
end
|
74
74
|
|
75
75
|
after do
|
@@ -85,13 +85,13 @@ describe Mongoid::Criterion::Scoping do
|
|
85
85
|
end
|
86
86
|
|
87
87
|
it "merges in the inclusions" do
|
88
|
-
scoped.inclusions.
|
88
|
+
expect(scoped.inclusions).to eq(
|
89
89
|
[ Band.relations["records"] ]
|
90
90
|
)
|
91
91
|
end
|
92
92
|
|
93
93
|
it "sets scoped to true" do
|
94
|
-
scoped.
|
94
|
+
expect(scoped).to be_scoped
|
95
95
|
end
|
96
96
|
end
|
97
97
|
|
@@ -104,13 +104,13 @@ describe Mongoid::Criterion::Scoping do
|
|
104
104
|
end
|
105
105
|
|
106
106
|
it "merges in the inclusions" do
|
107
|
-
scoped.inclusions.
|
107
|
+
expect(scoped.inclusions).to eq(
|
108
108
|
[ Band.relations["records"], Band.relations["label"] ]
|
109
109
|
)
|
110
110
|
end
|
111
111
|
|
112
112
|
it "sets scoped to true" do
|
113
|
-
scoped.
|
113
|
+
expect(scoped).to be_scoped
|
114
114
|
end
|
115
115
|
end
|
116
116
|
end
|
@@ -129,7 +129,7 @@ describe Mongoid::Criterion::Scoping do
|
|
129
129
|
end
|
130
130
|
|
131
131
|
it "removes the selection" do
|
132
|
-
criteria.selector.
|
132
|
+
expect(criteria.selector).to be_empty
|
133
133
|
end
|
134
134
|
end
|
135
135
|
|
@@ -146,7 +146,7 @@ describe Mongoid::Criterion::Scoping do
|
|
146
146
|
end
|
147
147
|
|
148
148
|
it "removes the options" do
|
149
|
-
criteria.options.
|
149
|
+
expect(criteria.options).to be_empty
|
150
150
|
end
|
151
151
|
end
|
152
152
|
|
@@ -161,11 +161,11 @@ describe Mongoid::Criterion::Scoping do
|
|
161
161
|
end
|
162
162
|
|
163
163
|
it "removes the options" do
|
164
|
-
criteria.options.
|
164
|
+
expect(criteria.options).to be_empty
|
165
165
|
end
|
166
166
|
|
167
167
|
it "does not remove the selector" do
|
168
|
-
criteria.selector.
|
168
|
+
expect(criteria.selector).to eq({ "name" => nil })
|
169
169
|
end
|
170
170
|
end
|
171
171
|
end
|
@@ -185,7 +185,7 @@ describe Mongoid::Criterion::Scoping do
|
|
185
185
|
end
|
186
186
|
|
187
187
|
it "removes the matching inclusions" do
|
188
|
-
criteria.inclusions.
|
188
|
+
expect(criteria.inclusions).to eq([ Band.relations["records"] ])
|
189
189
|
end
|
190
190
|
end
|
191
191
|
end
|
@@ -203,15 +203,15 @@ describe Mongoid::Criterion::Scoping do
|
|
203
203
|
end
|
204
204
|
|
205
205
|
it "returns a criteria" do
|
206
|
-
scoped.
|
206
|
+
expect(scoped).to be_a(Mongoid::Criteria)
|
207
207
|
end
|
208
208
|
|
209
209
|
it "contains an empty selector" do
|
210
|
-
scoped.selector.
|
210
|
+
expect(scoped.selector).to be_empty
|
211
211
|
end
|
212
212
|
|
213
213
|
it "contains empty options" do
|
214
|
-
scoped.options.
|
214
|
+
expect(scoped.options).to be_empty
|
215
215
|
end
|
216
216
|
end
|
217
217
|
|
@@ -222,15 +222,15 @@ describe Mongoid::Criterion::Scoping do
|
|
222
222
|
end
|
223
223
|
|
224
224
|
it "returns a criteria" do
|
225
|
-
scoped.
|
225
|
+
expect(scoped).to be_a(Mongoid::Criteria)
|
226
226
|
end
|
227
227
|
|
228
228
|
it "contains an empty selector" do
|
229
|
-
scoped.selector.
|
229
|
+
expect(scoped.selector).to be_empty
|
230
230
|
end
|
231
231
|
|
232
232
|
it "contains the options" do
|
233
|
-
scoped.options.
|
233
|
+
expect(scoped.options).to eq({ skip: 10, limit: 10 })
|
234
234
|
end
|
235
235
|
end
|
236
236
|
|
@@ -241,7 +241,7 @@ describe Mongoid::Criterion::Scoping do
|
|
241
241
|
end
|
242
242
|
|
243
243
|
before do
|
244
|
-
Band.default_scope criteria
|
244
|
+
Band.default_scope ->{ criteria }
|
245
245
|
end
|
246
246
|
|
247
247
|
after do
|
@@ -253,11 +253,11 @@ describe Mongoid::Criterion::Scoping do
|
|
253
253
|
end
|
254
254
|
|
255
255
|
it "allows the default scope to be added" do
|
256
|
-
scoped.selector.
|
256
|
+
expect(scoped.selector).to eq({ "name" => "Depeche Mode" })
|
257
257
|
end
|
258
258
|
|
259
259
|
it "flags as scoped" do
|
260
|
-
scoped.
|
260
|
+
expect(scoped).to be_scoped
|
261
261
|
end
|
262
262
|
|
263
263
|
context "when chained after an unscoped criteria" do
|
@@ -267,7 +267,7 @@ describe Mongoid::Criterion::Scoping do
|
|
267
267
|
end
|
268
268
|
|
269
269
|
it "reapplies the default scope" do
|
270
|
-
scoped.selector.
|
270
|
+
expect(scoped.selector).to eq({ "name" => "Depeche Mode" })
|
271
271
|
end
|
272
272
|
end
|
273
273
|
end
|
@@ -284,7 +284,7 @@ describe Mongoid::Criterion::Scoping do
|
|
284
284
|
end
|
285
285
|
|
286
286
|
it "returns the scoping options" do
|
287
|
-
criteria.scoping_options.
|
287
|
+
expect(criteria.scoping_options).to eq([ true, false ])
|
288
288
|
end
|
289
289
|
end
|
290
290
|
|
@@ -299,11 +299,11 @@ describe Mongoid::Criterion::Scoping do
|
|
299
299
|
end
|
300
300
|
|
301
301
|
it "sets the scoped flag" do
|
302
|
-
criteria.
|
302
|
+
expect(criteria).to be_scoped
|
303
303
|
end
|
304
304
|
|
305
305
|
it "sets the unscoped flag" do
|
306
|
-
criteria.
|
306
|
+
expect(criteria).to be_unscoped
|
307
307
|
end
|
308
308
|
end
|
309
309
|
|
@@ -318,7 +318,7 @@ describe Mongoid::Criterion::Scoping do
|
|
318
318
|
end
|
319
319
|
|
320
320
|
before do
|
321
|
-
Band.default_scope criteria
|
321
|
+
Band.default_scope ->{ criteria }
|
322
322
|
end
|
323
323
|
|
324
324
|
after do
|
@@ -332,7 +332,7 @@ describe Mongoid::Criterion::Scoping do
|
|
332
332
|
end
|
333
333
|
|
334
334
|
it "removes the default scope from the criteria" do
|
335
|
-
unscoped.selector.
|
335
|
+
expect(unscoped.selector).to be_empty
|
336
336
|
end
|
337
337
|
|
338
338
|
context "when chained after a scoped criteria" do
|
@@ -342,7 +342,7 @@ describe Mongoid::Criterion::Scoping do
|
|
342
342
|
end
|
343
343
|
|
344
344
|
it "removes all scoping" do
|
345
|
-
unscoped.selector.
|
345
|
+
expect(unscoped.selector).to be_empty
|
346
346
|
end
|
347
347
|
end
|
348
348
|
end
|
@@ -353,7 +353,7 @@ describe Mongoid::Criterion::Scoping do
|
|
353
353
|
|
354
354
|
it "does not allow default scoping to be added in the block" do
|
355
355
|
Band.unscoped do
|
356
|
-
empty.skip(10).selector.
|
356
|
+
expect(empty.skip(10).selector).to be_empty
|
357
357
|
end
|
358
358
|
end
|
359
359
|
end
|
@@ -362,7 +362,7 @@ describe Mongoid::Criterion::Scoping do
|
|
362
362
|
|
363
363
|
it "does not allow default scoping to be added in the block" do
|
364
364
|
Band.unscoped do
|
365
|
-
empty.scoped.selector.
|
365
|
+
expect(empty.scoped.selector).to be_empty
|
366
366
|
end
|
367
367
|
end
|
368
368
|
end
|
@@ -370,7 +370,7 @@ describe Mongoid::Criterion::Scoping do
|
|
370
370
|
context "when a named scope is called in the block" do
|
371
371
|
|
372
372
|
before do
|
373
|
-
Band.scope(:skipped, Band.skip(10))
|
373
|
+
Band.scope(:skipped, ->{ Band.skip(10) })
|
374
374
|
end
|
375
375
|
|
376
376
|
after do
|
@@ -382,7 +382,7 @@ describe Mongoid::Criterion::Scoping do
|
|
382
382
|
|
383
383
|
it "does not allow the default scope to be applied" do
|
384
384
|
Band.unscoped do
|
385
|
-
empty.skipped.selector.
|
385
|
+
expect(empty.skipped.selector).to be_empty
|
386
386
|
end
|
387
387
|
end
|
388
388
|
end
|
@@ -17,7 +17,7 @@ describe Mongoid::Criteria do
|
|
17
17
|
end
|
18
18
|
|
19
19
|
it "returns true" do
|
20
|
-
criteria.
|
20
|
+
expect(criteria).to eq(other)
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -28,7 +28,7 @@ describe Mongoid::Criteria do
|
|
28
28
|
end
|
29
29
|
|
30
30
|
it "returns false" do
|
31
|
-
criteria.
|
31
|
+
expect(criteria).to_not eq(other)
|
32
32
|
end
|
33
33
|
end
|
34
34
|
end
|
@@ -46,7 +46,7 @@ describe Mongoid::Criteria do
|
|
46
46
|
end
|
47
47
|
|
48
48
|
it "returns true" do
|
49
|
-
criteria.
|
49
|
+
expect(criteria).to eq(other)
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
@@ -65,7 +65,7 @@ describe Mongoid::Criteria do
|
|
65
65
|
end
|
66
66
|
|
67
67
|
it "returns false" do
|
68
|
-
criteria.
|
68
|
+
expect(criteria).to_not eq(other)
|
69
69
|
end
|
70
70
|
end
|
71
71
|
end
|
@@ -73,7 +73,7 @@ describe Mongoid::Criteria do
|
|
73
73
|
context "when the other is neither a criteria or enumerable" do
|
74
74
|
|
75
75
|
it "returns false" do
|
76
|
-
criteria.
|
76
|
+
expect(criteria).to_not eq("test")
|
77
77
|
end
|
78
78
|
end
|
79
79
|
end
|
@@ -87,14 +87,14 @@ describe Mongoid::Criteria do
|
|
87
87
|
end
|
88
88
|
|
89
89
|
it "returns true" do
|
90
|
-
(described_class === other).
|
90
|
+
expect(described_class === other).to be true
|
91
91
|
end
|
92
92
|
end
|
93
93
|
|
94
94
|
context "when the other is not a criteria" do
|
95
95
|
|
96
96
|
it "returns false" do
|
97
|
-
(described_class === []).
|
97
|
+
expect(described_class === []).to be false
|
98
98
|
end
|
99
99
|
end
|
100
100
|
end
|
@@ -124,7 +124,7 @@ describe Mongoid::Criteria do
|
|
124
124
|
end
|
125
125
|
|
126
126
|
it "returns the sorted documents" do
|
127
|
-
criteria.
|
127
|
+
expect(criteria).to eq([ friedel, hobrecht, pfluger ])
|
128
128
|
end
|
129
129
|
end
|
130
130
|
end
|
@@ -140,11 +140,11 @@ describe Mongoid::Criteria do
|
|
140
140
|
end
|
141
141
|
|
142
142
|
it "adds the batch size option" do
|
143
|
-
criteria.options[:batch_size].
|
143
|
+
expect(criteria.options[:batch_size]).to eq(1000)
|
144
144
|
end
|
145
145
|
|
146
146
|
it "returns the correct documents" do
|
147
|
-
criteria.
|
147
|
+
expect(criteria).to eq([ person ])
|
148
148
|
end
|
149
149
|
end
|
150
150
|
|
@@ -169,23 +169,23 @@ describe Mongoid::Criteria do
|
|
169
169
|
end
|
170
170
|
|
171
171
|
it "returns an avg" do
|
172
|
-
aggregates["avg"].
|
172
|
+
expect(aggregates["avg"]).to eq(750)
|
173
173
|
end
|
174
174
|
|
175
175
|
it "returns a count" do
|
176
|
-
aggregates["count"].
|
176
|
+
expect(aggregates["count"]).to eq(2)
|
177
177
|
end
|
178
178
|
|
179
179
|
it "returns a max" do
|
180
|
-
aggregates["max"].
|
180
|
+
expect(aggregates["max"]).to eq(1000)
|
181
181
|
end
|
182
182
|
|
183
183
|
it "returns a min" do
|
184
|
-
aggregates["min"].
|
184
|
+
expect(aggregates["min"]).to eq(500)
|
185
185
|
end
|
186
186
|
|
187
187
|
it "returns a sum" do
|
188
|
-
aggregates["sum"].
|
188
|
+
expect(aggregates["sum"]).to eq(1500)
|
189
189
|
end
|
190
190
|
end
|
191
191
|
end
|
@@ -211,7 +211,7 @@ describe Mongoid::Criteria do
|
|
211
211
|
end
|
212
212
|
|
213
213
|
it "returns the avg of the provided field" do
|
214
|
-
avg.
|
214
|
+
expect(avg).to eq(750)
|
215
215
|
end
|
216
216
|
end
|
217
217
|
end
|
@@ -233,7 +233,7 @@ describe Mongoid::Criteria do
|
|
233
233
|
end
|
234
234
|
|
235
235
|
it "returns the matching documents" do
|
236
|
-
criteria.
|
236
|
+
expect(criteria).to eq([ match ])
|
237
237
|
end
|
238
238
|
end
|
239
239
|
end
|
@@ -255,7 +255,7 @@ describe Mongoid::Criteria do
|
|
255
255
|
end
|
256
256
|
|
257
257
|
it "returns the matching documents" do
|
258
|
-
criteria.
|
258
|
+
expect(criteria).to eq([ match ])
|
259
259
|
end
|
260
260
|
end
|
261
261
|
end
|
@@ -271,7 +271,7 @@ describe Mongoid::Criteria do
|
|
271
271
|
end
|
272
272
|
|
273
273
|
it "returns the criteria as a json hash" do
|
274
|
-
criteria.as_json.
|
274
|
+
expect(criteria.as_json).to eq([ band.serializable_hash ])
|
275
275
|
end
|
276
276
|
end
|
277
277
|
|
@@ -290,7 +290,7 @@ describe Mongoid::Criteria do
|
|
290
290
|
end
|
291
291
|
|
292
292
|
it "returns the matching documents" do
|
293
|
-
criteria.
|
293
|
+
expect(criteria).to eq([ match ])
|
294
294
|
end
|
295
295
|
end
|
296
296
|
|
@@ -309,15 +309,15 @@ describe Mongoid::Criteria do
|
|
309
309
|
end
|
310
310
|
|
311
311
|
it "returns the new document" do
|
312
|
-
band.
|
312
|
+
expect(band).to be_new_record
|
313
313
|
end
|
314
314
|
|
315
315
|
it "sets the criteria attributes" do
|
316
|
-
band.name.
|
316
|
+
expect(band.name).to eq("Depeche Mode")
|
317
317
|
end
|
318
318
|
|
319
319
|
it "sets the attributes passed to build" do
|
320
|
-
band.genres.
|
320
|
+
expect(band.genres).to eq([ "electro" ])
|
321
321
|
end
|
322
322
|
end
|
323
323
|
|
@@ -330,15 +330,15 @@ describe Mongoid::Criteria do
|
|
330
330
|
end
|
331
331
|
|
332
332
|
it "returns the new document" do
|
333
|
-
band.
|
333
|
+
expect(band).to be_new_record
|
334
334
|
end
|
335
335
|
|
336
336
|
it "sets the criteria attributes" do
|
337
|
-
band.name.
|
337
|
+
expect(band.name).to eq("Depeche Mode")
|
338
338
|
end
|
339
339
|
|
340
340
|
it "sets the attributes passed to build" do
|
341
|
-
band.genres.
|
341
|
+
expect(band.genres).to eq([ "electro" ])
|
342
342
|
end
|
343
343
|
end
|
344
344
|
end
|
@@ -362,9 +362,9 @@ describe Mongoid::Criteria do
|
|
362
362
|
end
|
363
363
|
|
364
364
|
it "does not hit the database after first iteration" do
|
365
|
-
criteria.context.query.
|
365
|
+
expect(criteria.context.query).to receive(:each).never
|
366
366
|
criteria.each do |doc|
|
367
|
-
doc.
|
367
|
+
expect(doc).to eq(person)
|
368
368
|
end
|
369
369
|
end
|
370
370
|
end
|
@@ -380,9 +380,9 @@ describe Mongoid::Criteria do
|
|
380
380
|
end
|
381
381
|
|
382
382
|
it "does not hit the database after first iteration" do
|
383
|
-
criteria.context.query.
|
383
|
+
expect(criteria.context.query).to receive(:each).never
|
384
384
|
criteria.each do |doc|
|
385
|
-
doc.
|
385
|
+
expect(doc).to eq(person)
|
386
386
|
end
|
387
387
|
end
|
388
388
|
end
|
@@ -410,47 +410,47 @@ describe Mongoid::Criteria do
|
|
410
410
|
end
|
411
411
|
|
412
412
|
it "contains an equal selector" do
|
413
|
-
clone.selector.
|
413
|
+
expect(clone.selector).to eq({ "name" => "Depeche Mode" })
|
414
414
|
end
|
415
415
|
|
416
416
|
it "clones the selector" do
|
417
|
-
clone.selector.
|
417
|
+
expect(clone.selector).to_not equal(criteria.selector)
|
418
418
|
end
|
419
419
|
|
420
420
|
it "contains equal options" do
|
421
|
-
clone.options.
|
421
|
+
expect(clone.options).to eq({ sort: { "name" => 1 }})
|
422
422
|
end
|
423
423
|
|
424
424
|
it "clones the options" do
|
425
|
-
clone.options.
|
425
|
+
expect(clone.options).to_not equal(criteria.options)
|
426
426
|
end
|
427
427
|
|
428
428
|
it "contains equal inclusions" do
|
429
|
-
clone.inclusions.
|
429
|
+
expect(clone.inclusions).to eq([ Band.relations["records"] ])
|
430
430
|
end
|
431
431
|
|
432
432
|
it "clones the inclusions" do
|
433
|
-
clone.inclusions.
|
433
|
+
expect(clone.inclusions).to_not equal(criteria.inclusions)
|
434
434
|
end
|
435
435
|
|
436
436
|
it "contains equal documents" do
|
437
|
-
clone.documents.
|
437
|
+
expect(clone.documents).to eq([ band ])
|
438
438
|
end
|
439
439
|
|
440
440
|
it "clones the documents" do
|
441
|
-
clone.documents.
|
441
|
+
expect(clone.documents).to_not equal(criteria.documents)
|
442
442
|
end
|
443
443
|
|
444
444
|
it "contains equal scoping options" do
|
445
|
-
clone.scoping_options.
|
445
|
+
expect(clone.scoping_options).to eq([ nil, nil ])
|
446
446
|
end
|
447
447
|
|
448
448
|
it "clones the scoping options" do
|
449
|
-
clone.scoping_options.
|
449
|
+
expect(clone.scoping_options).to_not equal(criteria.scoping_options)
|
450
450
|
end
|
451
451
|
|
452
452
|
it "sets the context to nil" do
|
453
|
-
clone.instance_variable_get(:@context).
|
453
|
+
expect(clone.instance_variable_get(:@context)).to be_nil
|
454
454
|
end
|
455
455
|
end
|
456
456
|
end
|
@@ -462,7 +462,7 @@ describe Mongoid::Criteria do
|
|
462
462
|
end
|
463
463
|
|
464
464
|
it "sets the cache option to true" do
|
465
|
-
criteria.cache.
|
465
|
+
expect(criteria.cache).to be_cached
|
466
466
|
end
|
467
467
|
end
|
468
468
|
|
@@ -477,7 +477,7 @@ describe Mongoid::Criteria do
|
|
477
477
|
end
|
478
478
|
|
479
479
|
it "returns the embedded context" do
|
480
|
-
criteria.context.
|
480
|
+
expect(criteria.context).to be_a(Mongoid::Contextual::Memory)
|
481
481
|
end
|
482
482
|
end
|
483
483
|
|
@@ -488,71 +488,7 @@ describe Mongoid::Criteria do
|
|
488
488
|
end
|
489
489
|
|
490
490
|
it "returns the mongo context" do
|
491
|
-
criteria.context.
|
492
|
-
end
|
493
|
-
end
|
494
|
-
end
|
495
|
-
|
496
|
-
describe "#create" do
|
497
|
-
|
498
|
-
let(:criteria) do
|
499
|
-
Band.where(name: "Depeche Mode")
|
500
|
-
end
|
501
|
-
|
502
|
-
context "when provided valid attributes" do
|
503
|
-
let(:band) do
|
504
|
-
criteria.create(genres: [ "electro" ])
|
505
|
-
end
|
506
|
-
|
507
|
-
it "returns the created document" do
|
508
|
-
band.should be_persisted
|
509
|
-
end
|
510
|
-
|
511
|
-
it "sets the criteria attributes" do
|
512
|
-
band.name.should eq("Depeche Mode")
|
513
|
-
end
|
514
|
-
|
515
|
-
it "sets the attributes passed to build" do
|
516
|
-
band.genres.should eq([ "electro" ])
|
517
|
-
end
|
518
|
-
end
|
519
|
-
|
520
|
-
context "when provided a block" do
|
521
|
-
context "when provided valid attributes & using block" do
|
522
|
-
let(:band) do
|
523
|
-
criteria.create do |c|
|
524
|
-
c.genres = [ "electro" ]
|
525
|
-
end
|
526
|
-
end
|
527
|
-
|
528
|
-
it "returns the created document" do
|
529
|
-
band.should be_persisted
|
530
|
-
end
|
531
|
-
|
532
|
-
it "sets the criteria attributes" do
|
533
|
-
band.name.should eq("Depeche Mode")
|
534
|
-
end
|
535
|
-
|
536
|
-
it "sets the attributes passed to build" do
|
537
|
-
band.genres.should eq([ "electro" ])
|
538
|
-
end
|
539
|
-
end
|
540
|
-
end
|
541
|
-
|
542
|
-
end
|
543
|
-
|
544
|
-
describe "#create!" do
|
545
|
-
|
546
|
-
let(:criteria) do
|
547
|
-
Account.where(number: "11123213")
|
548
|
-
end
|
549
|
-
|
550
|
-
context "when provided invalid attributes" do
|
551
|
-
|
552
|
-
it "raises an error" do
|
553
|
-
expect {
|
554
|
-
criteria.create!
|
555
|
-
}.to raise_error(Mongoid::Errors::Validations)
|
491
|
+
expect(criteria.context).to be_a(Mongoid::Contextual::Mongo)
|
556
492
|
end
|
557
493
|
end
|
558
494
|
end
|
@@ -574,7 +510,7 @@ describe Mongoid::Criteria do
|
|
574
510
|
end
|
575
511
|
|
576
512
|
it "deletes all the documents from the database" do
|
577
|
-
Band.count.
|
513
|
+
expect(Band.count).to eq(0)
|
578
514
|
end
|
579
515
|
end
|
580
516
|
end
|
@@ -592,7 +528,7 @@ describe Mongoid::Criteria do
|
|
592
528
|
end
|
593
529
|
|
594
530
|
it "returns the documents" do
|
595
|
-
criteria.documents.
|
531
|
+
expect(criteria.documents).to eq([ band ])
|
596
532
|
end
|
597
533
|
end
|
598
534
|
|
@@ -611,7 +547,7 @@ describe Mongoid::Criteria do
|
|
611
547
|
end
|
612
548
|
|
613
549
|
it "sets the documents" do
|
614
|
-
criteria.documents.
|
550
|
+
expect(criteria.documents).to eq([ band ])
|
615
551
|
end
|
616
552
|
end
|
617
553
|
|
@@ -629,7 +565,7 @@ describe Mongoid::Criteria do
|
|
629
565
|
|
630
566
|
it "iterates over the matching documents" do
|
631
567
|
criteria.each do |doc|
|
632
|
-
doc.
|
568
|
+
expect(doc).to eq(band)
|
633
569
|
end
|
634
570
|
end
|
635
571
|
end
|
@@ -652,7 +588,7 @@ describe Mongoid::Criteria do
|
|
652
588
|
end
|
653
589
|
|
654
590
|
it "returns the matching documents" do
|
655
|
-
criteria.
|
591
|
+
expect(criteria).to eq([ match ])
|
656
592
|
end
|
657
593
|
end
|
658
594
|
|
@@ -669,7 +605,7 @@ describe Mongoid::Criteria do
|
|
669
605
|
end
|
670
606
|
|
671
607
|
it "returns true" do
|
672
|
-
criteria.
|
608
|
+
expect(criteria).to be_embedded
|
673
609
|
end
|
674
610
|
end
|
675
611
|
|
@@ -680,7 +616,7 @@ describe Mongoid::Criteria do
|
|
680
616
|
end
|
681
617
|
|
682
618
|
it "returns false" do
|
683
|
-
criteria.
|
619
|
+
expect(criteria).to_not be_embedded
|
684
620
|
end
|
685
621
|
end
|
686
622
|
end
|
@@ -733,7 +669,7 @@ describe Mongoid::Criteria do
|
|
733
669
|
end
|
734
670
|
|
735
671
|
it "returns the matching documents" do
|
736
|
-
criteria.
|
672
|
+
expect(criteria).to eq([ match ])
|
737
673
|
end
|
738
674
|
end
|
739
675
|
|
@@ -750,7 +686,7 @@ describe Mongoid::Criteria do
|
|
750
686
|
end
|
751
687
|
|
752
688
|
it "returns true" do
|
753
|
-
criteria.exists
|
689
|
+
expect(criteria.exists?).to be true
|
754
690
|
end
|
755
691
|
end
|
756
692
|
|
@@ -761,7 +697,7 @@ describe Mongoid::Criteria do
|
|
761
697
|
end
|
762
698
|
|
763
699
|
it "returns false" do
|
764
|
-
criteria.exists
|
700
|
+
expect(criteria.exists?).to be false
|
765
701
|
end
|
766
702
|
end
|
767
703
|
end
|
@@ -773,14 +709,14 @@ describe Mongoid::Criteria do
|
|
773
709
|
end
|
774
710
|
|
775
711
|
it "returns the criteria explain path" do
|
776
|
-
criteria.explain["cursor"].
|
712
|
+
expect(criteria.explain["cursor"]).to eq("BasicCursor")
|
777
713
|
end
|
778
714
|
end
|
779
715
|
|
780
716
|
describe "#extract_id" do
|
781
717
|
|
782
718
|
let(:id) do
|
783
|
-
|
719
|
+
BSON::ObjectId.new
|
784
720
|
end
|
785
721
|
|
786
722
|
context "when an id exists" do
|
@@ -792,7 +728,7 @@ describe Mongoid::Criteria do
|
|
792
728
|
end
|
793
729
|
|
794
730
|
it "returns the id" do
|
795
|
-
criteria.extract_id.
|
731
|
+
expect(criteria.extract_id).to eq(id)
|
796
732
|
end
|
797
733
|
end
|
798
734
|
|
@@ -805,7 +741,7 @@ describe Mongoid::Criteria do
|
|
805
741
|
end
|
806
742
|
|
807
743
|
it "returns the _id" do
|
808
|
-
criteria.extract_id.
|
744
|
+
expect(criteria.extract_id).to eq(id)
|
809
745
|
end
|
810
746
|
end
|
811
747
|
end
|
@@ -817,1612 +753,284 @@ describe Mongoid::Criteria do
|
|
817
753
|
end
|
818
754
|
|
819
755
|
it "returns the fields minus type" do
|
820
|
-
criteria.field_list.
|
756
|
+
expect(criteria.field_list).to eq([ "name" ])
|
821
757
|
end
|
822
758
|
end
|
823
759
|
|
824
|
-
describe "#
|
825
|
-
|
826
|
-
context "when finding by a document" do
|
760
|
+
describe "#find_and_modify" do
|
827
761
|
|
828
|
-
|
829
|
-
|
830
|
-
|
762
|
+
let!(:depeche) do
|
763
|
+
Band.create(name: "Depeche Mode")
|
764
|
+
end
|
831
765
|
|
832
|
-
|
833
|
-
|
834
|
-
|
766
|
+
let!(:tool) do
|
767
|
+
Band.create(name: "Tool")
|
768
|
+
end
|
835
769
|
|
836
|
-
|
770
|
+
context "when the selector matches" do
|
837
771
|
|
838
|
-
|
839
|
-
Band.find(band)
|
840
|
-
end
|
772
|
+
context "when the identity map is enabled" do
|
841
773
|
|
842
|
-
|
843
|
-
expect(found).to eq(band)
|
844
|
-
end
|
845
|
-
end
|
774
|
+
context "when returning the updated document" do
|
846
775
|
|
847
|
-
|
776
|
+
let(:criteria) do
|
777
|
+
Band.where(name: "Depeche Mode")
|
778
|
+
end
|
848
779
|
|
849
|
-
|
850
|
-
|
851
|
-
|
780
|
+
let(:result) do
|
781
|
+
criteria.find_and_modify({ "$inc" => { likes: 1 }}, new: true)
|
782
|
+
end
|
852
783
|
|
853
|
-
|
854
|
-
|
784
|
+
it "returns the first matching document" do
|
785
|
+
expect(result).to eq(depeche)
|
786
|
+
end
|
855
787
|
end
|
856
|
-
end
|
857
|
-
end
|
858
788
|
|
859
|
-
|
789
|
+
context "when not returning the updated document" do
|
860
790
|
|
861
|
-
|
862
|
-
|
863
|
-
|
791
|
+
let(:criteria) do
|
792
|
+
Band.where(name: "Depeche Mode")
|
793
|
+
end
|
864
794
|
|
865
|
-
|
866
|
-
|
867
|
-
|
795
|
+
let!(:result) do
|
796
|
+
criteria.find_and_modify("$inc" => { likes: 1 })
|
797
|
+
end
|
868
798
|
|
869
|
-
|
870
|
-
|
871
|
-
|
799
|
+
before do
|
800
|
+
depeche.reload
|
801
|
+
end
|
872
802
|
|
873
|
-
|
874
|
-
|
803
|
+
it "returns the first matching document" do
|
804
|
+
expect(result).to eq(depeche)
|
805
|
+
end
|
806
|
+
end
|
875
807
|
end
|
876
808
|
|
877
|
-
context "when
|
809
|
+
context "when not providing options" do
|
878
810
|
|
879
|
-
let
|
880
|
-
|
811
|
+
let(:criteria) do
|
812
|
+
Band.where(name: "Depeche Mode")
|
881
813
|
end
|
882
814
|
|
883
|
-
let!(:
|
884
|
-
|
815
|
+
let!(:result) do
|
816
|
+
criteria.find_and_modify("$inc" => { likes: 1 })
|
885
817
|
end
|
886
818
|
|
887
|
-
|
819
|
+
it "returns the first matching document" do
|
820
|
+
expect(result).to eq(depeche)
|
821
|
+
end
|
888
822
|
|
889
|
-
|
890
|
-
|
891
|
-
Browser.find(canvas.id)
|
892
|
-
}.to raise_error(Mongoid::Errors::DocumentNotFound)
|
893
|
-
end
|
823
|
+
it "updates the document in the database" do
|
824
|
+
expect(depeche.reload.likes).to eq(1)
|
894
825
|
end
|
895
826
|
end
|
896
827
|
|
897
|
-
context "when
|
828
|
+
context "when sorting" do
|
898
829
|
|
899
|
-
let(:
|
900
|
-
Band.
|
830
|
+
let(:criteria) do
|
831
|
+
Band.desc(:name)
|
901
832
|
end
|
902
833
|
|
903
|
-
|
904
|
-
|
834
|
+
let!(:result) do
|
835
|
+
criteria.find_and_modify("$inc" => { likes: 1 })
|
905
836
|
end
|
906
|
-
end
|
907
|
-
|
908
|
-
context "when providing a single id as a String" do
|
909
837
|
|
910
|
-
|
911
|
-
|
838
|
+
it "returns the first matching document" do
|
839
|
+
expect(result).to eq(tool)
|
912
840
|
end
|
913
841
|
|
914
|
-
it "
|
915
|
-
|
842
|
+
it "updates the document in the database" do
|
843
|
+
expect(tool.reload.likes).to eq(1)
|
916
844
|
end
|
917
845
|
end
|
918
846
|
|
919
|
-
context "when
|
847
|
+
context "when limiting fields" do
|
920
848
|
|
921
|
-
let(:
|
922
|
-
Band.
|
849
|
+
let(:criteria) do
|
850
|
+
Band.only(:_id)
|
923
851
|
end
|
924
852
|
|
925
|
-
|
926
|
-
|
853
|
+
let!(:result) do
|
854
|
+
criteria.find_and_modify("$inc" => { likes: 1 })
|
927
855
|
end
|
928
856
|
|
929
|
-
it "returns the
|
930
|
-
|
857
|
+
it "returns the first matching document" do
|
858
|
+
expect(result).to eq(depeche)
|
931
859
|
end
|
932
|
-
end
|
933
|
-
end
|
934
860
|
|
935
|
-
|
861
|
+
it "limits the returned fields" do
|
862
|
+
expect(result.name).to be_nil
|
863
|
+
end
|
936
864
|
|
937
|
-
|
938
|
-
|
865
|
+
it "updates the document in the database" do
|
866
|
+
expect(depeche.reload.likes).to eq(1)
|
867
|
+
end
|
939
868
|
end
|
940
869
|
|
941
|
-
context "when
|
870
|
+
context "when returning new" do
|
942
871
|
|
943
|
-
let
|
944
|
-
|
872
|
+
let(:criteria) do
|
873
|
+
Band.where(name: "Depeche Mode")
|
945
874
|
end
|
946
875
|
|
947
|
-
let!(:
|
948
|
-
|
876
|
+
let!(:result) do
|
877
|
+
criteria.find_and_modify({ "$inc" => { likes: 1 }}, new: true)
|
949
878
|
end
|
950
879
|
|
951
|
-
|
880
|
+
it "returns the first matching document" do
|
881
|
+
expect(result).to eq(depeche)
|
882
|
+
end
|
952
883
|
|
953
|
-
|
954
|
-
|
955
|
-
Browser.find(canvas.id)
|
956
|
-
}.to raise_error(Mongoid::Errors::DocumentNotFound)
|
957
|
-
end
|
884
|
+
it "returns the updated document" do
|
885
|
+
expect(result.likes).to eq(1)
|
958
886
|
end
|
959
887
|
end
|
960
888
|
|
961
|
-
context "when
|
889
|
+
context "when removing" do
|
962
890
|
|
963
|
-
|
891
|
+
let(:criteria) do
|
892
|
+
Band.where(name: "Depeche Mode")
|
893
|
+
end
|
964
894
|
|
965
|
-
|
966
|
-
|
967
|
-
|
895
|
+
let!(:result) do
|
896
|
+
criteria.find_and_modify({}, remove: true)
|
897
|
+
end
|
968
898
|
|
969
|
-
|
970
|
-
|
971
|
-
end
|
899
|
+
it "returns the first matching document" do
|
900
|
+
expect(result).to eq(depeche)
|
972
901
|
end
|
973
902
|
|
974
|
-
|
903
|
+
it "deletes the document from the database" do
|
904
|
+
expect {
|
905
|
+
depeche.reload
|
906
|
+
}.to raise_error(Mongoid::Errors::DocumentNotFound)
|
907
|
+
end
|
908
|
+
end
|
909
|
+
end
|
975
910
|
|
976
|
-
|
911
|
+
context "when the selector does not match" do
|
977
912
|
|
978
|
-
|
979
|
-
|
980
|
-
|
913
|
+
let(:criteria) do
|
914
|
+
Band.where(name: "Placebo")
|
915
|
+
end
|
981
916
|
|
982
|
-
|
983
|
-
|
984
|
-
|
917
|
+
let(:result) do
|
918
|
+
criteria.find_and_modify("$inc" => { likes: 1 })
|
919
|
+
end
|
985
920
|
|
986
|
-
|
987
|
-
|
988
|
-
|
989
|
-
|
990
|
-
|
991
|
-
end
|
921
|
+
it "returns nil" do
|
922
|
+
expect(result).to be_nil
|
923
|
+
end
|
924
|
+
end
|
925
|
+
end
|
992
926
|
|
993
|
-
|
927
|
+
describe "#freeze" do
|
994
928
|
|
995
|
-
|
996
|
-
|
997
|
-
|
929
|
+
let(:criteria) do
|
930
|
+
Band.all
|
931
|
+
end
|
998
932
|
|
999
|
-
|
1000
|
-
|
1001
|
-
|
933
|
+
before do
|
934
|
+
criteria.freeze
|
935
|
+
end
|
1002
936
|
|
1003
|
-
|
1004
|
-
|
1005
|
-
|
937
|
+
it "freezes the criteria" do
|
938
|
+
expect(criteria).to be_frozen
|
939
|
+
end
|
1006
940
|
|
1007
|
-
|
1008
|
-
|
1009
|
-
|
1010
|
-
end
|
1011
|
-
end
|
1012
|
-
end
|
941
|
+
it "initializes inclusions" do
|
942
|
+
expect(criteria.inclusions).to be_empty
|
943
|
+
end
|
1013
944
|
|
1014
|
-
|
945
|
+
it "initializes the context" do
|
946
|
+
expect(criteria.context).to_not be_nil
|
947
|
+
end
|
948
|
+
end
|
1015
949
|
|
1016
|
-
|
1017
|
-
Band.create(name: "Tool")
|
1018
|
-
end
|
950
|
+
describe "#geo_near" do
|
1019
951
|
|
1020
|
-
|
952
|
+
before do
|
953
|
+
Bar.create_indexes
|
954
|
+
end
|
1021
955
|
|
1022
|
-
|
1023
|
-
|
1024
|
-
|
956
|
+
let!(:match) do
|
957
|
+
Bar.create(location: [ 52.30, 13.25 ])
|
958
|
+
end
|
1025
959
|
|
1026
|
-
|
1027
|
-
|
1028
|
-
|
960
|
+
let(:criteria) do
|
961
|
+
Bar.geo_near([ 52, 13 ]).max_distance(10).spherical
|
962
|
+
end
|
1029
963
|
|
1030
|
-
|
1031
|
-
|
1032
|
-
|
964
|
+
it "returns the matching documents" do
|
965
|
+
expect(criteria).to eq([ match ])
|
966
|
+
end
|
967
|
+
end
|
1033
968
|
|
1034
|
-
|
1035
|
-
let(:found) do
|
1036
|
-
Band.find(band.id, band.id)
|
1037
|
-
end
|
969
|
+
describe "#gt" do
|
1038
970
|
|
1039
|
-
|
1040
|
-
|
1041
|
-
|
1042
|
-
end
|
1043
|
-
end
|
971
|
+
let!(:match) do
|
972
|
+
Band.create(member_count: 5)
|
973
|
+
end
|
1044
974
|
|
1045
|
-
|
975
|
+
let!(:non_match) do
|
976
|
+
Band.create(member_count: 1)
|
977
|
+
end
|
1046
978
|
|
1047
|
-
|
979
|
+
let(:criteria) do
|
980
|
+
Band.gt(member_count: 4)
|
981
|
+
end
|
1048
982
|
|
1049
|
-
|
1050
|
-
|
1051
|
-
|
983
|
+
it "returns the matching documents" do
|
984
|
+
expect(criteria).to eq([ match ])
|
985
|
+
end
|
986
|
+
end
|
1052
987
|
|
1053
|
-
|
1054
|
-
Band.find(band.id, Moped::BSON::ObjectId.new)
|
1055
|
-
end
|
988
|
+
describe "#gte" do
|
1056
989
|
|
1057
|
-
|
1058
|
-
|
1059
|
-
|
1060
|
-
}.to raise_error(Mongoid::Errors::DocumentNotFound)
|
1061
|
-
end
|
1062
|
-
end
|
990
|
+
let!(:match) do
|
991
|
+
Band.create(member_count: 5)
|
992
|
+
end
|
1063
993
|
|
1064
|
-
|
994
|
+
let!(:non_match) do
|
995
|
+
Band.create(member_count: 1)
|
996
|
+
end
|
1065
997
|
|
1066
|
-
|
1067
|
-
|
1068
|
-
|
998
|
+
let(:criteria) do
|
999
|
+
Band.gte(member_count: 5)
|
1000
|
+
end
|
1069
1001
|
|
1070
|
-
|
1071
|
-
|
1072
|
-
|
1002
|
+
it "returns the matching documents" do
|
1003
|
+
expect(criteria).to eq([ match ])
|
1004
|
+
end
|
1005
|
+
end
|
1073
1006
|
|
1074
|
-
|
1075
|
-
Band.find(band.id, Moped::BSON::ObjectId.new)
|
1076
|
-
end
|
1007
|
+
[ :in, :any_in ].each do |method|
|
1077
1008
|
|
1078
|
-
|
1079
|
-
found.should eq([ band ])
|
1080
|
-
end
|
1081
|
-
end
|
1082
|
-
end
|
1083
|
-
end
|
1009
|
+
describe "\##{method}" do
|
1084
1010
|
|
1085
|
-
context "when
|
1011
|
+
context "when querying on a normal field" do
|
1086
1012
|
|
1087
|
-
let!(:
|
1088
|
-
Band.create(
|
1013
|
+
let!(:match) do
|
1014
|
+
Band.create(genres: [ "electro", "dub" ])
|
1089
1015
|
end
|
1090
1016
|
|
1091
|
-
|
1017
|
+
let!(:non_match) do
|
1018
|
+
Band.create(genres: [ "house" ])
|
1019
|
+
end
|
1092
1020
|
|
1093
|
-
|
1094
|
-
|
1095
|
-
|
1096
|
-
|
1097
|
-
it "contains the first match" do
|
1098
|
-
found.should include(band)
|
1099
|
-
end
|
1100
|
-
|
1101
|
-
it "contains the second match" do
|
1102
|
-
found.should include(band_two)
|
1103
|
-
end
|
1104
|
-
|
1105
|
-
context "when ids are duplicates" do
|
1106
|
-
let(:found) do
|
1107
|
-
Band.find([ band.id, band.id ])
|
1108
|
-
end
|
1109
|
-
|
1110
|
-
it "contains only the first match" do
|
1111
|
-
found.should eq([band])
|
1112
|
-
end
|
1113
|
-
end
|
1114
|
-
end
|
1115
|
-
|
1116
|
-
context "when any id does not match" do
|
1117
|
-
|
1118
|
-
context "when raising a not found error" do
|
1119
|
-
|
1120
|
-
before do
|
1121
|
-
Mongoid.raise_not_found_error = true
|
1122
|
-
end
|
1123
|
-
|
1124
|
-
let(:found) do
|
1125
|
-
Band.find([ band.id, Moped::BSON::ObjectId.new ])
|
1126
|
-
end
|
1127
|
-
|
1128
|
-
it "raises an error" do
|
1129
|
-
expect {
|
1130
|
-
found
|
1131
|
-
}.to raise_error(Mongoid::Errors::DocumentNotFound)
|
1132
|
-
end
|
1133
|
-
end
|
1134
|
-
|
1135
|
-
context "when raising no error" do
|
1136
|
-
|
1137
|
-
before do
|
1138
|
-
Mongoid.raise_not_found_error = false
|
1139
|
-
end
|
1140
|
-
|
1141
|
-
after do
|
1142
|
-
Mongoid.raise_not_found_error = true
|
1143
|
-
end
|
1144
|
-
|
1145
|
-
let(:found) do
|
1146
|
-
Band.find([ band.id, Moped::BSON::ObjectId.new ])
|
1147
|
-
end
|
1148
|
-
|
1149
|
-
it "returns only the matching documents" do
|
1150
|
-
found.should eq([ band ])
|
1151
|
-
end
|
1152
|
-
end
|
1153
|
-
end
|
1154
|
-
end
|
1155
|
-
end
|
1156
|
-
|
1157
|
-
context "when using string ids" do
|
1158
|
-
|
1159
|
-
before(:all) do
|
1160
|
-
Band.field :_id, type: String
|
1161
|
-
end
|
1162
|
-
|
1163
|
-
after(:all) do
|
1164
|
-
Band.field :_id, type: Moped::BSON::ObjectId, default: ->{ Moped::BSON::ObjectId.new }
|
1165
|
-
end
|
1166
|
-
|
1167
|
-
let!(:band) do
|
1168
|
-
Band.create do |band|
|
1169
|
-
band.id = "tool"
|
1170
|
-
end
|
1171
|
-
end
|
1172
|
-
|
1173
|
-
context "when providing a single id" do
|
1174
|
-
|
1175
|
-
context "when the id matches" do
|
1176
|
-
|
1177
|
-
let(:found) do
|
1178
|
-
Band.find(band.id)
|
1179
|
-
end
|
1180
|
-
|
1181
|
-
it "returns the matching document" do
|
1182
|
-
found.should eq(band)
|
1183
|
-
end
|
1184
|
-
end
|
1185
|
-
|
1186
|
-
context "when the id does not match" do
|
1187
|
-
|
1188
|
-
context "when raising a not found error" do
|
1189
|
-
|
1190
|
-
before do
|
1191
|
-
Mongoid.raise_not_found_error = true
|
1192
|
-
end
|
1193
|
-
|
1194
|
-
let(:found) do
|
1195
|
-
Band.find("depeche-mode")
|
1196
|
-
end
|
1197
|
-
|
1198
|
-
it "raises an error" do
|
1199
|
-
expect {
|
1200
|
-
found
|
1201
|
-
}.to raise_error(Mongoid::Errors::DocumentNotFound)
|
1202
|
-
end
|
1203
|
-
end
|
1204
|
-
|
1205
|
-
context "when raising no error" do
|
1206
|
-
|
1207
|
-
before do
|
1208
|
-
Mongoid.raise_not_found_error = false
|
1209
|
-
end
|
1210
|
-
|
1211
|
-
after do
|
1212
|
-
Mongoid.raise_not_found_error = true
|
1213
|
-
end
|
1214
|
-
|
1215
|
-
let(:found) do
|
1216
|
-
Band.find("depeche-mode")
|
1217
|
-
end
|
1218
|
-
|
1219
|
-
it "returns nil" do
|
1220
|
-
found.should be_nil
|
1221
|
-
end
|
1222
|
-
end
|
1223
|
-
end
|
1224
|
-
end
|
1225
|
-
|
1226
|
-
context "when providing a splat of ids" do
|
1227
|
-
|
1228
|
-
let!(:band_two) do
|
1229
|
-
Band.create do |band|
|
1230
|
-
band.id = "depeche-mode"
|
1231
|
-
end
|
1232
|
-
end
|
1233
|
-
|
1234
|
-
context "when all ids match" do
|
1235
|
-
|
1236
|
-
let(:found) do
|
1237
|
-
Band.find(band.id, band_two.id)
|
1238
|
-
end
|
1239
|
-
|
1240
|
-
it "contains the first match" do
|
1241
|
-
found.should include(band)
|
1242
|
-
end
|
1243
|
-
|
1244
|
-
it "contains the second match" do
|
1245
|
-
found.should include(band_two)
|
1246
|
-
end
|
1247
|
-
end
|
1248
|
-
|
1249
|
-
context "when any id does not match" do
|
1250
|
-
|
1251
|
-
context "when raising a not found error" do
|
1252
|
-
|
1253
|
-
before do
|
1254
|
-
Mongoid.raise_not_found_error = true
|
1255
|
-
end
|
1256
|
-
|
1257
|
-
let(:found) do
|
1258
|
-
Band.find(band.id, "new-order")
|
1259
|
-
end
|
1260
|
-
|
1261
|
-
it "raises an error" do
|
1262
|
-
expect {
|
1263
|
-
found
|
1264
|
-
}.to raise_error(Mongoid::Errors::DocumentNotFound)
|
1265
|
-
end
|
1266
|
-
end
|
1267
|
-
|
1268
|
-
context "when raising no error" do
|
1269
|
-
|
1270
|
-
before do
|
1271
|
-
Mongoid.raise_not_found_error = false
|
1272
|
-
end
|
1273
|
-
|
1274
|
-
after do
|
1275
|
-
Mongoid.raise_not_found_error = true
|
1276
|
-
end
|
1277
|
-
|
1278
|
-
let(:found) do
|
1279
|
-
Band.find(band.id, "new-order")
|
1280
|
-
end
|
1281
|
-
|
1282
|
-
it "returns only the matching documents" do
|
1283
|
-
found.should eq([ band ])
|
1284
|
-
end
|
1285
|
-
end
|
1286
|
-
end
|
1287
|
-
end
|
1288
|
-
|
1289
|
-
context "when providing an array of ids" do
|
1290
|
-
|
1291
|
-
let!(:band_two) do
|
1292
|
-
Band.create do |band|
|
1293
|
-
band.id = "depeche-mode"
|
1294
|
-
end
|
1295
|
-
end
|
1296
|
-
|
1297
|
-
context "when all ids match" do
|
1298
|
-
|
1299
|
-
let(:found) do
|
1300
|
-
Band.find([ band.id, band_two.id ])
|
1301
|
-
end
|
1302
|
-
|
1303
|
-
it "contains the first match" do
|
1304
|
-
found.should include(band)
|
1305
|
-
end
|
1306
|
-
|
1307
|
-
it "contains the second match" do
|
1308
|
-
found.should include(band_two)
|
1309
|
-
end
|
1310
|
-
end
|
1311
|
-
|
1312
|
-
context "when any id does not match" do
|
1313
|
-
|
1314
|
-
context "when raising a not found error" do
|
1315
|
-
|
1316
|
-
before do
|
1317
|
-
Mongoid.raise_not_found_error = true
|
1318
|
-
end
|
1319
|
-
|
1320
|
-
let(:found) do
|
1321
|
-
Band.find([ band.id, "new-order" ])
|
1322
|
-
end
|
1323
|
-
|
1324
|
-
it "raises an error" do
|
1325
|
-
expect {
|
1326
|
-
found
|
1327
|
-
}.to raise_error(Mongoid::Errors::DocumentNotFound)
|
1328
|
-
end
|
1329
|
-
end
|
1330
|
-
|
1331
|
-
context "when raising no error" do
|
1332
|
-
|
1333
|
-
before do
|
1334
|
-
Mongoid.raise_not_found_error = false
|
1335
|
-
end
|
1336
|
-
|
1337
|
-
after do
|
1338
|
-
Mongoid.raise_not_found_error = true
|
1339
|
-
end
|
1340
|
-
|
1341
|
-
let(:found) do
|
1342
|
-
Band.find([ band.id, "new-order" ])
|
1343
|
-
end
|
1344
|
-
|
1345
|
-
it "returns only the matching documents" do
|
1346
|
-
found.should eq([ band ])
|
1347
|
-
end
|
1348
|
-
end
|
1349
|
-
end
|
1350
|
-
end
|
1351
|
-
end
|
1352
|
-
|
1353
|
-
context "when using hash ids" do
|
1354
|
-
|
1355
|
-
before(:all) do
|
1356
|
-
Band.field :_id, type: Hash
|
1357
|
-
end
|
1358
|
-
|
1359
|
-
after(:all) do
|
1360
|
-
Band.field :_id, type: Moped::BSON::ObjectId, default: ->{ Moped::BSON::ObjectId.new }
|
1361
|
-
end
|
1362
|
-
|
1363
|
-
let!(:band) do
|
1364
|
-
Band.create do |band|
|
1365
|
-
band.id = {"new-order" => true, "Depeche Mode" => false}
|
1366
|
-
end
|
1367
|
-
end
|
1368
|
-
|
1369
|
-
context "when providing a single id" do
|
1370
|
-
|
1371
|
-
context "when the id matches" do
|
1372
|
-
|
1373
|
-
let(:found) do
|
1374
|
-
Band.find(band.id)
|
1375
|
-
end
|
1376
|
-
|
1377
|
-
it "returns the matching document" do
|
1378
|
-
found.should eq(band)
|
1379
|
-
end
|
1380
|
-
end
|
1381
|
-
|
1382
|
-
context "when the id does not match" do
|
1383
|
-
|
1384
|
-
context "when raising a not found error" do
|
1385
|
-
|
1386
|
-
before do
|
1387
|
-
Mongoid.raise_not_found_error = true
|
1388
|
-
end
|
1389
|
-
|
1390
|
-
let(:found) do
|
1391
|
-
Band.find({"new-order" => false, "Faith no More" => true})
|
1392
|
-
end
|
1393
|
-
|
1394
|
-
it "raises an error" do
|
1395
|
-
expect {
|
1396
|
-
found
|
1397
|
-
}.to raise_error(Mongoid::Errors::DocumentNotFound)
|
1398
|
-
end
|
1399
|
-
end
|
1400
|
-
|
1401
|
-
context "when raising no error" do
|
1402
|
-
|
1403
|
-
before do
|
1404
|
-
Mongoid.raise_not_found_error = false
|
1405
|
-
end
|
1406
|
-
|
1407
|
-
after do
|
1408
|
-
Mongoid.raise_not_found_error = true
|
1409
|
-
end
|
1410
|
-
|
1411
|
-
let(:found) do
|
1412
|
-
Band.find({"new-order" => false, "Faith no More" => true})
|
1413
|
-
end
|
1414
|
-
|
1415
|
-
it "returns nil" do
|
1416
|
-
found.should be_nil
|
1417
|
-
end
|
1418
|
-
end
|
1419
|
-
end
|
1420
|
-
end
|
1421
|
-
|
1422
|
-
context "when providing a splat of ids" do
|
1423
|
-
|
1424
|
-
let!(:band_two) do
|
1425
|
-
Band.create do |band|
|
1426
|
-
band.id = {"Radiohead" => false, "Nirvana"=> true}
|
1427
|
-
end
|
1428
|
-
end
|
1429
|
-
|
1430
|
-
context "when all ids match" do
|
1431
|
-
|
1432
|
-
let(:found) do
|
1433
|
-
Band.find(band.id, band_two.id)
|
1434
|
-
end
|
1435
|
-
|
1436
|
-
it "contains the first match" do
|
1437
|
-
found.should include(band)
|
1438
|
-
end
|
1439
|
-
|
1440
|
-
it "contains the second match" do
|
1441
|
-
found.should include(band_two)
|
1442
|
-
end
|
1443
|
-
end
|
1444
|
-
|
1445
|
-
context "when any id does not match" do
|
1446
|
-
|
1447
|
-
context "when raising a not found error" do
|
1448
|
-
|
1449
|
-
before do
|
1450
|
-
Mongoid.raise_not_found_error = true
|
1451
|
-
end
|
1452
|
-
|
1453
|
-
let(:found) do
|
1454
|
-
Band.find(band.id, {"Radiohead" => true, "Nirvana"=> false})
|
1455
|
-
end
|
1456
|
-
|
1457
|
-
it "raises an error" do
|
1458
|
-
expect {
|
1459
|
-
found
|
1460
|
-
}.to raise_error(Mongoid::Errors::DocumentNotFound)
|
1461
|
-
end
|
1462
|
-
end
|
1463
|
-
|
1464
|
-
context "when raising no error" do
|
1465
|
-
|
1466
|
-
before do
|
1467
|
-
Mongoid.raise_not_found_error = false
|
1468
|
-
end
|
1469
|
-
|
1470
|
-
after do
|
1471
|
-
Mongoid.raise_not_found_error = true
|
1472
|
-
end
|
1473
|
-
|
1474
|
-
let(:found) do
|
1475
|
-
Band.find(band.id, {"Radiohead" => true, "Nirvana"=> false})
|
1476
|
-
end
|
1477
|
-
|
1478
|
-
it "returns only the matching documents" do
|
1479
|
-
found.should eq([ band ])
|
1480
|
-
end
|
1481
|
-
end
|
1482
|
-
end
|
1483
|
-
end
|
1484
|
-
|
1485
|
-
context "when providing an array of ids" do
|
1486
|
-
|
1487
|
-
let!(:band_two) do
|
1488
|
-
Band.create do |band|
|
1489
|
-
band.id = {"Radiohead" => false, "Nirvana"=> true}
|
1490
|
-
end
|
1491
|
-
end
|
1492
|
-
|
1493
|
-
context "when all ids match" do
|
1494
|
-
|
1495
|
-
let(:found) do
|
1496
|
-
Band.find([ band.id, band_two.id ])
|
1497
|
-
end
|
1498
|
-
|
1499
|
-
it "contains the first match" do
|
1500
|
-
found.should include(band)
|
1501
|
-
end
|
1502
|
-
|
1503
|
-
it "contains the second match" do
|
1504
|
-
found.should include(band_two)
|
1505
|
-
end
|
1506
|
-
end
|
1507
|
-
|
1508
|
-
context "when any id does not match" do
|
1509
|
-
|
1510
|
-
context "when raising a not found error" do
|
1511
|
-
|
1512
|
-
before do
|
1513
|
-
Mongoid.raise_not_found_error = true
|
1514
|
-
end
|
1515
|
-
|
1516
|
-
let(:found) do
|
1517
|
-
Band.find([ band.id, {"Radiohead" => true, "Nirvana"=> false} ])
|
1518
|
-
end
|
1519
|
-
|
1520
|
-
it "raises an error" do
|
1521
|
-
expect {
|
1522
|
-
found
|
1523
|
-
}.to raise_error(Mongoid::Errors::DocumentNotFound)
|
1524
|
-
end
|
1525
|
-
end
|
1526
|
-
|
1527
|
-
context "when raising no error" do
|
1528
|
-
|
1529
|
-
before do
|
1530
|
-
Mongoid.raise_not_found_error = false
|
1531
|
-
end
|
1532
|
-
|
1533
|
-
after do
|
1534
|
-
Mongoid.raise_not_found_error = true
|
1535
|
-
end
|
1536
|
-
|
1537
|
-
let(:found) do
|
1538
|
-
Band.find([ band.id, {"Radiohead" => true, "Nirvana"=> false} ])
|
1539
|
-
end
|
1540
|
-
|
1541
|
-
it "returns only the matching documents" do
|
1542
|
-
found.should eq([ band ])
|
1543
|
-
end
|
1544
|
-
end
|
1545
|
-
end
|
1546
|
-
end
|
1547
|
-
end
|
1548
|
-
|
1549
|
-
context "when using integer ids" do
|
1550
|
-
|
1551
|
-
before(:all) do
|
1552
|
-
Band.field :_id, type: Integer
|
1553
|
-
end
|
1554
|
-
|
1555
|
-
after(:all) do
|
1556
|
-
Band.field :_id, type: Moped::BSON::ObjectId, default: ->{ Moped::BSON::ObjectId.new }
|
1557
|
-
end
|
1558
|
-
|
1559
|
-
let!(:band) do
|
1560
|
-
Band.create do |band|
|
1561
|
-
band.id = 1
|
1562
|
-
end
|
1563
|
-
end
|
1564
|
-
|
1565
|
-
context "when providing a single id" do
|
1566
|
-
|
1567
|
-
context "when the id matches" do
|
1568
|
-
|
1569
|
-
let(:found) do
|
1570
|
-
Band.find(band.id)
|
1571
|
-
end
|
1572
|
-
|
1573
|
-
it "returns the matching document" do
|
1574
|
-
found.should eq(band)
|
1575
|
-
end
|
1576
|
-
end
|
1577
|
-
|
1578
|
-
context "when the id does not match" do
|
1579
|
-
|
1580
|
-
context "when raising a not found error" do
|
1581
|
-
|
1582
|
-
before do
|
1583
|
-
Mongoid.raise_not_found_error = true
|
1584
|
-
end
|
1585
|
-
|
1586
|
-
let(:found) do
|
1587
|
-
Band.find(3)
|
1588
|
-
end
|
1589
|
-
|
1590
|
-
it "raises an error" do
|
1591
|
-
expect {
|
1592
|
-
found
|
1593
|
-
}.to raise_error(Mongoid::Errors::DocumentNotFound)
|
1594
|
-
end
|
1595
|
-
end
|
1596
|
-
|
1597
|
-
context "when raising no error" do
|
1598
|
-
|
1599
|
-
before do
|
1600
|
-
Mongoid.raise_not_found_error = false
|
1601
|
-
end
|
1602
|
-
|
1603
|
-
after do
|
1604
|
-
Mongoid.raise_not_found_error = true
|
1605
|
-
end
|
1606
|
-
|
1607
|
-
let(:found) do
|
1608
|
-
Band.find(3)
|
1609
|
-
end
|
1610
|
-
|
1611
|
-
it "returns nil" do
|
1612
|
-
found.should be_nil
|
1613
|
-
end
|
1614
|
-
end
|
1615
|
-
end
|
1616
|
-
end
|
1617
|
-
|
1618
|
-
context "when providing a splat of ids" do
|
1619
|
-
|
1620
|
-
let!(:band_two) do
|
1621
|
-
Band.create do |band|
|
1622
|
-
band.id = 2
|
1623
|
-
end
|
1624
|
-
end
|
1625
|
-
|
1626
|
-
context "when all ids match" do
|
1627
|
-
|
1628
|
-
let(:found) do
|
1629
|
-
Band.find(band.id, band_two.id)
|
1630
|
-
end
|
1631
|
-
|
1632
|
-
it "contains the first match" do
|
1633
|
-
found.should include(band)
|
1634
|
-
end
|
1635
|
-
|
1636
|
-
it "contains the second match" do
|
1637
|
-
found.should include(band_two)
|
1638
|
-
end
|
1639
|
-
end
|
1640
|
-
|
1641
|
-
context "when any id does not match" do
|
1642
|
-
|
1643
|
-
context "when raising a not found error" do
|
1644
|
-
|
1645
|
-
before do
|
1646
|
-
Mongoid.raise_not_found_error = true
|
1647
|
-
end
|
1648
|
-
|
1649
|
-
let(:found) do
|
1650
|
-
Band.find(band.id, 3)
|
1651
|
-
end
|
1652
|
-
|
1653
|
-
it "raises an error" do
|
1654
|
-
expect {
|
1655
|
-
found
|
1656
|
-
}.to raise_error(Mongoid::Errors::DocumentNotFound)
|
1657
|
-
end
|
1658
|
-
end
|
1659
|
-
|
1660
|
-
context "when raising no error" do
|
1661
|
-
|
1662
|
-
before do
|
1663
|
-
Mongoid.raise_not_found_error = false
|
1664
|
-
end
|
1665
|
-
|
1666
|
-
after do
|
1667
|
-
Mongoid.raise_not_found_error = true
|
1668
|
-
end
|
1669
|
-
|
1670
|
-
let(:found) do
|
1671
|
-
Band.find(band.id, 3)
|
1672
|
-
end
|
1673
|
-
|
1674
|
-
it "returns only the matching documents" do
|
1675
|
-
found.should eq([ band ])
|
1676
|
-
end
|
1677
|
-
end
|
1678
|
-
end
|
1679
|
-
end
|
1680
|
-
|
1681
|
-
context "when providing an array of ids" do
|
1682
|
-
|
1683
|
-
let!(:band_two) do
|
1684
|
-
Band.create do |band|
|
1685
|
-
band.id = 2
|
1686
|
-
end
|
1687
|
-
end
|
1688
|
-
|
1689
|
-
context "when all ids match" do
|
1690
|
-
|
1691
|
-
let(:found) do
|
1692
|
-
Band.find([ band.id, band_two.id ])
|
1693
|
-
end
|
1694
|
-
|
1695
|
-
it "contains the first match" do
|
1696
|
-
found.should include(band)
|
1697
|
-
end
|
1698
|
-
|
1699
|
-
it "contains the second match" do
|
1700
|
-
found.should include(band_two)
|
1701
|
-
end
|
1702
|
-
end
|
1703
|
-
|
1704
|
-
context "when any id does not match" do
|
1705
|
-
|
1706
|
-
context "when raising a not found error" do
|
1707
|
-
|
1708
|
-
before do
|
1709
|
-
Mongoid.raise_not_found_error = true
|
1710
|
-
end
|
1711
|
-
|
1712
|
-
let(:found) do
|
1713
|
-
Band.find([ band.id, 3 ])
|
1714
|
-
end
|
1715
|
-
|
1716
|
-
it "raises an error" do
|
1717
|
-
expect {
|
1718
|
-
found
|
1719
|
-
}.to raise_error(Mongoid::Errors::DocumentNotFound)
|
1720
|
-
end
|
1721
|
-
end
|
1722
|
-
|
1723
|
-
context "when raising no error" do
|
1724
|
-
|
1725
|
-
before do
|
1726
|
-
Mongoid.raise_not_found_error = false
|
1727
|
-
end
|
1728
|
-
|
1729
|
-
after do
|
1730
|
-
Mongoid.raise_not_found_error = true
|
1731
|
-
end
|
1732
|
-
|
1733
|
-
let(:found) do
|
1734
|
-
Band.find([ band.id, 3 ])
|
1735
|
-
end
|
1736
|
-
|
1737
|
-
it "returns only the matching documents" do
|
1738
|
-
found.should eq([ band ])
|
1739
|
-
end
|
1740
|
-
end
|
1741
|
-
end
|
1742
|
-
end
|
1743
|
-
|
1744
|
-
context "when providing a range" do
|
1745
|
-
|
1746
|
-
let!(:band_two) do
|
1747
|
-
Band.create do |band|
|
1748
|
-
band.id = 2
|
1749
|
-
end
|
1750
|
-
end
|
1751
|
-
|
1752
|
-
context "when all ids match" do
|
1753
|
-
|
1754
|
-
let(:found) do
|
1755
|
-
Band.find(1..2)
|
1756
|
-
end
|
1757
|
-
|
1758
|
-
it "contains the first match" do
|
1759
|
-
found.should include(band)
|
1760
|
-
end
|
1761
|
-
|
1762
|
-
it "contains the second match" do
|
1763
|
-
found.should include(band_two)
|
1764
|
-
end
|
1765
|
-
end
|
1766
|
-
|
1767
|
-
context "when any id does not match" do
|
1768
|
-
|
1769
|
-
context "when raising a not found error" do
|
1770
|
-
|
1771
|
-
before do
|
1772
|
-
Mongoid.raise_not_found_error = true
|
1773
|
-
end
|
1774
|
-
|
1775
|
-
let(:found) do
|
1776
|
-
Band.find(1..3)
|
1777
|
-
end
|
1778
|
-
|
1779
|
-
it "raises an error" do
|
1780
|
-
expect {
|
1781
|
-
found
|
1782
|
-
}.to raise_error(Mongoid::Errors::DocumentNotFound)
|
1783
|
-
end
|
1784
|
-
end
|
1785
|
-
|
1786
|
-
context "when raising no error" do
|
1787
|
-
|
1788
|
-
before do
|
1789
|
-
Mongoid.raise_not_found_error = false
|
1790
|
-
end
|
1791
|
-
|
1792
|
-
after do
|
1793
|
-
Mongoid.raise_not_found_error = true
|
1794
|
-
end
|
1795
|
-
|
1796
|
-
let(:found) do
|
1797
|
-
Band.find(1..3)
|
1798
|
-
end
|
1799
|
-
|
1800
|
-
it "contains the first match" do
|
1801
|
-
found.should include(band)
|
1802
|
-
end
|
1803
|
-
|
1804
|
-
it "contains the second match" do
|
1805
|
-
found.should include(band_two)
|
1806
|
-
end
|
1807
|
-
|
1808
|
-
it "returns only the matches" do
|
1809
|
-
found.count.should eq(2)
|
1810
|
-
end
|
1811
|
-
end
|
1812
|
-
end
|
1813
|
-
end
|
1814
|
-
end
|
1815
|
-
|
1816
|
-
context "when using string and object ids" do
|
1817
|
-
|
1818
|
-
let!(:band) do
|
1819
|
-
Band.create
|
1820
|
-
end
|
1821
|
-
|
1822
|
-
context "when providing multiple ids" do
|
1823
|
-
|
1824
|
-
context "when ids are duplicates" do
|
1825
|
-
|
1826
|
-
let(:found) do
|
1827
|
-
Band.find([ band.id.to_s, band.id ])
|
1828
|
-
end
|
1829
|
-
|
1830
|
-
it "contains only the first match" do
|
1831
|
-
found.should eq([band])
|
1832
|
-
end
|
1833
|
-
end
|
1834
|
-
end
|
1835
|
-
end
|
1836
|
-
end
|
1837
|
-
|
1838
|
-
describe "#find_and_modify" do
|
1839
|
-
|
1840
|
-
let!(:depeche) do
|
1841
|
-
Band.create(name: "Depeche Mode")
|
1842
|
-
end
|
1843
|
-
|
1844
|
-
let!(:tool) do
|
1845
|
-
Band.create(name: "Tool")
|
1846
|
-
end
|
1847
|
-
|
1848
|
-
context "when the selector matches" do
|
1849
|
-
|
1850
|
-
context "when the identity map is enabled" do
|
1851
|
-
|
1852
|
-
before(:all) do
|
1853
|
-
Mongoid.identity_map_enabled = true
|
1854
|
-
end
|
1855
|
-
|
1856
|
-
after(:all) do
|
1857
|
-
Mongoid.identity_map_enabled = false
|
1858
|
-
end
|
1859
|
-
|
1860
|
-
context "when returning the updated document" do
|
1861
|
-
|
1862
|
-
let(:criteria) do
|
1863
|
-
Band.where(name: "Depeche Mode")
|
1864
|
-
end
|
1865
|
-
|
1866
|
-
let(:result) do
|
1867
|
-
criteria.find_and_modify({ "$inc" => { likes: 1 }}, new: true)
|
1868
|
-
end
|
1869
|
-
|
1870
|
-
it "returns the first matching document" do
|
1871
|
-
result.should eq(depeche)
|
1872
|
-
end
|
1873
|
-
|
1874
|
-
it "updates the document in the identity map" do
|
1875
|
-
Mongoid::IdentityMap.get(Band, result.id).likes.should eq(1)
|
1876
|
-
end
|
1877
|
-
end
|
1878
|
-
|
1879
|
-
context "when not returning the updated document" do
|
1880
|
-
|
1881
|
-
let(:criteria) do
|
1882
|
-
Band.where(name: "Depeche Mode")
|
1883
|
-
end
|
1884
|
-
|
1885
|
-
let!(:result) do
|
1886
|
-
criteria.find_and_modify("$inc" => { likes: 1 })
|
1887
|
-
end
|
1888
|
-
|
1889
|
-
before do
|
1890
|
-
depeche.reload
|
1891
|
-
end
|
1892
|
-
|
1893
|
-
it "returns the first matching document" do
|
1894
|
-
result.should eq(depeche)
|
1895
|
-
end
|
1896
|
-
|
1897
|
-
it "updates the document in the identity map" do
|
1898
|
-
Mongoid::IdentityMap.get(Band, depeche.id).likes.should eq(1)
|
1899
|
-
end
|
1900
|
-
end
|
1901
|
-
end
|
1902
|
-
|
1903
|
-
context "when not providing options" do
|
1904
|
-
|
1905
|
-
let(:criteria) do
|
1906
|
-
Band.where(name: "Depeche Mode")
|
1907
|
-
end
|
1908
|
-
|
1909
|
-
let!(:result) do
|
1910
|
-
criteria.find_and_modify("$inc" => { likes: 1 })
|
1911
|
-
end
|
1912
|
-
|
1913
|
-
it "returns the first matching document" do
|
1914
|
-
result.should eq(depeche)
|
1915
|
-
end
|
1916
|
-
|
1917
|
-
it "updates the document in the database" do
|
1918
|
-
depeche.reload.likes.should eq(1)
|
1919
|
-
end
|
1920
|
-
end
|
1921
|
-
|
1922
|
-
context "when sorting" do
|
1923
|
-
|
1924
|
-
let(:criteria) do
|
1925
|
-
Band.desc(:name)
|
1926
|
-
end
|
1927
|
-
|
1928
|
-
let!(:result) do
|
1929
|
-
criteria.find_and_modify("$inc" => { likes: 1 })
|
1930
|
-
end
|
1931
|
-
|
1932
|
-
it "returns the first matching document" do
|
1933
|
-
result.should eq(tool)
|
1934
|
-
end
|
1935
|
-
|
1936
|
-
it "updates the document in the database" do
|
1937
|
-
tool.reload.likes.should eq(1)
|
1938
|
-
end
|
1939
|
-
end
|
1940
|
-
|
1941
|
-
context "when limiting fields" do
|
1942
|
-
|
1943
|
-
let(:criteria) do
|
1944
|
-
Band.only(:_id)
|
1945
|
-
end
|
1946
|
-
|
1947
|
-
let!(:result) do
|
1948
|
-
criteria.find_and_modify("$inc" => { likes: 1 })
|
1949
|
-
end
|
1950
|
-
|
1951
|
-
it "returns the first matching document" do
|
1952
|
-
result.should eq(depeche)
|
1953
|
-
end
|
1954
|
-
|
1955
|
-
it "limits the returned fields" do
|
1956
|
-
result.name.should be_nil
|
1957
|
-
end
|
1958
|
-
|
1959
|
-
it "updates the document in the database" do
|
1960
|
-
depeche.reload.likes.should eq(1)
|
1961
|
-
end
|
1962
|
-
end
|
1963
|
-
|
1964
|
-
context "when returning new" do
|
1965
|
-
|
1966
|
-
let(:criteria) do
|
1967
|
-
Band.where(name: "Depeche Mode")
|
1968
|
-
end
|
1969
|
-
|
1970
|
-
let!(:result) do
|
1971
|
-
criteria.find_and_modify({ "$inc" => { likes: 1 }}, new: true)
|
1972
|
-
end
|
1973
|
-
|
1974
|
-
it "returns the first matching document" do
|
1975
|
-
result.should eq(depeche)
|
1976
|
-
end
|
1977
|
-
|
1978
|
-
it "returns the updated document" do
|
1979
|
-
result.likes.should eq(1)
|
1980
|
-
end
|
1981
|
-
end
|
1982
|
-
|
1983
|
-
context "when removing" do
|
1984
|
-
|
1985
|
-
let(:criteria) do
|
1986
|
-
Band.where(name: "Depeche Mode")
|
1987
|
-
end
|
1988
|
-
|
1989
|
-
let!(:result) do
|
1990
|
-
criteria.find_and_modify({}, remove: true)
|
1991
|
-
end
|
1992
|
-
|
1993
|
-
it "returns the first matching document" do
|
1994
|
-
result.should eq(depeche)
|
1995
|
-
end
|
1996
|
-
|
1997
|
-
it "deletes the document from the database" do
|
1998
|
-
expect {
|
1999
|
-
depeche.reload
|
2000
|
-
}.to raise_error(Mongoid::Errors::DocumentNotFound)
|
2001
|
-
end
|
2002
|
-
end
|
2003
|
-
end
|
2004
|
-
|
2005
|
-
context "when the selector does not match" do
|
2006
|
-
|
2007
|
-
let(:criteria) do
|
2008
|
-
Band.where(name: "Placebo")
|
2009
|
-
end
|
2010
|
-
|
2011
|
-
let(:result) do
|
2012
|
-
criteria.find_and_modify("$inc" => { likes: 1 })
|
2013
|
-
end
|
2014
|
-
|
2015
|
-
it "returns nil" do
|
2016
|
-
result.should be_nil
|
2017
|
-
end
|
2018
|
-
end
|
2019
|
-
end
|
2020
|
-
|
2021
|
-
describe "first_or_initialize" do
|
2022
|
-
|
2023
|
-
let!(:band) do
|
2024
|
-
Band.create(name: "Depeche Mode")
|
2025
|
-
end
|
2026
|
-
|
2027
|
-
context "when the document is found" do
|
2028
|
-
|
2029
|
-
let(:found) do
|
2030
|
-
Band.where(name: "Depeche Mode").first_or_initialize
|
2031
|
-
end
|
2032
|
-
|
2033
|
-
it "returns the document" do
|
2034
|
-
found.should eq(band)
|
2035
|
-
end
|
2036
|
-
end
|
2037
|
-
|
2038
|
-
context "when the document is not found" do
|
2039
|
-
|
2040
|
-
context "when attributes are provided" do
|
2041
|
-
|
2042
|
-
let(:document) do
|
2043
|
-
Band.where(name: "Tool").first_or_initialize(origin: "Essex")
|
2044
|
-
end
|
2045
|
-
|
2046
|
-
it "returns a new document" do
|
2047
|
-
document.name.should eq("Tool")
|
2048
|
-
end
|
2049
|
-
|
2050
|
-
it "returns a non persisted document" do
|
2051
|
-
document.should_not be_persisted
|
2052
|
-
end
|
2053
|
-
|
2054
|
-
it "sets the additional attributes" do
|
2055
|
-
document.origin.should eq("Essex")
|
2056
|
-
end
|
2057
|
-
end
|
2058
|
-
|
2059
|
-
context "when attributes are not provided" do
|
2060
|
-
|
2061
|
-
let(:document) do
|
2062
|
-
Band.where(name: "Tool").first_or_initialize
|
2063
|
-
end
|
2064
|
-
|
2065
|
-
it "returns a new document" do
|
2066
|
-
document.name.should eq("Tool")
|
2067
|
-
end
|
2068
|
-
|
2069
|
-
it "returns a non persisted document" do
|
2070
|
-
document.should_not be_persisted
|
2071
|
-
end
|
2072
|
-
end
|
2073
|
-
|
2074
|
-
context "when a block is provided" do
|
2075
|
-
|
2076
|
-
let(:document) do
|
2077
|
-
Band.where(name: "Tool").first_or_initialize do |doc|
|
2078
|
-
doc.active = false
|
2079
|
-
end
|
2080
|
-
end
|
2081
|
-
|
2082
|
-
it "returns a new document" do
|
2083
|
-
document.name.should eq("Tool")
|
2084
|
-
end
|
2085
|
-
|
2086
|
-
it "returns a non persisted document" do
|
2087
|
-
document.should_not be_persisted
|
2088
|
-
end
|
2089
|
-
|
2090
|
-
it "yields to the block" do
|
2091
|
-
document.active.should be_false
|
2092
|
-
end
|
2093
|
-
end
|
2094
|
-
|
2095
|
-
context "when the criteria is complex" do
|
2096
|
-
|
2097
|
-
context "when the document is not found" do
|
2098
|
-
|
2099
|
-
let(:document) do
|
2100
|
-
Band.in(name: [ "New Order" ]).first_or_initialize(active: false)
|
2101
|
-
end
|
2102
|
-
|
2103
|
-
it "returns a new document" do
|
2104
|
-
document.active.should be_false
|
2105
|
-
end
|
2106
|
-
|
2107
|
-
it "returns a non persisted document" do
|
2108
|
-
document.should_not be_persisted
|
2109
|
-
end
|
2110
|
-
end
|
2111
|
-
end
|
2112
|
-
end
|
2113
|
-
end
|
2114
|
-
|
2115
|
-
describe "#freeze" do
|
2116
|
-
|
2117
|
-
let(:criteria) do
|
2118
|
-
Band.all
|
2119
|
-
end
|
2120
|
-
|
2121
|
-
before do
|
2122
|
-
criteria.freeze
|
2123
|
-
end
|
2124
|
-
|
2125
|
-
it "freezes the criteria" do
|
2126
|
-
criteria.should be_frozen
|
2127
|
-
end
|
2128
|
-
|
2129
|
-
it "initializes inclusions" do
|
2130
|
-
criteria.inclusions.should be_empty
|
2131
|
-
end
|
2132
|
-
|
2133
|
-
it "initializes the context" do
|
2134
|
-
criteria.context.should_not be_nil
|
2135
|
-
end
|
2136
|
-
end
|
2137
|
-
|
2138
|
-
describe "#for_ids" do
|
2139
|
-
|
2140
|
-
context "when only 1 id exists" do
|
2141
|
-
|
2142
|
-
let(:id) do
|
2143
|
-
Moped::BSON::ObjectId.new
|
2144
|
-
end
|
2145
|
-
|
2146
|
-
let(:criteria) do
|
2147
|
-
Band.queryable.for_ids([ id ])
|
2148
|
-
end
|
2149
|
-
|
2150
|
-
it "does not turn the selector into an $in" do
|
2151
|
-
criteria.selector.should eq({ "_id" => id })
|
2152
|
-
end
|
2153
|
-
end
|
2154
|
-
end
|
2155
|
-
|
2156
|
-
describe "#from_map_or_db" do
|
2157
|
-
|
2158
|
-
before(:all) do
|
2159
|
-
Mongoid.identity_map_enabled = true
|
2160
|
-
end
|
2161
|
-
|
2162
|
-
after(:all) do
|
2163
|
-
Mongoid.identity_map_enabled = false
|
2164
|
-
end
|
2165
|
-
|
2166
|
-
context "when the document is in the identity map" do
|
2167
|
-
|
2168
|
-
let!(:band) do
|
2169
|
-
Band.create(name: "Depeche Mode")
|
2170
|
-
end
|
2171
|
-
|
2172
|
-
let(:criteria) do
|
2173
|
-
Band.where(_id: band.id)
|
2174
|
-
end
|
2175
|
-
|
2176
|
-
let(:from_map) do
|
2177
|
-
criteria.from_map_or_db
|
2178
|
-
end
|
2179
|
-
|
2180
|
-
it "returns the document from the map" do
|
2181
|
-
from_map.should equal(band)
|
2182
|
-
end
|
2183
|
-
end
|
2184
|
-
|
2185
|
-
context "when the document is not in the identity map" do
|
2186
|
-
|
2187
|
-
let!(:band) do
|
2188
|
-
Band.create(name: "Depeche Mode")
|
2189
|
-
end
|
2190
|
-
|
2191
|
-
let(:criteria) do
|
2192
|
-
Band.where(_id: band.id)
|
2193
|
-
end
|
2194
|
-
|
2195
|
-
before do
|
2196
|
-
Mongoid::IdentityMap.clear
|
2197
|
-
end
|
2198
|
-
|
2199
|
-
let(:from_db) do
|
2200
|
-
criteria.from_map_or_db
|
2201
|
-
end
|
2202
|
-
|
2203
|
-
it "returns the document from the database" do
|
2204
|
-
from_db.should_not equal(band)
|
2205
|
-
end
|
2206
|
-
|
2207
|
-
it "returns the correct document" do
|
2208
|
-
from_db.should eq(band)
|
2209
|
-
end
|
2210
|
-
end
|
2211
|
-
|
2212
|
-
context "when the selector is cleared in the identity map" do
|
2213
|
-
|
2214
|
-
let!(:band) do
|
2215
|
-
Band.create(name: "Depeche Mode")
|
2216
|
-
end
|
2217
|
-
|
2218
|
-
let(:criteria) do
|
2219
|
-
Band.where(name: "Depeche Mode")
|
2220
|
-
end
|
2221
|
-
|
2222
|
-
before do
|
2223
|
-
Mongoid::IdentityMap.clear
|
2224
|
-
Mongoid::IdentityMap.clear_many(Band, { "name" => "Depeche Mode" })
|
2225
|
-
end
|
2226
|
-
|
2227
|
-
let(:from_db) do
|
2228
|
-
criteria.from_map_or_db
|
2229
|
-
end
|
2230
|
-
|
2231
|
-
it "returns nil" do
|
2232
|
-
from_db.should be_nil
|
2233
|
-
end
|
2234
|
-
end
|
2235
|
-
end
|
2236
|
-
|
2237
|
-
describe "#multiple_from_map_or_db" do
|
2238
|
-
|
2239
|
-
before(:all) do
|
2240
|
-
Mongoid.identity_map_enabled = true
|
2241
|
-
end
|
2242
|
-
|
2243
|
-
after(:all) do
|
2244
|
-
Mongoid.identity_map_enabled = false
|
2245
|
-
end
|
2246
|
-
|
2247
|
-
context "when the document is in the identity map" do
|
2248
|
-
|
2249
|
-
let!(:band) do
|
2250
|
-
Band.create(name: "Depeche Mode")
|
2251
|
-
end
|
2252
|
-
|
2253
|
-
let!(:band_two) do
|
2254
|
-
Band.create(name: "Tool")
|
2255
|
-
end
|
2256
|
-
|
2257
|
-
context "when providing a single id" do
|
2258
|
-
|
2259
|
-
let(:criteria) do
|
2260
|
-
Band.where(_id: band.id)
|
2261
|
-
end
|
2262
|
-
|
2263
|
-
let(:from_map) do
|
2264
|
-
criteria.multiple_from_map_or_db([ band.id ])
|
2265
|
-
end
|
2266
|
-
|
2267
|
-
it "returns the document from the map" do
|
2268
|
-
from_map.should include(band)
|
2269
|
-
end
|
2270
|
-
end
|
2271
|
-
|
2272
|
-
context "when providing multiple ids" do
|
2273
|
-
|
2274
|
-
let(:criteria) do
|
2275
|
-
Band.where(:_id.in => [ band.id, band_two.id ])
|
2276
|
-
end
|
2277
|
-
|
2278
|
-
let(:from_map) do
|
2279
|
-
criteria.multiple_from_map_or_db([ band.id, band_two.id ])
|
2280
|
-
end
|
2281
|
-
|
2282
|
-
it "returns the documents from the map" do
|
2283
|
-
from_map.should include(band, band_two)
|
2284
|
-
end
|
2285
|
-
end
|
2286
|
-
end
|
2287
|
-
|
2288
|
-
context "when the document is not in the identity map" do
|
2289
|
-
|
2290
|
-
let!(:band) do
|
2291
|
-
Band.create(name: "Depeche Mode")
|
2292
|
-
end
|
2293
|
-
|
2294
|
-
let!(:band_two) do
|
2295
|
-
Band.create(name: "Tool")
|
2296
|
-
end
|
2297
|
-
|
2298
|
-
before do
|
2299
|
-
Mongoid::IdentityMap.clear
|
2300
|
-
end
|
2301
|
-
|
2302
|
-
context "when providing a single id" do
|
2303
|
-
|
2304
|
-
let(:criteria) do
|
2305
|
-
Band.where(_id: band.id)
|
2306
|
-
end
|
2307
|
-
|
2308
|
-
let(:from_db) do
|
2309
|
-
criteria.multiple_from_map_or_db([ band.id ])
|
2310
|
-
end
|
2311
|
-
|
2312
|
-
it "returns the document from the database" do
|
2313
|
-
from_db.first.should_not equal(band)
|
2314
|
-
end
|
2315
|
-
|
2316
|
-
it "returns the correct document" do
|
2317
|
-
from_db.first.should eq(band)
|
2318
|
-
end
|
2319
|
-
end
|
2320
|
-
|
2321
|
-
context "when providing multiple ids" do
|
2322
|
-
|
2323
|
-
let(:criteria) do
|
2324
|
-
Band.where(:_id.in => [ band.id, band_two.id ])
|
2325
|
-
end
|
2326
|
-
|
2327
|
-
let(:from_db) do
|
2328
|
-
criteria.multiple_from_map_or_db([ band.id, band_two.id ])
|
2329
|
-
end
|
2330
|
-
|
2331
|
-
it "returns the document from the database" do
|
2332
|
-
from_db.first.should_not equal(band)
|
2333
|
-
end
|
2334
|
-
|
2335
|
-
it "returns the correct document" do
|
2336
|
-
from_db.first.should eq(band)
|
2337
|
-
end
|
2338
|
-
end
|
2339
|
-
end
|
2340
|
-
end
|
2341
|
-
|
2342
|
-
describe "#geo_near" do
|
2343
|
-
|
2344
|
-
before do
|
2345
|
-
Bar.create_indexes
|
2346
|
-
end
|
2347
|
-
|
2348
|
-
let!(:match) do
|
2349
|
-
Bar.create(location: [ 52.30, 13.25 ])
|
2350
|
-
end
|
2351
|
-
|
2352
|
-
let(:criteria) do
|
2353
|
-
Bar.geo_near([ 52, 13 ]).max_distance(10).spherical
|
2354
|
-
end
|
2355
|
-
|
2356
|
-
it "returns the matching documents" do
|
2357
|
-
criteria.should eq([ match ])
|
2358
|
-
end
|
2359
|
-
end
|
2360
|
-
|
2361
|
-
describe "#gt" do
|
2362
|
-
|
2363
|
-
let!(:match) do
|
2364
|
-
Band.create(member_count: 5)
|
2365
|
-
end
|
2366
|
-
|
2367
|
-
let!(:non_match) do
|
2368
|
-
Band.create(member_count: 1)
|
2369
|
-
end
|
2370
|
-
|
2371
|
-
let(:criteria) do
|
2372
|
-
Band.gt(member_count: 4)
|
2373
|
-
end
|
2374
|
-
|
2375
|
-
it "returns the matching documents" do
|
2376
|
-
criteria.should eq([ match ])
|
2377
|
-
end
|
2378
|
-
end
|
2379
|
-
|
2380
|
-
describe "#gte" do
|
2381
|
-
|
2382
|
-
let!(:match) do
|
2383
|
-
Band.create(member_count: 5)
|
2384
|
-
end
|
2385
|
-
|
2386
|
-
let!(:non_match) do
|
2387
|
-
Band.create(member_count: 1)
|
2388
|
-
end
|
2389
|
-
|
2390
|
-
let(:criteria) do
|
2391
|
-
Band.gte(member_count: 5)
|
2392
|
-
end
|
2393
|
-
|
2394
|
-
it "returns the matching documents" do
|
2395
|
-
criteria.should eq([ match ])
|
2396
|
-
end
|
2397
|
-
end
|
2398
|
-
|
2399
|
-
[ :in, :any_in ].each do |method|
|
2400
|
-
|
2401
|
-
describe "\##{method}" do
|
2402
|
-
|
2403
|
-
context "when querying on a normal field" do
|
2404
|
-
|
2405
|
-
let!(:match) do
|
2406
|
-
Band.create(genres: [ "electro", "dub" ])
|
2407
|
-
end
|
2408
|
-
|
2409
|
-
let!(:non_match) do
|
2410
|
-
Band.create(genres: [ "house" ])
|
2411
|
-
end
|
2412
|
-
|
2413
|
-
let(:criteria) do
|
2414
|
-
Band.send(method, genres: [ "dub" ])
|
2415
|
-
end
|
1021
|
+
let(:criteria) do
|
1022
|
+
Band.send(method, genres: [ "dub" ])
|
1023
|
+
end
|
2416
1024
|
|
2417
1025
|
it "returns the matching documents" do
|
2418
|
-
criteria.
|
1026
|
+
expect(criteria).to eq([ match ])
|
2419
1027
|
end
|
2420
1028
|
end
|
2421
1029
|
|
2422
1030
|
context "when querying on a foreign key" do
|
2423
1031
|
|
2424
1032
|
let(:id) do
|
2425
|
-
|
1033
|
+
BSON::ObjectId.new
|
2426
1034
|
end
|
2427
1035
|
|
2428
1036
|
let!(:match_one) do
|
@@ -2436,7 +1044,7 @@ describe Mongoid::Criteria do
|
|
2436
1044
|
end
|
2437
1045
|
|
2438
1046
|
it "returns the matching documents" do
|
2439
|
-
criteria.
|
1047
|
+
expect(criteria).to eq([ match_one ])
|
2440
1048
|
end
|
2441
1049
|
end
|
2442
1050
|
|
@@ -2447,7 +1055,7 @@ describe Mongoid::Criteria do
|
|
2447
1055
|
end
|
2448
1056
|
|
2449
1057
|
it "returns the matching documents" do
|
2450
|
-
criteria.
|
1058
|
+
expect(criteria).to eq([ match_one ])
|
2451
1059
|
end
|
2452
1060
|
end
|
2453
1061
|
|
@@ -2460,7 +1068,7 @@ describe Mongoid::Criteria do
|
|
2460
1068
|
end
|
2461
1069
|
|
2462
1070
|
it "returns the matching documents" do
|
2463
|
-
criteria.
|
1071
|
+
expect(criteria).to eq([ match_one ])
|
2464
1072
|
end
|
2465
1073
|
end
|
2466
1074
|
|
@@ -2475,7 +1083,7 @@ describe Mongoid::Criteria do
|
|
2475
1083
|
end
|
2476
1084
|
|
2477
1085
|
it "returns the matching documents" do
|
2478
|
-
criteria.
|
1086
|
+
expect(criteria).to eq([ game ])
|
2479
1087
|
end
|
2480
1088
|
end
|
2481
1089
|
end
|
@@ -2490,30 +1098,22 @@ describe Mongoid::Criteria do
|
|
2490
1098
|
end
|
2491
1099
|
|
2492
1100
|
it "sets the class" do
|
2493
|
-
criteria.klass.
|
1101
|
+
expect(criteria.klass).to eq(Band)
|
2494
1102
|
end
|
2495
1103
|
|
2496
1104
|
it "sets the aliased fields" do
|
2497
|
-
criteria.aliased_fields.
|
1105
|
+
expect(criteria.aliased_fields).to eq(Band.aliased_fields)
|
2498
1106
|
end
|
2499
1107
|
|
2500
1108
|
it "sets the serializers" do
|
2501
|
-
criteria.serializers.
|
1109
|
+
expect(criteria.serializers).to eq(Band.fields)
|
2502
1110
|
end
|
2503
1111
|
end
|
2504
1112
|
|
2505
1113
|
describe "#includes" do
|
2506
1114
|
|
2507
|
-
before do
|
2508
|
-
Mongoid.identity_map_enabled = true
|
2509
|
-
end
|
2510
|
-
|
2511
|
-
after do
|
2512
|
-
Mongoid.identity_map_enabled = false
|
2513
|
-
end
|
2514
|
-
|
2515
1115
|
let!(:person) do
|
2516
|
-
Person.create
|
1116
|
+
Person.create(age: 1)
|
2517
1117
|
end
|
2518
1118
|
|
2519
1119
|
context "when providing a name that is not a relation" do
|
@@ -2571,30 +1171,20 @@ describe Mongoid::Criteria do
|
|
2571
1171
|
B.create(c: c_two)
|
2572
1172
|
end
|
2573
1173
|
|
2574
|
-
before do
|
2575
|
-
Mongoid::IdentityMap.clear
|
2576
|
-
end
|
2577
|
-
|
2578
1174
|
let!(:results) do
|
2579
|
-
C.includes(:b).
|
1175
|
+
C.includes(:b).to_a.detect do |c|
|
2580
1176
|
c.id == c_two.id
|
2581
1177
|
end
|
2582
1178
|
end
|
2583
1179
|
|
2584
|
-
let(:from_map) do
|
2585
|
-
Mongoid::IdentityMap[B.collection_name][b.id]
|
2586
|
-
end
|
2587
|
-
|
2588
1180
|
it "returns the correct documents" do
|
2589
|
-
results.
|
2590
|
-
end
|
2591
|
-
|
2592
|
-
it "inserts the first document into the identity map" do
|
2593
|
-
from_map.should eq(b)
|
1181
|
+
expect(results).to eq(c_two)
|
2594
1182
|
end
|
2595
1183
|
|
2596
|
-
it "
|
2597
|
-
|
1184
|
+
it "does not query the db" do
|
1185
|
+
expect_query(0) do
|
1186
|
+
results.b
|
1187
|
+
end
|
2598
1188
|
end
|
2599
1189
|
end
|
2600
1190
|
end
|
@@ -2648,42 +1238,26 @@ describe Mongoid::Criteria do
|
|
2648
1238
|
C.create(d: d_two)
|
2649
1239
|
end
|
2650
1240
|
|
2651
|
-
before do
|
2652
|
-
Mongoid::IdentityMap.clear
|
2653
|
-
end
|
2654
|
-
|
2655
1241
|
let!(:results) do
|
2656
1242
|
D.includes(:b, :c).entries.detect do |d|
|
2657
1243
|
d.id == d_two.id
|
2658
1244
|
end
|
2659
1245
|
end
|
2660
1246
|
|
2661
|
-
let(:from_map_b) do
|
2662
|
-
Mongoid::IdentityMap[B.collection_name][b.id]
|
2663
|
-
end
|
2664
|
-
|
2665
|
-
let(:from_map_c) do
|
2666
|
-
Mongoid::IdentityMap[C.collection_name][c.id]
|
2667
|
-
end
|
2668
|
-
|
2669
1247
|
it "returns the correct documents" do
|
2670
|
-
results.
|
2671
|
-
end
|
2672
|
-
|
2673
|
-
it "inserts the b document into the identity map" do
|
2674
|
-
from_map_b.should eq(b)
|
2675
|
-
end
|
2676
|
-
|
2677
|
-
it "inserts the c document into the identity map" do
|
2678
|
-
from_map_c.should eq(c)
|
1248
|
+
expect(results).to eq(d_two)
|
2679
1249
|
end
|
2680
1250
|
|
2681
|
-
it "
|
2682
|
-
|
1251
|
+
it "does not query the db on b" do
|
1252
|
+
expect_query(0) do
|
1253
|
+
results.b
|
1254
|
+
end
|
2683
1255
|
end
|
2684
1256
|
|
2685
|
-
it "
|
2686
|
-
|
1257
|
+
it "does not query the db on c" do
|
1258
|
+
expect_query(0) do
|
1259
|
+
results.b
|
1260
|
+
end
|
2687
1261
|
end
|
2688
1262
|
end
|
2689
1263
|
end
|
@@ -2735,42 +1309,26 @@ describe Mongoid::Criteria do
|
|
2735
1309
|
2.times.map { C.create(d: d_two) }
|
2736
1310
|
end
|
2737
1311
|
|
2738
|
-
before do
|
2739
|
-
Mongoid::IdentityMap.clear
|
2740
|
-
end
|
2741
|
-
|
2742
1312
|
let!(:results) do
|
2743
1313
|
D.includes(:b, :c).entries.detect do |d|
|
2744
1314
|
d.id == d_two.id
|
2745
1315
|
end
|
2746
1316
|
end
|
2747
1317
|
|
2748
|
-
let(:from_map_bs) do
|
2749
|
-
bs.map { |b| Mongoid::IdentityMap[B.collection_name][b.id] }
|
2750
|
-
end
|
2751
|
-
|
2752
|
-
let(:from_map_cs) do
|
2753
|
-
cs.map { |c| Mongoid::IdentityMap[C.collection_name][c.id] }
|
2754
|
-
end
|
2755
|
-
|
2756
1318
|
it "returns the correct documents" do
|
2757
|
-
results.
|
2758
|
-
end
|
2759
|
-
|
2760
|
-
it "inserts the b documents into the identity map" do
|
2761
|
-
from_map_bs.should eq(bs)
|
1319
|
+
expect(results).to eq(d_two)
|
2762
1320
|
end
|
2763
1321
|
|
2764
|
-
it "
|
2765
|
-
|
2766
|
-
|
2767
|
-
|
2768
|
-
it "retrieves the b documents from the identity map" do
|
2769
|
-
results.b.should match_array(from_map_bs)
|
1322
|
+
it "does not query the db on b" do
|
1323
|
+
expect_query(0) do
|
1324
|
+
results.b
|
1325
|
+
end
|
2770
1326
|
end
|
2771
1327
|
|
2772
|
-
it "
|
2773
|
-
|
1328
|
+
it "does not query the db on c" do
|
1329
|
+
expect_query(0) do
|
1330
|
+
results.b
|
1331
|
+
end
|
2774
1332
|
end
|
2775
1333
|
end
|
2776
1334
|
end
|
@@ -2787,35 +1345,102 @@ describe Mongoid::Criteria do
|
|
2787
1345
|
end
|
2788
1346
|
|
2789
1347
|
it "does not duplicate the metadata in the inclusions" do
|
2790
|
-
criteria.inclusions.
|
1348
|
+
expect(criteria.inclusions).to eq([ metadata ])
|
1349
|
+
end
|
1350
|
+
end
|
1351
|
+
|
1352
|
+
context "when mapping the results more than once" do
|
1353
|
+
|
1354
|
+
let!(:post) do
|
1355
|
+
person.posts.create(title: "one")
|
1356
|
+
end
|
1357
|
+
|
1358
|
+
let(:criteria) do
|
1359
|
+
Post.includes(:person)
|
1360
|
+
end
|
1361
|
+
|
1362
|
+
let!(:results) do
|
1363
|
+
criteria.map { |doc| doc }
|
1364
|
+
criteria.map { |doc| doc }
|
1365
|
+
end
|
1366
|
+
|
1367
|
+
it "returns the proper results" do
|
1368
|
+
expect(results.first.title).to eq("one")
|
2791
1369
|
end
|
2792
1370
|
end
|
2793
1371
|
|
2794
|
-
context "when
|
1372
|
+
context "when including a belongs to relation" do
|
1373
|
+
|
1374
|
+
context "when the criteria is from the root" do
|
1375
|
+
|
1376
|
+
let!(:person_two) do
|
1377
|
+
Person.create(age: 2)
|
1378
|
+
end
|
1379
|
+
|
1380
|
+
let!(:post_one) do
|
1381
|
+
person.posts.create(title: "one")
|
1382
|
+
end
|
1383
|
+
|
1384
|
+
let!(:post_two) do
|
1385
|
+
person_two.posts.create(title: "two")
|
1386
|
+
end
|
1387
|
+
|
1388
|
+
context "when calling first" do
|
1389
|
+
|
1390
|
+
let(:criteria) do
|
1391
|
+
Post.includes(:person)
|
1392
|
+
end
|
1393
|
+
|
1394
|
+
let!(:document) do
|
1395
|
+
criteria.first
|
1396
|
+
end
|
1397
|
+
|
1398
|
+
it "eager loads the first document" do
|
1399
|
+
expect_query(0) do
|
1400
|
+
expect(document.person).to eq(person)
|
1401
|
+
end
|
1402
|
+
end
|
1403
|
+
|
1404
|
+
it "does not eager load the last document" do
|
1405
|
+
doc = criteria.last
|
1406
|
+
expect_query(1) do
|
1407
|
+
expect(doc.person).to eq(person_two)
|
1408
|
+
end
|
1409
|
+
end
|
1410
|
+
|
1411
|
+
it "returns the first document" do
|
1412
|
+
expect(document).to eq(post_one)
|
1413
|
+
end
|
1414
|
+
end
|
1415
|
+
|
1416
|
+
context "when calling last" do
|
2795
1417
|
|
2796
|
-
|
2797
|
-
|
2798
|
-
|
1418
|
+
let!(:criteria) do
|
1419
|
+
Post.includes(:person)
|
1420
|
+
end
|
2799
1421
|
|
2800
|
-
|
2801
|
-
|
2802
|
-
|
1422
|
+
let!(:document) do
|
1423
|
+
criteria.last
|
1424
|
+
end
|
2803
1425
|
|
2804
|
-
|
2805
|
-
|
2806
|
-
|
1426
|
+
it "eager loads the last document" do
|
1427
|
+
expect_query(0) do
|
1428
|
+
expect(document.person).to eq(person_two)
|
1429
|
+
end
|
1430
|
+
end
|
2807
1431
|
|
2808
|
-
|
2809
|
-
|
2810
|
-
|
2811
|
-
|
1432
|
+
it "does not eager load the first document" do
|
1433
|
+
doc = criteria.first
|
1434
|
+
expect_query(1) do
|
1435
|
+
expect(doc.person).to eq(person)
|
1436
|
+
end
|
1437
|
+
end
|
2812
1438
|
|
2813
|
-
|
2814
|
-
|
1439
|
+
it "returns the last document" do
|
1440
|
+
expect(document).to eq(post_two)
|
1441
|
+
end
|
1442
|
+
end
|
2815
1443
|
end
|
2816
|
-
end
|
2817
|
-
|
2818
|
-
context "when including a belongs to relation" do
|
2819
1444
|
|
2820
1445
|
context "when the criteria is from an embedded relation" do
|
2821
1446
|
|
@@ -2848,10 +1473,6 @@ describe Mongoid::Criteria do
|
|
2848
1473
|
|
2849
1474
|
context "when calling first" do
|
2850
1475
|
|
2851
|
-
before do
|
2852
|
-
Mongoid::IdentityMap.clear
|
2853
|
-
end
|
2854
|
-
|
2855
1476
|
let(:criteria) do
|
2856
1477
|
peep.reload.addresses.includes(:band)
|
2857
1478
|
end
|
@@ -2860,37 +1481,30 @@ describe Mongoid::Criteria do
|
|
2860
1481
|
criteria.context
|
2861
1482
|
end
|
2862
1483
|
|
2863
|
-
before do
|
2864
|
-
context.should_receive(:eager_load_one).with(address_one).once.and_call_original
|
2865
|
-
end
|
2866
|
-
|
2867
1484
|
let!(:document) do
|
2868
1485
|
criteria.first
|
2869
1486
|
end
|
2870
1487
|
|
2871
|
-
let(:eager_loaded) do
|
2872
|
-
Mongoid::IdentityMap[Band.collection_name]
|
2873
|
-
end
|
2874
|
-
|
2875
1488
|
it "eager loads the first document" do
|
2876
|
-
|
1489
|
+
expect_query(0) do
|
1490
|
+
expect(document.band).to eq(depeche)
|
1491
|
+
end
|
2877
1492
|
end
|
2878
1493
|
|
2879
1494
|
it "does not eager load the last document" do
|
2880
|
-
|
1495
|
+
doc = criteria.last
|
1496
|
+
expect_query(1) do
|
1497
|
+
expect(doc.band).to eq(tool)
|
1498
|
+
end
|
2881
1499
|
end
|
2882
1500
|
|
2883
1501
|
it "returns the document" do
|
2884
|
-
document.
|
1502
|
+
expect(document).to eq(address_one)
|
2885
1503
|
end
|
2886
1504
|
end
|
2887
1505
|
|
2888
1506
|
context "when calling last" do
|
2889
1507
|
|
2890
|
-
before do
|
2891
|
-
Mongoid::IdentityMap.clear
|
2892
|
-
end
|
2893
|
-
|
2894
1508
|
let(:criteria) do
|
2895
1509
|
peep.reload.addresses.includes(:band)
|
2896
1510
|
end
|
@@ -2899,37 +1513,30 @@ describe Mongoid::Criteria do
|
|
2899
1513
|
criteria.context
|
2900
1514
|
end
|
2901
1515
|
|
2902
|
-
before do
|
2903
|
-
context.should_receive(:eager_load_one).with(address_two).once.and_call_original
|
2904
|
-
end
|
2905
|
-
|
2906
1516
|
let!(:document) do
|
2907
1517
|
criteria.last
|
2908
1518
|
end
|
2909
1519
|
|
2910
|
-
|
2911
|
-
|
1520
|
+
it "eager loads the last document" do
|
1521
|
+
expect_query(0) do
|
1522
|
+
expect(document.band).to eq(tool)
|
1523
|
+
end
|
2912
1524
|
end
|
2913
1525
|
|
2914
1526
|
it "does not eager load the first document" do
|
2915
|
-
|
2916
|
-
|
2917
|
-
|
2918
|
-
|
2919
|
-
eager_loaded[tool.id].should eq(tool)
|
1527
|
+
doc = criteria.first
|
1528
|
+
expect_query(1) do
|
1529
|
+
expect(doc.band).to eq(depeche)
|
1530
|
+
end
|
2920
1531
|
end
|
2921
1532
|
|
2922
1533
|
it "returns the document" do
|
2923
|
-
document.
|
1534
|
+
expect(document).to eq(address_two)
|
2924
1535
|
end
|
2925
1536
|
end
|
2926
1537
|
|
2927
1538
|
context "when iterating all documents" do
|
2928
1539
|
|
2929
|
-
before do
|
2930
|
-
Mongoid::IdentityMap.clear
|
2931
|
-
end
|
2932
|
-
|
2933
1540
|
let(:criteria) do
|
2934
1541
|
peep.reload.addresses.includes(:band)
|
2935
1542
|
end
|
@@ -2938,113 +1545,24 @@ describe Mongoid::Criteria do
|
|
2938
1545
|
criteria.context
|
2939
1546
|
end
|
2940
1547
|
|
2941
|
-
before do
|
2942
|
-
context.
|
2943
|
-
should_receive(:eager_load).
|
2944
|
-
with([ address_one, address_two ]).
|
2945
|
-
once.
|
2946
|
-
and_call_original
|
2947
|
-
end
|
2948
|
-
|
2949
1548
|
let!(:documents) do
|
2950
1549
|
criteria.to_a
|
2951
1550
|
end
|
2952
1551
|
|
2953
|
-
let(:eager_loaded) do
|
2954
|
-
Mongoid::IdentityMap[Band.collection_name]
|
2955
|
-
end
|
2956
|
-
|
2957
1552
|
it "eager loads the first document" do
|
2958
|
-
|
1553
|
+
expect_query(0) do
|
1554
|
+
expect(documents.first.band).to eq(depeche)
|
1555
|
+
end
|
2959
1556
|
end
|
2960
1557
|
|
2961
1558
|
it "eager loads the last document" do
|
2962
|
-
|
1559
|
+
expect_query(0) do
|
1560
|
+
expect(documents.last.band).to eq(tool)
|
1561
|
+
end
|
2963
1562
|
end
|
2964
1563
|
|
2965
1564
|
it "returns the documents" do
|
2966
|
-
documents.
|
2967
|
-
end
|
2968
|
-
end
|
2969
|
-
end
|
2970
|
-
|
2971
|
-
context "when the criteria is from the root" do
|
2972
|
-
|
2973
|
-
let!(:person_two) do
|
2974
|
-
Person.create
|
2975
|
-
end
|
2976
|
-
|
2977
|
-
let!(:post_one) do
|
2978
|
-
person.posts.create(title: "one")
|
2979
|
-
end
|
2980
|
-
|
2981
|
-
let!(:post_two) do
|
2982
|
-
person_two.posts.create(title: "two")
|
2983
|
-
end
|
2984
|
-
|
2985
|
-
before do
|
2986
|
-
Mongoid::IdentityMap.clear
|
2987
|
-
end
|
2988
|
-
|
2989
|
-
context "when calling first" do
|
2990
|
-
|
2991
|
-
let!(:criteria) do
|
2992
|
-
Post.includes(:person)
|
2993
|
-
end
|
2994
|
-
|
2995
|
-
let!(:context) do
|
2996
|
-
criteria.context
|
2997
|
-
end
|
2998
|
-
|
2999
|
-
before do
|
3000
|
-
context.should_receive(:eager_load_one).with(post_one).once.and_call_original
|
3001
|
-
end
|
3002
|
-
|
3003
|
-
let!(:document) do
|
3004
|
-
criteria.first
|
3005
|
-
end
|
3006
|
-
|
3007
|
-
it "eager loads for the first document" do
|
3008
|
-
Mongoid::IdentityMap[Person.collection_name][person.id].should eq(person)
|
3009
|
-
end
|
3010
|
-
|
3011
|
-
it "does not eager loads for the last document" do
|
3012
|
-
Mongoid::IdentityMap[Person.collection_name][person_two.id].should be_nil
|
3013
|
-
end
|
3014
|
-
|
3015
|
-
it "returns the first document" do
|
3016
|
-
document.should eq(post_one)
|
3017
|
-
end
|
3018
|
-
end
|
3019
|
-
|
3020
|
-
context "when calling last" do
|
3021
|
-
|
3022
|
-
let!(:criteria) do
|
3023
|
-
Post.includes(:person)
|
3024
|
-
end
|
3025
|
-
|
3026
|
-
let!(:context) do
|
3027
|
-
criteria.context
|
3028
|
-
end
|
3029
|
-
|
3030
|
-
before do
|
3031
|
-
context.should_receive(:eager_load_one).with(post_two).once.and_call_original
|
3032
|
-
end
|
3033
|
-
|
3034
|
-
let!(:document) do
|
3035
|
-
criteria.last
|
3036
|
-
end
|
3037
|
-
|
3038
|
-
it "eager loads for the first document" do
|
3039
|
-
Mongoid::IdentityMap[Person.collection_name][person_two.id].should eq(person_two)
|
3040
|
-
end
|
3041
|
-
|
3042
|
-
it "does not eager loads for the last document" do
|
3043
|
-
Mongoid::IdentityMap[Person.collection_name][person.id].should be_nil
|
3044
|
-
end
|
3045
|
-
|
3046
|
-
it "returns the last document" do
|
3047
|
-
document.should eq(post_two)
|
1565
|
+
expect(documents).to eq([ address_one, address_two ])
|
3048
1566
|
end
|
3049
1567
|
end
|
3050
1568
|
end
|
@@ -3053,7 +1571,7 @@ describe Mongoid::Criteria do
|
|
3053
1571
|
context "when providing inclusions to the default scope" do
|
3054
1572
|
|
3055
1573
|
before do
|
3056
|
-
Person.default_scope(Person.includes(:posts))
|
1574
|
+
Person.default_scope(->{ Person.includes(:posts) })
|
3057
1575
|
end
|
3058
1576
|
|
3059
1577
|
after do
|
@@ -3070,20 +1588,8 @@ describe Mongoid::Criteria do
|
|
3070
1588
|
|
3071
1589
|
context "when the criteria has no options" do
|
3072
1590
|
|
3073
|
-
before do
|
3074
|
-
Mongoid::IdentityMap.clear
|
3075
|
-
end
|
3076
|
-
|
3077
1591
|
let!(:criteria) do
|
3078
|
-
Person.all
|
3079
|
-
end
|
3080
|
-
|
3081
|
-
let!(:context) do
|
3082
|
-
criteria.context
|
3083
|
-
end
|
3084
|
-
|
3085
|
-
before do
|
3086
|
-
context.should_receive(:eager_load).with([ person ]).once.and_call_original
|
1592
|
+
Person.asc(:age).all
|
3087
1593
|
end
|
3088
1594
|
|
3089
1595
|
let!(:documents) do
|
@@ -3091,15 +1597,19 @@ describe Mongoid::Criteria do
|
|
3091
1597
|
end
|
3092
1598
|
|
3093
1599
|
it "returns the correct documents" do
|
3094
|
-
documents.
|
1600
|
+
expect(documents).to eq([ person ])
|
3095
1601
|
end
|
3096
1602
|
|
3097
|
-
it "
|
3098
|
-
|
1603
|
+
it "eager loads the first document" do
|
1604
|
+
expect_query(0) do
|
1605
|
+
expect(documents.first.posts.first).to eq(post_one)
|
1606
|
+
end
|
3099
1607
|
end
|
3100
1608
|
|
3101
|
-
it "
|
3102
|
-
|
1609
|
+
it "eager loads the last document" do
|
1610
|
+
expect_query(0) do
|
1611
|
+
expect(documents.first.posts.last).to eq(post_two)
|
1612
|
+
end
|
3103
1613
|
end
|
3104
1614
|
|
3105
1615
|
context "when executing the query twice" do
|
@@ -3113,43 +1623,23 @@ describe Mongoid::Criteria do
|
|
3113
1623
|
end
|
3114
1624
|
|
3115
1625
|
before do
|
3116
|
-
new_context.
|
1626
|
+
expect(new_context).to receive(:eager_load_one).with(person).once.and_call_original
|
3117
1627
|
end
|
3118
1628
|
|
3119
1629
|
let!(:from_db) do
|
3120
1630
|
new_criteria.first
|
3121
1631
|
end
|
3122
1632
|
|
3123
|
-
let(:mapped) do
|
3124
|
-
Mongoid::IdentityMap[Post.collection_name][{"person_id" => person.id}]
|
3125
|
-
end
|
3126
|
-
|
3127
1633
|
it "does not duplicate documents in the relation" do
|
3128
|
-
person.posts.size.
|
3129
|
-
end
|
3130
|
-
|
3131
|
-
it "does not duplicate documents in the map" do
|
3132
|
-
mapped.size.should eq(2)
|
1634
|
+
expect(person.posts.size).to eq(2)
|
3133
1635
|
end
|
3134
1636
|
end
|
3135
1637
|
end
|
3136
1638
|
|
3137
1639
|
context "when calling first on the criteria" do
|
3138
1640
|
|
3139
|
-
before do
|
3140
|
-
Mongoid::IdentityMap.clear
|
3141
|
-
end
|
3142
|
-
|
3143
1641
|
let(:criteria) do
|
3144
|
-
Person.all
|
3145
|
-
end
|
3146
|
-
|
3147
|
-
let!(:context) do
|
3148
|
-
criteria.context
|
3149
|
-
end
|
3150
|
-
|
3151
|
-
before do
|
3152
|
-
context.should_receive(:eager_load_one).with(person).once.and_call_original
|
1642
|
+
Person.asc(:age).all
|
3153
1643
|
end
|
3154
1644
|
|
3155
1645
|
let!(:from_db) do
|
@@ -3157,26 +1647,26 @@ describe Mongoid::Criteria do
|
|
3157
1647
|
end
|
3158
1648
|
|
3159
1649
|
it "returns the correct documents" do
|
3160
|
-
from_db.
|
1650
|
+
expect(from_db).to eq(person)
|
3161
1651
|
end
|
3162
1652
|
|
3163
|
-
it "
|
3164
|
-
|
1653
|
+
it "eager loads the first document" do
|
1654
|
+
expect_query(0) do
|
1655
|
+
expect(from_db.posts.first).to eq(post_one)
|
1656
|
+
end
|
3165
1657
|
end
|
3166
1658
|
|
3167
|
-
it "
|
3168
|
-
|
1659
|
+
it "eager loads the last document" do
|
1660
|
+
expect_query(0) do
|
1661
|
+
expect(from_db.posts.last).to eq(post_two)
|
1662
|
+
end
|
3169
1663
|
end
|
3170
1664
|
end
|
3171
1665
|
|
3172
1666
|
context "when calling last on the criteria" do
|
3173
1667
|
|
3174
|
-
before do
|
3175
|
-
Mongoid::IdentityMap.clear
|
3176
|
-
end
|
3177
|
-
|
3178
1668
|
let(:criteria) do
|
3179
|
-
Person.all
|
1669
|
+
Person.asc(:age).all
|
3180
1670
|
end
|
3181
1671
|
|
3182
1672
|
let!(:context) do
|
@@ -3184,7 +1674,7 @@ describe Mongoid::Criteria do
|
|
3184
1674
|
end
|
3185
1675
|
|
3186
1676
|
before do
|
3187
|
-
context.
|
1677
|
+
expect(context).to receive(:eager_load_one).with(person).once.and_call_original
|
3188
1678
|
end
|
3189
1679
|
|
3190
1680
|
let!(:from_db) do
|
@@ -3192,15 +1682,19 @@ describe Mongoid::Criteria do
|
|
3192
1682
|
end
|
3193
1683
|
|
3194
1684
|
it "returns the correct documents" do
|
3195
|
-
from_db.
|
1685
|
+
expect(from_db).to eq(person)
|
3196
1686
|
end
|
3197
1687
|
|
3198
|
-
it "
|
3199
|
-
|
1688
|
+
it "eager loads the first document" do
|
1689
|
+
expect_query(0) do
|
1690
|
+
expect(from_db.posts.first).to eq(post_one)
|
1691
|
+
end
|
3200
1692
|
end
|
3201
1693
|
|
3202
|
-
it "
|
3203
|
-
|
1694
|
+
it "eager loads the last document" do
|
1695
|
+
expect_query(0) do
|
1696
|
+
expect(from_db.posts.last).to eq(post_two)
|
1697
|
+
end
|
3204
1698
|
end
|
3205
1699
|
end
|
3206
1700
|
|
@@ -3214,20 +1708,8 @@ describe Mongoid::Criteria do
|
|
3214
1708
|
person_two.posts.create(title: "three")
|
3215
1709
|
end
|
3216
1710
|
|
3217
|
-
before do
|
3218
|
-
Mongoid::IdentityMap.clear
|
3219
|
-
end
|
3220
|
-
|
3221
1711
|
let!(:criteria) do
|
3222
|
-
Person.asc(:
|
3223
|
-
end
|
3224
|
-
|
3225
|
-
let(:context) do
|
3226
|
-
criteria.context
|
3227
|
-
end
|
3228
|
-
|
3229
|
-
before do
|
3230
|
-
context.should_receive(:eager_load).with([ person ]).once.and_call_original
|
1712
|
+
Person.asc(:age).limit(1)
|
3231
1713
|
end
|
3232
1714
|
|
3233
1715
|
let!(:documents) do
|
@@ -3235,19 +1717,19 @@ describe Mongoid::Criteria do
|
|
3235
1717
|
end
|
3236
1718
|
|
3237
1719
|
it "returns the correct documents" do
|
3238
|
-
criteria.
|
3239
|
-
end
|
3240
|
-
|
3241
|
-
it "inserts the first document into the identity map" do
|
3242
|
-
Mongoid::IdentityMap[Post.collection_name][post_one.id].should eq(post_one)
|
1720
|
+
expect(criteria).to eq([ person ])
|
3243
1721
|
end
|
3244
1722
|
|
3245
|
-
it "
|
3246
|
-
|
1723
|
+
it "eager loads the first document" do
|
1724
|
+
expect_query(0) do
|
1725
|
+
expect(documents.first.posts.first).to eq(post_one)
|
1726
|
+
end
|
3247
1727
|
end
|
3248
1728
|
|
3249
|
-
it "
|
3250
|
-
|
1729
|
+
it "eager loads the second document" do
|
1730
|
+
expect_query(0) do
|
1731
|
+
expect(documents.first.posts.last).to eq(post_two)
|
1732
|
+
end
|
3251
1733
|
end
|
3252
1734
|
end
|
3253
1735
|
end
|
@@ -3264,20 +1746,8 @@ describe Mongoid::Criteria do
|
|
3264
1746
|
|
3265
1747
|
context "when the criteria has no options" do
|
3266
1748
|
|
3267
|
-
before do
|
3268
|
-
Mongoid::IdentityMap.clear
|
3269
|
-
end
|
3270
|
-
|
3271
1749
|
let!(:criteria) do
|
3272
|
-
Person.includes(:preferences)
|
3273
|
-
end
|
3274
|
-
|
3275
|
-
let(:context) do
|
3276
|
-
criteria.context
|
3277
|
-
end
|
3278
|
-
|
3279
|
-
before do
|
3280
|
-
context.should_receive(:eager_load).with([ person ]).once.and_call_original
|
1750
|
+
Person.asc(:age).includes(:preferences)
|
3281
1751
|
end
|
3282
1752
|
|
3283
1753
|
let!(:documents) do
|
@@ -3285,148 +1755,73 @@ describe Mongoid::Criteria do
|
|
3285
1755
|
end
|
3286
1756
|
|
3287
1757
|
it "returns the correct documents" do
|
3288
|
-
documents.
|
1758
|
+
expect(documents).to eq([ person ])
|
3289
1759
|
end
|
3290
1760
|
|
3291
|
-
|
3292
|
-
|
3293
|
-
|
3294
|
-
|
3295
|
-
it "inserts the first document into the identity map" do
|
3296
|
-
preference_map[preference_one.id].should eq(preference_one)
|
1761
|
+
it "eager loads the first document" do
|
1762
|
+
expect_query(0) do
|
1763
|
+
expect(documents.first.preferences.first).to eq(preference_one)
|
1764
|
+
end
|
3297
1765
|
end
|
3298
1766
|
|
3299
|
-
it "
|
3300
|
-
|
1767
|
+
it "eager loads the last document" do
|
1768
|
+
expect_query(0) do
|
1769
|
+
expect(documents.first.preferences.last).to eq(preference_two)
|
1770
|
+
end
|
3301
1771
|
end
|
3302
1772
|
end
|
3303
1773
|
|
3304
1774
|
context "when calling first on the criteria" do
|
3305
1775
|
|
3306
|
-
before do
|
3307
|
-
Mongoid::IdentityMap.clear
|
3308
|
-
end
|
3309
|
-
|
3310
|
-
let!(:criteria) do
|
3311
|
-
Person.includes(:preferences)
|
3312
|
-
end
|
3313
|
-
|
3314
|
-
let(:context) do
|
3315
|
-
criteria.context
|
3316
|
-
end
|
3317
|
-
|
3318
|
-
before do
|
3319
|
-
context.should_receive(:eager_load_one).with(person).once.and_call_original
|
3320
|
-
end
|
3321
|
-
|
3322
|
-
let!(:from_db) do
|
3323
|
-
criteria.first
|
3324
|
-
end
|
3325
|
-
|
3326
|
-
it "returns the correct documents" do
|
3327
|
-
from_db.should eq(person)
|
3328
|
-
end
|
3329
|
-
|
3330
|
-
let(:preference_map) do
|
3331
|
-
Mongoid::IdentityMap[Preference.collection_name]
|
3332
|
-
end
|
3333
|
-
|
3334
|
-
it "inserts the first document into the identity map" do
|
3335
|
-
preference_map[preference_one.id].should eq(preference_one)
|
3336
|
-
end
|
3337
|
-
|
3338
|
-
it "inserts the second document into the identity map" do
|
3339
|
-
preference_map[preference_two.id].should eq(preference_two)
|
3340
|
-
end
|
3341
|
-
end
|
3342
|
-
|
3343
|
-
context "when calling last on the criteria" do
|
3344
|
-
|
3345
|
-
before do
|
3346
|
-
Mongoid::IdentityMap.clear
|
3347
|
-
end
|
3348
|
-
|
3349
1776
|
let!(:criteria) do
|
3350
|
-
Person.includes(:preferences)
|
3351
|
-
end
|
3352
|
-
|
3353
|
-
let(:context) do
|
3354
|
-
criteria.context
|
3355
|
-
end
|
3356
|
-
|
3357
|
-
before do
|
3358
|
-
context.should_receive(:eager_load_one).with(person).once.and_call_original
|
3359
|
-
end
|
3360
|
-
|
3361
|
-
let!(:from_db) do
|
3362
|
-
criteria.last
|
3363
|
-
end
|
3364
|
-
|
3365
|
-
it "returns the correct documents" do
|
3366
|
-
from_db.should eq(person)
|
3367
|
-
end
|
3368
|
-
|
3369
|
-
let(:preference_map) do
|
3370
|
-
Mongoid::IdentityMap[Preference.collection_name]
|
3371
|
-
end
|
3372
|
-
|
3373
|
-
it "inserts the first document into the identity map" do
|
3374
|
-
preference_map[preference_one.id].should eq(preference_one)
|
3375
|
-
end
|
3376
|
-
|
3377
|
-
it "inserts the second document into the identity map" do
|
3378
|
-
preference_map[preference_two.id].should eq(preference_two)
|
3379
|
-
end
|
3380
|
-
end
|
3381
|
-
|
3382
|
-
context "when the criteria has limiting options" do
|
3383
|
-
|
3384
|
-
let!(:person_two) do
|
3385
|
-
Person.create
|
3386
|
-
end
|
3387
|
-
|
3388
|
-
let!(:preference_three) do
|
3389
|
-
person_two.preferences.create(name: "three")
|
3390
|
-
end
|
3391
|
-
|
3392
|
-
before do
|
3393
|
-
Mongoid::IdentityMap.clear
|
1777
|
+
Person.asc(:age).includes(:preferences)
|
3394
1778
|
end
|
3395
1779
|
|
3396
|
-
let!(:
|
3397
|
-
|
1780
|
+
let!(:from_db) do
|
1781
|
+
criteria.first
|
3398
1782
|
end
|
3399
1783
|
|
3400
|
-
|
3401
|
-
|
1784
|
+
it "returns the correct documents" do
|
1785
|
+
expect(from_db).to eq(person)
|
3402
1786
|
end
|
3403
1787
|
|
3404
|
-
|
3405
|
-
|
1788
|
+
it "eager loads the first document" do
|
1789
|
+
expect_query(0) do
|
1790
|
+
expect(from_db.preferences.first).to eq(preference_one)
|
1791
|
+
end
|
3406
1792
|
end
|
3407
1793
|
|
3408
|
-
|
3409
|
-
|
1794
|
+
it "eager loads the last document" do
|
1795
|
+
expect_query(0) do
|
1796
|
+
expect(from_db.preferences.last).to eq(preference_two)
|
1797
|
+
end
|
3410
1798
|
end
|
1799
|
+
end
|
3411
1800
|
|
3412
|
-
|
3413
|
-
|
1801
|
+
context "when calling last on the criteria" do
|
1802
|
+
|
1803
|
+
let!(:criteria) do
|
1804
|
+
Person.asc(:age).includes(:preferences)
|
3414
1805
|
end
|
3415
1806
|
|
3416
|
-
|
3417
|
-
|
1807
|
+
let!(:from_db) do
|
1808
|
+
criteria.last
|
3418
1809
|
end
|
3419
1810
|
|
3420
|
-
it "
|
3421
|
-
|
1811
|
+
it "returns the correct documents" do
|
1812
|
+
expect(from_db).to eq(person)
|
3422
1813
|
end
|
3423
1814
|
|
3424
|
-
it "
|
3425
|
-
|
1815
|
+
it "eager loads the first document" do
|
1816
|
+
expect_query(0) do
|
1817
|
+
expect(from_db.preferences.first).to eq(preference_one)
|
1818
|
+
end
|
3426
1819
|
end
|
3427
1820
|
|
3428
|
-
it "
|
3429
|
-
|
1821
|
+
it "eager loads the last document" do
|
1822
|
+
expect_query(0) do
|
1823
|
+
expect(from_db.preferences.last).to eq(preference_two)
|
1824
|
+
end
|
3430
1825
|
end
|
3431
1826
|
end
|
3432
1827
|
end
|
@@ -3443,20 +1838,8 @@ describe Mongoid::Criteria do
|
|
3443
1838
|
|
3444
1839
|
context "when the criteria has no options" do
|
3445
1840
|
|
3446
|
-
before do
|
3447
|
-
Mongoid::IdentityMap.clear
|
3448
|
-
end
|
3449
|
-
|
3450
1841
|
let!(:criteria) do
|
3451
|
-
Person.includes(:posts)
|
3452
|
-
end
|
3453
|
-
|
3454
|
-
let(:context) do
|
3455
|
-
criteria.context
|
3456
|
-
end
|
3457
|
-
|
3458
|
-
before do
|
3459
|
-
context.should_receive(:eager_load).with([ person ]).once.and_call_original
|
1842
|
+
Person.asc(:age).includes(:posts)
|
3460
1843
|
end
|
3461
1844
|
|
3462
1845
|
let!(:documents) do
|
@@ -3464,34 +1847,26 @@ describe Mongoid::Criteria do
|
|
3464
1847
|
end
|
3465
1848
|
|
3466
1849
|
it "returns the correct documents" do
|
3467
|
-
documents.
|
1850
|
+
expect(documents).to eq([ person ])
|
3468
1851
|
end
|
3469
1852
|
|
3470
|
-
it "
|
3471
|
-
|
1853
|
+
it "eager loads the first document" do
|
1854
|
+
expect_query(0) do
|
1855
|
+
expect(documents.first.posts.first).to eq(post_one)
|
1856
|
+
end
|
3472
1857
|
end
|
3473
1858
|
|
3474
|
-
it "
|
3475
|
-
|
1859
|
+
it "eager loads the last document" do
|
1860
|
+
expect_query(0) do
|
1861
|
+
expect(documents.first.posts.last).to eq(post_two)
|
1862
|
+
end
|
3476
1863
|
end
|
3477
1864
|
end
|
3478
1865
|
|
3479
1866
|
context "when calling first on the criteria" do
|
3480
1867
|
|
3481
|
-
before do
|
3482
|
-
Mongoid::IdentityMap.clear
|
3483
|
-
end
|
3484
|
-
|
3485
1868
|
let!(:criteria) do
|
3486
|
-
Person.includes(:posts)
|
3487
|
-
end
|
3488
|
-
|
3489
|
-
let(:context) do
|
3490
|
-
criteria.context
|
3491
|
-
end
|
3492
|
-
|
3493
|
-
before do
|
3494
|
-
context.should_receive(:eager_load_one).with(person).once.and_call_original
|
1869
|
+
Person.asc(:age).includes(:posts)
|
3495
1870
|
end
|
3496
1871
|
|
3497
1872
|
let!(:from_db) do
|
@@ -3499,49 +1874,25 @@ describe Mongoid::Criteria do
|
|
3499
1874
|
end
|
3500
1875
|
|
3501
1876
|
it "returns the correct documents" do
|
3502
|
-
from_db.
|
3503
|
-
end
|
3504
|
-
|
3505
|
-
it "inserts the first document into the identity map" do
|
3506
|
-
Mongoid::IdentityMap[Post.collection_name][post_one.id].should eq(post_one)
|
3507
|
-
end
|
3508
|
-
|
3509
|
-
it "inserts the second document into the identity map" do
|
3510
|
-
Mongoid::IdentityMap[Post.collection_name][post_two.id].should eq(post_two)
|
1877
|
+
expect(from_db).to eq(person)
|
3511
1878
|
end
|
3512
1879
|
|
3513
1880
|
context "when subsequently getting all documents" do
|
3514
1881
|
|
3515
|
-
before do
|
3516
|
-
context.should_receive(:eager_load).with([ person ]).once.and_call_original
|
3517
|
-
end
|
3518
|
-
|
3519
1882
|
let!(:documents) do
|
3520
1883
|
criteria.entries
|
3521
1884
|
end
|
3522
1885
|
|
3523
1886
|
it "returns the correct documents" do
|
3524
|
-
documents.
|
1887
|
+
expect(documents).to eq([ person ])
|
3525
1888
|
end
|
3526
1889
|
end
|
3527
1890
|
end
|
3528
1891
|
|
3529
1892
|
context "when calling last on the criteria" do
|
3530
1893
|
|
3531
|
-
before do
|
3532
|
-
Mongoid::IdentityMap.clear
|
3533
|
-
end
|
3534
|
-
|
3535
1894
|
let!(:criteria) do
|
3536
|
-
Person.includes(:posts)
|
3537
|
-
end
|
3538
|
-
|
3539
|
-
let(:context) do
|
3540
|
-
criteria.context
|
3541
|
-
end
|
3542
|
-
|
3543
|
-
before do
|
3544
|
-
context.should_receive(:eager_load_one).with(person).once.and_call_original
|
1895
|
+
Person.asc(:age).includes(:posts)
|
3545
1896
|
end
|
3546
1897
|
|
3547
1898
|
let!(:from_db) do
|
@@ -3549,29 +1900,17 @@ describe Mongoid::Criteria do
|
|
3549
1900
|
end
|
3550
1901
|
|
3551
1902
|
it "returns the correct documents" do
|
3552
|
-
from_db.
|
3553
|
-
end
|
3554
|
-
|
3555
|
-
it "inserts the first document into the identity map" do
|
3556
|
-
Mongoid::IdentityMap[Post.collection_name][post_one.id].should eq(post_one)
|
3557
|
-
end
|
3558
|
-
|
3559
|
-
it "inserts the second document into the identity map" do
|
3560
|
-
Mongoid::IdentityMap[Post.collection_name][post_two.id].should eq(post_two)
|
1903
|
+
expect(from_db).to eq(person)
|
3561
1904
|
end
|
3562
1905
|
|
3563
1906
|
context "when subsequently getting all documents" do
|
3564
1907
|
|
3565
|
-
before do
|
3566
|
-
context.should_receive(:eager_load).with([ person ]).once.and_call_original
|
3567
|
-
end
|
3568
|
-
|
3569
1908
|
let!(:documents) do
|
3570
1909
|
criteria.entries
|
3571
1910
|
end
|
3572
1911
|
|
3573
1912
|
it "returns the correct documents" do
|
3574
|
-
documents.
|
1913
|
+
expect(documents).to eq([ person ])
|
3575
1914
|
end
|
3576
1915
|
end
|
3577
1916
|
end
|
@@ -3586,12 +1925,8 @@ describe Mongoid::Criteria do
|
|
3586
1925
|
person_two.posts.create(title: "three")
|
3587
1926
|
end
|
3588
1927
|
|
3589
|
-
before do
|
3590
|
-
Mongoid::IdentityMap.clear
|
3591
|
-
end
|
3592
|
-
|
3593
1928
|
let!(:criteria) do
|
3594
|
-
Person.includes(:posts).asc(:
|
1929
|
+
Person.includes(:posts).asc(:age).limit(1)
|
3595
1930
|
end
|
3596
1931
|
|
3597
1932
|
let(:context) do
|
@@ -3599,7 +1934,7 @@ describe Mongoid::Criteria do
|
|
3599
1934
|
end
|
3600
1935
|
|
3601
1936
|
before do
|
3602
|
-
context.
|
1937
|
+
expect(context).to receive(:eager_load).with([ person ]).once.and_call_original
|
3603
1938
|
end
|
3604
1939
|
|
3605
1940
|
let!(:documents) do
|
@@ -3607,19 +1942,7 @@ describe Mongoid::Criteria do
|
|
3607
1942
|
end
|
3608
1943
|
|
3609
1944
|
it "returns the correct documents" do
|
3610
|
-
documents.
|
3611
|
-
end
|
3612
|
-
|
3613
|
-
it "inserts the first document into the identity map" do
|
3614
|
-
Mongoid::IdentityMap[Post.collection_name][post_one.id].should eq(post_one)
|
3615
|
-
end
|
3616
|
-
|
3617
|
-
it "inserts the second document into the identity map" do
|
3618
|
-
Mongoid::IdentityMap[Post.collection_name][post_two.id].should eq(post_two)
|
3619
|
-
end
|
3620
|
-
|
3621
|
-
it "does not insert the third post into the identity map" do
|
3622
|
-
Mongoid::IdentityMap[Post.collection_name][post_three.id].should be_nil
|
1945
|
+
expect(documents).to eq([ person ])
|
3623
1946
|
end
|
3624
1947
|
end
|
3625
1948
|
end
|
@@ -3636,12 +1959,8 @@ describe Mongoid::Criteria do
|
|
3636
1959
|
|
3637
1960
|
context "when the criteria has no options" do
|
3638
1961
|
|
3639
|
-
before do
|
3640
|
-
Mongoid::IdentityMap.clear
|
3641
|
-
end
|
3642
|
-
|
3643
1962
|
let!(:criteria) do
|
3644
|
-
Person.includes(:game)
|
1963
|
+
Person.asc(:age).includes(:game)
|
3645
1964
|
end
|
3646
1965
|
|
3647
1966
|
let(:context) do
|
@@ -3649,7 +1968,7 @@ describe Mongoid::Criteria do
|
|
3649
1968
|
end
|
3650
1969
|
|
3651
1970
|
before do
|
3652
|
-
context.
|
1971
|
+
expect(context).to receive(:eager_load).with([ person ]).once.and_call_original
|
3653
1972
|
end
|
3654
1973
|
|
3655
1974
|
let!(:documents) do
|
@@ -3657,49 +1976,22 @@ describe Mongoid::Criteria do
|
|
3657
1976
|
end
|
3658
1977
|
|
3659
1978
|
it "returns the correct documents" do
|
3660
|
-
documents.
|
3661
|
-
end
|
3662
|
-
|
3663
|
-
it "deletes the replaced document from the identity map" do
|
3664
|
-
Mongoid::IdentityMap[Game.collection_name][game_one.id].should be_nil
|
3665
|
-
end
|
3666
|
-
|
3667
|
-
it "inserts the second document into the identity map" do
|
3668
|
-
Mongoid::IdentityMap[Game.collection_name][game_two.id].should eq(game_two)
|
3669
|
-
end
|
3670
|
-
|
3671
|
-
context "when asking from map or db" do
|
3672
|
-
|
3673
|
-
let(:in_map) do
|
3674
|
-
Mongoid::IdentityMap[Game.collection_name][game_two.id]
|
3675
|
-
end
|
3676
|
-
|
3677
|
-
let(:game) do
|
3678
|
-
Game.where("person_id" => person.id).from_map_or_db
|
3679
|
-
end
|
3680
|
-
|
3681
|
-
it "returns the document from the map" do
|
3682
|
-
game.should equal(in_map)
|
3683
|
-
end
|
1979
|
+
expect(documents).to eq([ person ])
|
3684
1980
|
end
|
3685
1981
|
end
|
3686
1982
|
|
3687
1983
|
context "when the criteria has limiting options" do
|
3688
1984
|
|
3689
1985
|
let!(:person_two) do
|
3690
|
-
Person.create
|
1986
|
+
Person.create(age: 2)
|
3691
1987
|
end
|
3692
1988
|
|
3693
1989
|
let!(:game_three) do
|
3694
1990
|
person_two.create_game(name: "Skyrim")
|
3695
1991
|
end
|
3696
1992
|
|
3697
|
-
before do
|
3698
|
-
Mongoid::IdentityMap.clear
|
3699
|
-
end
|
3700
|
-
|
3701
1993
|
let!(:criteria) do
|
3702
|
-
Person.where(id: person.id).includes(:game).asc(:
|
1994
|
+
Person.where(id: person.id).includes(:game).asc(:age).limit(1)
|
3703
1995
|
end
|
3704
1996
|
|
3705
1997
|
let(:context) do
|
@@ -3707,7 +1999,7 @@ describe Mongoid::Criteria do
|
|
3707
1999
|
end
|
3708
2000
|
|
3709
2001
|
before do
|
3710
|
-
context.
|
2002
|
+
expect(context).to receive(:eager_load).with([ person ]).once.and_call_original
|
3711
2003
|
end
|
3712
2004
|
|
3713
2005
|
let!(:documents) do
|
@@ -3715,15 +2007,7 @@ describe Mongoid::Criteria do
|
|
3715
2007
|
end
|
3716
2008
|
|
3717
2009
|
it "returns the correct documents" do
|
3718
|
-
documents.
|
3719
|
-
end
|
3720
|
-
|
3721
|
-
it "inserts the second document into the identity map" do
|
3722
|
-
Mongoid::IdentityMap[Game.collection_name][game_two.id].should eq(game_two)
|
3723
|
-
end
|
3724
|
-
|
3725
|
-
it "does not load the extra child into the map" do
|
3726
|
-
Mongoid::IdentityMap[Game.collection_name][game_three.id].should be_nil
|
2010
|
+
expect(documents).to eq([ person ])
|
3727
2011
|
end
|
3728
2012
|
end
|
3729
2013
|
end
|
@@ -3731,7 +2015,7 @@ describe Mongoid::Criteria do
|
|
3731
2015
|
context "when including a belongs to" do
|
3732
2016
|
|
3733
2017
|
let(:person_two) do
|
3734
|
-
Person.create
|
2018
|
+
Person.create(age: 2)
|
3735
2019
|
end
|
3736
2020
|
|
3737
2021
|
let!(:game_one) do
|
@@ -3742,10 +2026,6 @@ describe Mongoid::Criteria do
|
|
3742
2026
|
person_two.create_game(name: "two")
|
3743
2027
|
end
|
3744
2028
|
|
3745
|
-
before do
|
3746
|
-
Mongoid::IdentityMap.clear
|
3747
|
-
end
|
3748
|
-
|
3749
2029
|
context "when providing no options" do
|
3750
2030
|
|
3751
2031
|
let!(:criteria) do
|
@@ -3757,11 +2037,7 @@ describe Mongoid::Criteria do
|
|
3757
2037
|
end
|
3758
2038
|
|
3759
2039
|
before do
|
3760
|
-
context.
|
3761
|
-
should_receive(:eager_load).
|
3762
|
-
with([ game_one, game_two ]).
|
3763
|
-
once.
|
3764
|
-
and_call_original
|
2040
|
+
expect(context).to receive(:eager_load).with([ game_one, game_two ]).once.and_call_original
|
3765
2041
|
end
|
3766
2042
|
|
3767
2043
|
let!(:documents) do
|
@@ -3769,15 +2045,7 @@ describe Mongoid::Criteria do
|
|
3769
2045
|
end
|
3770
2046
|
|
3771
2047
|
it "returns the correct documents" do
|
3772
|
-
criteria.
|
3773
|
-
end
|
3774
|
-
|
3775
|
-
it "inserts the first document into the identity map" do
|
3776
|
-
Mongoid::IdentityMap[Person.collection_name][person.id].should eq(person)
|
3777
|
-
end
|
3778
|
-
|
3779
|
-
it "inserts the second document into the identity map" do
|
3780
|
-
Mongoid::IdentityMap[Person.collection_name][person_two.id].should eq(person_two)
|
2048
|
+
expect(criteria).to eq([ game_one, game_two ])
|
3781
2049
|
end
|
3782
2050
|
end
|
3783
2051
|
|
@@ -3792,7 +2060,7 @@ describe Mongoid::Criteria do
|
|
3792
2060
|
end
|
3793
2061
|
|
3794
2062
|
before do
|
3795
|
-
context.
|
2063
|
+
expect(context).to receive(:eager_load).with([ game_one ]).once.and_call_original
|
3796
2064
|
end
|
3797
2065
|
|
3798
2066
|
let!(:documents) do
|
@@ -3800,15 +2068,7 @@ describe Mongoid::Criteria do
|
|
3800
2068
|
end
|
3801
2069
|
|
3802
2070
|
it "returns the correct documents" do
|
3803
|
-
documents.
|
3804
|
-
end
|
3805
|
-
|
3806
|
-
it "inserts the first document into the identity map" do
|
3807
|
-
Mongoid::IdentityMap[Person.collection_name][person.id].should eq(person)
|
3808
|
-
end
|
3809
|
-
|
3810
|
-
it "does not load the documents outside of the limit" do
|
3811
|
-
Mongoid::IdentityMap[Person.collection_name][person_two.id].should be_nil
|
2071
|
+
expect(documents).to eq([ game_one ])
|
3812
2072
|
end
|
3813
2073
|
end
|
3814
2074
|
end
|
@@ -3831,12 +2091,8 @@ describe Mongoid::Criteria do
|
|
3831
2091
|
person.create_game(name: "two")
|
3832
2092
|
end
|
3833
2093
|
|
3834
|
-
before do
|
3835
|
-
Mongoid::IdentityMap.clear
|
3836
|
-
end
|
3837
|
-
|
3838
2094
|
let!(:criteria) do
|
3839
|
-
Person.includes(:posts, :game)
|
2095
|
+
Person.includes(:posts, :game).asc(:age)
|
3840
2096
|
end
|
3841
2097
|
|
3842
2098
|
let(:context) do
|
@@ -3844,7 +2100,7 @@ describe Mongoid::Criteria do
|
|
3844
2100
|
end
|
3845
2101
|
|
3846
2102
|
before do
|
3847
|
-
context.
|
2103
|
+
expect(context).to receive(:eager_load).with([ person ]).once.and_call_original
|
3848
2104
|
end
|
3849
2105
|
|
3850
2106
|
let!(:documents) do
|
@@ -3852,23 +2108,7 @@ describe Mongoid::Criteria do
|
|
3852
2108
|
end
|
3853
2109
|
|
3854
2110
|
it "returns the correct documents" do
|
3855
|
-
criteria.
|
3856
|
-
end
|
3857
|
-
|
3858
|
-
it "inserts the first has many document into the identity map" do
|
3859
|
-
Mongoid::IdentityMap[Post.collection_name][post_one.id].should eq(post_one)
|
3860
|
-
end
|
3861
|
-
|
3862
|
-
it "inserts the second has many document into the identity map" do
|
3863
|
-
Mongoid::IdentityMap[Post.collection_name][post_two.id].should eq(post_two)
|
3864
|
-
end
|
3865
|
-
|
3866
|
-
it "removes the first has one document from the identity map" do
|
3867
|
-
Mongoid::IdentityMap[Game.collection_name][game_one.id].should be_nil
|
3868
|
-
end
|
3869
|
-
|
3870
|
-
it "inserts the second has one document into the identity map" do
|
3871
|
-
Mongoid::IdentityMap[Game.collection_name][game_two.id].should eq(game_two)
|
2111
|
+
expect(criteria).to eq([ person ])
|
3872
2112
|
end
|
3873
2113
|
end
|
3874
2114
|
end
|
@@ -3884,7 +2124,7 @@ describe Mongoid::Criteria do
|
|
3884
2124
|
end
|
3885
2125
|
|
3886
2126
|
it "returns the inclusions" do
|
3887
|
-
criteria.inclusions.
|
2127
|
+
expect(criteria.inclusions).to eq([ metadata ])
|
3888
2128
|
end
|
3889
2129
|
end
|
3890
2130
|
|
@@ -3903,7 +2143,7 @@ describe Mongoid::Criteria do
|
|
3903
2143
|
end
|
3904
2144
|
|
3905
2145
|
it "sets the inclusions" do
|
3906
|
-
criteria.inclusions.
|
2146
|
+
expect(criteria.inclusions).to eq([ metadata ])
|
3907
2147
|
end
|
3908
2148
|
end
|
3909
2149
|
|
@@ -3922,7 +2162,7 @@ describe Mongoid::Criteria do
|
|
3922
2162
|
end
|
3923
2163
|
|
3924
2164
|
it "returns the matching documents" do
|
3925
|
-
criteria.
|
2165
|
+
expect(criteria).to eq([ match ])
|
3926
2166
|
end
|
3927
2167
|
end
|
3928
2168
|
|
@@ -3941,7 +2181,7 @@ describe Mongoid::Criteria do
|
|
3941
2181
|
end
|
3942
2182
|
|
3943
2183
|
it "returns the matching documents" do
|
3944
|
-
criteria.
|
2184
|
+
expect(criteria).to eq([ match ])
|
3945
2185
|
end
|
3946
2186
|
end
|
3947
2187
|
|
@@ -3973,15 +2213,29 @@ describe Mongoid::Criteria do
|
|
3973
2213
|
Band.create(name: "Tool", likes: 100)
|
3974
2214
|
end
|
3975
2215
|
|
3976
|
-
|
3977
|
-
|
2216
|
+
context "when no timeout options are provided" do
|
2217
|
+
|
2218
|
+
let(:map_reduce) do
|
2219
|
+
Band.limit(2).map_reduce(map, reduce).out(inline: 1)
|
2220
|
+
end
|
2221
|
+
|
2222
|
+
it "returns the map/reduce results" do
|
2223
|
+
expect(map_reduce).to eq([
|
2224
|
+
{ "_id" => "Depeche Mode", "value" => { "likes" => 200 }},
|
2225
|
+
{ "_id" => "Tool", "value" => { "likes" => 100 }}
|
2226
|
+
])
|
2227
|
+
end
|
3978
2228
|
end
|
3979
2229
|
|
3980
|
-
|
3981
|
-
|
3982
|
-
|
3983
|
-
|
3984
|
-
|
2230
|
+
context "when timeout options are provided" do
|
2231
|
+
|
2232
|
+
let(:map_reduce) do
|
2233
|
+
Band.limit(2).no_timeout.map_reduce(map, reduce).out(replace: "test_bands")
|
2234
|
+
end
|
2235
|
+
|
2236
|
+
it "sets the timeout option on the query" do
|
2237
|
+
expect(map_reduce.send(:documents).operation.flags).to eq([ :no_cursor_timeout ])
|
2238
|
+
end
|
3985
2239
|
end
|
3986
2240
|
end
|
3987
2241
|
|
@@ -4008,7 +2262,7 @@ describe Mongoid::Criteria do
|
|
4008
2262
|
end
|
4009
2263
|
|
4010
2264
|
it "returns the max of the provided field" do
|
4011
|
-
max.
|
2265
|
+
expect(max).to eq(1000)
|
4012
2266
|
end
|
4013
2267
|
end
|
4014
2268
|
|
@@ -4021,7 +2275,7 @@ describe Mongoid::Criteria do
|
|
4021
2275
|
end
|
4022
2276
|
|
4023
2277
|
it "returns the document with the max value for the field" do
|
4024
|
-
max.
|
2278
|
+
expect(max).to eq(depeche)
|
4025
2279
|
end
|
4026
2280
|
end
|
4027
2281
|
end
|
@@ -4046,7 +2300,7 @@ describe Mongoid::Criteria do
|
|
4046
2300
|
end
|
4047
2301
|
|
4048
2302
|
it "returns the matching documents" do
|
4049
|
-
criteria.
|
2303
|
+
expect(criteria).to eq([ match ])
|
4050
2304
|
end
|
4051
2305
|
end
|
4052
2306
|
|
@@ -4077,27 +2331,27 @@ describe Mongoid::Criteria do
|
|
4077
2331
|
end
|
4078
2332
|
|
4079
2333
|
it "merges the selector" do
|
4080
|
-
merged.selector.
|
2334
|
+
expect(merged.selector).to eq({ "name" => "Depeche Mode" })
|
4081
2335
|
end
|
4082
2336
|
|
4083
2337
|
it "merges the options" do
|
4084
|
-
merged.options.
|
2338
|
+
expect(merged.options).to eq({ sort: { "name" => 1 }})
|
4085
2339
|
end
|
4086
2340
|
|
4087
2341
|
it "merges the documents" do
|
4088
|
-
merged.documents.
|
2342
|
+
expect(merged.documents).to eq([ band ])
|
4089
2343
|
end
|
4090
2344
|
|
4091
2345
|
it "merges the scoping options" do
|
4092
|
-
merged.scoping_options.
|
2346
|
+
expect(merged.scoping_options).to eq([ nil, nil ])
|
4093
2347
|
end
|
4094
2348
|
|
4095
2349
|
it "merges the inclusions" do
|
4096
|
-
merged.inclusions.
|
2350
|
+
expect(merged.inclusions).to eq([ metadata ])
|
4097
2351
|
end
|
4098
2352
|
|
4099
2353
|
it "returns a new criteria" do
|
4100
|
-
merged.
|
2354
|
+
expect(merged).to_not equal(criteria)
|
4101
2355
|
end
|
4102
2356
|
end
|
4103
2357
|
|
@@ -4116,23 +2370,23 @@ describe Mongoid::Criteria do
|
|
4116
2370
|
end
|
4117
2371
|
|
4118
2372
|
it "merges the selector" do
|
4119
|
-
merged.selector.
|
2373
|
+
expect(merged.selector).to eq({ "name" => "Depeche Mode" })
|
4120
2374
|
end
|
4121
2375
|
|
4122
2376
|
it "merges the options" do
|
4123
|
-
merged.options.
|
2377
|
+
expect(merged.options).to eq({ sort: { "name" => 1 }})
|
4124
2378
|
end
|
4125
2379
|
|
4126
2380
|
it "merges the scoping options" do
|
4127
|
-
merged.scoping_options.
|
2381
|
+
expect(merged.scoping_options).to eq([ nil, nil ])
|
4128
2382
|
end
|
4129
2383
|
|
4130
2384
|
it "merges the inclusions" do
|
4131
|
-
merged.inclusions.
|
2385
|
+
expect(merged.inclusions).to eq([ metadata ])
|
4132
2386
|
end
|
4133
2387
|
|
4134
2388
|
it "returns a new criteria" do
|
4135
|
-
merged.
|
2389
|
+
expect(merged).to_not equal(criteria)
|
4136
2390
|
end
|
4137
2391
|
end
|
4138
2392
|
end
|
@@ -4162,27 +2416,27 @@ describe Mongoid::Criteria do
|
|
4162
2416
|
end
|
4163
2417
|
|
4164
2418
|
it "merges the selector" do
|
4165
|
-
merged.selector.
|
2419
|
+
expect(merged.selector).to eq({ "name" => "Depeche Mode" })
|
4166
2420
|
end
|
4167
2421
|
|
4168
2422
|
it "merges the options" do
|
4169
|
-
merged.options.
|
2423
|
+
expect(merged.options).to eq({ sort: { "name" => 1 }})
|
4170
2424
|
end
|
4171
2425
|
|
4172
2426
|
it "merges the documents" do
|
4173
|
-
merged.documents.
|
2427
|
+
expect(merged.documents).to eq([ band ])
|
4174
2428
|
end
|
4175
2429
|
|
4176
2430
|
it "merges the scoping options" do
|
4177
|
-
merged.scoping_options.
|
2431
|
+
expect(merged.scoping_options).to eq([ nil, nil ])
|
4178
2432
|
end
|
4179
2433
|
|
4180
2434
|
it "merges the inclusions" do
|
4181
|
-
merged.inclusions.
|
2435
|
+
expect(merged.inclusions).to eq([ metadata ])
|
4182
2436
|
end
|
4183
2437
|
|
4184
2438
|
it "returns the same criteria" do
|
4185
|
-
merged.
|
2439
|
+
expect(merged).to equal(criteria)
|
4186
2440
|
end
|
4187
2441
|
end
|
4188
2442
|
|
@@ -4209,7 +2463,7 @@ describe Mongoid::Criteria do
|
|
4209
2463
|
end
|
4210
2464
|
|
4211
2465
|
it "returns the min of the provided field" do
|
4212
|
-
min.
|
2466
|
+
expect(min).to eq(500)
|
4213
2467
|
end
|
4214
2468
|
end
|
4215
2469
|
|
@@ -4222,7 +2476,7 @@ describe Mongoid::Criteria do
|
|
4222
2476
|
end
|
4223
2477
|
|
4224
2478
|
it "returns the document with the min value for the field" do
|
4225
|
-
min.
|
2479
|
+
expect(min).to eq(tool)
|
4226
2480
|
end
|
4227
2481
|
end
|
4228
2482
|
end
|
@@ -4243,7 +2497,7 @@ describe Mongoid::Criteria do
|
|
4243
2497
|
end
|
4244
2498
|
|
4245
2499
|
it "returns the matching documents" do
|
4246
|
-
criteria.
|
2500
|
+
expect(criteria).to eq([ match ])
|
4247
2501
|
end
|
4248
2502
|
end
|
4249
2503
|
|
@@ -4262,7 +2516,7 @@ describe Mongoid::Criteria do
|
|
4262
2516
|
end
|
4263
2517
|
|
4264
2518
|
it "returns the matching documents" do
|
4265
|
-
criteria.
|
2519
|
+
expect(criteria).to eq([ match ])
|
4266
2520
|
end
|
4267
2521
|
end
|
4268
2522
|
|
@@ -4281,7 +2535,7 @@ describe Mongoid::Criteria do
|
|
4281
2535
|
end
|
4282
2536
|
|
4283
2537
|
it "returns the matching documents" do
|
4284
|
-
criteria.
|
2538
|
+
expect(criteria).to eq([ match ])
|
4285
2539
|
end
|
4286
2540
|
end
|
4287
2541
|
|
@@ -4300,7 +2554,7 @@ describe Mongoid::Criteria do
|
|
4300
2554
|
end
|
4301
2555
|
|
4302
2556
|
it "returns the matching documents" do
|
4303
|
-
criteria.
|
2557
|
+
expect(criteria).to eq([ match ])
|
4304
2558
|
end
|
4305
2559
|
end
|
4306
2560
|
|
@@ -4319,7 +2573,7 @@ describe Mongoid::Criteria do
|
|
4319
2573
|
end
|
4320
2574
|
|
4321
2575
|
it "returns the matching documents" do
|
4322
|
-
criteria.
|
2576
|
+
expect(criteria).to eq([ match ])
|
4323
2577
|
end
|
4324
2578
|
end
|
4325
2579
|
|
@@ -4338,7 +2592,7 @@ describe Mongoid::Criteria do
|
|
4338
2592
|
end
|
4339
2593
|
|
4340
2594
|
it "returns the matching documents" do
|
4341
|
-
criteria.
|
2595
|
+
expect(criteria).to eq([ match ])
|
4342
2596
|
end
|
4343
2597
|
end
|
4344
2598
|
|
@@ -4357,11 +2611,13 @@ describe Mongoid::Criteria do
|
|
4357
2611
|
end
|
4358
2612
|
|
4359
2613
|
it "limits the returned fields" do
|
4360
|
-
|
2614
|
+
expect {
|
2615
|
+
criteria.first.name
|
2616
|
+
}.to raise_error(ActiveModel::MissingAttributeError)
|
4361
2617
|
end
|
4362
2618
|
|
4363
2619
|
it "does not add _type to the fields" do
|
4364
|
-
criteria.options[:fields]["_type"].
|
2620
|
+
expect(criteria.options[:fields]["_type"]).to be_nil
|
4365
2621
|
end
|
4366
2622
|
end
|
4367
2623
|
|
@@ -4372,15 +2628,17 @@ describe Mongoid::Criteria do
|
|
4372
2628
|
end
|
4373
2629
|
|
4374
2630
|
it "includes the limited fields" do
|
4375
|
-
criteria.first.name.
|
2631
|
+
expect(criteria.first.name).to_not be_nil
|
4376
2632
|
end
|
4377
2633
|
|
4378
2634
|
it "excludes the non included fields" do
|
4379
|
-
|
2635
|
+
expect {
|
2636
|
+
criteria.first.active
|
2637
|
+
}.to raise_error(ActiveModel::MissingAttributeError)
|
4380
2638
|
end
|
4381
2639
|
|
4382
2640
|
it "does not add _type to the fields" do
|
4383
|
-
criteria.options[:fields]["_type"].
|
2641
|
+
expect(criteria.options[:fields]["_type"]).to be_nil
|
4384
2642
|
end
|
4385
2643
|
end
|
4386
2644
|
|
@@ -4392,7 +2650,7 @@ describe Mongoid::Criteria do
|
|
4392
2650
|
|
4393
2651
|
it "only limits the fields on the correct model" do
|
4394
2652
|
criteria.each do |band|
|
4395
|
-
Person.new.age.
|
2653
|
+
expect(Person.new.age).to eq(100)
|
4396
2654
|
end
|
4397
2655
|
end
|
4398
2656
|
end
|
@@ -4405,7 +2663,7 @@ describe Mongoid::Criteria do
|
|
4405
2663
|
|
4406
2664
|
it "only limits the fields on the correct criteria" do
|
4407
2665
|
criteria.each do |band|
|
4408
|
-
Band.new.active.
|
2666
|
+
expect(Band.new.active).to be true
|
4409
2667
|
end
|
4410
2668
|
end
|
4411
2669
|
end
|
@@ -4419,7 +2677,7 @@ describe Mongoid::Criteria do
|
|
4419
2677
|
it "only limits the fields on the correct criteria" do
|
4420
2678
|
criteria.each do |band|
|
4421
2679
|
Band.all.each do |b|
|
4422
|
-
b.active.
|
2680
|
+
expect(b.active).to be true
|
4423
2681
|
end
|
4424
2682
|
end
|
4425
2683
|
end
|
@@ -4433,7 +2691,7 @@ describe Mongoid::Criteria do
|
|
4433
2691
|
end
|
4434
2692
|
|
4435
2693
|
it "adds _type to the fields" do
|
4436
|
-
criteria.options[:fields]["_type"].
|
2694
|
+
expect(criteria.options[:fields]["_type"]).to eq(1)
|
4437
2695
|
end
|
4438
2696
|
end
|
4439
2697
|
|
@@ -4446,7 +2704,7 @@ describe Mongoid::Criteria do
|
|
4446
2704
|
end
|
4447
2705
|
|
4448
2706
|
it "properly uses the database field name" do
|
4449
|
-
criteria.options.
|
2707
|
+
expect(criteria.options).to eq(fields: { "mobile_phones" => 1 })
|
4450
2708
|
end
|
4451
2709
|
end
|
4452
2710
|
end
|
@@ -4471,7 +2729,7 @@ describe Mongoid::Criteria do
|
|
4471
2729
|
end
|
4472
2730
|
|
4473
2731
|
it "returns the matching documents" do
|
4474
|
-
criteria.
|
2732
|
+
expect(criteria).to eq([ match ])
|
4475
2733
|
end
|
4476
2734
|
end
|
4477
2735
|
|
@@ -4482,7 +2740,7 @@ describe Mongoid::Criteria do
|
|
4482
2740
|
end
|
4483
2741
|
|
4484
2742
|
it "returns the matching documents" do
|
4485
|
-
criteria.
|
2743
|
+
expect(criteria).to eq([ match ])
|
4486
2744
|
end
|
4487
2745
|
end
|
4488
2746
|
end
|
@@ -4502,6 +2760,28 @@ describe Mongoid::Criteria do
|
|
4502
2760
|
Band.create(name: "Photek", likes: 1)
|
4503
2761
|
end
|
4504
2762
|
|
2763
|
+
context "when the field is aliased" do
|
2764
|
+
|
2765
|
+
let!(:expensive) do
|
2766
|
+
Product.create(price: 100000)
|
2767
|
+
end
|
2768
|
+
|
2769
|
+
let!(:cheap) do
|
2770
|
+
Product.create(price: 1)
|
2771
|
+
end
|
2772
|
+
|
2773
|
+
context "when using alias_attribute" do
|
2774
|
+
|
2775
|
+
let(:plucked) do
|
2776
|
+
Product.pluck(:price)
|
2777
|
+
end
|
2778
|
+
|
2779
|
+
it "uses the aliases" do
|
2780
|
+
expect(plucked).to eq([ 100000, 1 ])
|
2781
|
+
end
|
2782
|
+
end
|
2783
|
+
end
|
2784
|
+
|
4505
2785
|
context "when the criteria matches" do
|
4506
2786
|
|
4507
2787
|
context "when there are no duplicate values" do
|
@@ -4515,7 +2795,7 @@ describe Mongoid::Criteria do
|
|
4515
2795
|
end
|
4516
2796
|
|
4517
2797
|
it "returns the values" do
|
4518
|
-
plucked.
|
2798
|
+
expect(plucked).to eq([ "Depeche Mode", "Tool", "Photek" ])
|
4519
2799
|
end
|
4520
2800
|
|
4521
2801
|
context "when subsequently executing the criteria without a pluck" do
|
@@ -4526,6 +2806,17 @@ describe Mongoid::Criteria do
|
|
4526
2806
|
end
|
4527
2807
|
end
|
4528
2808
|
|
2809
|
+
context "when plucking mult-fields" do
|
2810
|
+
|
2811
|
+
let(:plucked) do
|
2812
|
+
Band.where(:name.exists => true).pluck(:name, :likes)
|
2813
|
+
end
|
2814
|
+
|
2815
|
+
it "returns the values" do
|
2816
|
+
expect(plucked).to eq([ ["Depeche Mode", 3], ["Tool", 3], ["Photek", 1] ])
|
2817
|
+
end
|
2818
|
+
end
|
2819
|
+
|
4529
2820
|
context "when there are duplicate values" do
|
4530
2821
|
|
4531
2822
|
let(:plucked) do
|
@@ -4533,7 +2824,7 @@ describe Mongoid::Criteria do
|
|
4533
2824
|
end
|
4534
2825
|
|
4535
2826
|
it "returns the duplicates" do
|
4536
|
-
plucked.
|
2827
|
+
expect(plucked).to eq([ 3, 3, 1 ])
|
4537
2828
|
end
|
4538
2829
|
end
|
4539
2830
|
end
|
@@ -4545,7 +2836,7 @@ describe Mongoid::Criteria do
|
|
4545
2836
|
end
|
4546
2837
|
|
4547
2838
|
it "returns an empty array" do
|
4548
|
-
plucked.
|
2839
|
+
expect(plucked).to be_empty
|
4549
2840
|
end
|
4550
2841
|
end
|
4551
2842
|
|
@@ -4556,7 +2847,43 @@ describe Mongoid::Criteria do
|
|
4556
2847
|
end
|
4557
2848
|
|
4558
2849
|
it "returns the field values" do
|
4559
|
-
plucked.
|
2850
|
+
expect(plucked).to eq([ depeche.id, tool.id, photek.id ])
|
2851
|
+
end
|
2852
|
+
end
|
2853
|
+
|
2854
|
+
context "when plucking existent and non-existent fields" do
|
2855
|
+
|
2856
|
+
let(:plucked) do
|
2857
|
+
Band.all.pluck(:id, :fooz)
|
2858
|
+
end
|
2859
|
+
|
2860
|
+
it "returns nil for the field that doesnt exist" do
|
2861
|
+
expect(plucked).to eq([[depeche.id, nil], [tool.id, nil], [photek.id, nil] ])
|
2862
|
+
end
|
2863
|
+
end
|
2864
|
+
|
2865
|
+
context "when plucking a field that doesnt exist" do
|
2866
|
+
|
2867
|
+
context "when pluck one field" do
|
2868
|
+
|
2869
|
+
let(:plucked) do
|
2870
|
+
Band.all.pluck(:foo)
|
2871
|
+
end
|
2872
|
+
|
2873
|
+
it "returns a array with nil values" do
|
2874
|
+
expect(plucked).to eq([nil, nil, nil])
|
2875
|
+
end
|
2876
|
+
end
|
2877
|
+
|
2878
|
+
context "when pluck multiple fields" do
|
2879
|
+
|
2880
|
+
let(:plucked) do
|
2881
|
+
Band.all.pluck(:foo, :bar)
|
2882
|
+
end
|
2883
|
+
|
2884
|
+
it "returns a nil arrays" do
|
2885
|
+
expect(plucked).to eq([[nil, nil], [nil, nil], [nil, nil]])
|
2886
|
+
end
|
4560
2887
|
end
|
4561
2888
|
end
|
4562
2889
|
end
|
@@ -4576,7 +2903,7 @@ describe Mongoid::Criteria do
|
|
4576
2903
|
context "when asking about a model public class method" do
|
4577
2904
|
|
4578
2905
|
it "returns true" do
|
4579
|
-
criteria.
|
2906
|
+
expect(criteria).to respond_to(:ages)
|
4580
2907
|
end
|
4581
2908
|
end
|
4582
2909
|
|
@@ -4585,7 +2912,7 @@ describe Mongoid::Criteria do
|
|
4585
2912
|
context "when including private methods" do
|
4586
2913
|
|
4587
2914
|
it "returns true" do
|
4588
|
-
criteria.respond_to?(:for_ids, true).
|
2915
|
+
expect(criteria.respond_to?(:for_ids, true)).to be true
|
4589
2916
|
end
|
4590
2917
|
end
|
4591
2918
|
end
|
@@ -4593,7 +2920,7 @@ describe Mongoid::Criteria do
|
|
4593
2920
|
context "when asking about a model class public instance method" do
|
4594
2921
|
|
4595
2922
|
it "returns true" do
|
4596
|
-
criteria.respond_to?(:join).
|
2923
|
+
expect(criteria.respond_to?(:join)).to be true
|
4597
2924
|
end
|
4598
2925
|
end
|
4599
2926
|
|
@@ -4602,14 +2929,14 @@ describe Mongoid::Criteria do
|
|
4602
2929
|
context "when not including private methods" do
|
4603
2930
|
|
4604
2931
|
it "returns false" do
|
4605
|
-
criteria.
|
2932
|
+
expect(criteria).to_not respond_to(:fork)
|
4606
2933
|
end
|
4607
2934
|
end
|
4608
2935
|
|
4609
2936
|
context "when including private methods" do
|
4610
2937
|
|
4611
2938
|
it "returns true" do
|
4612
|
-
criteria.respond_to?(:fork, true).
|
2939
|
+
expect(criteria.respond_to?(:fork, true)).to be true
|
4613
2940
|
end
|
4614
2941
|
end
|
4615
2942
|
end
|
@@ -4617,7 +2944,7 @@ describe Mongoid::Criteria do
|
|
4617
2944
|
context "when asking about a criteria instance method" do
|
4618
2945
|
|
4619
2946
|
it "returns true" do
|
4620
|
-
criteria.
|
2947
|
+
expect(criteria).to respond_to(:context)
|
4621
2948
|
end
|
4622
2949
|
end
|
4623
2950
|
|
@@ -4626,14 +2953,14 @@ describe Mongoid::Criteria do
|
|
4626
2953
|
context "when not including private methods" do
|
4627
2954
|
|
4628
2955
|
it "returns false" do
|
4629
|
-
criteria.
|
2956
|
+
expect(criteria).to_not respond_to(:puts)
|
4630
2957
|
end
|
4631
2958
|
end
|
4632
2959
|
|
4633
2960
|
context "when including private methods" do
|
4634
2961
|
|
4635
2962
|
it "returns true" do
|
4636
|
-
criteria.respond_to?(:puts, true).
|
2963
|
+
expect(criteria.respond_to?(:puts, true)).to be true
|
4637
2964
|
end
|
4638
2965
|
end
|
4639
2966
|
end
|
@@ -4656,7 +2983,7 @@ describe Mongoid::Criteria do
|
|
4656
2983
|
end
|
4657
2984
|
|
4658
2985
|
it "sorts the results in memory" do
|
4659
|
-
sorted.
|
2986
|
+
expect(sorted).to eq([ tool, depeche ])
|
4660
2987
|
end
|
4661
2988
|
end
|
4662
2989
|
|
@@ -4683,7 +3010,7 @@ describe Mongoid::Criteria do
|
|
4683
3010
|
end
|
4684
3011
|
|
4685
3012
|
it "returns the sum of the provided field" do
|
4686
|
-
sum.
|
3013
|
+
expect(sum).to eq(1500)
|
4687
3014
|
end
|
4688
3015
|
end
|
4689
3016
|
|
@@ -4694,7 +3021,7 @@ describe Mongoid::Criteria do
|
|
4694
3021
|
end
|
4695
3022
|
|
4696
3023
|
it "returns the sum for the provided block" do
|
4697
|
-
sum.
|
3024
|
+
expect(sum).to eq(1500)
|
4698
3025
|
end
|
4699
3026
|
end
|
4700
3027
|
end
|
@@ -4711,7 +3038,7 @@ describe Mongoid::Criteria do
|
|
4711
3038
|
end
|
4712
3039
|
|
4713
3040
|
it "returns the executed criteria" do
|
4714
|
-
criteria.to_ary.
|
3041
|
+
expect(criteria.to_ary).to eq([ band ])
|
4715
3042
|
end
|
4716
3043
|
end
|
4717
3044
|
|
@@ -4726,7 +3053,7 @@ describe Mongoid::Criteria do
|
|
4726
3053
|
end
|
4727
3054
|
|
4728
3055
|
it "executes the criteria while properly giving the hint to Mongo" do
|
4729
|
-
expect { criteria.to_ary }.to raise_error(Moped::Errors::QueryFailure
|
3056
|
+
expect { criteria.to_ary }.to raise_error(Moped::Errors::QueryFailure)
|
4730
3057
|
end
|
4731
3058
|
end
|
4732
3059
|
|
@@ -4741,7 +3068,7 @@ describe Mongoid::Criteria do
|
|
4741
3068
|
end
|
4742
3069
|
|
4743
3070
|
it "executes the criteria while properly giving the hint to Mongo" do
|
4744
|
-
expect { criteria.to_ary }.to raise_error(Moped::Errors::QueryFailure
|
3071
|
+
expect { criteria.to_ary }.to raise_error(Moped::Errors::QueryFailure)
|
4745
3072
|
end
|
4746
3073
|
end
|
4747
3074
|
|
@@ -4759,7 +3086,32 @@ describe Mongoid::Criteria do
|
|
4759
3086
|
end
|
4760
3087
|
|
4761
3088
|
it "executes the criteria while properly giving the max scan to Mongo" do
|
4762
|
-
criteria.to_ary.
|
3089
|
+
expect(criteria.to_ary).to eq [band]
|
3090
|
+
end
|
3091
|
+
end
|
3092
|
+
|
3093
|
+
describe "#text_search" do
|
3094
|
+
|
3095
|
+
let(:criteria) do
|
3096
|
+
Word.all
|
3097
|
+
end
|
3098
|
+
|
3099
|
+
before do
|
3100
|
+
Word.with(database: "admin").mongo_session.command(setParameter: 1, textSearchEnabled: true)
|
3101
|
+
Word.create_indexes
|
3102
|
+
Word.create!(name: "phase", origin: "latin")
|
3103
|
+
end
|
3104
|
+
|
3105
|
+
after(:all) do
|
3106
|
+
Word.remove_indexes
|
3107
|
+
end
|
3108
|
+
|
3109
|
+
let(:search) do
|
3110
|
+
criteria.text_search("phase")
|
3111
|
+
end
|
3112
|
+
|
3113
|
+
it "returns all fields" do
|
3114
|
+
expect(search.first.origin).to eq("latin")
|
4763
3115
|
end
|
4764
3116
|
end
|
4765
3117
|
|
@@ -4770,7 +3122,7 @@ describe Mongoid::Criteria do
|
|
4770
3122
|
end
|
4771
3123
|
|
4772
3124
|
it "returns self" do
|
4773
|
-
criteria.to_criteria.
|
3125
|
+
expect(criteria.to_criteria).to eq(criteria)
|
4774
3126
|
end
|
4775
3127
|
end
|
4776
3128
|
|
@@ -4781,11 +3133,11 @@ describe Mongoid::Criteria do
|
|
4781
3133
|
end
|
4782
3134
|
|
4783
3135
|
it "returns a proc" do
|
4784
|
-
criteria.to_proc.
|
3136
|
+
expect(criteria.to_proc).to be_a(Proc)
|
4785
3137
|
end
|
4786
3138
|
|
4787
3139
|
it "wraps the criteria in the proc" do
|
4788
|
-
criteria.to_proc[].
|
3140
|
+
expect(criteria.to_proc[]).to eq(criteria)
|
4789
3141
|
end
|
4790
3142
|
end
|
4791
3143
|
|
@@ -4802,7 +3154,7 @@ describe Mongoid::Criteria do
|
|
4802
3154
|
end
|
4803
3155
|
|
4804
3156
|
it "returns documents with the provided type" do
|
4805
|
-
criteria.
|
3157
|
+
expect(criteria).to eq([ browser ])
|
4806
3158
|
end
|
4807
3159
|
end
|
4808
3160
|
|
@@ -4817,7 +3169,7 @@ describe Mongoid::Criteria do
|
|
4817
3169
|
end
|
4818
3170
|
|
4819
3171
|
it "returns documents with the provided types" do
|
4820
|
-
criteria.
|
3172
|
+
expect(criteria).to eq([ browser ])
|
4821
3173
|
end
|
4822
3174
|
end
|
4823
3175
|
end
|
@@ -4850,7 +3202,7 @@ describe Mongoid::Criteria do
|
|
4850
3202
|
end
|
4851
3203
|
|
4852
3204
|
it "returns the matching documents" do
|
4853
|
-
criteria.
|
3205
|
+
expect(criteria).to eq([ match ])
|
4854
3206
|
end
|
4855
3207
|
end
|
4856
3208
|
end
|
@@ -4864,18 +3216,18 @@ describe Mongoid::Criteria do
|
|
4864
3216
|
end
|
4865
3217
|
|
4866
3218
|
it "returns the matching documents" do
|
4867
|
-
criteria.
|
3219
|
+
expect(criteria).to eq([ match ])
|
4868
3220
|
end
|
4869
3221
|
end
|
4870
3222
|
|
4871
3223
|
context "when the criteria is an exact fk array match" do
|
4872
3224
|
|
4873
3225
|
let(:id_one) do
|
4874
|
-
|
3226
|
+
BSON::ObjectId.new
|
4875
3227
|
end
|
4876
3228
|
|
4877
3229
|
let(:id_two) do
|
4878
|
-
|
3230
|
+
BSON::ObjectId.new
|
4879
3231
|
end
|
4880
3232
|
|
4881
3233
|
let(:criteria) do
|
@@ -4883,7 +3235,7 @@ describe Mongoid::Criteria do
|
|
4883
3235
|
end
|
4884
3236
|
|
4885
3237
|
it "does not wrap the array in another array" do
|
4886
|
-
criteria.selector.
|
3238
|
+
expect(criteria.selector).to eq({ "agent_ids" => [ id_one, id_two ]})
|
4887
3239
|
end
|
4888
3240
|
end
|
4889
3241
|
end
|
@@ -4902,7 +3254,7 @@ describe Mongoid::Criteria do
|
|
4902
3254
|
end
|
4903
3255
|
|
4904
3256
|
it "returns the matching documents" do
|
4905
|
-
criteria.
|
3257
|
+
expect(criteria).to eq([ match ])
|
4906
3258
|
end
|
4907
3259
|
end
|
4908
3260
|
|
@@ -4913,7 +3265,7 @@ describe Mongoid::Criteria do
|
|
4913
3265
|
end
|
4914
3266
|
|
4915
3267
|
it "returns the matching documents" do
|
4916
|
-
criteria.
|
3268
|
+
expect(criteria).to eq([ match ])
|
4917
3269
|
end
|
4918
3270
|
end
|
4919
3271
|
end
|
@@ -4927,7 +3279,7 @@ describe Mongoid::Criteria do
|
|
4927
3279
|
context "when the method exists on the class" do
|
4928
3280
|
|
4929
3281
|
before do
|
4930
|
-
Person.
|
3282
|
+
expect(Person).to receive(:minor).and_call_original
|
4931
3283
|
end
|
4932
3284
|
|
4933
3285
|
it "calls the method on the class" do
|
@@ -4938,7 +3290,7 @@ describe Mongoid::Criteria do
|
|
4938
3290
|
context "when the method exists on the criteria" do
|
4939
3291
|
|
4940
3292
|
before do
|
4941
|
-
criteria.
|
3293
|
+
expect(criteria).to receive(:to_criteria).and_call_original
|
4942
3294
|
end
|
4943
3295
|
|
4944
3296
|
it "calls the method on the criteria" do
|
@@ -4949,7 +3301,7 @@ describe Mongoid::Criteria do
|
|
4949
3301
|
context "when the method exists on array" do
|
4950
3302
|
|
4951
3303
|
before do
|
4952
|
-
criteria.
|
3304
|
+
expect(criteria).to receive(:entries).and_call_original
|
4953
3305
|
end
|
4954
3306
|
|
4955
3307
|
it "calls the method on the criteria" do
|
@@ -4960,7 +3312,7 @@ describe Mongoid::Criteria do
|
|
4960
3312
|
context "when the method does not exist" do
|
4961
3313
|
|
4962
3314
|
before do
|
4963
|
-
criteria.
|
3315
|
+
expect(criteria).to receive(:entries).never
|
4964
3316
|
end
|
4965
3317
|
|
4966
3318
|
it "raises an error" do
|
@@ -4986,7 +3338,7 @@ describe Mongoid::Criteria do
|
|
4986
3338
|
end
|
4987
3339
|
|
4988
3340
|
it "passes the block through method_missing" do
|
4989
|
-
criteria.uniq(&:name).
|
3341
|
+
expect(criteria.uniq(&:name)).to eq([ band_one ])
|
4990
3342
|
end
|
4991
3343
|
end
|
4992
3344
|
|
@@ -4996,94 +3348,36 @@ describe Mongoid::Criteria do
|
|
4996
3348
|
Band.where(name: "Depeche Mode").with(collection: "artists")
|
4997
3349
|
end
|
4998
3350
|
|
4999
|
-
after do
|
5000
|
-
Band.persistence_options.clear
|
5001
|
-
end
|
5002
|
-
|
5003
3351
|
it "retains the criteria selection" do
|
5004
|
-
criteria.selector.
|
3352
|
+
expect(criteria.selector).to eq("name" => "Depeche Mode")
|
5005
3353
|
end
|
5006
3354
|
|
5007
3355
|
it "sets the persistence options" do
|
5008
|
-
|
3356
|
+
expect(criteria.persistence_options).to eq(collection: "artists")
|
5009
3357
|
end
|
5010
3358
|
end
|
5011
3359
|
|
5012
|
-
describe "#
|
3360
|
+
describe "#geo_spacial" do
|
5013
3361
|
|
5014
|
-
|
5015
|
-
Bar.create_indexes
|
5016
|
-
end
|
5017
|
-
|
5018
|
-
let!(:match) do
|
5019
|
-
Bar.create(location: [ 52.30, 13.25 ])
|
5020
|
-
end
|
5021
|
-
|
5022
|
-
let(:criteria) do
|
5023
|
-
Bar.within_box(location: [[ 50, 10 ], [ 60, 20 ]])
|
5024
|
-
end
|
5025
|
-
|
5026
|
-
it "returns the matching documents" do
|
5027
|
-
criteria.should eq([ match ])
|
5028
|
-
end
|
5029
|
-
end
|
5030
|
-
|
5031
|
-
describe "#within_circle" do
|
5032
|
-
|
5033
|
-
before do
|
5034
|
-
Bar.create_indexes
|
5035
|
-
end
|
5036
|
-
|
5037
|
-
let!(:match) do
|
5038
|
-
Bar.create(location: [ 52.30, 13.25 ])
|
5039
|
-
end
|
5040
|
-
|
5041
|
-
let(:criteria) do
|
5042
|
-
Bar.within_circle(location: [[ 52, 13 ], 0.5 ])
|
5043
|
-
end
|
5044
|
-
|
5045
|
-
it "returns the matching documents" do
|
5046
|
-
criteria.should eq([ match ])
|
5047
|
-
end
|
5048
|
-
end
|
5049
|
-
|
5050
|
-
describe "#within_polygon" do
|
5051
|
-
|
5052
|
-
before do
|
5053
|
-
Bar.create_indexes
|
5054
|
-
end
|
5055
|
-
|
5056
|
-
let!(:match) do
|
5057
|
-
Bar.create(location: [ 52.30, 13.25 ])
|
5058
|
-
end
|
5059
|
-
|
5060
|
-
let(:criteria) do
|
5061
|
-
Bar.within_polygon(
|
5062
|
-
location: [[ 50, 10 ], [ 50, 20 ], [ 60, 20 ], [ 60, 10 ]]
|
5063
|
-
)
|
5064
|
-
end
|
5065
|
-
|
5066
|
-
it "returns the matching documents" do
|
5067
|
-
criteria.should eq([ match ])
|
5068
|
-
end
|
5069
|
-
end
|
3362
|
+
context "when checking within a polygon" do
|
5070
3363
|
|
5071
|
-
|
5072
|
-
|
5073
|
-
|
5074
|
-
Bar.create_indexes
|
5075
|
-
end
|
3364
|
+
before do
|
3365
|
+
Bar.create_indexes
|
3366
|
+
end
|
5076
3367
|
|
5077
|
-
|
5078
|
-
|
5079
|
-
|
3368
|
+
let!(:match) do
|
3369
|
+
Bar.create(location: [ 52.30, 13.25 ])
|
3370
|
+
end
|
5080
3371
|
|
5081
|
-
|
5082
|
-
|
5083
|
-
|
3372
|
+
let(:criteria) do
|
3373
|
+
Bar.geo_spacial(
|
3374
|
+
:location.within_polygon => [[[ 50, 10 ], [ 50, 20 ], [ 60, 20 ], [ 60, 10 ], [ 50, 10 ]]]
|
3375
|
+
)
|
3376
|
+
end
|
5084
3377
|
|
5085
|
-
|
5086
|
-
|
3378
|
+
it "returns the matching documents" do
|
3379
|
+
expect(criteria).to eq([ match ])
|
3380
|
+
end
|
5087
3381
|
end
|
5088
3382
|
end
|
5089
3383
|
|
@@ -5102,7 +3396,7 @@ describe Mongoid::Criteria do
|
|
5102
3396
|
end
|
5103
3397
|
|
5104
3398
|
it "returns the matching documents" do
|
5105
|
-
criteria.
|
3399
|
+
expect(criteria).to eq([ match ])
|
5106
3400
|
end
|
5107
3401
|
end
|
5108
3402
|
|
@@ -5117,7 +3411,7 @@ describe Mongoid::Criteria do
|
|
5117
3411
|
end
|
5118
3412
|
|
5119
3413
|
it "returns the matching documents" do
|
5120
|
-
criteria.
|
3414
|
+
expect(criteria).to eq([ match ])
|
5121
3415
|
end
|
5122
3416
|
end
|
5123
3417
|
|
@@ -5132,7 +3426,7 @@ describe Mongoid::Criteria do
|
|
5132
3426
|
end
|
5133
3427
|
|
5134
3428
|
it "properly uses the database field name" do
|
5135
|
-
criteria.options.
|
3429
|
+
expect(criteria.options).to eq(fields: { "mobile_phones" => 0 })
|
5136
3430
|
end
|
5137
3431
|
end
|
5138
3432
|
end
|