mongoid 9.0.11 → 9.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -8
- data/README.md +5 -11
- data/Rakefile +35 -38
- data/lib/config/locales/en.yml +60 -0
- data/lib/mongoid/association/accessors.rb +27 -26
- data/lib/mongoid/association/bindable.rb +43 -45
- data/lib/mongoid/association/builders.rb +18 -15
- data/lib/mongoid/association/constrainable.rb +3 -5
- data/lib/mongoid/association/depending.rb +43 -43
- data/lib/mongoid/association/eager.rb +56 -13
- data/lib/mongoid/association/eager_load/discriminated_inclusion.rb +88 -0
- data/lib/mongoid/association/eager_load/embedded_distributor.rb +151 -0
- data/lib/mongoid/association/eager_load/inclusion.rb +173 -0
- data/lib/mongoid/association/eager_load/inclusion_tree.rb +96 -0
- data/lib/mongoid/association/eager_load/lookup_pipeline.rb +121 -0
- data/lib/mongoid/association/eager_load/polymorphic_preloader.rb +61 -0
- data/lib/mongoid/association/eager_load/polymorphic_targets.rb +151 -0
- data/lib/mongoid/association/eager_loadable.rb +108 -23
- data/lib/mongoid/association/embedded/batchable.rb +28 -30
- data/lib/mongoid/association/embedded/cyclic.rb +8 -11
- data/lib/mongoid/association/embedded/eager.rb +1 -6
- data/lib/mongoid/association/embedded/embedded_in/binding.rb +6 -9
- data/lib/mongoid/association/embedded/embedded_in/buildable.rb +2 -3
- data/lib/mongoid/association/embedded/embedded_in/proxy.rb +2 -0
- data/lib/mongoid/association/embedded/embedded_in.rb +25 -22
- data/lib/mongoid/association/embedded/embeds_many/binding.rb +0 -2
- data/lib/mongoid/association/embedded/embeds_many/buildable.rb +2 -3
- data/lib/mongoid/association/embedded/embeds_many/proxy.rb +7 -2
- data/lib/mongoid/association/embedded/embeds_many.rb +24 -21
- data/lib/mongoid/association/embedded/embeds_one/binding.rb +0 -2
- data/lib/mongoid/association/embedded/embeds_one/buildable.rb +2 -6
- data/lib/mongoid/association/embedded/embeds_one.rb +21 -18
- data/lib/mongoid/association/embedded.rb +0 -1
- data/lib/mongoid/association/macros.rb +16 -8
- data/lib/mongoid/association/many.rb +75 -3
- data/lib/mongoid/association/marshalable.rb +0 -3
- data/lib/mongoid/association/nested/many.rb +52 -18
- data/lib/mongoid/association/nested/nested_buildable.rb +20 -9
- data/lib/mongoid/association/nested/one.rb +10 -8
- data/lib/mongoid/association/nested.rb +1 -3
- data/lib/mongoid/association/one.rb +0 -3
- data/lib/mongoid/association/options.rb +25 -7
- data/lib/mongoid/association/referenced/auto_save.rb +0 -2
- data/lib/mongoid/association/referenced/belongs_to/binding.rb +11 -15
- data/lib/mongoid/association/referenced/belongs_to/buildable.rb +23 -5
- data/lib/mongoid/association/referenced/belongs_to/eager.rb +7 -4
- data/lib/mongoid/association/referenced/belongs_to.rb +49 -43
- data/lib/mongoid/association/referenced/counter_cache.rb +7 -12
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/binding.rb +0 -2
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb +8 -4
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb +0 -3
- data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +57 -47
- data/lib/mongoid/association/referenced/has_many/binding.rb +0 -2
- data/lib/mongoid/association/referenced/has_many/buildable.rb +11 -5
- data/lib/mongoid/association/referenced/has_many/eager.rb +0 -3
- data/lib/mongoid/association/referenced/has_many/enumerable.rb +109 -38
- data/lib/mongoid/association/referenced/has_many/proxy.rb +110 -7
- data/lib/mongoid/association/referenced/has_many.rb +30 -24
- data/lib/mongoid/association/referenced/has_many_through/eager.rb +99 -0
- data/lib/mongoid/association/referenced/has_many_through/proxy.rb +68 -0
- data/lib/mongoid/association/referenced/has_many_through.rb +201 -0
- data/lib/mongoid/association/referenced/has_one/binding.rb +0 -2
- data/lib/mongoid/association/referenced/has_one/buildable.rb +11 -9
- data/lib/mongoid/association/referenced/has_one/eager.rb +0 -3
- data/lib/mongoid/association/referenced/has_one.rb +27 -21
- data/lib/mongoid/association/referenced/has_one_through/eager.rb +65 -0
- data/lib/mongoid/association/referenced/has_one_through/proxy.rb +30 -0
- data/lib/mongoid/association/referenced/has_one_through.rb +167 -0
- data/lib/mongoid/association/referenced/syncable.rb +30 -34
- data/lib/mongoid/association/referenced.rb +2 -1
- data/lib/mongoid/association/reflections.rb +0 -3
- data/lib/mongoid/association/relatable.rb +108 -65
- data/lib/mongoid/association.rb +19 -15
- data/lib/mongoid/atomic/modifiers.rb +65 -25
- data/lib/mongoid/atomic/paths/embedded/many.rb +4 -7
- data/lib/mongoid/atomic/paths/embedded/one.rb +2 -4
- data/lib/mongoid/atomic/paths/embedded.rb +3 -6
- data/lib/mongoid/atomic/paths/root.rb +1 -4
- data/lib/mongoid/atomic/paths.rb +2 -3
- data/lib/mongoid/atomic.rb +0 -3
- data/lib/mongoid/attributes/dynamic.rb +6 -7
- data/lib/mongoid/attributes/nested.rb +8 -10
- data/lib/mongoid/attributes/projector.rb +7 -20
- data/lib/mongoid/attributes/readonly.rb +6 -11
- data/lib/mongoid/attributes.rb +18 -34
- data/lib/mongoid/cacheable.rb +24 -6
- data/lib/mongoid/clients/factory.rb +13 -22
- data/lib/mongoid/clients/options.rb +2 -5
- data/lib/mongoid/clients/sessions.rb +50 -60
- data/lib/mongoid/clients/storage_options.rb +1 -4
- data/lib/mongoid/clients/validators/storage.rb +2 -3
- data/lib/mongoid/clients/validators.rb +1 -2
- data/lib/mongoid/clients.rb +8 -11
- data/lib/mongoid/collection_configurable.rb +18 -14
- data/lib/mongoid/composable.rb +30 -34
- data/lib/mongoid/config/defaults.rb +6 -4
- data/lib/mongoid/config/encryption.rb +36 -21
- data/lib/mongoid/config/environment.rb +7 -16
- data/lib/mongoid/config/introspection.rb +14 -16
- data/lib/mongoid/config/options.rb +16 -10
- data/lib/mongoid/config/validators/async_query_executor.rb +3 -6
- data/lib/mongoid/config/validators/client.rb +12 -15
- data/lib/mongoid/config/validators/option.rb +3 -5
- data/lib/mongoid/config/validators.rb +3 -4
- data/lib/mongoid/config.rb +188 -47
- data/lib/mongoid/contextual/aggregable/memory.rb +7 -5
- data/lib/mongoid/contextual/aggregable/mongo.rb +20 -22
- data/lib/mongoid/contextual/aggregable/none.rb +2 -4
- data/lib/mongoid/contextual/aggregable.rb +5 -7
- data/lib/mongoid/contextual/atomic.rb +20 -24
- data/lib/mongoid/contextual/command.rb +0 -3
- data/lib/mongoid/contextual/map_reduce.rb +12 -16
- data/lib/mongoid/contextual/memory.rb +87 -69
- data/lib/mongoid/contextual/mongo/documents_loader.rb +2 -3
- data/lib/mongoid/contextual/mongo.rb +169 -85
- data/lib/mongoid/contextual/none.rb +9 -9
- data/lib/mongoid/contextual/queryable.rb +1 -4
- data/lib/mongoid/contextual.rb +18 -7
- data/lib/mongoid/copyable.rb +7 -9
- data/lib/mongoid/criteria/findable.rb +5 -7
- data/lib/mongoid/criteria/includable.rb +67 -14
- data/lib/mongoid/criteria/inspectable.rb +2 -5
- data/lib/mongoid/criteria/marshalable.rb +4 -8
- data/lib/mongoid/criteria/modifiable.rb +3 -11
- data/lib/mongoid/criteria/options.rb +0 -3
- data/lib/mongoid/criteria/permission.rb +34 -37
- data/lib/mongoid/criteria/queryable/aggregable.rb +9 -10
- data/lib/mongoid/criteria/queryable/expandable.rb +4 -9
- data/lib/mongoid/criteria/queryable/extensions/array.rb +7 -10
- data/lib/mongoid/criteria/queryable/extensions/big_decimal.rb +3 -5
- data/lib/mongoid/criteria/queryable/extensions/boolean.rb +1 -4
- data/lib/mongoid/criteria/queryable/extensions/date.rb +2 -6
- data/lib/mongoid/criteria/queryable/extensions/date_time.rb +3 -7
- data/lib/mongoid/criteria/queryable/extensions/hash.rb +1 -4
- data/lib/mongoid/criteria/queryable/extensions/nil_class.rb +20 -11
- data/lib/mongoid/criteria/queryable/extensions/numeric.rb +15 -11
- data/lib/mongoid/criteria/queryable/extensions/object.rb +7 -8
- data/lib/mongoid/criteria/queryable/extensions/range.rb +6 -9
- data/lib/mongoid/criteria/queryable/extensions/regexp.rb +10 -12
- data/lib/mongoid/criteria/queryable/extensions/set.rb +4 -6
- data/lib/mongoid/criteria/queryable/extensions/string.rb +5 -9
- data/lib/mongoid/criteria/queryable/extensions/symbol.rb +3 -7
- data/lib/mongoid/criteria/queryable/extensions/time.rb +2 -6
- data/lib/mongoid/criteria/queryable/extensions/time_with_zone.rb +2 -12
- data/lib/mongoid/criteria/queryable/extensions.rb +18 -21
- data/lib/mongoid/criteria/queryable/key.rb +12 -18
- data/lib/mongoid/criteria/queryable/macroable.rb +0 -3
- data/lib/mongoid/criteria/queryable/mergeable.rb +35 -39
- data/lib/mongoid/criteria/queryable/optional.rb +17 -18
- data/lib/mongoid/criteria/queryable/options.rb +20 -9
- data/lib/mongoid/criteria/queryable/pipeline.rb +6 -9
- data/lib/mongoid/criteria/queryable/selectable.rb +253 -199
- data/lib/mongoid/criteria/queryable/selector.rb +20 -29
- data/lib/mongoid/criteria/queryable/smash.rb +1 -6
- data/lib/mongoid/criteria/queryable/storable.rb +20 -39
- data/lib/mongoid/criteria/queryable.rb +25 -15
- data/lib/mongoid/criteria/scopable.rb +5 -11
- data/lib/mongoid/criteria/translator.rb +5 -8
- data/lib/mongoid/criteria.rb +109 -54
- data/lib/mongoid/deprecable.rb +45 -5
- data/lib/mongoid/deprecation.rb +2 -6
- data/lib/mongoid/document.rb +1 -1
- data/lib/mongoid/encryptable.rb +47 -2
- data/lib/mongoid/equality.rb +4 -5
- data/lib/mongoid/errors/ambiguous_relationship.rb +2 -5
- data/lib/mongoid/errors/attribute_not_loaded.rb +1 -4
- data/lib/mongoid/errors/callback.rb +1 -4
- data/lib/mongoid/errors/config_redactor.rb +43 -0
- data/lib/mongoid/errors/create_collection_failure.rb +8 -11
- data/lib/mongoid/errors/criteria_argument_required.rb +2 -5
- data/lib/mongoid/errors/delete_restriction.rb +1 -4
- data/lib/mongoid/errors/document_not_destroyed.rb +1 -4
- data/lib/mongoid/errors/document_not_found.rb +16 -21
- data/lib/mongoid/errors/drop_collection_failure.rb +7 -10
- data/lib/mongoid/errors/empty_config_file.rb +1 -4
- data/lib/mongoid/errors/immutable_attribute.rb +1 -4
- data/lib/mongoid/errors/in_memory_collation_not_supported.rb +1 -4
- data/lib/mongoid/errors/in_memory_regexp_timeout.rb +26 -0
- data/lib/mongoid/errors/invalid_async_query_executor.rb +2 -5
- data/lib/mongoid/errors/invalid_auto_encryption_configuration.rb +2 -5
- data/lib/mongoid/errors/invalid_collection.rb +1 -3
- data/lib/mongoid/errors/invalid_config_file.rb +1 -4
- data/lib/mongoid/errors/invalid_config_option.rb +2 -5
- data/lib/mongoid/errors/invalid_dependent_strategy.rb +6 -10
- data/lib/mongoid/errors/invalid_discriminator_key_target.rb +2 -4
- data/lib/mongoid/errors/invalid_dot_dollar_assignment.rb +1 -4
- data/lib/mongoid/errors/invalid_elem_match_operator.rb +5 -9
- data/lib/mongoid/errors/invalid_estimated_count_criteria.rb +1 -4
- data/lib/mongoid/errors/invalid_estimated_count_scoping.rb +1 -4
- data/lib/mongoid/errors/invalid_expression_operator.rb +3 -7
- data/lib/mongoid/errors/invalid_field.rb +2 -5
- data/lib/mongoid/errors/invalid_field_operator.rb +5 -9
- data/lib/mongoid/errors/invalid_field_option.rb +2 -5
- data/lib/mongoid/errors/invalid_field_type.rb +1 -4
- data/lib/mongoid/errors/invalid_find.rb +1 -4
- data/lib/mongoid/errors/invalid_global_executor_concurrency.rb +1 -4
- data/lib/mongoid/errors/invalid_includes.rb +3 -6
- data/lib/mongoid/errors/invalid_index.rb +1 -4
- data/lib/mongoid/errors/invalid_options.rb +1 -4
- data/lib/mongoid/errors/invalid_path.rb +1 -4
- data/lib/mongoid/errors/invalid_persistence_option.rb +4 -7
- data/lib/mongoid/errors/invalid_query.rb +6 -11
- data/lib/mongoid/errors/invalid_relation.rb +10 -13
- data/lib/mongoid/errors/invalid_relation_option.rb +4 -7
- data/lib/mongoid/errors/invalid_scope.rb +1 -4
- data/lib/mongoid/errors/invalid_session_nesting.rb +0 -3
- data/lib/mongoid/errors/invalid_set_polymorphic_relation.rb +2 -4
- data/lib/mongoid/errors/invalid_storage_options.rb +1 -4
- data/lib/mongoid/errors/invalid_time.rb +1 -4
- data/lib/mongoid/errors/invalid_transaction_nesting.rb +0 -3
- data/lib/mongoid/errors/inverse_not_found.rb +1 -4
- data/lib/mongoid/errors/mixed_client_configuration.rb +2 -5
- data/lib/mongoid/errors/mixed_relations.rb +1 -3
- data/lib/mongoid/errors/mongoid_error.rb +7 -11
- data/lib/mongoid/errors/nested_attributes_metadata_not_found.rb +1 -4
- data/lib/mongoid/errors/no_client_config.rb +1 -4
- data/lib/mongoid/errors/no_client_database.rb +2 -5
- data/lib/mongoid/errors/no_client_hosts.rb +2 -5
- data/lib/mongoid/errors/no_clients_config.rb +1 -4
- data/lib/mongoid/errors/no_default_client.rb +1 -4
- data/lib/mongoid/errors/no_encryption_schema.rb +28 -0
- data/lib/mongoid/errors/no_environment.rb +1 -4
- data/lib/mongoid/errors/no_map_reduce_output.rb +1 -4
- data/lib/mongoid/errors/no_metadata.rb +1 -4
- data/lib/mongoid/errors/no_parent.rb +1 -4
- data/lib/mongoid/errors/readonly_association.rb +29 -0
- data/lib/mongoid/errors/readonly_attribute.rb +1 -4
- data/lib/mongoid/errors/readonly_document.rb +1 -4
- data/lib/mongoid/errors/rollback.rb +0 -2
- data/lib/mongoid/errors/scope_overwrite.rb +2 -4
- data/lib/mongoid/errors/sessions_not_supported.rb +0 -3
- data/lib/mongoid/errors/too_many_nested_attribute_records.rb +1 -3
- data/lib/mongoid/errors/transaction_error.rb +0 -3
- data/lib/mongoid/errors/transactions_not_supported.rb +0 -3
- data/lib/mongoid/errors/unknown_attribute.rb +1 -4
- data/lib/mongoid/errors/unknown_model.rb +1 -4
- data/lib/mongoid/errors/unregistered_class.rb +1 -1
- data/lib/mongoid/errors/unsaved_document.rb +1 -3
- data/lib/mongoid/errors/unsupported_isolation_level.rb +22 -0
- data/lib/mongoid/errors/unsupported_javascript.rb +1 -4
- data/lib/mongoid/errors/validations.rb +3 -5
- data/lib/mongoid/errors.rb +76 -72
- data/lib/mongoid/evolvable.rb +0 -3
- data/lib/mongoid/extensions/array.rb +5 -7
- data/lib/mongoid/extensions/big_decimal.rb +6 -7
- data/lib/mongoid/extensions/binary.rb +3 -6
- data/lib/mongoid/extensions/boolean.rb +4 -7
- data/lib/mongoid/extensions/bson_document.rb +28 -0
- data/lib/mongoid/extensions/date.rb +10 -14
- data/lib/mongoid/extensions/date_time.rb +2 -6
- data/lib/mongoid/extensions/decimal128.rb +1 -5
- data/lib/mongoid/extensions/false_class.rb +3 -5
- data/lib/mongoid/extensions/float.rb +5 -10
- data/lib/mongoid/extensions/hash.rb +5 -10
- data/lib/mongoid/extensions/integer.rb +5 -10
- data/lib/mongoid/extensions/module.rb +1 -4
- data/lib/mongoid/extensions/nil_class.rb +0 -1
- data/lib/mongoid/extensions/object.rb +9 -14
- data/lib/mongoid/extensions/object_id.rb +2 -6
- data/lib/mongoid/extensions/range.rb +14 -15
- data/lib/mongoid/extensions/raw_value.rb +0 -3
- data/lib/mongoid/extensions/regexp.rb +3 -6
- data/lib/mongoid/extensions/set.rb +3 -4
- data/lib/mongoid/extensions/string.rb +8 -12
- data/lib/mongoid/extensions/symbol.rb +3 -7
- data/lib/mongoid/extensions/time.rb +24 -26
- data/lib/mongoid/extensions/time_with_zone.rb +4 -7
- data/lib/mongoid/extensions/true_class.rb +3 -5
- data/lib/mongoid/extensions/vector.rb +64 -0
- data/lib/mongoid/extensions.rb +27 -26
- data/lib/mongoid/factory.rb +5 -9
- data/lib/mongoid/field_readable.rb +69 -0
- data/lib/mongoid/fields/encrypted.rb +0 -1
- data/lib/mongoid/fields/foreign_key.rb +9 -10
- data/lib/mongoid/fields/localized.rb +12 -12
- data/lib/mongoid/fields/standard.rb +5 -7
- data/lib/mongoid/fields/validators/macro.rb +23 -34
- data/lib/mongoid/fields/validators.rb +1 -2
- data/lib/mongoid/fields.rb +151 -58
- data/lib/mongoid/findable.rb +82 -53
- data/lib/mongoid/indexable/specification.rb +20 -6
- data/lib/mongoid/indexable/validators/options.rb +33 -40
- data/lib/mongoid/indexable.rb +53 -40
- data/lib/mongoid/inspectable.rb +4 -7
- data/lib/mongoid/interceptable.rb +41 -40
- data/lib/mongoid/loadable.rb +17 -18
- data/lib/mongoid/loggable.rb +4 -8
- data/lib/mongoid/matchable.rb +6 -3
- data/lib/mongoid/matcher/all.rb +2 -5
- data/lib/mongoid/matcher/and.rb +0 -3
- data/lib/mongoid/matcher/bits.rb +13 -8
- data/lib/mongoid/matcher/bits_all_clear.rb +1 -3
- data/lib/mongoid/matcher/bits_all_set.rb +1 -3
- data/lib/mongoid/matcher/bits_any_clear.rb +1 -3
- data/lib/mongoid/matcher/bits_any_set.rb +1 -3
- data/lib/mongoid/matcher/elem_match.rb +13 -15
- data/lib/mongoid/matcher/elem_match_expression.rb +0 -3
- data/lib/mongoid/matcher/eq.rb +0 -3
- data/lib/mongoid/matcher/eq_impl.rb +12 -17
- data/lib/mongoid/matcher/eq_impl_with_regexp.rb +4 -9
- data/lib/mongoid/matcher/exists.rb +1 -4
- data/lib/mongoid/matcher/expression.rb +5 -10
- data/lib/mongoid/matcher/expression_operator.rb +1 -3
- data/lib/mongoid/matcher/field_expression.rb +10 -13
- data/lib/mongoid/matcher/field_operator.rb +4 -6
- data/lib/mongoid/matcher/gt.rb +0 -3
- data/lib/mongoid/matcher/gte.rb +0 -3
- data/lib/mongoid/matcher/in.rb +9 -12
- data/lib/mongoid/matcher/lt.rb +0 -3
- data/lib/mongoid/matcher/lte.rb +0 -3
- data/lib/mongoid/matcher/mod.rb +4 -8
- data/lib/mongoid/matcher/ne.rb +0 -3
- data/lib/mongoid/matcher/nin.rb +0 -3
- data/lib/mongoid/matcher/nor.rb +1 -6
- data/lib/mongoid/matcher/not.rb +0 -3
- data/lib/mongoid/matcher/or.rb +0 -3
- data/lib/mongoid/matcher/regex.rb +9 -13
- data/lib/mongoid/matcher/regexp_budget.rb +372 -0
- data/lib/mongoid/matcher/size.rb +2 -5
- data/lib/mongoid/matcher/type.rb +32 -37
- data/lib/mongoid/matcher.rb +8 -15
- data/lib/mongoid/model_resolver.rb +1 -0
- data/lib/mongoid/persistable/creatable.rb +23 -7
- data/lib/mongoid/persistable/deletable.rb +6 -7
- data/lib/mongoid/persistable/destroyable.rb +2 -6
- data/lib/mongoid/persistable/incrementable.rb +1 -3
- data/lib/mongoid/persistable/logical.rb +3 -5
- data/lib/mongoid/persistable/maxable.rb +2 -4
- data/lib/mongoid/persistable/minable.rb +2 -4
- data/lib/mongoid/persistable/multipliable.rb +1 -3
- data/lib/mongoid/persistable/poppable.rb +2 -4
- data/lib/mongoid/persistable/pullable.rb +3 -5
- data/lib/mongoid/persistable/pushable.rb +5 -7
- data/lib/mongoid/persistable/renamable.rb +1 -3
- data/lib/mongoid/persistable/savable.rb +0 -3
- data/lib/mongoid/persistable/settable.rb +4 -13
- data/lib/mongoid/persistable/unsettable.rb +1 -3
- data/lib/mongoid/persistable/updatable.rb +23 -33
- data/lib/mongoid/persistable/upsertable.rb +8 -10
- data/lib/mongoid/persistable.rb +28 -27
- data/lib/mongoid/persistence_context.rb +69 -41
- data/lib/mongoid/pluckable.rb +0 -2
- data/lib/mongoid/positional.rb +5 -11
- data/lib/mongoid/railtie.rb +16 -22
- data/lib/mongoid/railties/console_sandbox.rb +6 -8
- data/lib/mongoid/railties/controller_runtime.rb +7 -11
- data/lib/mongoid/railties/database.rake +42 -44
- data/lib/mongoid/scopable.rb +23 -31
- data/lib/mongoid/search_indexable.rb +378 -7
- data/lib/mongoid/selectable.rb +1 -3
- data/lib/mongoid/serializable.rb +18 -12
- data/lib/mongoid/shardable.rb +8 -17
- data/lib/mongoid/stateful.rb +5 -10
- data/lib/mongoid/stringified_symbol.rb +5 -9
- data/lib/mongoid/tasks/database.rake +31 -32
- data/lib/mongoid/tasks/database.rb +51 -39
- data/lib/mongoid/tasks/encryption.rb +10 -15
- data/lib/mongoid/threaded/lifecycle.rb +6 -9
- data/lib/mongoid/threaded.rb +168 -32
- data/lib/mongoid/timestamps/created/short.rb +2 -3
- data/lib/mongoid/timestamps/created.rb +1 -2
- data/lib/mongoid/timestamps/short.rb +0 -1
- data/lib/mongoid/timestamps/timeless.rb +92 -23
- data/lib/mongoid/timestamps/updated/short.rb +2 -3
- data/lib/mongoid/timestamps/updated.rb +2 -5
- data/lib/mongoid/timestamps.rb +4 -6
- data/lib/mongoid/touchable.rb +19 -11
- data/lib/mongoid/traversable.rb +5 -4
- data/lib/mongoid/validatable/associated.rb +19 -7
- data/lib/mongoid/validatable/format.rb +0 -2
- data/lib/mongoid/validatable/length.rb +0 -2
- data/lib/mongoid/validatable/localizable.rb +0 -3
- data/lib/mongoid/validatable/macros.rb +0 -2
- data/lib/mongoid/validatable/numericality.rb +24 -3
- data/lib/mongoid/validatable/presence.rb +8 -10
- data/lib/mongoid/validatable/queryable.rb +0 -3
- data/lib/mongoid/validatable/uniqueness.rb +12 -14
- data/lib/mongoid/validatable.rb +15 -20
- data/lib/mongoid/version.rb +1 -1
- data/lib/mongoid/warnings.rb +10 -11
- data/lib/mongoid.rb +30 -34
- data/lib/rails/generators/mongoid/config/config_generator.rb +2 -4
- data/lib/rails/generators/mongoid/config/templates/mongoid.rb +4 -5
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +2 -2
- data/lib/rails/generators/mongoid/model/model_generator.rb +5 -8
- data/lib/rails/generators/mongoid_generator.rb +6 -10
- data/lib/rails/mongoid.rb +1 -3
- data/spec/integration/active_job_spec.rb +1 -2
- data/spec/integration/app_spec.rb +149 -179
- data/spec/integration/associations/belongs_to_spec.rb +43 -28
- data/spec/integration/associations/embedded_dirty_spec.rb +0 -1
- data/spec/integration/associations/embedded_spec.rb +41 -43
- data/spec/integration/associations/embeds_many_spec.rb +46 -29
- data/spec/integration/associations/embeds_one_spec.rb +2 -1
- data/spec/integration/associations/foreign_key_spec.rb +10 -11
- data/spec/integration/associations/foreign_key_spec_models.rb +8 -9
- data/spec/integration/associations/has_and_belongs_to_many_spec.rb +33 -15
- data/spec/integration/associations/has_many_spec.rb +17 -18
- data/spec/integration/associations/has_one_spec.rb +42 -45
- data/spec/integration/associations/nested_attributes_assignment_spec.rb +36 -39
- data/spec/integration/associations/reverse_population_spec.rb +4 -5
- data/spec/integration/associations/reverse_population_spec_models.rb +0 -1
- data/spec/integration/associations/scope_option_spec.rb +18 -20
- data/spec/integration/atomic/modifiers_spec.rb +72 -52
- data/spec/integration/bson_regexp_raw_spec.rb +2 -3
- data/spec/integration/caching_spec.rb +68 -0
- data/spec/integration/callbacks_models.rb +11 -9
- data/spec/integration/callbacks_spec.rb +78 -80
- data/spec/integration/contextual/empty_spec.rb +19 -20
- data/spec/integration/criteria/alias_query_spec.rb +12 -19
- data/spec/integration/criteria/date_field_spec.rb +1 -2
- data/spec/integration/criteria/default_scope_spec.rb +23 -18
- data/spec/integration/criteria/logical_spec.rb +18 -19
- data/spec/integration/criteria/range_spec.rb +77 -86
- data/spec/integration/criteria/raw_value_spec.rb +122 -109
- data/spec/integration/criteria/time_with_zone_spec.rb +33 -24
- data/spec/integration/discriminator_key_spec.rb +85 -85
- data/spec/integration/discriminator_value_spec.rb +70 -73
- data/spec/integration/document_spec.rb +9 -10
- data/spec/integration/dots_and_dollars_spec.rb +115 -120
- data/spec/integration/encryption_spec.rb +157 -9
- data/spec/integration/i18n_fallbacks_spec.rb +7 -8
- data/spec/integration/isolation_state_spec.rb +241 -0
- data/spec/integration/matcher_examples_spec.rb +145 -168
- data/spec/integration/matcher_operator_data/in.yml +6 -6
- data/spec/integration/matcher_operator_data/regex.yml +21 -0
- data/spec/integration/matcher_operator_spec.rb +16 -26
- data/spec/integration/matcher_regexp_timeout_spec.rb +215 -0
- data/spec/integration/matcher_spec.rb +31 -32
- data/spec/integration/persistence/collection_options_spec.rb +0 -2
- data/spec/integration/persistence/range_field_spec.rb +24 -2
- data/spec/integration/query_operator_guard_spec.rb +89 -0
- data/spec/integration/server_query_spec.rb +43 -37
- data/spec/integration/shardable_spec.rb +10 -11
- data/spec/integration/stringified_symbol_field_spec.rb +28 -43
- data/spec/lite_spec_helper.rb +11 -12
- data/spec/mongoid/association/accessors_spec.rb +197 -285
- data/spec/mongoid/association/auto_save_spec.rb +62 -93
- data/spec/mongoid/association/builders_spec.rb +121 -74
- data/spec/mongoid/association/constrainable_spec.rb +9 -22
- data/spec/mongoid/association/counter_cache_spec.rb +71 -104
- data/spec/mongoid/association/depending_spec.rb +166 -208
- data/spec/mongoid/association/eager_loadable_spec.rb +47 -0
- data/spec/mongoid/association/eager_spec.rb +416 -80
- data/spec/mongoid/association/embedded/cyclic_spec.rb +36 -48
- data/spec/mongoid/association/embedded/dirty_spec.rb +15 -20
- data/spec/mongoid/association/embedded/embedded_in/binding_spec.rb +36 -54
- data/spec/mongoid/association/embedded/embedded_in/buildable_spec.rb +8 -15
- data/spec/mongoid/association/embedded/embedded_in/proxy_spec.rb +135 -175
- data/spec/mongoid/association/embedded/embedded_in_spec.rb +25 -127
- data/spec/mongoid/association/embedded/embeds_many/binding_spec.rb +9 -14
- data/spec/mongoid/association/embedded/embeds_many/buildable_spec.rb +38 -51
- data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +1184 -1340
- data/spec/mongoid/association/embedded/embeds_many_models.rb +17 -17
- data/spec/mongoid/association/embedded/embeds_many_query_spec.rb +11 -11
- data/spec/mongoid/association/embedded/embeds_many_spec.rb +25 -138
- data/spec/mongoid/association/embedded/embeds_one/binding_spec.rb +15 -23
- data/spec/mongoid/association/embedded/embeds_one/buildable_spec.rb +18 -26
- data/spec/mongoid/association/embedded/embeds_one/proxy_spec.rb +204 -273
- data/spec/mongoid/association/embedded/embeds_one_dnl_models.rb +0 -1
- data/spec/mongoid/association/embedded/embeds_one_models.rb +0 -1
- data/spec/mongoid/association/embedded/embeds_one_query_spec.rb +5 -6
- data/spec/mongoid/association/embedded/embeds_one_spec.rb +28 -143
- data/spec/mongoid/association/fallback_spec.rb +425 -0
- data/spec/mongoid/association/macros_spec.rb +239 -300
- data/spec/mongoid/association/nested/many_spec.rb +51 -73
- data/spec/mongoid/association/nested/one_spec.rb +56 -80
- data/spec/mongoid/association/options_spec.rb +187 -310
- data/spec/mongoid/association/polymorphic_spec.rb +35 -53
- data/spec/mongoid/association/referenced/belongs_to/binding_spec.rb +55 -76
- data/spec/mongoid/association/referenced/belongs_to/buildable_spec.rb +36 -41
- data/spec/mongoid/association/referenced/belongs_to/eager_spec.rb +98 -101
- data/spec/mongoid/association/referenced/belongs_to/proxy_spec.rb +258 -345
- data/spec/mongoid/association/referenced/belongs_to_models.rb +4 -3
- data/spec/mongoid/association/referenced/belongs_to_query_spec.rb +3 -4
- data/spec/mongoid/association/referenced/belongs_to_spec.rb +91 -294
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/binding_spec.rb +42 -56
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/buildable_spec.rb +41 -32
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/eager_spec.rb +36 -42
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_persistence_spec.rb +8 -9
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +819 -975
- data/spec/mongoid/association/referenced/has_and_belongs_to_many_models.rb +9 -9
- data/spec/mongoid/association/referenced/has_and_belongs_to_many_query_spec.rb +5 -6
- data/spec/mongoid/association/referenced/has_and_belongs_to_many_spec.rb +109 -177
- data/spec/mongoid/association/referenced/has_many/binding_spec.rb +29 -43
- data/spec/mongoid/association/referenced/has_many/buildable_spec.rb +53 -53
- data/spec/mongoid/association/referenced/has_many/eager_spec.rb +137 -67
- data/spec/mongoid/association/referenced/has_many/enumerable_spec.rb +431 -675
- data/spec/mongoid/association/referenced/has_many/proxy_query_spec.rb +1 -2
- data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +318 -11
- data/spec/mongoid/association/referenced/has_many_models.rb +1 -2
- data/spec/mongoid/association/referenced/has_many_query_spec.rb +3 -4
- data/spec/mongoid/association/referenced/has_many_spec.rb +44 -188
- data/spec/mongoid/association/referenced/has_many_through/eager_spec.rb +58 -0
- data/spec/mongoid/association/referenced/has_many_through/proxy_spec.rb +62 -0
- data/spec/mongoid/association/referenced/has_many_through_spec.rb +399 -0
- data/spec/mongoid/association/referenced/has_one/binding_spec.rb +26 -38
- data/spec/mongoid/association/referenced/has_one/buildable_spec.rb +28 -29
- data/spec/mongoid/association/referenced/has_one/eager_spec.rb +29 -42
- data/spec/mongoid/association/referenced/has_one/proxy_spec.rb +270 -332
- data/spec/mongoid/association/referenced/has_one_models.rb +1 -2
- data/spec/mongoid/association/referenced/has_one_query_spec.rb +3 -4
- data/spec/mongoid/association/referenced/has_one_spec.rb +40 -188
- data/spec/mongoid/association/referenced/has_one_through/eager_spec.rb +58 -0
- data/spec/mongoid/association/referenced/has_one_through/proxy_spec.rb +49 -0
- data/spec/mongoid/association/referenced/has_one_through_spec.rb +211 -0
- data/spec/mongoid/association/reflections_spec.rb +14 -24
- data/spec/mongoid/association/syncable_spec.rb +102 -125
- data/spec/mongoid/association_spec.rb +47 -70
- data/spec/mongoid/atomic/modifiers_spec.rb +184 -197
- data/spec/mongoid/atomic/paths/embedded/many_spec.rb +39 -52
- data/spec/mongoid/atomic/paths/embedded/one_spec.rb +35 -49
- data/spec/mongoid/atomic/paths/root_spec.rb +9 -15
- data/spec/mongoid/atomic/paths_spec.rb +81 -117
- data/spec/mongoid/atomic_spec.rb +156 -171
- data/spec/mongoid/attributes/dynamic_spec.rb +5 -6
- data/spec/mongoid/attributes/nested_spec.rb +1461 -1693
- data/spec/mongoid/attributes/nested_spec_models.rb +8 -9
- data/spec/mongoid/attributes/projector_spec.rb +2 -6
- data/spec/mongoid/attributes/readonly_spec.rb +73 -97
- data/spec/mongoid/attributes_spec.rb +669 -826
- data/spec/mongoid/cacheable_spec.rb +74 -44
- data/spec/mongoid/changeable_spec.rb +591 -733
- data/spec/mongoid/clients/factory_spec.rb +122 -154
- data/spec/mongoid/clients/options_spec.rb +40 -59
- data/spec/mongoid/clients/sessions_spec.rb +25 -44
- data/spec/mongoid/clients/transactions_spec.rb +33 -50
- data/spec/mongoid/clients/transactions_spec_models.rb +3 -4
- data/spec/mongoid/clients_spec.rb +265 -355
- data/spec/mongoid/collection_configurable_spec.rb +9 -12
- data/spec/mongoid/composable_spec.rb +2 -9
- data/spec/mongoid/config/defaults_spec.rb +56 -30
- data/spec/mongoid/config/encryption_spec.rb +282 -57
- data/spec/mongoid/config/environment_spec.rb +30 -40
- data/spec/mongoid/config/introspection_spec.rb +42 -37
- data/spec/mongoid/config/options_spec.rb +14 -21
- data/spec/mongoid/config_spec.rb +145 -195
- data/spec/mongoid/contextual/aggregable/memory_spec.rb +189 -135
- data/spec/mongoid/contextual/aggregable/memory_table_spec.rb +4 -7
- data/spec/mongoid/contextual/aggregable/mongo_spec.rb +149 -187
- data/spec/mongoid/contextual/aggregable/none_spec.rb +14 -16
- data/spec/mongoid/contextual/atomic_spec.rb +223 -271
- data/spec/mongoid/contextual/map_reduce_spec.rb +110 -138
- data/spec/mongoid/contextual/memory_spec.rb +883 -854
- data/spec/mongoid/contextual/mongo/documents_loader_spec.rb +12 -13
- data/spec/mongoid/contextual/mongo_spec.rb +1008 -1213
- data/spec/mongoid/contextual/none_spec.rb +54 -59
- data/spec/mongoid/contextual/short_circuit_spec.rb +139 -0
- data/spec/mongoid/copyable_spec.rb +254 -300
- data/spec/mongoid/copyable_spec_models.rb +0 -1
- data/spec/mongoid/criteria/findable_spec.rb +322 -387
- data/spec/mongoid/criteria/includable_spec.rb +1356 -432
- data/spec/mongoid/criteria/includable_spec_models.rb +16 -7
- data/spec/mongoid/criteria/inspectable_spec.rb +11 -14
- data/spec/mongoid/criteria/marshalable_spec.rb +11 -14
- data/spec/mongoid/criteria/modifiable_spec.rb +366 -512
- data/spec/mongoid/criteria/options_spec.rb +2 -7
- data/spec/mongoid/criteria/queryable/aggregable_spec.rb +112 -141
- data/spec/mongoid/criteria/queryable/expandable_spec.rb +8 -11
- data/spec/mongoid/criteria/queryable/extensions/array_spec.rb +127 -184
- data/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb +123 -60
- data/spec/mongoid/criteria/queryable/extensions/boolean_spec.rb +84 -117
- data/spec/mongoid/criteria/queryable/extensions/date_spec.rb +44 -65
- data/spec/mongoid/criteria/queryable/extensions/date_time_spec.rb +86 -108
- data/spec/mongoid/criteria/queryable/extensions/float_spec.rb +94 -33
- data/spec/mongoid/criteria/queryable/extensions/hash_spec.rb +103 -138
- data/spec/mongoid/criteria/queryable/extensions/integer_spec.rb +95 -34
- data/spec/mongoid/criteria/queryable/extensions/nil_class_spec.rb +25 -38
- data/spec/mongoid/criteria/queryable/extensions/numeric_spec.rb +26 -28
- data/spec/mongoid/criteria/queryable/extensions/object_spec.rb +32 -54
- data/spec/mongoid/criteria/queryable/extensions/range_spec.rb +136 -130
- data/spec/mongoid/criteria/queryable/extensions/regexp_raw_spec.rb +16 -24
- data/spec/mongoid/criteria/queryable/extensions/regexp_spec.rb +15 -24
- data/spec/mongoid/criteria/queryable/extensions/set_spec.rb +12 -19
- data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +46 -63
- data/spec/mongoid/criteria/queryable/extensions/symbol_spec.rb +28 -42
- data/spec/mongoid/criteria/queryable/extensions/time_spec.rb +73 -96
- data/spec/mongoid/criteria/queryable/extensions/time_with_zone_spec.rb +71 -92
- data/spec/mongoid/criteria/queryable/key_spec.rb +32 -36
- data/spec/mongoid/criteria/queryable/mergeable_spec.rb +44 -49
- data/spec/mongoid/criteria/queryable/optional_spec.rb +373 -486
- data/spec/mongoid/criteria/queryable/options_spec.rb +172 -123
- data/spec/mongoid/criteria/queryable/pipeline_spec.rb +65 -87
- data/spec/mongoid/criteria/queryable/queryable_spec.rb +41 -53
- data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +589 -679
- data/spec/mongoid/criteria/queryable/selectable_shared_examples.rb +7 -11
- data/spec/mongoid/criteria/queryable/selectable_spec.rb +807 -998
- data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +354 -190
- data/spec/mongoid/criteria/queryable/selector_spec.rb +294 -390
- data/spec/mongoid/criteria/queryable/smash_spec.rb +6 -11
- data/spec/mongoid/criteria/queryable/storable_spec.rb +54 -81
- data/spec/mongoid/criteria/queryable_mql_spec.rb +92 -0
- data/spec/mongoid/criteria/scopable_spec.rb +102 -138
- data/spec/mongoid/criteria/translator_spec.rb +59 -60
- data/spec/mongoid/criteria_projection_spec.rb +81 -99
- data/spec/mongoid/criteria_spec.rb +937 -949
- data/spec/mongoid/document_fields_spec.rb +28 -30
- data/spec/mongoid/document_persistence_context_spec.rb +13 -15
- data/spec/mongoid/document_query_spec.rb +9 -12
- data/spec/mongoid/document_spec.rb +317 -409
- data/spec/mongoid/encryptable_spec.rb +61 -0
- data/spec/mongoid/equality_spec.rb +69 -102
- data/spec/mongoid/errors/ambiguous_relationship_spec.rb +8 -11
- data/spec/mongoid/errors/attribute_not_loaded_spec.rb +7 -10
- data/spec/mongoid/errors/callback_spec.rb +8 -11
- data/spec/mongoid/errors/config_redactor_spec.rb +112 -0
- data/spec/mongoid/errors/delete_restriction_spec.rb +5 -8
- data/spec/mongoid/errors/document_not_destroyed_spec.rb +7 -10
- data/spec/mongoid/errors/document_not_found_spec.rb +51 -61
- data/spec/mongoid/errors/invalid_collection_spec.rb +9 -13
- data/spec/mongoid/errors/invalid_config_file_spec.rb +8 -11
- data/spec/mongoid/errors/invalid_config_option_spec.rb +8 -11
- data/spec/mongoid/errors/invalid_field_option_spec.rb +7 -10
- data/spec/mongoid/errors/invalid_field_spec.rb +10 -11
- data/spec/mongoid/errors/invalid_field_type_spec.rb +9 -12
- data/spec/mongoid/errors/invalid_find_spec.rb +8 -11
- data/spec/mongoid/errors/invalid_includes_spec.rb +9 -13
- data/spec/mongoid/errors/invalid_index_spec.rb +8 -11
- data/spec/mongoid/errors/invalid_options_spec.rb +8 -11
- data/spec/mongoid/errors/invalid_path_spec.rb +7 -10
- data/spec/mongoid/errors/invalid_relation_spec.rb +15 -16
- data/spec/mongoid/errors/invalid_scope_spec.rb +8 -11
- data/spec/mongoid/errors/invalid_set_polymorphic_relation_spec.rb +3 -6
- data/spec/mongoid/errors/invalid_storage_options_spec.rb +8 -11
- data/spec/mongoid/errors/invalid_time_spec.rb +8 -11
- data/spec/mongoid/errors/inverse_not_found_spec.rb +9 -12
- data/spec/mongoid/errors/mixed_client_configuration_spec.rb +40 -12
- data/spec/mongoid/errors/mixed_relations_spec.rb +8 -11
- data/spec/mongoid/errors/mongoid_error_spec.rb +21 -24
- data/spec/mongoid/errors/nested_attributes_metadata_not_found_spec.rb +6 -9
- data/spec/mongoid/errors/no_client_config_spec.rb +7 -10
- data/spec/mongoid/errors/no_client_database_spec.rb +38 -12
- data/spec/mongoid/errors/no_client_hosts_spec.rb +35 -12
- data/spec/mongoid/errors/no_clients_config_spec.rb +7 -10
- data/spec/mongoid/errors/no_environment_spec.rb +8 -11
- data/spec/mongoid/errors/no_map_reduce_output_spec.rb +8 -11
- data/spec/mongoid/errors/no_metadata_spec.rb +7 -10
- data/spec/mongoid/errors/no_parent_spec.rb +7 -10
- data/spec/mongoid/errors/readonly_association_spec.rb +34 -0
- data/spec/mongoid/errors/readonly_attribute_spec.rb +7 -10
- data/spec/mongoid/errors/readonly_document_spec.rb +6 -9
- data/spec/mongoid/errors/scope_overwrite_spec.rb +9 -12
- data/spec/mongoid/errors/too_many_nested_attribute_records_spec.rb +9 -12
- data/spec/mongoid/errors/unknown_attribute_spec.rb +5 -8
- data/spec/mongoid/errors/unsaved_document_spec.rb +8 -11
- data/spec/mongoid/errors/unsupported_javascript_spec.rb +9 -12
- data/spec/mongoid/errors/validations_spec.rb +11 -14
- data/spec/mongoid/extensions/array_spec.rb +70 -92
- data/spec/mongoid/extensions/big_decimal_spec.rb +233 -313
- data/spec/mongoid/extensions/binary_spec.rb +21 -32
- data/spec/mongoid/extensions/boolean_spec.rb +52 -75
- data/spec/mongoid/extensions/date_class_mongoize_spec.rb +43 -54
- data/spec/mongoid/extensions/date_spec.rb +26 -38
- data/spec/mongoid/extensions/date_time_spec.rb +22 -32
- data/spec/mongoid/extensions/decimal128_spec.rb +12 -18
- data/spec/mongoid/extensions/false_class_spec.rb +10 -17
- data/spec/mongoid/extensions/float_spec.rb +40 -61
- data/spec/mongoid/extensions/hash_spec.rb +46 -64
- data/spec/mongoid/extensions/integer_spec.rb +40 -61
- data/spec/mongoid/extensions/module_spec.rb +13 -18
- data/spec/mongoid/extensions/nil_class_spec.rb +2 -5
- data/spec/mongoid/extensions/object_id_spec.rb +159 -247
- data/spec/mongoid/extensions/object_spec.rb +31 -47
- data/spec/mongoid/extensions/range_spec.rb +112 -115
- data/spec/mongoid/extensions/raw_value_spec.rb +10 -12
- data/spec/mongoid/extensions/regexp_spec.rb +23 -36
- data/spec/mongoid/extensions/set_spec.rb +38 -49
- data/spec/mongoid/extensions/string_spec.rb +118 -167
- data/spec/mongoid/extensions/stringified_symbol_spec.rb +29 -43
- data/spec/mongoid/extensions/symbol_spec.rb +12 -20
- data/spec/mongoid/extensions/time_spec.rb +160 -202
- data/spec/mongoid/extensions/time_with_zone_spec.rb +79 -105
- data/spec/mongoid/extensions/true_class_spec.rb +10 -17
- data/spec/mongoid/extensions/vector_spec.rb +175 -0
- data/spec/mongoid/factory_spec.rb +125 -148
- data/spec/mongoid/fields/foreign_key_spec.rb +124 -169
- data/spec/mongoid/fields/localized_spec.rb +140 -185
- data/spec/mongoid/fields/standard_spec.rb +43 -59
- data/spec/mongoid/fields_spec.rb +751 -620
- data/spec/mongoid/findable_spec.rb +260 -228
- data/spec/mongoid/indexable/specification_spec.rb +42 -58
- data/spec/mongoid/indexable_spec.rb +260 -264
- data/spec/mongoid/inspectable_spec.rb +45 -55
- data/spec/mongoid/interceptable_spec.rb +784 -922
- data/spec/mongoid/interceptable_spec_models.rb +35 -35
- data/spec/mongoid/loading_spec.rb +47 -43
- data/spec/mongoid/loggable_spec.rb +3 -6
- data/spec/mongoid/matcher/extract_attribute_spec.rb +2 -6
- data/spec/mongoid/matcher/regexp_budget_spec.rb +570 -0
- data/spec/mongoid/mongoizable_spec.rb +81 -90
- data/spec/mongoid/monkey_patches_spec.rb +4 -0
- data/spec/mongoid/persistable/creatable_spec.rb +181 -220
- data/spec/mongoid/persistable/deletable_spec.rb +139 -179
- data/spec/mongoid/persistable/destroyable_spec.rb +135 -164
- data/spec/mongoid/persistable/incrementable_spec.rb +65 -84
- data/spec/mongoid/persistable/logical_spec.rb +48 -61
- data/spec/mongoid/persistable/maxable_spec.rb +45 -44
- data/spec/mongoid/persistable/minable_spec.rb +45 -44
- data/spec/mongoid/persistable/multipliable_spec.rb +54 -69
- data/spec/mongoid/persistable/poppable_spec.rb +47 -60
- data/spec/mongoid/persistable/pullable_spec.rb +79 -103
- data/spec/mongoid/persistable/pushable_spec.rb +106 -131
- data/spec/mongoid/persistable/renamable_spec.rb +52 -65
- data/spec/mongoid/persistable/savable_spec.rb +152 -189
- data/spec/mongoid/persistable/settable_spec.rb +121 -145
- data/spec/mongoid/persistable/unsettable_spec.rb +56 -73
- data/spec/mongoid/persistable/updatable_spec.rb +185 -232
- data/spec/mongoid/persistable/upsertable_spec.rb +66 -77
- data/spec/mongoid/persistable_spec.rb +132 -139
- data/spec/mongoid/persistence_context_spec.rb +31 -89
- data/spec/mongoid/positional_spec.rb +67 -79
- data/spec/mongoid/railties/bson_object_id_serializer_spec.rb +0 -3
- data/spec/mongoid/railties/console_sandbox_spec.rb +0 -1
- data/spec/mongoid/relations/proxy_spec.rb +0 -1
- data/spec/mongoid/reloadable_spec.rb +250 -145
- data/spec/mongoid/scopable_spec.rb +319 -398
- data/spec/mongoid/search_indexable_spec.rb +735 -44
- data/spec/mongoid/selectable_spec.rb +36 -51
- data/spec/mongoid/serializable_spec.rb +269 -312
- data/spec/mongoid/shardable_models.rb +5 -5
- data/spec/mongoid/shardable_spec.rb +44 -46
- data/spec/mongoid/stateful_spec.rb +55 -75
- data/spec/mongoid/tasks/database_rake_spec.rb +138 -141
- data/spec/mongoid/tasks/database_spec.rb +98 -80
- data/spec/mongoid/tasks/encryption_spec.rb +22 -20
- data/spec/mongoid/threaded_spec.rb +57 -83
- data/spec/mongoid/timestamps/created/short_spec.rb +12 -16
- data/spec/mongoid/timestamps/created_spec.rb +16 -20
- data/spec/mongoid/timestamps/timeless_spec.rb +228 -48
- data/spec/mongoid/timestamps/updated/short_spec.rb +21 -28
- data/spec/mongoid/timestamps/updated_spec.rb +18 -25
- data/spec/mongoid/timestamps_spec.rb +171 -188
- data/spec/mongoid/timestamps_spec_models.rb +12 -13
- data/spec/mongoid/touchable_spec.rb +355 -294
- data/spec/mongoid/touchable_spec_models.rb +45 -37
- data/spec/mongoid/traversable_spec.rb +435 -471
- data/spec/mongoid/validatable/associated_spec.rb +60 -59
- data/spec/mongoid/validatable/format_spec.rb +24 -34
- data/spec/mongoid/validatable/length_spec.rb +62 -86
- data/spec/mongoid/validatable/numericality_spec.rb +114 -23
- data/spec/mongoid/validatable/presence_spec.rb +149 -203
- data/spec/mongoid/validatable/uniqueness_spec.rb +632 -830
- data/spec/mongoid/validatable_spec.rb +71 -103
- data/spec/mongoid/warnings_spec.rb +5 -9
- data/spec/mongoid_spec.rb +25 -41
- data/spec/rails/controller_extension/controller_runtime_spec.rb +45 -49
- data/spec/rails/mongoid_spec.rb +15 -15
- data/spec/shared/lib/mrss/cluster_config.rb +5 -40
- data/spec/shared/lib/mrss/constraints.rb +7 -46
- data/spec/shared/lib/mrss/docker_runner.rb +10 -21
- data/spec/shared/lib/mrss/eg_config_utils.rb +0 -31
- data/spec/shared/lib/mrss/release/candidate.rb +18 -12
- data/spec/shared/lib/mrss/server_version_registry.rb +1 -5
- data/spec/shared/share/Dockerfile.erb +6 -8
- data/spec/shared/shlib/server.sh +0 -50
- data/spec/shared/shlib/set_env.sh +4 -10
- data/spec/spec_helper.rb +59 -32
- data/spec/support/authorization.rb +0 -1
- data/spec/support/client_registry.rb +1 -2
- data/spec/support/constraints.rb +8 -25
- data/spec/support/crypt/models.rb +164 -8
- data/spec/support/crypt.rb +6 -9
- data/spec/support/expectations.rb +21 -7
- data/spec/support/feature_sandbox.rb +1 -4
- data/spec/support/helpers.rb +0 -1
- data/spec/support/immutable_ids.rb +20 -21
- data/spec/support/macros.rb +1 -3
- data/spec/support/models/account.rb +4 -5
- data/spec/support/models/acolyte.rb +5 -5
- data/spec/support/models/actor.rb +3 -3
- data/spec/support/models/actress.rb +0 -1
- data/spec/support/models/address.rb +11 -11
- data/spec/support/models/address_component.rb +1 -1
- data/spec/support/models/address_number.rb +1 -1
- data/spec/support/models/agency.rb +1 -1
- data/spec/support/models/agent.rb +1 -1
- data/spec/support/models/album.rb +1 -2
- data/spec/support/models/alert.rb +1 -1
- data/spec/support/models/animal.rb +4 -5
- data/spec/support/models/answer.rb +1 -1
- data/spec/support/models/appointment.rb +2 -2
- data/spec/support/models/armrest.rb +0 -1
- data/spec/support/models/array_field.rb +0 -1
- data/spec/support/models/article.rb +0 -1
- data/spec/support/models/artist.rb +16 -12
- data/spec/support/models/artwork.rb +1 -1
- data/spec/support/models/audible_sound.rb +0 -1
- data/spec/support/models/audio.rb +2 -2
- data/spec/support/models/augmentation.rb +1 -2
- data/spec/support/models/author.rb +1 -1
- data/spec/support/models/baby.rb +1 -1
- data/spec/support/models/band.rb +0 -1
- data/spec/support/models/bar.rb +2 -2
- data/spec/support/models/basic.rb +0 -1
- data/spec/support/models/bed.rb +0 -1
- data/spec/support/models/big_palette.rb +0 -1
- data/spec/support/models/birthday.rb +3 -5
- data/spec/support/models/bolt.rb +0 -1
- data/spec/support/models/bomb.rb +1 -1
- data/spec/support/models/book.rb +1 -1
- data/spec/support/models/breed.rb +1 -1
- data/spec/support/models/browser.rb +1 -2
- data/spec/support/models/building.rb +0 -1
- data/spec/support/models/building_address.rb +1 -1
- data/spec/support/models/bus.rb +1 -1
- data/spec/support/models/business.rb +2 -2
- data/spec/support/models/callback_test.rb +1 -1
- data/spec/support/models/canvas.rb +5 -6
- data/spec/support/models/car.rb +0 -1
- data/spec/support/models/cat.rb +0 -2
- data/spec/support/models/catalog.rb +0 -1
- data/spec/support/models/category.rb +1 -1
- data/spec/support/models/child.rb +1 -1
- data/spec/support/models/child_doc.rb +5 -6
- data/spec/support/models/church.rb +1 -1
- data/spec/support/models/circle.rb +0 -1
- data/spec/support/models/circuit.rb +2 -2
- data/spec/support/models/circus.rb +0 -1
- data/spec/support/models/code.rb +2 -1
- data/spec/support/models/coding/pull_request.rb +0 -1
- data/spec/support/models/coding.rb +0 -1
- data/spec/support/models/comment.rb +0 -1
- data/spec/support/models/company.rb +0 -1
- data/spec/support/models/consumption_period.rb +0 -1
- data/spec/support/models/contextable_item.rb +1 -1
- data/spec/support/models/contractor.rb +1 -1
- data/spec/support/models/cookie.rb +0 -1
- data/spec/support/models/country_code.rb +2 -3
- data/spec/support/models/courier_job.rb +2 -2
- data/spec/support/models/cover.rb +0 -1
- data/spec/support/models/crate.rb +0 -1
- data/spec/support/models/customer.rb +0 -1
- data/spec/support/models/customer_address.rb +0 -1
- data/spec/support/models/deed.rb +1 -1
- data/spec/support/models/definition.rb +1 -1
- data/spec/support/models/delegating_patient.rb +0 -1
- data/spec/support/models/description.rb +0 -1
- data/spec/support/models/dictionary.rb +1 -1
- data/spec/support/models/division.rb +2 -2
- data/spec/support/models/doctor.rb +0 -1
- data/spec/support/models/dog.rb +2 -2
- data/spec/support/models/dokument.rb +1 -1
- data/spec/support/models/draft.rb +0 -1
- data/spec/support/models/dragon.rb +1 -1
- data/spec/support/models/driver.rb +2 -2
- data/spec/support/models/drug.rb +1 -1
- data/spec/support/models/dungeon.rb +1 -1
- data/spec/support/models/edit.rb +1 -1
- data/spec/support/models/email.rb +1 -1
- data/spec/support/models/employer.rb +1 -2
- data/spec/support/models/entry.rb +1 -1
- data/spec/support/models/eraser.rb +0 -1
- data/spec/support/models/even.rb +1 -1
- data/spec/support/models/event.rb +3 -4
- data/spec/support/models/exhibition.rb +1 -1
- data/spec/support/models/exhibitor.rb +1 -1
- data/spec/support/models/explosion.rb +1 -1
- data/spec/support/models/eye.rb +0 -1
- data/spec/support/models/eye_bowl.rb +2 -3
- data/spec/support/models/face.rb +2 -3
- data/spec/support/models/fanatic.rb +1 -1
- data/spec/support/models/favorite.rb +1 -1
- data/spec/support/models/filesystem.rb +1 -1
- data/spec/support/models/fire_hydrant.rb +1 -1
- data/spec/support/models/firefox.rb +0 -1
- data/spec/support/models/fish.rb +0 -1
- data/spec/support/models/folder.rb +0 -2
- data/spec/support/models/folder_item.rb +1 -3
- data/spec/support/models/fruits.rb +9 -5
- data/spec/support/models/game.rb +3 -4
- data/spec/support/models/ghost.rb +0 -1
- data/spec/support/models/guitar.rb +0 -1
- data/spec/support/models/hole.rb +0 -1
- data/spec/support/models/home.rb +1 -1
- data/spec/support/models/house.rb +2 -2
- data/spec/support/models/html_writer.rb +0 -1
- data/spec/support/models/id_key.rb +3 -3
- data/spec/support/models/idnodef.rb +0 -1
- data/spec/support/models/image.rb +0 -1
- data/spec/support/models/implant.rb +1 -2
- data/spec/support/models/instrument.rb +2 -3
- data/spec/support/models/item.rb +2 -2
- data/spec/support/models/jar.rb +1 -2
- data/spec/support/models/kaleidoscope.rb +1 -1
- data/spec/support/models/kangaroo.rb +1 -1
- data/spec/support/models/label.rb +2 -2
- data/spec/support/models/language.rb +1 -1
- data/spec/support/models/lat_lng.rb +0 -1
- data/spec/support/models/league.rb +1 -1
- data/spec/support/models/learner.rb +0 -1
- data/spec/support/models/line_item.rb +1 -1
- data/spec/support/models/location.rb +1 -1
- data/spec/support/models/login.rb +1 -2
- data/spec/support/models/manufacturer.rb +0 -1
- data/spec/support/models/meat.rb +1 -1
- data/spec/support/models/membership.rb +1 -1
- data/spec/support/models/message.rb +1 -2
- data/spec/support/models/minim.rb +0 -1
- data/spec/support/models/mixed_drink.rb +1 -1
- data/spec/support/models/modifier_models.rb +25 -0
- data/spec/support/models/mop.rb +0 -1
- data/spec/support/models/movie.rb +1 -1
- data/spec/support/models/my_hash.rb +1 -2
- data/spec/support/models/name.rb +4 -5
- data/spec/support/models/name_only.rb +0 -1
- data/spec/support/models/node.rb +1 -1
- data/spec/support/models/note.rb +3 -3
- data/spec/support/models/nut.rb +0 -1
- data/spec/support/models/odd.rb +1 -1
- data/spec/support/models/order.rb +1 -1
- data/spec/support/models/ordered_post.rb +1 -1
- data/spec/support/models/ordered_preference.rb +1 -1
- data/spec/support/models/oscar.rb +1 -1
- data/spec/support/models/other_owner_object.rb +0 -1
- data/spec/support/models/override.rb +3 -11
- data/spec/support/models/ownable.rb +1 -1
- data/spec/support/models/owner.rb +1 -1
- data/spec/support/models/pack.rb +0 -1
- data/spec/support/models/page.rb +3 -3
- data/spec/support/models/page_question.rb +1 -1
- data/spec/support/models/palette.rb +2 -2
- data/spec/support/models/parent.rb +2 -2
- data/spec/support/models/parent_doc.rb +1 -1
- data/spec/support/models/passport.rb +0 -1
- data/spec/support/models/patient.rb +2 -2
- data/spec/support/models/pdf_writer.rb +0 -1
- data/spec/support/models/pencil.rb +0 -1
- data/spec/support/models/person.rb +32 -28
- data/spec/support/models/pet.rb +1 -1
- data/spec/support/models/pet_owner.rb +1 -1
- data/spec/support/models/phone.rb +1 -2
- data/spec/support/models/piano.rb +0 -1
- data/spec/support/models/pizza.rb +1 -1
- data/spec/support/models/player.rb +5 -5
- data/spec/support/models/post.rb +12 -12
- data/spec/support/models/post_genre.rb +1 -1
- data/spec/support/models/powerup.rb +1 -2
- data/spec/support/models/preference.rb +2 -2
- data/spec/support/models/princess.rb +1 -1
- data/spec/support/models/product.rb +3 -3
- data/spec/support/models/profile.rb +2 -1
- data/spec/support/models/pronunciation.rb +1 -1
- data/spec/support/models/pub.rb +2 -2
- data/spec/support/models/publication/encyclopedia.rb +0 -1
- data/spec/support/models/publication/review.rb +0 -1
- data/spec/support/models/publication.rb +0 -1
- data/spec/support/models/purchase.rb +1 -1
- data/spec/support/models/purchased_item.rb +1 -1
- data/spec/support/models/question.rb +2 -2
- data/spec/support/models/quiz.rb +1 -1
- data/spec/support/models/rating.rb +1 -1
- data/spec/support/models/record.rb +2 -3
- data/spec/support/models/registry.rb +1 -1
- data/spec/support/models/role.rb +1 -1
- data/spec/support/models/root_category.rb +1 -1
- data/spec/support/models/sandbox/app/models/app_models_message.rb +0 -1
- data/spec/support/models/sandbox/lib/models/lib_models_message.rb +0 -1
- data/spec/support/models/sandbox/sandbox_message.rb +0 -1
- data/spec/support/models/sandbox/sandbox_user.rb +0 -1
- data/spec/support/models/sandbox/subdir/sandbox_comment.rb +0 -1
- data/spec/support/models/sandwich.rb +1 -1
- data/spec/support/models/scheduler.rb +0 -1
- data/spec/support/models/school.rb +0 -1
- data/spec/support/models/scribe.rb +1 -1
- data/spec/support/models/sealer.rb +0 -1
- data/spec/support/models/seat.rb +0 -1
- data/spec/support/models/seo.rb +1 -1
- data/spec/support/models/series.rb +1 -1
- data/spec/support/models/server.rb +1 -1
- data/spec/support/models/service.rb +2 -2
- data/spec/support/models/shape.rb +3 -3
- data/spec/support/models/shelf.rb +1 -1
- data/spec/support/models/shield.rb +2 -2
- data/spec/support/models/shipment_address.rb +0 -1
- data/spec/support/models/shipping_container.rb +1 -1
- data/spec/support/models/shipping_pack.rb +0 -1
- data/spec/support/models/shirt.rb +0 -1
- data/spec/support/models/shop.rb +2 -2
- data/spec/support/models/short_agent.rb +0 -1
- data/spec/support/models/short_quiz.rb +1 -1
- data/spec/support/models/simple.rb +1 -1
- data/spec/support/models/slave.rb +1 -1
- data/spec/support/models/song.rb +1 -2
- data/spec/support/models/sound.rb +2 -2
- data/spec/support/models/spacer.rb +0 -1
- data/spec/support/models/square.rb +0 -1
- data/spec/support/models/staff.rb +0 -1
- data/spec/support/models/store_as_dup_test1.rb +2 -2
- data/spec/support/models/store_as_dup_test2.rb +1 -1
- data/spec/support/models/store_as_dup_test3.rb +2 -2
- data/spec/support/models/store_as_dup_test4.rb +1 -1
- data/spec/support/models/strategy.rb +0 -1
- data/spec/support/models/student.rb +1 -2
- data/spec/support/models/sub_item.rb +0 -1
- data/spec/support/models/subscription.rb +2 -2
- data/spec/support/models/survey.rb +2 -2
- data/spec/support/models/symptom.rb +2 -2
- data/spec/support/models/system_role.rb +2 -3
- data/spec/support/models/tag.rb +2 -2
- data/spec/support/models/target.rb +1 -1
- data/spec/support/models/template.rb +1 -1
- data/spec/support/models/thing.rb +1 -1
- data/spec/support/models/threadlocker.rb +0 -1
- data/spec/support/models/title.rb +0 -1
- data/spec/support/models/tool.rb +3 -3
- data/spec/support/models/topping.rb +1 -1
- data/spec/support/models/toy.rb +0 -1
- data/spec/support/models/track.rb +1 -1
- data/spec/support/models/translation.rb +1 -1
- data/spec/support/models/tree.rb +2 -3
- data/spec/support/models/truck.rb +0 -1
- data/spec/support/models/updatable.rb +0 -1
- data/spec/support/models/user.rb +3 -4
- data/spec/support/models/user_account.rb +3 -3
- data/spec/support/models/validation_callback.rb +4 -4
- data/spec/support/models/vehicle.rb +2 -3
- data/spec/support/models/version.rb +1 -1
- data/spec/support/models/vertex.rb +0 -1
- data/spec/support/models/vet_visit.rb +1 -1
- data/spec/support/models/video.rb +2 -2
- data/spec/support/models/video_game.rb +0 -1
- data/spec/support/models/washer.rb +0 -1
- data/spec/support/models/weapon.rb +1 -2
- data/spec/support/models/wiki_page.rb +2 -3
- data/spec/support/models/word.rb +2 -2
- data/spec/support/models/word_origin.rb +1 -2
- data/spec/support/models/writer.rb +3 -3
- data/spec/support/rails_mock.rb +7 -12
- data/spec/support/shared/time.rb +0 -1
- data/spec/support/sinatra_mock.rb +4 -2
- data/spec/support/spec_config.rb +19 -18
- metadata +64 -2
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
|
-
# rubocop:todo all
|
|
3
2
|
|
|
4
|
-
require
|
|
5
|
-
require_relative
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
require_relative '../has_and_belongs_to_many_models'
|
|
6
5
|
|
|
7
6
|
describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
8
7
|
config_override :raise_not_found_error, true
|
|
9
8
|
|
|
10
|
-
around
|
|
11
|
-
original_preferences_association = Person.relations[
|
|
9
|
+
around do |example|
|
|
10
|
+
original_preferences_association = Person.relations['preferences']
|
|
12
11
|
Person.has_and_belongs_to_many :preferences, autosave: true
|
|
13
12
|
example.run
|
|
14
|
-
Person.relations[
|
|
13
|
+
Person.relations['preferences'] = original_preferences_association
|
|
15
14
|
end
|
|
16
15
|
|
|
17
16
|
after(:all) do
|
|
@@ -19,18 +18,14 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
19
18
|
Person.reset_callbacks(:destroy)
|
|
20
19
|
end
|
|
21
20
|
|
|
22
|
-
[
|
|
23
|
-
|
|
21
|
+
%i[<< push].each do |method|
|
|
24
22
|
describe "##{method}" do
|
|
25
|
-
|
|
26
|
-
context "when the inverse_of is nil" do
|
|
27
|
-
|
|
23
|
+
context 'when the inverse_of is nil' do
|
|
28
24
|
let!(:article) do
|
|
29
25
|
Article.create!
|
|
30
26
|
end
|
|
31
27
|
|
|
32
|
-
context
|
|
33
|
-
|
|
28
|
+
context 'when the child document is new' do
|
|
34
29
|
let(:preference) do
|
|
35
30
|
Preference.new
|
|
36
31
|
end
|
|
@@ -39,26 +34,24 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
39
34
|
article.preferences.send(method, preference)
|
|
40
35
|
end
|
|
41
36
|
|
|
42
|
-
it
|
|
37
|
+
it 'persists the child document' do
|
|
43
38
|
expect(preference).to be_persisted
|
|
44
39
|
end
|
|
45
40
|
end
|
|
46
41
|
|
|
47
|
-
context
|
|
48
|
-
|
|
42
|
+
context 'when the child document is not new' do
|
|
49
43
|
let(:preference) do
|
|
50
44
|
Preference.create!
|
|
51
45
|
end
|
|
52
46
|
|
|
53
|
-
it
|
|
54
|
-
expect(preference).
|
|
47
|
+
it 'does not persist the child document' do
|
|
48
|
+
expect(preference).not_to receive(:save)
|
|
55
49
|
article.preferences.send(method, preference)
|
|
56
50
|
end
|
|
57
51
|
end
|
|
58
52
|
end
|
|
59
53
|
|
|
60
|
-
context
|
|
61
|
-
|
|
54
|
+
context 'when the parent is a new record' do
|
|
62
55
|
let(:person) do
|
|
63
56
|
Person.new
|
|
64
57
|
end
|
|
@@ -71,13 +64,12 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
71
64
|
person.preferences.send(method, preference)
|
|
72
65
|
end
|
|
73
66
|
|
|
74
|
-
it
|
|
67
|
+
it 'returns an array of loaded documents' do
|
|
75
68
|
expect(result).to eq([ preference ])
|
|
76
69
|
end
|
|
77
70
|
end
|
|
78
71
|
|
|
79
|
-
context
|
|
80
|
-
|
|
72
|
+
context 'when the parent is not a new record' do
|
|
81
73
|
let(:person) do
|
|
82
74
|
Person.create!
|
|
83
75
|
end
|
|
@@ -90,15 +82,13 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
90
82
|
person.preferences.send(method, preference)
|
|
91
83
|
end
|
|
92
84
|
|
|
93
|
-
it
|
|
85
|
+
it 'returns an array of loaded documents' do
|
|
94
86
|
expect(result).to eq([ preference ])
|
|
95
87
|
end
|
|
96
88
|
end
|
|
97
89
|
|
|
98
|
-
context
|
|
99
|
-
|
|
100
|
-
context "when the inverse relation is not defined" do
|
|
101
|
-
|
|
90
|
+
context 'when the relations are not polymorphic' do
|
|
91
|
+
context 'when the inverse relation is not defined' do
|
|
102
92
|
let(:person) do
|
|
103
93
|
Person.new
|
|
104
94
|
end
|
|
@@ -111,19 +101,18 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
111
101
|
person.houses << house
|
|
112
102
|
end
|
|
113
103
|
|
|
114
|
-
it
|
|
104
|
+
it 'appends the document to the relation' do
|
|
115
105
|
expect(person.houses).to eq([ house ])
|
|
116
106
|
end
|
|
117
107
|
|
|
118
|
-
it
|
|
108
|
+
it 'sets the foreign key on the relation' do
|
|
119
109
|
expect(person.house_ids).to eq([ house.id ])
|
|
120
110
|
end
|
|
121
111
|
end
|
|
122
112
|
|
|
123
|
-
context
|
|
124
|
-
|
|
113
|
+
context 'when appending in a parent create block' do
|
|
125
114
|
let!(:preference) do
|
|
126
|
-
Preference.create!(name:
|
|
115
|
+
Preference.create!(name: 'testing')
|
|
127
116
|
end
|
|
128
117
|
|
|
129
118
|
let!(:person) do
|
|
@@ -132,39 +121,37 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
132
121
|
end
|
|
133
122
|
end
|
|
134
123
|
|
|
135
|
-
it
|
|
124
|
+
it 'adds the documents to the relation' do
|
|
136
125
|
expect(person.preferences).to eq([ preference ])
|
|
137
126
|
end
|
|
138
127
|
|
|
139
|
-
it
|
|
128
|
+
it 'sets the foreign key on the relation' do
|
|
140
129
|
expect(person.preference_ids).to eq([ preference.id ])
|
|
141
130
|
end
|
|
142
131
|
|
|
143
|
-
it
|
|
132
|
+
it 'sets the foreign key on the inverse relation' do
|
|
144
133
|
expect(preference.person_ids).to eq([ person.id ])
|
|
145
134
|
end
|
|
146
135
|
|
|
147
|
-
it
|
|
136
|
+
it 'saves the target' do
|
|
148
137
|
expect(preference).to be_persisted
|
|
149
138
|
end
|
|
150
139
|
|
|
151
|
-
it
|
|
140
|
+
it 'adds the correct number of documents' do
|
|
152
141
|
expect(person.preferences.size).to eq(1)
|
|
153
142
|
end
|
|
154
143
|
|
|
155
|
-
it
|
|
144
|
+
it 'persists the link' do
|
|
156
145
|
expect(person.reload.preferences).to eq([ preference ])
|
|
157
146
|
end
|
|
158
147
|
end
|
|
159
148
|
|
|
160
|
-
context
|
|
161
|
-
|
|
149
|
+
context 'when the parent is a new record' do
|
|
162
150
|
let(:person) do
|
|
163
151
|
Person.new
|
|
164
152
|
end
|
|
165
153
|
|
|
166
|
-
context
|
|
167
|
-
|
|
154
|
+
context 'when the child is new' do
|
|
168
155
|
let(:preference) do
|
|
169
156
|
Preference.new
|
|
170
157
|
end
|
|
@@ -173,46 +160,44 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
173
160
|
person.preferences.send(method, preference)
|
|
174
161
|
end
|
|
175
162
|
|
|
176
|
-
it
|
|
163
|
+
it 'adds the documents to the relation' do
|
|
177
164
|
expect(person.preferences).to eq([ preference ])
|
|
178
165
|
end
|
|
179
166
|
|
|
180
|
-
it
|
|
167
|
+
it 'sets the foreign key on the relation' do
|
|
181
168
|
expect(person.preference_ids).to eq([ preference.id ])
|
|
182
169
|
end
|
|
183
170
|
|
|
184
|
-
it
|
|
171
|
+
it 'sets the foreign key on the inverse relation' do
|
|
185
172
|
expect(preference.person_ids).to eq([ person.id ])
|
|
186
173
|
end
|
|
187
174
|
|
|
188
|
-
it
|
|
175
|
+
it 'does not save the target' do
|
|
189
176
|
expect(preference).to be_new_record
|
|
190
177
|
end
|
|
191
178
|
|
|
192
|
-
it
|
|
179
|
+
it 'adds the correct number of documents' do
|
|
193
180
|
expect(person.preferences.size).to eq(1)
|
|
194
181
|
end
|
|
195
182
|
|
|
196
|
-
context
|
|
197
|
-
|
|
183
|
+
context 'when appending a second time' do
|
|
198
184
|
before do
|
|
199
185
|
person.preferences.send(method, preference)
|
|
200
186
|
end
|
|
201
187
|
|
|
202
|
-
it
|
|
188
|
+
it 'does not allow the document to be added again' do
|
|
203
189
|
expect(person.preferences).to eq([ preference ])
|
|
204
190
|
end
|
|
205
191
|
|
|
206
|
-
it
|
|
192
|
+
it 'does not allow duplicate ids' do
|
|
207
193
|
expect(person.preference_ids).to eq([ preference.id ])
|
|
208
194
|
end
|
|
209
195
|
end
|
|
210
196
|
end
|
|
211
197
|
|
|
212
|
-
context
|
|
213
|
-
|
|
198
|
+
context 'when the child is already persisted' do
|
|
214
199
|
let!(:persisted) do
|
|
215
|
-
Preference.create!(name:
|
|
200
|
+
Preference.create!(name: 'testy')
|
|
216
201
|
end
|
|
217
202
|
|
|
218
203
|
let(:preference) do
|
|
@@ -224,35 +209,34 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
224
209
|
person.save!
|
|
225
210
|
end
|
|
226
211
|
|
|
227
|
-
it
|
|
212
|
+
it 'adds the documents to the relation' do
|
|
228
213
|
expect(person.preferences).to eq([ preference ])
|
|
229
214
|
end
|
|
230
215
|
|
|
231
|
-
it
|
|
216
|
+
it 'sets the foreign key on the relation' do
|
|
232
217
|
expect(person.preference_ids).to eq([ preference.id ])
|
|
233
218
|
end
|
|
234
219
|
|
|
235
|
-
it
|
|
220
|
+
it 'sets the foreign key on the inverse relation' do
|
|
236
221
|
expect(preference.person_ids).to eq([ person.id ])
|
|
237
222
|
end
|
|
238
223
|
|
|
239
|
-
it
|
|
224
|
+
it 'saves the target' do
|
|
240
225
|
expect(preference).to be_persisted
|
|
241
226
|
end
|
|
242
227
|
|
|
243
|
-
it
|
|
228
|
+
it 'adds the correct number of documents' do
|
|
244
229
|
expect(person.preferences.size).to eq(1)
|
|
245
230
|
end
|
|
246
231
|
|
|
247
|
-
it
|
|
232
|
+
it 'persists the link' do
|
|
248
233
|
expect(person.reload.preferences).to eq([ preference ])
|
|
249
234
|
end
|
|
250
235
|
end
|
|
251
236
|
|
|
252
|
-
context
|
|
253
|
-
|
|
237
|
+
context 'when setting via the associated ids' do
|
|
254
238
|
let!(:persisted) do
|
|
255
|
-
Preference.create!(name:
|
|
239
|
+
Preference.create!(name: 'testy')
|
|
256
240
|
end
|
|
257
241
|
|
|
258
242
|
let(:preference) do
|
|
@@ -267,30 +251,29 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
267
251
|
person.save!
|
|
268
252
|
end
|
|
269
253
|
|
|
270
|
-
it
|
|
254
|
+
it 'adds the documents to the relation' do
|
|
271
255
|
expect(person.preferences).to eq([ preference ])
|
|
272
256
|
end
|
|
273
257
|
|
|
274
|
-
it
|
|
258
|
+
it 'sets the foreign key on the relation' do
|
|
275
259
|
expect(person.preference_ids).to eq([ preference.id ])
|
|
276
260
|
end
|
|
277
261
|
|
|
278
|
-
it
|
|
262
|
+
it 'sets the foreign key on the inverse relation' do
|
|
279
263
|
expect(preference.reload.person_ids).to eq([ person.id ])
|
|
280
264
|
end
|
|
281
265
|
|
|
282
|
-
it
|
|
266
|
+
it 'adds the correct number of documents' do
|
|
283
267
|
expect(person.preferences.size).to eq(1)
|
|
284
268
|
end
|
|
285
269
|
|
|
286
|
-
it
|
|
270
|
+
it 'persists the link' do
|
|
287
271
|
expect(person.reload.preferences).to eq([ preference ])
|
|
288
272
|
end
|
|
289
273
|
end
|
|
290
274
|
end
|
|
291
275
|
|
|
292
|
-
context
|
|
293
|
-
|
|
276
|
+
context 'when the parent is not a new record' do
|
|
294
277
|
let(:person) do
|
|
295
278
|
Person.create!
|
|
296
279
|
end
|
|
@@ -303,36 +286,35 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
303
286
|
person.preferences.send(method, preference)
|
|
304
287
|
end
|
|
305
288
|
|
|
306
|
-
it
|
|
289
|
+
it 'adds the documents to the relation' do
|
|
307
290
|
expect(person.preferences).to eq([ preference ])
|
|
308
291
|
end
|
|
309
292
|
|
|
310
|
-
it
|
|
293
|
+
it 'sets the foreign key on the relation' do
|
|
311
294
|
expect(person.preference_ids).to eq([ preference.id ])
|
|
312
295
|
end
|
|
313
296
|
|
|
314
|
-
it
|
|
297
|
+
it 'sets the foreign key on the inverse relation' do
|
|
315
298
|
expect(preference.person_ids).to eq([ person.id ])
|
|
316
299
|
end
|
|
317
300
|
|
|
318
|
-
it
|
|
301
|
+
it 'sets the base on the inverse relation' do
|
|
319
302
|
expect(preference.people).to eq([ person ])
|
|
320
303
|
end
|
|
321
304
|
|
|
322
|
-
it
|
|
305
|
+
it 'sets the same instance on the inverse relation' do
|
|
323
306
|
expect(preference.people.first).to eql(person)
|
|
324
307
|
end
|
|
325
308
|
|
|
326
|
-
it
|
|
327
|
-
expect(preference).
|
|
309
|
+
it 'saves the target' do
|
|
310
|
+
expect(preference).not_to be_new_record
|
|
328
311
|
end
|
|
329
312
|
|
|
330
|
-
it
|
|
313
|
+
it 'adds the document to the target' do
|
|
331
314
|
expect(person.preferences.count).to eq(1)
|
|
332
315
|
end
|
|
333
316
|
|
|
334
|
-
context
|
|
335
|
-
|
|
317
|
+
context 'when documents already exist on the relation' do
|
|
336
318
|
let(:preference_two) do
|
|
337
319
|
Preference.new
|
|
338
320
|
end
|
|
@@ -341,38 +323,37 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
341
323
|
person.preferences.send(method, preference_two)
|
|
342
324
|
end
|
|
343
325
|
|
|
344
|
-
it
|
|
326
|
+
it 'adds the documents to the relation' do
|
|
345
327
|
expect(person.preferences).to eq([ preference, preference_two ])
|
|
346
328
|
end
|
|
347
329
|
|
|
348
|
-
it
|
|
330
|
+
it 'sets the foreign key on the relation' do
|
|
349
331
|
expect(person.preference_ids).to eq([ preference.id, preference_two.id ])
|
|
350
332
|
end
|
|
351
333
|
|
|
352
|
-
it
|
|
334
|
+
it 'sets the foreign key on the inverse relation' do
|
|
353
335
|
expect(preference_two.person_ids).to eq([ person.id ])
|
|
354
336
|
end
|
|
355
337
|
|
|
356
|
-
it
|
|
338
|
+
it 'sets the base on the inverse relation' do
|
|
357
339
|
expect(preference_two.people).to eq([ person ])
|
|
358
340
|
end
|
|
359
341
|
|
|
360
|
-
it
|
|
342
|
+
it 'sets the same instance on the inverse relation' do
|
|
361
343
|
expect(preference_two.people.first).to eql(person)
|
|
362
344
|
end
|
|
363
345
|
|
|
364
|
-
it
|
|
365
|
-
expect(preference).
|
|
346
|
+
it 'saves the target' do
|
|
347
|
+
expect(preference).not_to be_new_record
|
|
366
348
|
end
|
|
367
349
|
|
|
368
|
-
it
|
|
350
|
+
it 'adds the document to the target' do
|
|
369
351
|
expect(person.preferences.count).to eq(2)
|
|
370
352
|
end
|
|
371
353
|
end
|
|
372
354
|
end
|
|
373
355
|
|
|
374
|
-
context
|
|
375
|
-
|
|
356
|
+
context 'when both sides have been persisted' do
|
|
376
357
|
let(:person) do
|
|
377
358
|
Person.create!
|
|
378
359
|
end
|
|
@@ -385,27 +366,25 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
385
366
|
person.administrated_events << event
|
|
386
367
|
end
|
|
387
368
|
|
|
388
|
-
it
|
|
369
|
+
it 'sets the front side of the relation' do
|
|
389
370
|
expect(person.administrated_events).to eq([ event ])
|
|
390
371
|
end
|
|
391
372
|
|
|
392
|
-
it
|
|
373
|
+
it 'sets the inverse side of the relation' do
|
|
393
374
|
expect(event.administrators(true)).to eq([ person ])
|
|
394
375
|
end
|
|
395
376
|
|
|
396
|
-
context
|
|
397
|
-
|
|
398
|
-
it "sets the front side of the relation" do
|
|
377
|
+
context 'when reloading' do
|
|
378
|
+
it 'sets the front side of the relation' do
|
|
399
379
|
expect(person.reload.administrated_events).to eq([ event ])
|
|
400
380
|
end
|
|
401
381
|
|
|
402
|
-
it
|
|
382
|
+
it 'sets the inverse side of the relation' do
|
|
403
383
|
expect(event.reload.administrators).to eq([ person ])
|
|
404
384
|
end
|
|
405
385
|
end
|
|
406
386
|
|
|
407
|
-
context
|
|
408
|
-
|
|
387
|
+
context 'when performing a new database query' do
|
|
409
388
|
let(:loaded_person) do
|
|
410
389
|
Person.find(person.id)
|
|
411
390
|
end
|
|
@@ -414,18 +393,17 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
414
393
|
Event.find(event.id)
|
|
415
394
|
end
|
|
416
395
|
|
|
417
|
-
it
|
|
396
|
+
it 'sets the front side of the relation' do
|
|
418
397
|
expect(loaded_person.administrated_events).to eq([ event ])
|
|
419
398
|
end
|
|
420
399
|
|
|
421
|
-
it
|
|
400
|
+
it 'sets the inverse side of the relation' do
|
|
422
401
|
expect(loaded_event.administrators).to eq([ person ])
|
|
423
402
|
end
|
|
424
403
|
end
|
|
425
404
|
end
|
|
426
405
|
|
|
427
|
-
context
|
|
428
|
-
|
|
406
|
+
context 'when the relation also includes a has_many relation' do
|
|
429
407
|
let(:artwork) do
|
|
430
408
|
Artwork.create!
|
|
431
409
|
end
|
|
@@ -442,46 +420,43 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
442
420
|
artwork.exhibitors << exhibitor
|
|
443
421
|
end
|
|
444
422
|
|
|
445
|
-
it
|
|
423
|
+
it 'creates a single artwork object' do
|
|
446
424
|
expect(Artwork.count).to eq(1)
|
|
447
425
|
end
|
|
448
426
|
end
|
|
449
427
|
|
|
450
|
-
context
|
|
451
|
-
|
|
428
|
+
context 'when the relation is self referencing' do
|
|
452
429
|
let(:tag_one) do
|
|
453
|
-
Tag.create!(text:
|
|
430
|
+
Tag.create!(text: 'one')
|
|
454
431
|
end
|
|
455
432
|
|
|
456
433
|
let(:tag_two) do
|
|
457
|
-
Tag.create!(text:
|
|
434
|
+
Tag.create!(text: 'two')
|
|
458
435
|
end
|
|
459
436
|
|
|
460
437
|
before do
|
|
461
438
|
tag_one.related << tag_two
|
|
462
439
|
end
|
|
463
440
|
|
|
464
|
-
it
|
|
441
|
+
it 'sets the front side of the relation' do
|
|
465
442
|
expect(tag_one.related).to eq([ tag_two ])
|
|
466
443
|
end
|
|
467
444
|
|
|
468
|
-
it
|
|
445
|
+
it 'sets the inverse side of the relation' do
|
|
469
446
|
expect(tag_two.related(true)).to eq([ tag_one ])
|
|
470
447
|
end
|
|
471
448
|
|
|
472
|
-
context
|
|
473
|
-
|
|
474
|
-
it "sets the front side of the relation" do
|
|
449
|
+
context 'when reloading' do
|
|
450
|
+
it 'sets the front side of the relation' do
|
|
475
451
|
expect(tag_one.reload.related).to eq([ tag_two ])
|
|
476
452
|
end
|
|
477
453
|
|
|
478
|
-
it
|
|
454
|
+
it 'sets the inverse side of the relation' do
|
|
479
455
|
expect(tag_two.reload.related).to eq([ tag_one ])
|
|
480
456
|
end
|
|
481
457
|
end
|
|
482
458
|
|
|
483
|
-
context
|
|
484
|
-
|
|
459
|
+
context 'when performing a new database query' do
|
|
485
460
|
let(:loaded_tag_one) do
|
|
486
461
|
Tag.find(tag_one.id)
|
|
487
462
|
end
|
|
@@ -490,19 +465,18 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
490
465
|
Tag.find(tag_two.id)
|
|
491
466
|
end
|
|
492
467
|
|
|
493
|
-
it
|
|
468
|
+
it 'sets the front side of the relation' do
|
|
494
469
|
expect(loaded_tag_one.related).to eq([ tag_two ])
|
|
495
470
|
end
|
|
496
471
|
|
|
497
|
-
it
|
|
472
|
+
it 'sets the inverse side of the relation' do
|
|
498
473
|
expect(loaded_tag_two.related).to eq([ tag_one ])
|
|
499
474
|
end
|
|
500
475
|
end
|
|
501
476
|
end
|
|
502
477
|
end
|
|
503
478
|
|
|
504
|
-
context
|
|
505
|
-
|
|
479
|
+
context 'when association has callbacks' do
|
|
506
480
|
let(:post) do
|
|
507
481
|
Post.new
|
|
508
482
|
end
|
|
@@ -511,41 +485,37 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
511
485
|
Tag.new
|
|
512
486
|
end
|
|
513
487
|
|
|
514
|
-
context
|
|
515
|
-
|
|
516
|
-
it "executes the callback" do
|
|
488
|
+
context 'when the callback is a before_add' do
|
|
489
|
+
it 'executes the callback' do
|
|
517
490
|
post.tags.send(method, tag)
|
|
518
491
|
expect(post.before_add_called).to be true
|
|
519
492
|
end
|
|
520
493
|
|
|
521
|
-
context
|
|
522
|
-
|
|
494
|
+
context 'when errors are raised' do
|
|
523
495
|
before do
|
|
524
496
|
expect(post).to receive(:before_add_tag).and_raise
|
|
525
|
-
begin; post.tags.send(method, tag); rescue; end
|
|
497
|
+
begin; post.tags.send(method, tag); rescue StandardError; end
|
|
526
498
|
end
|
|
527
499
|
|
|
528
|
-
it
|
|
500
|
+
it 'does not add the document to the relation' do
|
|
529
501
|
expect(post.tags).to be_empty
|
|
530
502
|
end
|
|
531
503
|
end
|
|
532
504
|
end
|
|
533
505
|
|
|
534
|
-
context
|
|
535
|
-
|
|
536
|
-
it "executes the callback" do
|
|
506
|
+
context 'when the callback is an after_add' do
|
|
507
|
+
it 'executes the callback' do
|
|
537
508
|
post.tags.send(method, tag)
|
|
538
509
|
expect(post.after_add_called).to be true
|
|
539
510
|
end
|
|
540
511
|
|
|
541
|
-
context
|
|
542
|
-
|
|
512
|
+
context 'when errors are raised' do
|
|
543
513
|
before do
|
|
544
514
|
expect(post).to receive(:after_add_tag).and_raise
|
|
545
|
-
begin; post.tags.send(method, tag); rescue; end
|
|
515
|
+
begin; post.tags.send(method, tag); rescue StandardError; end
|
|
546
516
|
end
|
|
547
517
|
|
|
548
|
-
it
|
|
518
|
+
it 'adds the document to the relation' do
|
|
549
519
|
expect(post.tags).to eq([ tag ])
|
|
550
520
|
end
|
|
551
521
|
end
|
|
@@ -554,60 +524,55 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
554
524
|
end
|
|
555
525
|
end
|
|
556
526
|
|
|
557
|
-
describe
|
|
558
|
-
|
|
559
|
-
context "when trying to add duplicate entries" do
|
|
560
|
-
|
|
527
|
+
describe '#=' do
|
|
528
|
+
context 'when trying to add duplicate entries' do
|
|
561
529
|
let(:person) do
|
|
562
530
|
Person.new
|
|
563
531
|
end
|
|
564
532
|
|
|
565
533
|
let(:preference) do
|
|
566
|
-
Preference.create!(name:
|
|
534
|
+
Preference.create!(name: 'one')
|
|
567
535
|
end
|
|
568
536
|
|
|
569
537
|
before do
|
|
570
538
|
person.preferences = [ preference, preference ]
|
|
571
539
|
end
|
|
572
540
|
|
|
573
|
-
context
|
|
574
|
-
|
|
575
|
-
it "does not add the duplicates" do
|
|
541
|
+
context 'when the document is new' do
|
|
542
|
+
it 'does not add the duplicates' do
|
|
576
543
|
expect(person.preferences).to eq([ preference ])
|
|
577
544
|
end
|
|
578
545
|
|
|
579
|
-
it
|
|
546
|
+
it 'does not create duplicate keys' do
|
|
580
547
|
expect(person.preference_ids).to eq([ preference.id ])
|
|
581
548
|
end
|
|
582
549
|
end
|
|
583
550
|
|
|
584
|
-
context
|
|
585
|
-
|
|
551
|
+
context 'when the document is persisted' do
|
|
586
552
|
before do
|
|
587
553
|
person.save!
|
|
588
554
|
end
|
|
589
555
|
|
|
590
|
-
it
|
|
556
|
+
it 'does not add the duplicates' do
|
|
591
557
|
expect(person.preferences).to eq([ preference ])
|
|
592
558
|
end
|
|
593
559
|
|
|
594
|
-
it
|
|
560
|
+
it 'does not create duplicate keys' do
|
|
595
561
|
expect(person.preference_ids).to eq([ preference.id ])
|
|
596
562
|
end
|
|
597
563
|
|
|
598
|
-
it
|
|
564
|
+
it 'does not add duplicates on the inverse' do
|
|
599
565
|
expect(preference.people).to eq([ person ])
|
|
600
566
|
end
|
|
601
567
|
|
|
602
|
-
it
|
|
568
|
+
it 'does not add duplicate inverse keys' do
|
|
603
569
|
expect(preference.person_ids).to eq([ person.id ])
|
|
604
570
|
end
|
|
605
571
|
|
|
606
|
-
context
|
|
607
|
-
|
|
572
|
+
context 'when reloading document from db' do
|
|
608
573
|
let(:from_db) { Preference.last }
|
|
609
574
|
|
|
610
|
-
it
|
|
575
|
+
it 'does not create duplicate keys' do
|
|
611
576
|
person.preferences = [ from_db ]
|
|
612
577
|
expect(from_db.person_ids).to eq([ person.id ])
|
|
613
578
|
end
|
|
@@ -615,10 +580,8 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
615
580
|
end
|
|
616
581
|
end
|
|
617
582
|
|
|
618
|
-
context
|
|
619
|
-
|
|
620
|
-
context "when the parent and relation are new records" do
|
|
621
|
-
|
|
583
|
+
context 'when the relation is not polymorphic' do
|
|
584
|
+
context 'when the parent and relation are new records' do
|
|
622
585
|
let(:person) do
|
|
623
586
|
Person.new
|
|
624
587
|
end
|
|
@@ -631,25 +594,24 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
631
594
|
person.preferences = [ preference ]
|
|
632
595
|
end
|
|
633
596
|
|
|
634
|
-
it
|
|
597
|
+
it 'sets the relation' do
|
|
635
598
|
expect(person.preferences).to eq([ preference ])
|
|
636
599
|
end
|
|
637
600
|
|
|
638
|
-
it
|
|
601
|
+
it 'sets the foreign key on the relation' do
|
|
639
602
|
expect(person.preference_ids).to eq([ preference.id ])
|
|
640
603
|
end
|
|
641
604
|
|
|
642
|
-
it
|
|
605
|
+
it 'sets the foreign key on the inverse relation' do
|
|
643
606
|
expect(preference.person_ids).to eq([ person.id ])
|
|
644
607
|
end
|
|
645
608
|
|
|
646
|
-
it
|
|
609
|
+
it 'does not save the target' do
|
|
647
610
|
expect(preference).to be_new_record
|
|
648
611
|
end
|
|
649
612
|
end
|
|
650
613
|
|
|
651
|
-
context
|
|
652
|
-
|
|
614
|
+
context 'when the parent is new but the relation exists' do
|
|
653
615
|
let(:person) do
|
|
654
616
|
Person.new
|
|
655
617
|
end
|
|
@@ -662,45 +624,43 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
662
624
|
person.preferences = [ preference ]
|
|
663
625
|
end
|
|
664
626
|
|
|
665
|
-
it
|
|
627
|
+
it 'sets the relation' do
|
|
666
628
|
expect(person.preferences).to eq([ preference ])
|
|
667
629
|
end
|
|
668
630
|
|
|
669
|
-
it
|
|
631
|
+
it 'sets the foreign key on the relation' do
|
|
670
632
|
expect(person.preference_ids).to eq([ preference.id ])
|
|
671
633
|
end
|
|
672
634
|
|
|
673
|
-
it
|
|
635
|
+
it 'sets the foreign key on the inverse relation' do
|
|
674
636
|
expect(preference.person_ids).to eq([ person.id ])
|
|
675
637
|
end
|
|
676
638
|
|
|
677
|
-
context
|
|
678
|
-
|
|
639
|
+
context 'and the parent is persisted' do
|
|
679
640
|
before do
|
|
680
641
|
person.save!
|
|
681
642
|
preference.reload
|
|
682
643
|
end
|
|
683
644
|
|
|
684
|
-
it
|
|
645
|
+
it 'maintains the relation' do
|
|
685
646
|
expect(person.preferences).to eq([ preference ])
|
|
686
647
|
end
|
|
687
648
|
|
|
688
|
-
it
|
|
649
|
+
it 'maintains the foreign key on the relation' do
|
|
689
650
|
expect(person.preference_ids).to eq([ preference.id ])
|
|
690
651
|
end
|
|
691
652
|
|
|
692
|
-
it
|
|
653
|
+
it 'maintains the foreign key on the inverse relation' do
|
|
693
654
|
expect(preference.person_ids).to eq([ person.id ])
|
|
694
655
|
end
|
|
695
656
|
|
|
696
|
-
it
|
|
657
|
+
it 'maintains the base on the inverse relation' do
|
|
697
658
|
expect(preference.people.first).to eq(person)
|
|
698
659
|
end
|
|
699
660
|
end
|
|
700
661
|
end
|
|
701
662
|
|
|
702
|
-
context
|
|
703
|
-
|
|
663
|
+
context 'when the parent is not a new record' do
|
|
704
664
|
let(:person) do
|
|
705
665
|
Person.create!
|
|
706
666
|
end
|
|
@@ -713,32 +673,31 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
713
673
|
person.preferences = [ preference ]
|
|
714
674
|
end
|
|
715
675
|
|
|
716
|
-
it
|
|
676
|
+
it 'sets the relation' do
|
|
717
677
|
expect(person.preferences).to eq([ preference ])
|
|
718
678
|
end
|
|
719
679
|
|
|
720
|
-
it
|
|
680
|
+
it 'sets the foreign key on the relation' do
|
|
721
681
|
expect(person.preference_ids).to eq([ preference.id ])
|
|
722
682
|
end
|
|
723
683
|
|
|
724
|
-
it
|
|
684
|
+
it 'sets the foreign key on the inverse relation' do
|
|
725
685
|
expect(preference.person_ids).to eq([ person.id ])
|
|
726
686
|
end
|
|
727
687
|
|
|
728
|
-
it
|
|
688
|
+
it 'sets the base on the inverse relation' do
|
|
729
689
|
expect(preference.people.first).to eq(person)
|
|
730
690
|
end
|
|
731
691
|
|
|
732
|
-
it
|
|
692
|
+
it 'saves the target' do
|
|
733
693
|
expect(preference).to be_persisted
|
|
734
694
|
end
|
|
735
695
|
|
|
736
|
-
it
|
|
696
|
+
it 'persists the relation' do
|
|
737
697
|
person.reload.preferences == [ preference ]
|
|
738
698
|
end
|
|
739
699
|
|
|
740
|
-
context
|
|
741
|
-
|
|
700
|
+
context 'when overwriting an existing relation' do
|
|
742
701
|
let(:another_preference) do
|
|
743
702
|
Preference.new
|
|
744
703
|
end
|
|
@@ -747,71 +706,67 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
747
706
|
person.preferences = [ another_preference ]
|
|
748
707
|
end
|
|
749
708
|
|
|
750
|
-
it
|
|
709
|
+
it 'sets the relation' do
|
|
751
710
|
expect(person.preferences).to eq([ another_preference ])
|
|
752
711
|
end
|
|
753
712
|
|
|
754
|
-
it
|
|
713
|
+
it 'saves the target' do
|
|
755
714
|
expect(another_preference).to be_persisted
|
|
756
715
|
end
|
|
757
716
|
|
|
758
|
-
it
|
|
717
|
+
it 'does not leave foreign keys of the previous relation' do
|
|
759
718
|
expect(person.preference_ids).to eq([ another_preference.id ])
|
|
760
719
|
end
|
|
761
720
|
|
|
762
|
-
it
|
|
721
|
+
it 'clears its own key on the foreign relation' do
|
|
763
722
|
expect(preference.person_ids).to be_empty
|
|
764
723
|
end
|
|
765
724
|
|
|
766
|
-
context
|
|
767
|
-
|
|
725
|
+
context 'and then overwriting it again with the same value' do
|
|
768
726
|
before do
|
|
769
727
|
person.preferences = [ another_preference ]
|
|
770
728
|
end
|
|
771
729
|
|
|
772
|
-
it
|
|
730
|
+
it 'persists the relation between another_preference and person' do
|
|
773
731
|
expect(another_preference.reload.people).to eq([ person ])
|
|
774
732
|
end
|
|
775
|
-
|
|
776
733
|
end
|
|
777
734
|
|
|
778
|
-
context
|
|
779
|
-
|
|
735
|
+
context 'and person reloaded instead of saved' do
|
|
780
736
|
before do
|
|
781
737
|
person.reload
|
|
782
738
|
another_preference.reload
|
|
783
739
|
end
|
|
784
740
|
|
|
785
|
-
it
|
|
741
|
+
it 'persists the relation between person and another_preference' do
|
|
786
742
|
expect(person.preferences).to eq([ another_preference ])
|
|
787
743
|
end
|
|
788
744
|
|
|
789
|
-
it
|
|
745
|
+
it 'persists the relation between another_preference and person' do
|
|
790
746
|
expect(another_preference.people).to eq([ person ])
|
|
791
747
|
end
|
|
792
748
|
|
|
793
|
-
it
|
|
749
|
+
it 'no longer has any relation between preference and person' do
|
|
794
750
|
expect(preference.people).to be_empty
|
|
795
751
|
end
|
|
796
752
|
end
|
|
797
753
|
|
|
798
|
-
context
|
|
799
|
-
|
|
754
|
+
context 'and person is saved' do
|
|
800
755
|
before do
|
|
801
756
|
person.save!
|
|
802
757
|
person.reload
|
|
803
758
|
another_preference.reload
|
|
804
759
|
end
|
|
805
760
|
|
|
806
|
-
it
|
|
761
|
+
it 'persists the relation between person and another_preference' do
|
|
807
762
|
expect(person.preferences).to eq([ another_preference ])
|
|
808
763
|
end
|
|
809
764
|
|
|
810
|
-
it
|
|
765
|
+
it 'persists the relation between another_preference and person' do
|
|
811
766
|
expect(another_preference.people).to eq([ person ])
|
|
812
767
|
end
|
|
813
768
|
|
|
814
|
-
it
|
|
769
|
+
it 'no longer has any relation between preference and person' do
|
|
815
770
|
expect(preference.people).to be_empty
|
|
816
771
|
end
|
|
817
772
|
end
|
|
@@ -820,13 +775,13 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
820
775
|
end
|
|
821
776
|
end
|
|
822
777
|
|
|
823
|
-
describe
|
|
778
|
+
describe 'when self-referencing' do
|
|
824
779
|
let!(:parent) do
|
|
825
780
|
Vertex.create!
|
|
826
781
|
end
|
|
827
782
|
|
|
828
783
|
let!(:child) do
|
|
829
|
-
Vertex.create!(parents: [parent])
|
|
784
|
+
Vertex.create!(parents: [ parent ])
|
|
830
785
|
end
|
|
831
786
|
|
|
832
787
|
before do
|
|
@@ -834,62 +789,62 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
834
789
|
child.reload
|
|
835
790
|
end
|
|
836
791
|
|
|
837
|
-
it
|
|
792
|
+
it 'sets the parent' do
|
|
838
793
|
expect(child.parents).to include(parent)
|
|
839
794
|
end
|
|
840
795
|
|
|
841
|
-
it
|
|
796
|
+
it 'sets the parent id' do
|
|
842
797
|
expect(child.parent_ids).to include(parent.id)
|
|
843
798
|
end
|
|
844
799
|
|
|
845
|
-
it
|
|
800
|
+
it 'sets the child' do
|
|
846
801
|
expect(parent.children).to include(child)
|
|
847
802
|
end
|
|
848
803
|
|
|
849
|
-
it
|
|
804
|
+
it 'sets the child id' do
|
|
850
805
|
expect(parent.child_ids).to include(child.id)
|
|
851
806
|
end
|
|
852
807
|
|
|
853
|
-
describe
|
|
808
|
+
describe '#update' do
|
|
854
809
|
before do
|
|
855
|
-
child.update(parents: [parent])
|
|
810
|
+
child.update(parents: [ parent ])
|
|
856
811
|
end
|
|
857
812
|
|
|
858
|
-
it
|
|
813
|
+
it 'sets the parent' do
|
|
859
814
|
expect(child.parents).to include(parent)
|
|
860
815
|
end
|
|
861
816
|
|
|
862
|
-
it
|
|
817
|
+
it 'sets the parent id' do
|
|
863
818
|
expect(child.parent_ids).to include(parent.id)
|
|
864
819
|
end
|
|
865
820
|
|
|
866
|
-
it
|
|
821
|
+
it 'sets the child' do
|
|
867
822
|
expect(parent.children).to include(child)
|
|
868
823
|
end
|
|
869
824
|
|
|
870
|
-
it
|
|
825
|
+
it 'sets the child id' do
|
|
871
826
|
expect(parent.child_ids).to include(child.id)
|
|
872
827
|
end
|
|
873
828
|
|
|
874
|
-
describe
|
|
829
|
+
describe 'when reloading' do
|
|
875
830
|
before do
|
|
876
831
|
parent.reload
|
|
877
832
|
child.reload
|
|
878
833
|
end
|
|
879
834
|
|
|
880
|
-
it
|
|
835
|
+
it 'sets the parent' do
|
|
881
836
|
expect(child.parents).to include(parent)
|
|
882
837
|
end
|
|
883
838
|
|
|
884
|
-
it
|
|
839
|
+
it 'sets the parent id' do
|
|
885
840
|
expect(child.parent_ids).to include(parent.id)
|
|
886
841
|
end
|
|
887
842
|
|
|
888
|
-
it
|
|
843
|
+
it 'sets the child' do
|
|
889
844
|
expect(parent.children).to include(child)
|
|
890
845
|
end
|
|
891
846
|
|
|
892
|
-
it
|
|
847
|
+
it 'sets the child id' do
|
|
893
848
|
expect(parent.child_ids).to include(child.id)
|
|
894
849
|
end
|
|
895
850
|
end
|
|
@@ -897,13 +852,9 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
897
852
|
end
|
|
898
853
|
|
|
899
854
|
[ nil, [] ].each do |value|
|
|
900
|
-
|
|
901
855
|
describe "#= #{value}" do
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
context "when the inverse relation is not defined" do
|
|
906
|
-
|
|
856
|
+
context 'when the relation is not polymorphic' do
|
|
857
|
+
context 'when the inverse relation is not defined' do
|
|
907
858
|
let(:person) do
|
|
908
859
|
Person.new
|
|
909
860
|
end
|
|
@@ -917,17 +868,16 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
917
868
|
person.houses = value
|
|
918
869
|
end
|
|
919
870
|
|
|
920
|
-
it
|
|
871
|
+
it 'clears the relation' do
|
|
921
872
|
expect(person.houses).to be_empty
|
|
922
873
|
end
|
|
923
874
|
|
|
924
|
-
it
|
|
875
|
+
it 'clears the foreign keys' do
|
|
925
876
|
expect(person.house_ids).to be_empty
|
|
926
877
|
end
|
|
927
878
|
end
|
|
928
879
|
|
|
929
|
-
context
|
|
930
|
-
|
|
880
|
+
context 'when the parent is a new record' do
|
|
931
881
|
let(:person) do
|
|
932
882
|
Person.new
|
|
933
883
|
end
|
|
@@ -941,27 +891,25 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
941
891
|
person.preferences = value
|
|
942
892
|
end
|
|
943
893
|
|
|
944
|
-
it
|
|
894
|
+
it 'sets the relation to an empty array' do
|
|
945
895
|
expect(person.preferences).to be_empty
|
|
946
896
|
end
|
|
947
897
|
|
|
948
|
-
it
|
|
898
|
+
it 'removed the inverse relation' do
|
|
949
899
|
expect(preference.people).to be_empty
|
|
950
900
|
end
|
|
951
901
|
|
|
952
|
-
it
|
|
902
|
+
it 'removes the foreign key values' do
|
|
953
903
|
expect(person.preference_ids).to be_empty
|
|
954
904
|
end
|
|
955
905
|
|
|
956
|
-
it
|
|
906
|
+
it 'removes the inverse foreign key values' do
|
|
957
907
|
expect(preference.person_ids).to be_empty
|
|
958
908
|
end
|
|
959
909
|
end
|
|
960
910
|
|
|
961
|
-
context
|
|
962
|
-
|
|
963
|
-
context "when the relation has been loaded" do
|
|
964
|
-
|
|
911
|
+
context 'when the parent is not a new record' do
|
|
912
|
+
context 'when the relation has been loaded' do
|
|
965
913
|
let(:person) do
|
|
966
914
|
Person.create!
|
|
967
915
|
end
|
|
@@ -975,29 +923,28 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
975
923
|
person.preferences = value
|
|
976
924
|
end
|
|
977
925
|
|
|
978
|
-
it
|
|
926
|
+
it 'sets the relation to an empty array' do
|
|
979
927
|
expect(person.preferences).to be_empty
|
|
980
928
|
end
|
|
981
929
|
|
|
982
|
-
it
|
|
930
|
+
it 'removed the inverse relation' do
|
|
983
931
|
expect(preference.people).to be_empty
|
|
984
932
|
end
|
|
985
933
|
|
|
986
|
-
it
|
|
934
|
+
it 'removes the foreign key values' do
|
|
987
935
|
expect(person.preference_ids).to be_empty
|
|
988
936
|
end
|
|
989
937
|
|
|
990
|
-
it
|
|
938
|
+
it 'removes the inverse foreign key values' do
|
|
991
939
|
expect(preference.person_ids).to be_empty
|
|
992
940
|
end
|
|
993
941
|
|
|
994
|
-
it
|
|
995
|
-
expect(preference).
|
|
942
|
+
it 'does not delete the target from the database' do
|
|
943
|
+
expect(preference).not_to be_destroyed
|
|
996
944
|
end
|
|
997
945
|
end
|
|
998
946
|
|
|
999
|
-
context
|
|
1000
|
-
|
|
947
|
+
context 'when the relation has not been loaded' do
|
|
1001
948
|
let(:preference) do
|
|
1002
949
|
Preference.new
|
|
1003
950
|
end
|
|
@@ -1016,11 +963,11 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
1016
963
|
from_db.preferences = value
|
|
1017
964
|
end
|
|
1018
965
|
|
|
1019
|
-
it
|
|
966
|
+
it 'sets the relation to an empty array' do
|
|
1020
967
|
expect(from_db.preferences).to be_empty
|
|
1021
968
|
end
|
|
1022
969
|
|
|
1023
|
-
it
|
|
970
|
+
it 'removes the foreign key values' do
|
|
1024
971
|
expect(from_db.preference_ids).to be_empty
|
|
1025
972
|
end
|
|
1026
973
|
end
|
|
@@ -1029,92 +976,86 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
1029
976
|
end
|
|
1030
977
|
end
|
|
1031
978
|
|
|
1032
|
-
[
|
|
1033
|
-
|
|
979
|
+
%i[build new].each do |method|
|
|
1034
980
|
describe "##{method}" do
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
context "when the parent is a new record" do
|
|
1039
|
-
|
|
981
|
+
context 'when the relation is not polymorphic' do
|
|
982
|
+
context 'when the parent is a new record' do
|
|
1040
983
|
let(:person) do
|
|
1041
984
|
Person.new
|
|
1042
985
|
end
|
|
1043
986
|
|
|
1044
987
|
let!(:preference) do
|
|
1045
|
-
person.preferences.send(method, name:
|
|
988
|
+
person.preferences.send(method, name: 'settings')
|
|
1046
989
|
end
|
|
1047
990
|
|
|
1048
|
-
it
|
|
991
|
+
it 'adds the document to the relation' do
|
|
1049
992
|
expect(person.preferences).to eq([ preference ])
|
|
1050
993
|
end
|
|
1051
994
|
|
|
1052
|
-
it
|
|
995
|
+
it 'sets the foreign key on the relation' do
|
|
1053
996
|
expect(person.preference_ids).to eq([ preference.id ])
|
|
1054
997
|
end
|
|
1055
998
|
|
|
1056
|
-
it
|
|
999
|
+
it 'sets the inverse foreign key on the relation' do
|
|
1057
1000
|
expect(preference.person_ids).to eq([ person.id ])
|
|
1058
1001
|
end
|
|
1059
1002
|
|
|
1060
|
-
it
|
|
1061
|
-
expect(preference.name).to eq(
|
|
1003
|
+
it 'sets the attributes' do
|
|
1004
|
+
expect(preference.name).to eq('settings')
|
|
1062
1005
|
end
|
|
1063
1006
|
|
|
1064
|
-
it
|
|
1007
|
+
it 'does not save the target' do
|
|
1065
1008
|
expect(preference).to be_new_record
|
|
1066
1009
|
end
|
|
1067
1010
|
|
|
1068
|
-
it
|
|
1011
|
+
it 'adds the correct number of documents' do
|
|
1069
1012
|
expect(person.preferences.size).to eq(1)
|
|
1070
1013
|
end
|
|
1071
1014
|
end
|
|
1072
1015
|
|
|
1073
|
-
context
|
|
1074
|
-
|
|
1016
|
+
context 'when the parent is not a new record' do
|
|
1075
1017
|
let(:person) do
|
|
1076
1018
|
Person.create!
|
|
1077
1019
|
end
|
|
1078
1020
|
|
|
1079
1021
|
let!(:preference) do
|
|
1080
|
-
person.preferences.send(method, name:
|
|
1022
|
+
person.preferences.send(method, name: 'settings')
|
|
1081
1023
|
end
|
|
1082
1024
|
|
|
1083
|
-
it
|
|
1025
|
+
it 'adds the document to the relation' do
|
|
1084
1026
|
expect(person.preferences).to eq([ preference ])
|
|
1085
1027
|
end
|
|
1086
1028
|
|
|
1087
|
-
it
|
|
1029
|
+
it 'sets the foreign key on the relation' do
|
|
1088
1030
|
expect(person.preference_ids).to eq([ preference.id ])
|
|
1089
1031
|
end
|
|
1090
1032
|
|
|
1091
|
-
it
|
|
1033
|
+
it 'sets the inverse foreign key on the relation' do
|
|
1092
1034
|
expect(preference.person_ids).to eq([ person.id ])
|
|
1093
1035
|
end
|
|
1094
1036
|
|
|
1095
|
-
it
|
|
1037
|
+
it 'sets the base on the inverse relation' do
|
|
1096
1038
|
expect(preference.people).to eq([ person ])
|
|
1097
1039
|
end
|
|
1098
1040
|
|
|
1099
|
-
it
|
|
1100
|
-
expect(preference.name).to eq(
|
|
1041
|
+
it 'sets the attributes' do
|
|
1042
|
+
expect(preference.name).to eq('settings')
|
|
1101
1043
|
end
|
|
1102
1044
|
|
|
1103
|
-
it
|
|
1045
|
+
it 'does not save the target' do
|
|
1104
1046
|
expect(preference).to be_new_record
|
|
1105
1047
|
end
|
|
1106
1048
|
|
|
1107
|
-
it
|
|
1049
|
+
it 'adds the correct number of documents' do
|
|
1108
1050
|
expect(person.preferences.size).to eq(1)
|
|
1109
1051
|
end
|
|
1110
1052
|
|
|
1111
|
-
context
|
|
1112
|
-
|
|
1053
|
+
context 'when saving the target' do
|
|
1113
1054
|
before do
|
|
1114
1055
|
preference.save!
|
|
1115
1056
|
end
|
|
1116
1057
|
|
|
1117
|
-
it
|
|
1058
|
+
it 'persists the parent keys' do
|
|
1118
1059
|
expect(person.reload.preference_ids).to eq([ preference.id ])
|
|
1119
1060
|
end
|
|
1120
1061
|
end
|
|
@@ -1123,93 +1064,86 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
1123
1064
|
end
|
|
1124
1065
|
end
|
|
1125
1066
|
|
|
1126
|
-
describe
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
context "when the parent has been persisted" do
|
|
1131
|
-
|
|
1067
|
+
describe '#clear' do
|
|
1068
|
+
context 'when the relation is not polymorphic' do
|
|
1069
|
+
context 'when the parent has been persisted' do
|
|
1132
1070
|
let!(:person) do
|
|
1133
1071
|
Person.create!
|
|
1134
1072
|
end
|
|
1135
1073
|
|
|
1136
|
-
context
|
|
1137
|
-
|
|
1074
|
+
context 'when the children are persisted' do
|
|
1138
1075
|
let!(:preference) do
|
|
1139
|
-
person.preferences.create!(name:
|
|
1076
|
+
person.preferences.create!(name: 'settings')
|
|
1140
1077
|
end
|
|
1141
1078
|
|
|
1142
1079
|
let!(:relation) do
|
|
1143
1080
|
person.preferences.clear
|
|
1144
1081
|
end
|
|
1145
1082
|
|
|
1146
|
-
it
|
|
1083
|
+
it 'clears out the relation' do
|
|
1147
1084
|
expect(person.preferences).to be_empty
|
|
1148
1085
|
end
|
|
1149
1086
|
|
|
1150
|
-
it
|
|
1151
|
-
expect(preference.people).
|
|
1087
|
+
it 'removes the parent from the inverse relation' do
|
|
1088
|
+
expect(preference.people).not_to include(person)
|
|
1152
1089
|
end
|
|
1153
1090
|
|
|
1154
|
-
it
|
|
1091
|
+
it 'removes the foreign keys' do
|
|
1155
1092
|
expect(person.preference_ids).to be_empty
|
|
1156
1093
|
end
|
|
1157
1094
|
|
|
1158
|
-
it
|
|
1159
|
-
expect(preference.person_ids).
|
|
1095
|
+
it 'removes the parent key from the inverse' do
|
|
1096
|
+
expect(preference.person_ids).not_to include(person.id)
|
|
1160
1097
|
end
|
|
1161
1098
|
|
|
1162
|
-
it
|
|
1163
|
-
expect(preference).
|
|
1099
|
+
it 'does not delete the documents' do
|
|
1100
|
+
expect(preference).not_to be_destroyed
|
|
1164
1101
|
end
|
|
1165
1102
|
|
|
1166
|
-
it
|
|
1103
|
+
it 'persists the nullification' do
|
|
1167
1104
|
expect(person.reload.preferences).to be_empty
|
|
1168
1105
|
end
|
|
1169
1106
|
|
|
1170
|
-
it
|
|
1107
|
+
it 'returns the relation' do
|
|
1171
1108
|
expect(relation).to be_empty
|
|
1172
1109
|
end
|
|
1173
1110
|
end
|
|
1174
1111
|
|
|
1175
|
-
context
|
|
1176
|
-
|
|
1112
|
+
context 'when the children are not persisted' do
|
|
1177
1113
|
let!(:preference) do
|
|
1178
|
-
person.preferences.build(name:
|
|
1114
|
+
person.preferences.build(name: 'setting')
|
|
1179
1115
|
end
|
|
1180
1116
|
|
|
1181
1117
|
let!(:relation) do
|
|
1182
1118
|
person.preferences.clear
|
|
1183
1119
|
end
|
|
1184
1120
|
|
|
1185
|
-
it
|
|
1121
|
+
it 'clears out the relation' do
|
|
1186
1122
|
expect(person.preferences).to be_empty
|
|
1187
1123
|
end
|
|
1188
1124
|
end
|
|
1189
1125
|
end
|
|
1190
1126
|
|
|
1191
|
-
context
|
|
1192
|
-
|
|
1127
|
+
context 'when the parent is not persisted' do
|
|
1193
1128
|
let(:person) do
|
|
1194
1129
|
Person.new
|
|
1195
1130
|
end
|
|
1196
1131
|
|
|
1197
1132
|
let!(:preference) do
|
|
1198
|
-
person.preferences.build(name:
|
|
1133
|
+
person.preferences.build(name: 'setting')
|
|
1199
1134
|
end
|
|
1200
1135
|
|
|
1201
1136
|
let!(:relation) do
|
|
1202
1137
|
person.preferences.clear
|
|
1203
1138
|
end
|
|
1204
1139
|
|
|
1205
|
-
it
|
|
1140
|
+
it 'clears out the relation' do
|
|
1206
1141
|
expect(person.preferences).to be_empty
|
|
1207
1142
|
end
|
|
1208
1143
|
end
|
|
1209
1144
|
end
|
|
1210
1145
|
|
|
1211
|
-
context
|
|
1212
|
-
|
|
1146
|
+
context 'when the association has callbacks' do
|
|
1213
1147
|
let(:post) do
|
|
1214
1148
|
Post.new
|
|
1215
1149
|
end
|
|
@@ -1222,61 +1156,55 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
1222
1156
|
post.tags << tag
|
|
1223
1157
|
end
|
|
1224
1158
|
|
|
1225
|
-
context
|
|
1226
|
-
|
|
1227
|
-
context "when no errors are raised" do
|
|
1228
|
-
|
|
1159
|
+
context 'when the callback is a before_remove' do
|
|
1160
|
+
context 'when no errors are raised' do
|
|
1229
1161
|
before do
|
|
1230
1162
|
post.tags.clear
|
|
1231
1163
|
end
|
|
1232
1164
|
|
|
1233
|
-
it
|
|
1165
|
+
it 'executes the callback' do
|
|
1234
1166
|
expect(post.before_remove_called).to be true
|
|
1235
1167
|
end
|
|
1236
1168
|
|
|
1237
|
-
it
|
|
1169
|
+
it 'removes the document from the relation' do
|
|
1238
1170
|
expect(post.tags).to be_empty
|
|
1239
1171
|
end
|
|
1240
1172
|
end
|
|
1241
1173
|
|
|
1242
|
-
context
|
|
1243
|
-
|
|
1174
|
+
context 'when errors are raised' do
|
|
1244
1175
|
before do
|
|
1245
1176
|
expect(post).to receive(:before_remove_tag).and_raise
|
|
1246
|
-
begin; post.tags.clear; rescue; end
|
|
1177
|
+
begin; post.tags.clear; rescue StandardError; end
|
|
1247
1178
|
end
|
|
1248
1179
|
|
|
1249
|
-
it
|
|
1180
|
+
it 'does not remove the document from the relation' do
|
|
1250
1181
|
expect(post.tags).to eq([ tag ])
|
|
1251
1182
|
end
|
|
1252
1183
|
end
|
|
1253
1184
|
end
|
|
1254
1185
|
|
|
1255
|
-
context
|
|
1256
|
-
|
|
1257
|
-
context "when no errors are raised" do
|
|
1258
|
-
|
|
1186
|
+
context 'when the callback is an after_remove' do
|
|
1187
|
+
context 'when no errors are raised' do
|
|
1259
1188
|
before do
|
|
1260
1189
|
post.tags.clear
|
|
1261
1190
|
end
|
|
1262
1191
|
|
|
1263
|
-
it
|
|
1192
|
+
it 'executes the callback' do
|
|
1264
1193
|
expect(post.after_remove_called).to be true
|
|
1265
1194
|
end
|
|
1266
1195
|
|
|
1267
|
-
it
|
|
1196
|
+
it 'removes the document from the relation' do
|
|
1268
1197
|
expect(post.tags).to be_empty
|
|
1269
1198
|
end
|
|
1270
1199
|
end
|
|
1271
1200
|
|
|
1272
|
-
context
|
|
1273
|
-
|
|
1201
|
+
context 'when errors are raised' do
|
|
1274
1202
|
before do
|
|
1275
1203
|
expect(post).to receive(:after_remove_tag).and_raise
|
|
1276
|
-
begin; post.tags.clear; rescue; end
|
|
1204
|
+
begin; post.tags.clear; rescue StandardError; end
|
|
1277
1205
|
end
|
|
1278
1206
|
|
|
1279
|
-
it
|
|
1207
|
+
it 'removes the document from the relation' do
|
|
1280
1208
|
expect(post.tags).to be_empty
|
|
1281
1209
|
end
|
|
1282
1210
|
end
|
|
@@ -1284,10 +1212,8 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
1284
1212
|
end
|
|
1285
1213
|
end
|
|
1286
1214
|
|
|
1287
|
-
describe
|
|
1288
|
-
|
|
1289
|
-
context "when the parent is a new record" do
|
|
1290
|
-
|
|
1215
|
+
describe '#concat' do
|
|
1216
|
+
context 'when the parent is a new record' do
|
|
1291
1217
|
let(:person) do
|
|
1292
1218
|
Person.new
|
|
1293
1219
|
end
|
|
@@ -1300,13 +1226,12 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
1300
1226
|
person.preferences.concat([ preference ])
|
|
1301
1227
|
end
|
|
1302
1228
|
|
|
1303
|
-
it
|
|
1229
|
+
it 'returns an array of loaded documents' do
|
|
1304
1230
|
expect(result).to eq([ preference ])
|
|
1305
1231
|
end
|
|
1306
1232
|
end
|
|
1307
1233
|
|
|
1308
|
-
context
|
|
1309
|
-
|
|
1234
|
+
context 'when the parent is not a new record' do
|
|
1310
1235
|
let(:person) do
|
|
1311
1236
|
Person.create!
|
|
1312
1237
|
end
|
|
@@ -1319,15 +1244,13 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
1319
1244
|
person.preferences.concat([ preference ])
|
|
1320
1245
|
end
|
|
1321
1246
|
|
|
1322
|
-
it
|
|
1247
|
+
it 'returns an array of loaded documents' do
|
|
1323
1248
|
expect(result).to eq([ preference ])
|
|
1324
1249
|
end
|
|
1325
1250
|
end
|
|
1326
1251
|
|
|
1327
|
-
context
|
|
1328
|
-
|
|
1329
|
-
context "when the inverse relation is not defined" do
|
|
1330
|
-
|
|
1252
|
+
context 'when the relations are not polymorphic' do
|
|
1253
|
+
context 'when the inverse relation is not defined' do
|
|
1331
1254
|
let(:person) do
|
|
1332
1255
|
Person.new
|
|
1333
1256
|
end
|
|
@@ -1340,19 +1263,18 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
1340
1263
|
person.houses.concat([ house ])
|
|
1341
1264
|
end
|
|
1342
1265
|
|
|
1343
|
-
it
|
|
1266
|
+
it 'appends the document to the relation' do
|
|
1344
1267
|
expect(person.houses).to eq([ house ])
|
|
1345
1268
|
end
|
|
1346
1269
|
|
|
1347
|
-
it
|
|
1270
|
+
it 'sets the foreign key on the relation' do
|
|
1348
1271
|
expect(person.house_ids).to eq([ house.id ])
|
|
1349
1272
|
end
|
|
1350
1273
|
end
|
|
1351
1274
|
|
|
1352
|
-
context
|
|
1353
|
-
|
|
1275
|
+
context 'when appending in a parent create block' do
|
|
1354
1276
|
let!(:preference) do
|
|
1355
|
-
Preference.create!(name:
|
|
1277
|
+
Preference.create!(name: 'testing')
|
|
1356
1278
|
end
|
|
1357
1279
|
|
|
1358
1280
|
let!(:person) do
|
|
@@ -1361,39 +1283,37 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
1361
1283
|
end
|
|
1362
1284
|
end
|
|
1363
1285
|
|
|
1364
|
-
it
|
|
1286
|
+
it 'adds the documents to the relation' do
|
|
1365
1287
|
expect(person.preferences).to eq([ preference ])
|
|
1366
1288
|
end
|
|
1367
1289
|
|
|
1368
|
-
it
|
|
1290
|
+
it 'sets the foreign key on the relation' do
|
|
1369
1291
|
expect(person.preference_ids).to eq([ preference.id ])
|
|
1370
1292
|
end
|
|
1371
1293
|
|
|
1372
|
-
it
|
|
1294
|
+
it 'sets the foreign key on the inverse relation' do
|
|
1373
1295
|
expect(preference.person_ids).to eq([ person.id ])
|
|
1374
1296
|
end
|
|
1375
1297
|
|
|
1376
|
-
it
|
|
1298
|
+
it 'saves the target' do
|
|
1377
1299
|
expect(preference).to be_persisted
|
|
1378
1300
|
end
|
|
1379
1301
|
|
|
1380
|
-
it
|
|
1302
|
+
it 'adds the correct number of documents' do
|
|
1381
1303
|
expect(person.preferences.size).to eq(1)
|
|
1382
1304
|
end
|
|
1383
1305
|
|
|
1384
|
-
it
|
|
1306
|
+
it 'persists the link' do
|
|
1385
1307
|
expect(person.reload.preferences).to eq([ preference ])
|
|
1386
1308
|
end
|
|
1387
1309
|
end
|
|
1388
1310
|
|
|
1389
|
-
context
|
|
1390
|
-
|
|
1311
|
+
context 'when the parent is a new record' do
|
|
1391
1312
|
let(:person) do
|
|
1392
1313
|
Person.new
|
|
1393
1314
|
end
|
|
1394
1315
|
|
|
1395
|
-
context
|
|
1396
|
-
|
|
1316
|
+
context 'when the child is new' do
|
|
1397
1317
|
let(:preference) do
|
|
1398
1318
|
Preference.new
|
|
1399
1319
|
end
|
|
@@ -1402,46 +1322,44 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
1402
1322
|
person.preferences.concat([ preference ])
|
|
1403
1323
|
end
|
|
1404
1324
|
|
|
1405
|
-
it
|
|
1325
|
+
it 'adds the documents to the relation' do
|
|
1406
1326
|
expect(person.preferences).to eq([ preference ])
|
|
1407
1327
|
end
|
|
1408
1328
|
|
|
1409
|
-
it
|
|
1329
|
+
it 'sets the foreign key on the relation' do
|
|
1410
1330
|
expect(person.preference_ids).to eq([ preference.id ])
|
|
1411
1331
|
end
|
|
1412
1332
|
|
|
1413
|
-
it
|
|
1333
|
+
it 'sets the foreign key on the inverse relation' do
|
|
1414
1334
|
expect(preference.person_ids).to eq([ person.id ])
|
|
1415
1335
|
end
|
|
1416
1336
|
|
|
1417
|
-
it
|
|
1337
|
+
it 'does not save the target' do
|
|
1418
1338
|
expect(preference).to be_new_record
|
|
1419
1339
|
end
|
|
1420
1340
|
|
|
1421
|
-
it
|
|
1341
|
+
it 'adds the correct number of documents' do
|
|
1422
1342
|
expect(person.preferences.size).to eq(1)
|
|
1423
1343
|
end
|
|
1424
1344
|
|
|
1425
|
-
context
|
|
1426
|
-
|
|
1345
|
+
context 'when appending a second time' do
|
|
1427
1346
|
before do
|
|
1428
1347
|
person.preferences.concat([ preference ])
|
|
1429
1348
|
end
|
|
1430
1349
|
|
|
1431
|
-
it
|
|
1350
|
+
it 'does not allow the document to be added again' do
|
|
1432
1351
|
expect(person.preferences).to eq([ preference ])
|
|
1433
1352
|
end
|
|
1434
1353
|
|
|
1435
|
-
it
|
|
1354
|
+
it 'does not allow duplicate ids' do
|
|
1436
1355
|
expect(person.preference_ids).to eq([ preference.id ])
|
|
1437
1356
|
end
|
|
1438
1357
|
end
|
|
1439
1358
|
end
|
|
1440
1359
|
|
|
1441
|
-
context
|
|
1442
|
-
|
|
1360
|
+
context 'when the child is already persisted' do
|
|
1443
1361
|
let!(:persisted) do
|
|
1444
|
-
Preference.create!(name:
|
|
1362
|
+
Preference.create!(name: 'testy')
|
|
1445
1363
|
end
|
|
1446
1364
|
|
|
1447
1365
|
let(:preference) do
|
|
@@ -1453,35 +1371,34 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
1453
1371
|
person.save!
|
|
1454
1372
|
end
|
|
1455
1373
|
|
|
1456
|
-
it
|
|
1374
|
+
it 'adds the documents to the relation' do
|
|
1457
1375
|
expect(person.preferences).to eq([ preference ])
|
|
1458
1376
|
end
|
|
1459
1377
|
|
|
1460
|
-
it
|
|
1378
|
+
it 'sets the foreign key on the relation' do
|
|
1461
1379
|
expect(person.preference_ids).to eq([ preference.id ])
|
|
1462
1380
|
end
|
|
1463
1381
|
|
|
1464
|
-
it
|
|
1382
|
+
it 'sets the foreign key on the inverse relation' do
|
|
1465
1383
|
expect(preference.person_ids).to eq([ person.id ])
|
|
1466
1384
|
end
|
|
1467
1385
|
|
|
1468
|
-
it
|
|
1386
|
+
it 'saves the target' do
|
|
1469
1387
|
expect(preference).to be_persisted
|
|
1470
1388
|
end
|
|
1471
1389
|
|
|
1472
|
-
it
|
|
1390
|
+
it 'adds the correct number of documents' do
|
|
1473
1391
|
expect(person.preferences.size).to eq(1)
|
|
1474
1392
|
end
|
|
1475
1393
|
|
|
1476
|
-
it
|
|
1394
|
+
it 'persists the link' do
|
|
1477
1395
|
expect(person.reload.preferences).to eq([ preference ])
|
|
1478
1396
|
end
|
|
1479
1397
|
end
|
|
1480
1398
|
|
|
1481
|
-
context
|
|
1482
|
-
|
|
1399
|
+
context 'when setting via the associated ids' do
|
|
1483
1400
|
let!(:persisted) do
|
|
1484
|
-
Preference.create!(name:
|
|
1401
|
+
Preference.create!(name: 'testy')
|
|
1485
1402
|
end
|
|
1486
1403
|
|
|
1487
1404
|
let(:preference) do
|
|
@@ -1496,30 +1413,29 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
1496
1413
|
person.save!
|
|
1497
1414
|
end
|
|
1498
1415
|
|
|
1499
|
-
it
|
|
1416
|
+
it 'adds the documents to the relation' do
|
|
1500
1417
|
expect(person.preferences).to eq([ preference ])
|
|
1501
1418
|
end
|
|
1502
1419
|
|
|
1503
|
-
it
|
|
1420
|
+
it 'sets the foreign key on the relation' do
|
|
1504
1421
|
expect(person.preference_ids).to eq([ preference.id ])
|
|
1505
1422
|
end
|
|
1506
1423
|
|
|
1507
|
-
it
|
|
1424
|
+
it 'sets the foreign key on the inverse relation' do
|
|
1508
1425
|
expect(preference.reload.person_ids).to eq([ person.id ])
|
|
1509
1426
|
end
|
|
1510
1427
|
|
|
1511
|
-
it
|
|
1428
|
+
it 'adds the correct number of documents' do
|
|
1512
1429
|
expect(person.preferences.size).to eq(1)
|
|
1513
1430
|
end
|
|
1514
1431
|
|
|
1515
|
-
it
|
|
1432
|
+
it 'persists the link' do
|
|
1516
1433
|
expect(person.reload.preferences).to eq([ preference ])
|
|
1517
1434
|
end
|
|
1518
1435
|
end
|
|
1519
1436
|
end
|
|
1520
1437
|
|
|
1521
|
-
context
|
|
1522
|
-
|
|
1438
|
+
context 'when the parent is not a new record' do
|
|
1523
1439
|
let(:person) do
|
|
1524
1440
|
Person.create!
|
|
1525
1441
|
end
|
|
@@ -1532,36 +1448,35 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
1532
1448
|
person.preferences.concat([ preference ])
|
|
1533
1449
|
end
|
|
1534
1450
|
|
|
1535
|
-
it
|
|
1451
|
+
it 'adds the documents to the relation' do
|
|
1536
1452
|
expect(person.preferences).to eq([ preference ])
|
|
1537
1453
|
end
|
|
1538
1454
|
|
|
1539
|
-
it
|
|
1455
|
+
it 'sets the foreign key on the relation' do
|
|
1540
1456
|
expect(person.preference_ids).to eq([ preference.id ])
|
|
1541
1457
|
end
|
|
1542
1458
|
|
|
1543
|
-
it
|
|
1459
|
+
it 'sets the foreign key on the inverse relation' do
|
|
1544
1460
|
expect(preference.person_ids).to eq([ person.id ])
|
|
1545
1461
|
end
|
|
1546
1462
|
|
|
1547
|
-
it
|
|
1463
|
+
it 'sets the base on the inverse relation' do
|
|
1548
1464
|
expect(preference.people).to eq([ person ])
|
|
1549
1465
|
end
|
|
1550
1466
|
|
|
1551
|
-
it
|
|
1467
|
+
it 'sets the same instance on the inverse relation' do
|
|
1552
1468
|
expect(preference.people.first).to eql(person)
|
|
1553
1469
|
end
|
|
1554
1470
|
|
|
1555
|
-
it
|
|
1556
|
-
expect(preference).
|
|
1471
|
+
it 'saves the target' do
|
|
1472
|
+
expect(preference).not_to be_new_record
|
|
1557
1473
|
end
|
|
1558
1474
|
|
|
1559
|
-
it
|
|
1475
|
+
it 'adds the document to the target' do
|
|
1560
1476
|
expect(person.preferences.count).to eq(1)
|
|
1561
1477
|
end
|
|
1562
1478
|
|
|
1563
|
-
context
|
|
1564
|
-
|
|
1479
|
+
context 'when documents already exist on the relation' do
|
|
1565
1480
|
let(:preference_two) do
|
|
1566
1481
|
Preference.new
|
|
1567
1482
|
end
|
|
@@ -1570,38 +1485,37 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
1570
1485
|
person.preferences.concat([ preference_two ])
|
|
1571
1486
|
end
|
|
1572
1487
|
|
|
1573
|
-
it
|
|
1488
|
+
it 'adds the documents to the relation' do
|
|
1574
1489
|
expect(person.preferences).to eq([ preference, preference_two ])
|
|
1575
1490
|
end
|
|
1576
1491
|
|
|
1577
|
-
it
|
|
1492
|
+
it 'sets the foreign key on the relation' do
|
|
1578
1493
|
expect(person.preference_ids).to eq([ preference.id, preference_two.id ])
|
|
1579
1494
|
end
|
|
1580
1495
|
|
|
1581
|
-
it
|
|
1496
|
+
it 'sets the foreign key on the inverse relation' do
|
|
1582
1497
|
expect(preference_two.person_ids).to eq([ person.id ])
|
|
1583
1498
|
end
|
|
1584
1499
|
|
|
1585
|
-
it
|
|
1500
|
+
it 'sets the base on the inverse relation' do
|
|
1586
1501
|
expect(preference_two.people).to eq([ person ])
|
|
1587
1502
|
end
|
|
1588
1503
|
|
|
1589
|
-
it
|
|
1504
|
+
it 'sets the same instance on the inverse relation' do
|
|
1590
1505
|
expect(preference_two.people.first).to eql(person)
|
|
1591
1506
|
end
|
|
1592
1507
|
|
|
1593
|
-
it
|
|
1594
|
-
expect(preference).
|
|
1508
|
+
it 'saves the target' do
|
|
1509
|
+
expect(preference).not_to be_new_record
|
|
1595
1510
|
end
|
|
1596
1511
|
|
|
1597
|
-
it
|
|
1512
|
+
it 'adds the document to the target' do
|
|
1598
1513
|
expect(person.preferences.count).to eq(2)
|
|
1599
1514
|
end
|
|
1600
1515
|
end
|
|
1601
1516
|
end
|
|
1602
1517
|
|
|
1603
|
-
context
|
|
1604
|
-
|
|
1518
|
+
context 'when both sides have been persisted' do
|
|
1605
1519
|
let(:person) do
|
|
1606
1520
|
Person.create!
|
|
1607
1521
|
end
|
|
@@ -1614,27 +1528,25 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
1614
1528
|
person.administrated_events.concat([ event ])
|
|
1615
1529
|
end
|
|
1616
1530
|
|
|
1617
|
-
it
|
|
1531
|
+
it 'sets the front side of the relation' do
|
|
1618
1532
|
expect(person.administrated_events).to eq([ event ])
|
|
1619
1533
|
end
|
|
1620
1534
|
|
|
1621
|
-
it
|
|
1535
|
+
it 'sets the inverse side of the relation' do
|
|
1622
1536
|
expect(event.administrators(true)).to eq([ person ])
|
|
1623
1537
|
end
|
|
1624
1538
|
|
|
1625
|
-
context
|
|
1626
|
-
|
|
1627
|
-
it "sets the front side of the relation" do
|
|
1539
|
+
context 'when reloading' do
|
|
1540
|
+
it 'sets the front side of the relation' do
|
|
1628
1541
|
expect(person.reload.administrated_events).to eq([ event ])
|
|
1629
1542
|
end
|
|
1630
1543
|
|
|
1631
|
-
it
|
|
1544
|
+
it 'sets the inverse side of the relation' do
|
|
1632
1545
|
expect(event.reload.administrators).to eq([ person ])
|
|
1633
1546
|
end
|
|
1634
1547
|
end
|
|
1635
1548
|
|
|
1636
|
-
context
|
|
1637
|
-
|
|
1549
|
+
context 'when performing a new database query' do
|
|
1638
1550
|
let(:loaded_person) do
|
|
1639
1551
|
Person.find(person.id)
|
|
1640
1552
|
end
|
|
@@ -1643,18 +1555,17 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
1643
1555
|
Event.find(event.id)
|
|
1644
1556
|
end
|
|
1645
1557
|
|
|
1646
|
-
it
|
|
1558
|
+
it 'sets the front side of the relation' do
|
|
1647
1559
|
expect(loaded_person.administrated_events).to eq([ event ])
|
|
1648
1560
|
end
|
|
1649
1561
|
|
|
1650
|
-
it
|
|
1562
|
+
it 'sets the inverse side of the relation' do
|
|
1651
1563
|
expect(loaded_event.administrators).to eq([ person ])
|
|
1652
1564
|
end
|
|
1653
1565
|
end
|
|
1654
1566
|
end
|
|
1655
1567
|
|
|
1656
|
-
context
|
|
1657
|
-
|
|
1568
|
+
context 'when the relation also includes a has_many relation' do
|
|
1658
1569
|
let(:artwork) do
|
|
1659
1570
|
Artwork.create!
|
|
1660
1571
|
end
|
|
@@ -1671,46 +1582,43 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
1671
1582
|
artwork.exhibitors.concat([ exhibitor ])
|
|
1672
1583
|
end
|
|
1673
1584
|
|
|
1674
|
-
it
|
|
1585
|
+
it 'creates a single artwork object' do
|
|
1675
1586
|
expect(Artwork.count).to eq(1)
|
|
1676
1587
|
end
|
|
1677
1588
|
end
|
|
1678
1589
|
|
|
1679
|
-
context
|
|
1680
|
-
|
|
1590
|
+
context 'when the relation is self referencing' do
|
|
1681
1591
|
let(:tag_one) do
|
|
1682
|
-
Tag.create!(text:
|
|
1592
|
+
Tag.create!(text: 'one')
|
|
1683
1593
|
end
|
|
1684
1594
|
|
|
1685
1595
|
let(:tag_two) do
|
|
1686
|
-
Tag.create!(text:
|
|
1596
|
+
Tag.create!(text: 'two')
|
|
1687
1597
|
end
|
|
1688
1598
|
|
|
1689
1599
|
before do
|
|
1690
1600
|
tag_one.related.concat([ tag_two ])
|
|
1691
1601
|
end
|
|
1692
1602
|
|
|
1693
|
-
it
|
|
1603
|
+
it 'sets the front side of the relation' do
|
|
1694
1604
|
expect(tag_one.related).to eq([ tag_two ])
|
|
1695
1605
|
end
|
|
1696
1606
|
|
|
1697
|
-
it
|
|
1607
|
+
it 'sets the inverse side of the relation' do
|
|
1698
1608
|
expect(tag_two.related(true)).to eq([ tag_one ])
|
|
1699
1609
|
end
|
|
1700
1610
|
|
|
1701
|
-
context
|
|
1702
|
-
|
|
1703
|
-
it "sets the front side of the relation" do
|
|
1611
|
+
context 'when reloading' do
|
|
1612
|
+
it 'sets the front side of the relation' do
|
|
1704
1613
|
expect(tag_one.reload.related).to eq([ tag_two ])
|
|
1705
1614
|
end
|
|
1706
1615
|
|
|
1707
|
-
it
|
|
1616
|
+
it 'sets the inverse side of the relation' do
|
|
1708
1617
|
expect(tag_two.reload.related).to eq([ tag_one ])
|
|
1709
1618
|
end
|
|
1710
1619
|
end
|
|
1711
1620
|
|
|
1712
|
-
context
|
|
1713
|
-
|
|
1621
|
+
context 'when performing a new database query' do
|
|
1714
1622
|
let(:loaded_tag_one) do
|
|
1715
1623
|
Tag.find(tag_one.id)
|
|
1716
1624
|
end
|
|
@@ -1719,11 +1627,11 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
1719
1627
|
Tag.find(tag_two.id)
|
|
1720
1628
|
end
|
|
1721
1629
|
|
|
1722
|
-
it
|
|
1630
|
+
it 'sets the front side of the relation' do
|
|
1723
1631
|
expect(loaded_tag_one.related).to eq([ tag_two ])
|
|
1724
1632
|
end
|
|
1725
1633
|
|
|
1726
|
-
it
|
|
1634
|
+
it 'sets the inverse side of the relation' do
|
|
1727
1635
|
expect(loaded_tag_two.related).to eq([ tag_one ])
|
|
1728
1636
|
end
|
|
1729
1637
|
end
|
|
@@ -1731,16 +1639,13 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
1731
1639
|
end
|
|
1732
1640
|
end
|
|
1733
1641
|
|
|
1734
|
-
describe
|
|
1735
|
-
|
|
1642
|
+
describe '#count' do
|
|
1736
1643
|
let(:person) do
|
|
1737
1644
|
Person.create!
|
|
1738
1645
|
end
|
|
1739
1646
|
|
|
1740
|
-
context
|
|
1741
|
-
|
|
1742
|
-
context "when the document is destroyed" do
|
|
1743
|
-
|
|
1647
|
+
context 'when nothing exists on the relation' do
|
|
1648
|
+
context 'when the document is destroyed' do
|
|
1744
1649
|
before do
|
|
1745
1650
|
Meat.create!
|
|
1746
1651
|
end
|
|
@@ -1749,123 +1654,110 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
1749
1654
|
Sandwich.create!
|
|
1750
1655
|
end
|
|
1751
1656
|
|
|
1752
|
-
it
|
|
1657
|
+
it 'returns zero' do
|
|
1753
1658
|
sandwich.destroy
|
|
1754
1659
|
expect(sandwich.meats.count).to eq(0)
|
|
1755
1660
|
end
|
|
1756
1661
|
end
|
|
1757
1662
|
end
|
|
1758
1663
|
|
|
1759
|
-
context
|
|
1760
|
-
|
|
1664
|
+
context 'when documents have been persisted' do
|
|
1761
1665
|
let!(:preference) do
|
|
1762
|
-
person.preferences.create!(name:
|
|
1666
|
+
person.preferences.create!(name: 'setting')
|
|
1763
1667
|
end
|
|
1764
1668
|
|
|
1765
|
-
it
|
|
1669
|
+
it 'returns the number of persisted documents' do
|
|
1766
1670
|
expect(person.preferences.count).to eq(1)
|
|
1767
1671
|
end
|
|
1768
1672
|
end
|
|
1769
1673
|
|
|
1770
|
-
context
|
|
1771
|
-
|
|
1674
|
+
context 'when appending to a loaded relation' do
|
|
1772
1675
|
let!(:preference) do
|
|
1773
|
-
person.preferences.create!(name:
|
|
1676
|
+
person.preferences.create!(name: 'setting')
|
|
1774
1677
|
end
|
|
1775
1678
|
|
|
1776
1679
|
before do
|
|
1777
1680
|
person.preferences.count
|
|
1778
|
-
person.preferences << Preference.create!(name:
|
|
1681
|
+
person.preferences << Preference.create!(name: 'two')
|
|
1779
1682
|
end
|
|
1780
1683
|
|
|
1781
|
-
it
|
|
1684
|
+
it 'returns the number of persisted documents' do
|
|
1782
1685
|
expect(person.preferences.count).to eq(2)
|
|
1783
1686
|
end
|
|
1784
1687
|
end
|
|
1785
1688
|
|
|
1786
|
-
context
|
|
1787
|
-
|
|
1689
|
+
context 'when documents have not been persisted' do
|
|
1788
1690
|
let!(:preference) do
|
|
1789
|
-
person.preferences.build(name:
|
|
1691
|
+
person.preferences.build(name: 'settings')
|
|
1790
1692
|
end
|
|
1791
1693
|
|
|
1792
|
-
it
|
|
1694
|
+
it 'returns 0' do
|
|
1793
1695
|
expect(person.preferences.count).to eq(0)
|
|
1794
1696
|
end
|
|
1795
1697
|
end
|
|
1796
1698
|
|
|
1797
|
-
context
|
|
1798
|
-
|
|
1799
|
-
context "when the documents are part of the relation" do
|
|
1800
|
-
|
|
1699
|
+
context 'when new documents exist in the database' do
|
|
1700
|
+
context 'when the documents are part of the relation' do
|
|
1801
1701
|
before do
|
|
1802
1702
|
Preference.create!(person_ids: [ person.id ])
|
|
1803
1703
|
end
|
|
1804
1704
|
|
|
1805
|
-
it
|
|
1705
|
+
it 'returns the count from the db' do
|
|
1806
1706
|
expect(person.reload.preferences.count).to eq(1)
|
|
1807
1707
|
end
|
|
1808
1708
|
end
|
|
1809
1709
|
|
|
1810
|
-
context
|
|
1811
|
-
|
|
1710
|
+
context 'when the documents are not part of the relation' do
|
|
1812
1711
|
before do
|
|
1813
1712
|
Preference.create!
|
|
1814
1713
|
end
|
|
1815
1714
|
|
|
1816
|
-
it
|
|
1715
|
+
it 'returns the count from the db' do
|
|
1817
1716
|
expect(person.preferences.count).to eq(0)
|
|
1818
1717
|
end
|
|
1819
1718
|
end
|
|
1820
1719
|
end
|
|
1821
1720
|
|
|
1822
|
-
context
|
|
1823
|
-
|
|
1824
|
-
context "when documents have been persisted" do
|
|
1825
|
-
|
|
1721
|
+
context 'when the inverse relation is not defined' do
|
|
1722
|
+
context 'when documents have been persisted' do
|
|
1826
1723
|
let!(:house) do
|
|
1827
|
-
person.houses.create!(name:
|
|
1724
|
+
person.houses.create!(name: 'Wayne Manor')
|
|
1828
1725
|
end
|
|
1829
1726
|
|
|
1830
|
-
it
|
|
1727
|
+
it 'returns the number of persisted documents' do
|
|
1831
1728
|
expect(person.houses.count).to eq(1)
|
|
1832
1729
|
end
|
|
1833
1730
|
end
|
|
1834
1731
|
|
|
1835
|
-
context
|
|
1836
|
-
|
|
1732
|
+
context 'when documents have not been persisted' do
|
|
1837
1733
|
let!(:house) do
|
|
1838
|
-
person.houses.build(name:
|
|
1734
|
+
person.houses.build(name: 'Ryugyong Hotel')
|
|
1839
1735
|
end
|
|
1840
1736
|
|
|
1841
|
-
it
|
|
1737
|
+
it 'returns 0' do
|
|
1842
1738
|
expect(person.preferences.count).to eq(0)
|
|
1843
1739
|
end
|
|
1844
1740
|
end
|
|
1845
1741
|
end
|
|
1846
1742
|
end
|
|
1847
1743
|
|
|
1848
|
-
describe
|
|
1849
|
-
|
|
1744
|
+
describe '#any?' do
|
|
1850
1745
|
let(:sandwich) do
|
|
1851
1746
|
Sandwich.create!
|
|
1852
1747
|
end
|
|
1853
1748
|
|
|
1854
|
-
context
|
|
1855
|
-
|
|
1856
|
-
context "when no document is added" do
|
|
1857
|
-
|
|
1749
|
+
context 'when nothing exists on the relation' do
|
|
1750
|
+
context 'when no document is added' do
|
|
1858
1751
|
let!(:sandwich) do
|
|
1859
1752
|
Sandwich.create!
|
|
1860
1753
|
end
|
|
1861
1754
|
|
|
1862
|
-
it
|
|
1755
|
+
it 'returns false' do
|
|
1863
1756
|
expect(sandwich.meats.any?).to be false
|
|
1864
1757
|
end
|
|
1865
1758
|
end
|
|
1866
1759
|
|
|
1867
|
-
context
|
|
1868
|
-
|
|
1760
|
+
context 'when the document is destroyed' do
|
|
1869
1761
|
before do
|
|
1870
1762
|
Meat.create!
|
|
1871
1763
|
end
|
|
@@ -1874,15 +1766,14 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
1874
1766
|
Sandwich.create!
|
|
1875
1767
|
end
|
|
1876
1768
|
|
|
1877
|
-
it
|
|
1769
|
+
it 'returns false' do
|
|
1878
1770
|
sandwich.destroy
|
|
1879
1771
|
expect(sandwich.meats.any?).to be false
|
|
1880
1772
|
end
|
|
1881
1773
|
end
|
|
1882
1774
|
end
|
|
1883
1775
|
|
|
1884
|
-
context
|
|
1885
|
-
|
|
1776
|
+
context 'when appending to a relation and _loaded/_unloaded are empty' do
|
|
1886
1777
|
let!(:sandwich) do
|
|
1887
1778
|
Sandwich.create!
|
|
1888
1779
|
end
|
|
@@ -1891,138 +1782,144 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
1891
1782
|
sandwich.meats << Meat.new
|
|
1892
1783
|
end
|
|
1893
1784
|
|
|
1894
|
-
it
|
|
1785
|
+
it 'returns true' do
|
|
1895
1786
|
expect(sandwich.meats.any?).to be true
|
|
1896
1787
|
end
|
|
1897
1788
|
end
|
|
1898
1789
|
|
|
1899
|
-
context
|
|
1790
|
+
context 'when appending to a relation in a transaction' do
|
|
1900
1791
|
require_transaction_support
|
|
1901
1792
|
|
|
1793
|
+
# for some reason this test fails intermittently on any sharded
|
|
1794
|
+
# topology (sharded or load-balanced). The error is:
|
|
1795
|
+
#
|
|
1796
|
+
# [13388:StaleConfig]: Transaction <id>...eats is not currently
|
|
1797
|
+
# known and needs to be recovered
|
|
1798
|
+
#
|
|
1799
|
+
# It will fail on one run, succeed on the next, fail on the next,
|
|
1800
|
+
# etc. Tested on both db versions 7 and 8, with both sharded and
|
|
1801
|
+
# load-balanced topologies. For now, we'll just test this on a
|
|
1802
|
+
# replica set.
|
|
1803
|
+
require_topology :replica_set
|
|
1804
|
+
|
|
1805
|
+
# this also fails on server version 4.0, even with replica set,
|
|
1806
|
+
# so we'll just skip it
|
|
1807
|
+
min_server_version '5.0'
|
|
1808
|
+
|
|
1902
1809
|
let!(:sandwich) do
|
|
1903
1810
|
Sandwich.create!
|
|
1904
1811
|
end
|
|
1905
1812
|
|
|
1906
|
-
it
|
|
1907
|
-
sandwich.
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
expect(sandwich.meats.any?).to be true
|
|
1911
|
-
end
|
|
1813
|
+
it 'returns true' do
|
|
1814
|
+
sandwich.transaction do
|
|
1815
|
+
expect { sandwich.meats << Meat.new }.not_to raise_error
|
|
1816
|
+
expect(sandwich.meats.any?).to be true
|
|
1912
1817
|
end
|
|
1913
1818
|
end
|
|
1914
1819
|
end
|
|
1915
1820
|
|
|
1916
|
-
context
|
|
1917
|
-
|
|
1821
|
+
context 'when documents have been persisted' do
|
|
1918
1822
|
let!(:meat) do
|
|
1919
1823
|
sandwich.meats.create!
|
|
1920
1824
|
end
|
|
1921
1825
|
|
|
1922
|
-
it
|
|
1826
|
+
it 'returns true' do
|
|
1923
1827
|
expect(sandwich.meats.any?).to be true
|
|
1924
1828
|
end
|
|
1925
1829
|
end
|
|
1926
1830
|
|
|
1927
|
-
context
|
|
1928
|
-
|
|
1831
|
+
context 'when documents have not been persisted' do
|
|
1929
1832
|
let!(:meat) do
|
|
1930
1833
|
sandwich.meats.build
|
|
1931
1834
|
end
|
|
1932
1835
|
|
|
1933
|
-
it
|
|
1836
|
+
it 'returns false' do
|
|
1934
1837
|
expect(sandwich.meats.any?).to be true
|
|
1935
1838
|
end
|
|
1936
1839
|
end
|
|
1937
1840
|
|
|
1938
|
-
context
|
|
1841
|
+
context 'when new documents exist in the database' do
|
|
1939
1842
|
before do
|
|
1940
|
-
Meat.create!(sandwiches: [sandwich])
|
|
1843
|
+
Meat.create!(sandwiches: [ sandwich ])
|
|
1941
1844
|
end
|
|
1942
1845
|
|
|
1943
|
-
it
|
|
1846
|
+
it 'returns true' do
|
|
1944
1847
|
expect(sandwich.meats.any?).to be true
|
|
1945
1848
|
end
|
|
1946
1849
|
end
|
|
1947
1850
|
end
|
|
1948
1851
|
|
|
1949
|
-
[
|
|
1950
|
-
|
|
1852
|
+
%i[create create!].each do |method|
|
|
1951
1853
|
describe "##{method}" do
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
context "when using string keys" do
|
|
1956
|
-
|
|
1854
|
+
context 'when the relation is not polymorphic' do
|
|
1855
|
+
context 'when using string keys' do
|
|
1957
1856
|
let(:agent) do
|
|
1958
|
-
Agent.create!(number:
|
|
1857
|
+
Agent.create!(number: '007')
|
|
1959
1858
|
end
|
|
1960
1859
|
|
|
1961
1860
|
before do
|
|
1962
|
-
agent.accounts.send(method, name:
|
|
1861
|
+
agent.accounts.send(method, name: 'test again')
|
|
1963
1862
|
end
|
|
1964
1863
|
|
|
1965
|
-
it
|
|
1966
|
-
expect(agent.account_ids).to eq([
|
|
1864
|
+
it 'does not convert the string key to an object id' do
|
|
1865
|
+
expect(agent.account_ids).to eq([ 'test-again' ])
|
|
1967
1866
|
end
|
|
1968
1867
|
end
|
|
1969
1868
|
|
|
1970
|
-
context
|
|
1971
|
-
|
|
1869
|
+
context 'when the parent is a new record' do
|
|
1972
1870
|
let(:person) do
|
|
1973
1871
|
Person.new
|
|
1974
1872
|
end
|
|
1975
1873
|
|
|
1976
|
-
it
|
|
1977
|
-
expect
|
|
1978
|
-
person.preferences.send(method, name:
|
|
1979
|
-
|
|
1874
|
+
it 'raises an unsaved document error' do
|
|
1875
|
+
expect do
|
|
1876
|
+
person.preferences.send(method, name: 'Testing')
|
|
1877
|
+
end.to raise_error(Mongoid::Errors::UnsavedDocument)
|
|
1980
1878
|
end
|
|
1981
1879
|
end
|
|
1982
1880
|
|
|
1983
|
-
context
|
|
1984
|
-
|
|
1881
|
+
context 'when the parent is not a new record' do
|
|
1985
1882
|
let(:person) do
|
|
1986
1883
|
Person.send(method)
|
|
1987
1884
|
end
|
|
1988
1885
|
|
|
1989
1886
|
let!(:preference) do
|
|
1990
|
-
person.preferences.send(method, name:
|
|
1887
|
+
person.preferences.send(method, name: 'Testing')
|
|
1991
1888
|
end
|
|
1992
1889
|
|
|
1993
|
-
it
|
|
1890
|
+
it 'sets the foreign key on the relation' do
|
|
1994
1891
|
expect(person.preference_ids).to eq([ preference.id ])
|
|
1995
1892
|
end
|
|
1996
1893
|
|
|
1997
|
-
it
|
|
1894
|
+
it 'sets the foreign key on the inverse relation' do
|
|
1998
1895
|
expect(preference.person_ids).to eq([ person.id ])
|
|
1999
1896
|
end
|
|
2000
1897
|
|
|
2001
|
-
it
|
|
1898
|
+
it 'adds the document' do
|
|
2002
1899
|
expect(person.preferences).to eq([ preference ])
|
|
2003
1900
|
end
|
|
2004
1901
|
|
|
2005
|
-
it
|
|
1902
|
+
it 'sets the base on the inverse relation' do
|
|
2006
1903
|
expect(preference.people).to eq([ person ])
|
|
2007
1904
|
end
|
|
2008
1905
|
|
|
2009
|
-
it
|
|
2010
|
-
expect(preference.name).to eq(
|
|
1906
|
+
it 'sets the attributes' do
|
|
1907
|
+
expect(preference.name).to eq('Testing')
|
|
2011
1908
|
end
|
|
2012
1909
|
|
|
2013
|
-
it
|
|
1910
|
+
it 'saves the target' do
|
|
2014
1911
|
expect(preference).to be_persisted
|
|
2015
1912
|
end
|
|
2016
1913
|
|
|
2017
|
-
it
|
|
1914
|
+
it 'adds the document to the target' do
|
|
2018
1915
|
expect(person.preferences.count).to eq(1)
|
|
2019
1916
|
end
|
|
2020
1917
|
|
|
2021
|
-
it
|
|
1918
|
+
it 'does not duplicate documents' do
|
|
2022
1919
|
expect(person.reload.preferences.count).to eq(1)
|
|
2023
1920
|
end
|
|
2024
1921
|
|
|
2025
|
-
it
|
|
1922
|
+
it 'does not duplicate ids' do
|
|
2026
1923
|
expect(person.reload.preference_ids.count).to eq(1)
|
|
2027
1924
|
end
|
|
2028
1925
|
end
|
|
@@ -2030,30 +1927,27 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
2030
1927
|
end
|
|
2031
1928
|
end
|
|
2032
1929
|
|
|
2033
|
-
describe
|
|
2034
|
-
|
|
2035
|
-
context "when validation fails" do
|
|
2036
|
-
|
|
1930
|
+
describe '#create!' do
|
|
1931
|
+
context 'when validation fails' do
|
|
2037
1932
|
let(:person) do
|
|
2038
1933
|
Person.create!
|
|
2039
1934
|
end
|
|
2040
1935
|
|
|
2041
|
-
context
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
}.to raise_error(Mongoid::Errors::Validations)
|
|
1936
|
+
context 'when the relation is not polymorphic' do
|
|
1937
|
+
it 'raises an error' do
|
|
1938
|
+
expect do
|
|
1939
|
+
person.preferences.create!(name: 'a')
|
|
1940
|
+
end.to raise_error(Mongoid::Errors::Validations)
|
|
2047
1941
|
end
|
|
2048
1942
|
end
|
|
2049
1943
|
end
|
|
2050
1944
|
end
|
|
2051
1945
|
|
|
2052
1946
|
%i[ delete delete_one ].each do |method|
|
|
2053
|
-
describe "
|
|
1947
|
+
describe "##{method}" do
|
|
2054
1948
|
let(:person) { Person.create! }
|
|
2055
|
-
let(:preference_one) { Preference.create!(name:
|
|
2056
|
-
let(:preference_two) { Preference.create!(name:
|
|
1949
|
+
let(:preference_one) { Preference.create!(name: 'Testing') }
|
|
1950
|
+
let(:preference_two) { Preference.create!(name: 'Test') }
|
|
2057
1951
|
|
|
2058
1952
|
before do
|
|
2059
1953
|
person.preferences << [ preference_one, preference_two ]
|
|
@@ -2134,8 +2028,8 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
2134
2028
|
end
|
|
2135
2029
|
|
|
2136
2030
|
context 'when the relationships are self referencing' do
|
|
2137
|
-
let(:tag_one) { Tag.create!(text:
|
|
2138
|
-
let(:tag_two) { Tag.create!(text:
|
|
2031
|
+
let(:tag_one) { Tag.create!(text: 'one') }
|
|
2032
|
+
let(:tag_two) { Tag.create!(text: 'two') }
|
|
2139
2033
|
|
|
2140
2034
|
before do
|
|
2141
2035
|
tag_one.related << tag_two
|
|
@@ -2162,7 +2056,7 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
2162
2056
|
end
|
|
2163
2057
|
|
|
2164
2058
|
context 'when deleting with reloading' do
|
|
2165
|
-
context
|
|
2059
|
+
context 'when deleting from the front side' do
|
|
2166
2060
|
let(:reloaded) { tag_one.reload }
|
|
2167
2061
|
let!(:deleted) { reloaded.related.send(method, tag_two) }
|
|
2168
2062
|
|
|
@@ -2225,10 +2119,10 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
2225
2119
|
end
|
|
2226
2120
|
end
|
|
2227
2121
|
|
|
2228
|
-
context
|
|
2122
|
+
context 'when errors are raised' do
|
|
2229
2123
|
before do
|
|
2230
2124
|
expect(post).to receive(:before_remove_tag).and_raise
|
|
2231
|
-
begin; post.tags.send(method, tag); rescue; end
|
|
2125
|
+
begin; post.tags.send(method, tag); rescue StandardError; end
|
|
2232
2126
|
end
|
|
2233
2127
|
|
|
2234
2128
|
it 'does not remove the document from the relation' do
|
|
@@ -2255,7 +2149,7 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
2255
2149
|
context 'when errors are raised' do
|
|
2256
2150
|
before do
|
|
2257
2151
|
expect(post).to receive(:after_remove_tag).and_raise
|
|
2258
|
-
begin; post.tags.send(method, tag); rescue; end
|
|
2152
|
+
begin; post.tags.send(method, tag); rescue StandardError; end
|
|
2259
2153
|
end
|
|
2260
2154
|
|
|
2261
2155
|
it 'removes the document from the relation' do
|
|
@@ -2267,54 +2161,60 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
2267
2161
|
end
|
|
2268
2162
|
end
|
|
2269
2163
|
|
|
2164
|
+
# Whether the conditions carry a pattern decides which branch of remove_all
|
|
2165
|
+
# runs, so anything that has to behave the same on both is exercised twice.
|
|
2166
|
+
conditions_variants = [
|
|
2167
|
+
[ 'no regular expression', nil ],
|
|
2168
|
+
[ 'a regular expression', { name: /Test/ } ]
|
|
2169
|
+
].freeze
|
|
2170
|
+
|
|
2270
2171
|
%i[ delete_all destroy_all ].each do |method|
|
|
2271
|
-
describe "
|
|
2172
|
+
describe "##{method}" do
|
|
2272
2173
|
context 'when the relation is not polymorphic' do
|
|
2273
2174
|
context 'when conditions are provided' do
|
|
2274
2175
|
let(:person) { Person.create! }
|
|
2275
2176
|
|
|
2276
2177
|
let!(:preference_one) do
|
|
2277
|
-
person.preferences.create!(name:
|
|
2178
|
+
person.preferences.create!(name: 'Testing')
|
|
2278
2179
|
end
|
|
2279
2180
|
|
|
2280
2181
|
let!(:preference_two) do
|
|
2281
|
-
person.preferences.create!(name:
|
|
2182
|
+
person.preferences.create!(name: 'Test')
|
|
2282
2183
|
end
|
|
2283
2184
|
|
|
2284
2185
|
let!(:deleted) do
|
|
2285
2186
|
person.preferences.send(
|
|
2286
|
-
|
|
2287
|
-
|
|
2187
|
+
method,
|
|
2188
|
+
{ name: 'Testing' }
|
|
2288
2189
|
)
|
|
2289
2190
|
end
|
|
2290
2191
|
|
|
2291
|
-
it
|
|
2192
|
+
it 'removes the correct preferences' do
|
|
2292
2193
|
expect(person.preferences.count).to eq(1)
|
|
2293
2194
|
end
|
|
2294
2195
|
|
|
2295
|
-
it
|
|
2296
|
-
expect(Preference.where(name:
|
|
2196
|
+
it 'deletes the documents from the database' do
|
|
2197
|
+
expect(Preference.where(name: 'Testing').count).to eq(0)
|
|
2297
2198
|
end
|
|
2298
2199
|
|
|
2299
|
-
it
|
|
2200
|
+
it 'returns the number of documents deleted' do
|
|
2300
2201
|
expect(deleted).to eq(1)
|
|
2301
2202
|
end
|
|
2302
2203
|
|
|
2303
|
-
it
|
|
2204
|
+
it 'removes the ids from the foreign key' do
|
|
2304
2205
|
expect(person.preference_ids).to eq([ preference_two.id ])
|
|
2305
2206
|
end
|
|
2306
2207
|
|
|
2307
|
-
it
|
|
2308
|
-
expect(person.preferences).to eq([preference_two])
|
|
2208
|
+
it 'sets the association locally' do
|
|
2209
|
+
expect(person.preferences).to eq([ preference_two ])
|
|
2309
2210
|
end
|
|
2310
2211
|
end
|
|
2311
2212
|
|
|
2312
|
-
context
|
|
2313
|
-
|
|
2213
|
+
context 'when conditions are not provided' do
|
|
2314
2214
|
let(:person) do
|
|
2315
2215
|
Person.create!.tap do |person|
|
|
2316
|
-
person.preferences.create!(name:
|
|
2317
|
-
person.preferences.create!(name:
|
|
2216
|
+
person.preferences.create!(name: 'Testing')
|
|
2217
|
+
person.preferences.create!(name: 'Test')
|
|
2318
2218
|
end
|
|
2319
2219
|
end
|
|
2320
2220
|
|
|
@@ -2322,186 +2222,217 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
2322
2222
|
person.preferences.send(method)
|
|
2323
2223
|
end
|
|
2324
2224
|
|
|
2325
|
-
it
|
|
2225
|
+
it 'removes the correct preferences' do
|
|
2326
2226
|
expect(person.preferences.count).to eq(0)
|
|
2327
2227
|
end
|
|
2328
2228
|
|
|
2329
|
-
it
|
|
2229
|
+
it 'deletes the documents from the database' do
|
|
2330
2230
|
expect(Preference.count).to eq(0)
|
|
2331
2231
|
end
|
|
2332
2232
|
|
|
2333
|
-
it
|
|
2233
|
+
it 'returns the number of documents deleted' do
|
|
2334
2234
|
expect(deleted).to eq(2)
|
|
2335
2235
|
end
|
|
2336
2236
|
|
|
2337
|
-
it
|
|
2237
|
+
it 'sets the association locally' do
|
|
2338
2238
|
expect(person.preferences).to eq([])
|
|
2339
2239
|
end
|
|
2340
2240
|
end
|
|
2241
|
+
|
|
2242
|
+
context 'when the base holds the foreign keys' do
|
|
2243
|
+
# unbind_one pulls ids out of the base's foreign key array and marks
|
|
2244
|
+
# the base dirty, so which documents get unbound is visible here.
|
|
2245
|
+
# Only what the association already held is unbound, and that has to
|
|
2246
|
+
# hold whether or not the conditions carry a regular expression:
|
|
2247
|
+
# scanning under a regexp budget loads the association first, and
|
|
2248
|
+
# that load must not turn into an unbind of its own. See the note in
|
|
2249
|
+
# HasMany::Proxy#remove_all_bounded.
|
|
2250
|
+
let!(:person) do
|
|
2251
|
+
Person.create!.tap do |base|
|
|
2252
|
+
base.preferences.create!(name: 'Testing')
|
|
2253
|
+
base.preferences.create!(name: 'Test')
|
|
2254
|
+
end
|
|
2255
|
+
end
|
|
2256
|
+
|
|
2257
|
+
let(:ids) { person.preference_ids.dup }
|
|
2258
|
+
|
|
2259
|
+
conditions_variants.each do |description, conditions|
|
|
2260
|
+
context "when the conditions carry #{description}" do
|
|
2261
|
+
context 'when the association is already in memory' do
|
|
2262
|
+
it 'unbinds the deleted documents' do
|
|
2263
|
+
person.preferences.send(method, conditions)
|
|
2264
|
+
expect(person.preference_ids).to eq([])
|
|
2265
|
+
expect(person).to be_changed
|
|
2266
|
+
end
|
|
2267
|
+
end
|
|
2268
|
+
|
|
2269
|
+
context 'when the association has not been loaded' do
|
|
2270
|
+
let(:cold) { Person.find(person._id) }
|
|
2271
|
+
|
|
2272
|
+
it 'leaves the foreign keys alone' do
|
|
2273
|
+
expect(ids.length).to eq(2)
|
|
2274
|
+
cold.preferences.send(method, conditions)
|
|
2275
|
+
expect(cold.preference_ids).to eq(ids)
|
|
2276
|
+
expect(cold).not_to be_changed
|
|
2277
|
+
end
|
|
2278
|
+
end
|
|
2279
|
+
end
|
|
2280
|
+
end
|
|
2281
|
+
end
|
|
2341
2282
|
end
|
|
2342
2283
|
end
|
|
2343
2284
|
end
|
|
2344
2285
|
|
|
2345
|
-
describe
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
expect(described_class).to_not be_embedded
|
|
2286
|
+
describe '.embedded?' do
|
|
2287
|
+
it 'returns false' do
|
|
2288
|
+
expect(described_class).not_to be_embedded
|
|
2349
2289
|
end
|
|
2350
2290
|
end
|
|
2351
2291
|
|
|
2352
|
-
describe
|
|
2353
|
-
|
|
2292
|
+
describe '#exists?' do
|
|
2354
2293
|
let!(:person) do
|
|
2355
2294
|
Person.create!
|
|
2356
2295
|
end
|
|
2357
2296
|
|
|
2358
|
-
context
|
|
2359
|
-
|
|
2297
|
+
context 'when documents exist in the database' do
|
|
2360
2298
|
before do
|
|
2361
2299
|
person.preferences.create!
|
|
2362
2300
|
end
|
|
2363
2301
|
|
|
2364
|
-
it
|
|
2302
|
+
it 'returns true' do
|
|
2365
2303
|
expect(person.preferences.exists?).to be true
|
|
2366
2304
|
end
|
|
2367
2305
|
end
|
|
2368
2306
|
|
|
2369
|
-
context
|
|
2370
|
-
|
|
2307
|
+
context 'when no documents exist in the database' do
|
|
2371
2308
|
before do
|
|
2372
2309
|
person.preferences.build
|
|
2373
2310
|
end
|
|
2374
2311
|
|
|
2375
|
-
it
|
|
2312
|
+
it 'returns false' do
|
|
2376
2313
|
expect(person.preferences.exists?).to be false
|
|
2377
2314
|
end
|
|
2378
2315
|
end
|
|
2379
2316
|
end
|
|
2380
2317
|
|
|
2381
|
-
describe
|
|
2382
|
-
|
|
2383
|
-
context "when the relation is not polymorphic" do
|
|
2384
|
-
|
|
2318
|
+
describe '#find' do
|
|
2319
|
+
context 'when the relation is not polymorphic' do
|
|
2385
2320
|
let(:person) do
|
|
2386
2321
|
Person.create!
|
|
2387
2322
|
end
|
|
2388
2323
|
|
|
2389
2324
|
let!(:preference_one) do
|
|
2390
|
-
person.preferences.create!(name:
|
|
2325
|
+
person.preferences.create!(name: 'Test')
|
|
2391
2326
|
end
|
|
2392
2327
|
|
|
2393
2328
|
let!(:preference_two) do
|
|
2394
|
-
person.preferences.create!(name:
|
|
2329
|
+
person.preferences.create!(name: 'OMG I has relations')
|
|
2395
2330
|
end
|
|
2396
2331
|
|
|
2397
2332
|
let!(:unrelated_pref) do
|
|
2398
|
-
Preference.create!(name:
|
|
2333
|
+
Preference.create!(name: 'orphan annie')
|
|
2399
2334
|
end
|
|
2400
2335
|
|
|
2401
2336
|
let!(:unrelated_pref_two) do
|
|
2402
|
-
Preference.create!(name:
|
|
2337
|
+
Preference.create!(name: 'orphan two')
|
|
2403
2338
|
end
|
|
2404
2339
|
|
|
2405
|
-
context
|
|
2406
|
-
|
|
2407
|
-
context "when the id matches" do
|
|
2408
|
-
|
|
2340
|
+
context 'when providing an id' do
|
|
2341
|
+
context 'when the id matches' do
|
|
2409
2342
|
let(:preference) do
|
|
2410
2343
|
person.preferences.find(preference_one.id)
|
|
2411
2344
|
end
|
|
2412
2345
|
|
|
2413
|
-
it
|
|
2346
|
+
it 'returns the matching document' do
|
|
2414
2347
|
expect(preference).to eq(preference_one)
|
|
2415
2348
|
end
|
|
2416
2349
|
end
|
|
2417
2350
|
|
|
2418
|
-
context
|
|
2419
|
-
|
|
2351
|
+
context 'when the id matches an unreferenced document' do
|
|
2420
2352
|
let(:preference) do
|
|
2421
2353
|
person.preferences.find(unrelated_pref.id)
|
|
2422
2354
|
end
|
|
2423
2355
|
|
|
2424
|
-
it
|
|
2425
|
-
expect
|
|
2356
|
+
it 'raises an error' do
|
|
2357
|
+
expect do
|
|
2426
2358
|
preference
|
|
2427
|
-
|
|
2359
|
+
end.to raise_error(Mongoid::Errors::DocumentNotFound,
|
|
2360
|
+
/Document\(s\) not found for class Preference with id\(s\)/)
|
|
2428
2361
|
end
|
|
2429
2362
|
end
|
|
2430
2363
|
|
|
2431
|
-
context
|
|
2432
|
-
|
|
2433
|
-
context "when config set to raise error" do
|
|
2364
|
+
context 'when the id does not match' do
|
|
2365
|
+
context 'when config set to raise error' do
|
|
2434
2366
|
config_override :raise_not_found_error, true
|
|
2435
2367
|
|
|
2436
|
-
it
|
|
2437
|
-
expect
|
|
2368
|
+
it 'raises an error' do
|
|
2369
|
+
expect do
|
|
2438
2370
|
person.preferences.find(BSON::ObjectId.new)
|
|
2439
|
-
|
|
2371
|
+
end.to raise_error(Mongoid::Errors::DocumentNotFound,
|
|
2372
|
+
/Document\(s\) not found for class Preference with id\(s\)/)
|
|
2440
2373
|
end
|
|
2441
2374
|
end
|
|
2442
2375
|
|
|
2443
|
-
context
|
|
2376
|
+
context 'when config set not to raise error' do
|
|
2444
2377
|
config_override :raise_not_found_error, false
|
|
2445
2378
|
|
|
2446
2379
|
let(:preference) do
|
|
2447
2380
|
person.preferences.find(BSON::ObjectId.new)
|
|
2448
2381
|
end
|
|
2449
2382
|
|
|
2450
|
-
it
|
|
2383
|
+
it 'returns nil' do
|
|
2451
2384
|
expect(preference).to be_nil
|
|
2452
2385
|
end
|
|
2453
2386
|
end
|
|
2454
2387
|
end
|
|
2455
2388
|
end
|
|
2456
2389
|
|
|
2457
|
-
context
|
|
2458
|
-
|
|
2459
|
-
context "when the ids match" do
|
|
2460
|
-
|
|
2390
|
+
context 'when providing an array of ids' do
|
|
2391
|
+
context 'when the ids match' do
|
|
2461
2392
|
let(:preferences) do
|
|
2462
2393
|
person.preferences.find([ preference_one.id, preference_two.id ])
|
|
2463
2394
|
end
|
|
2464
2395
|
|
|
2465
|
-
it
|
|
2396
|
+
it 'returns the matching documents' do
|
|
2466
2397
|
expect(preferences).to eq([ preference_one, preference_two ])
|
|
2467
2398
|
end
|
|
2468
2399
|
end
|
|
2469
2400
|
|
|
2470
|
-
context
|
|
2471
|
-
|
|
2401
|
+
context 'when the ids match unreferenced documents' do
|
|
2472
2402
|
let(:preferences) do
|
|
2473
2403
|
person.preferences.find(
|
|
2474
|
-
|
|
2404
|
+
[ unrelated_pref.id, unrelated_pref_two.id ]
|
|
2475
2405
|
)
|
|
2476
2406
|
end
|
|
2477
2407
|
|
|
2478
|
-
it
|
|
2479
|
-
expect
|
|
2408
|
+
it 'raises an error' do
|
|
2409
|
+
expect do
|
|
2480
2410
|
preferences
|
|
2481
|
-
|
|
2411
|
+
end.to raise_error(Mongoid::Errors::DocumentNotFound,
|
|
2412
|
+
/Document\(s\) not found for class Preference with id\(s\)/)
|
|
2482
2413
|
end
|
|
2483
2414
|
end
|
|
2484
2415
|
|
|
2485
|
-
context
|
|
2486
|
-
|
|
2487
|
-
context "when config set to raise error" do
|
|
2416
|
+
context 'when the ids do not match' do
|
|
2417
|
+
context 'when config set to raise error' do
|
|
2488
2418
|
config_override :raise_not_found_error, true
|
|
2489
2419
|
|
|
2490
|
-
it
|
|
2491
|
-
expect
|
|
2420
|
+
it 'raises an error' do
|
|
2421
|
+
expect do
|
|
2492
2422
|
person.preferences.find([ BSON::ObjectId.new ])
|
|
2493
|
-
|
|
2423
|
+
end.to raise_error(Mongoid::Errors::DocumentNotFound,
|
|
2424
|
+
/Document\(s\) not found for class Preference with id\(s\)/)
|
|
2494
2425
|
end
|
|
2495
2426
|
end
|
|
2496
2427
|
|
|
2497
|
-
context
|
|
2428
|
+
context 'when config set not to raise error' do
|
|
2498
2429
|
config_override :raise_not_found_error, false
|
|
2499
2430
|
|
|
2500
2431
|
let(:preferences) do
|
|
2501
2432
|
person.preferences.find([ BSON::ObjectId.new ])
|
|
2502
2433
|
end
|
|
2503
2434
|
|
|
2504
|
-
it
|
|
2435
|
+
it 'returns an empty array' do
|
|
2505
2436
|
expect(preferences).to be_empty
|
|
2506
2437
|
end
|
|
2507
2438
|
end
|
|
@@ -2510,140 +2441,129 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
2510
2441
|
end
|
|
2511
2442
|
end
|
|
2512
2443
|
|
|
2513
|
-
describe
|
|
2514
|
-
|
|
2515
|
-
context "when the relation is not polymorphic" do
|
|
2516
|
-
|
|
2444
|
+
describe '#find_or_create_by' do
|
|
2445
|
+
context 'when the relation is not polymorphic' do
|
|
2517
2446
|
let(:person) do
|
|
2518
2447
|
Person.create!
|
|
2519
2448
|
end
|
|
2520
2449
|
|
|
2521
2450
|
let!(:preference) do
|
|
2522
|
-
person.preferences.create!(name:
|
|
2451
|
+
person.preferences.create!(name: 'Testing')
|
|
2523
2452
|
end
|
|
2524
2453
|
|
|
2525
|
-
context
|
|
2526
|
-
|
|
2454
|
+
context 'when the document exists' do
|
|
2527
2455
|
let(:found) do
|
|
2528
|
-
person.preferences.find_or_create_by(name:
|
|
2456
|
+
person.preferences.find_or_create_by(name: 'Testing')
|
|
2529
2457
|
end
|
|
2530
2458
|
|
|
2531
|
-
it
|
|
2459
|
+
it 'returns the document' do
|
|
2532
2460
|
expect(found).to eq(preference)
|
|
2533
2461
|
end
|
|
2534
2462
|
end
|
|
2535
2463
|
|
|
2536
|
-
context
|
|
2537
|
-
|
|
2464
|
+
context 'when the document does not exist' do
|
|
2538
2465
|
let(:found) do
|
|
2539
|
-
person.preferences.find_or_create_by(name:
|
|
2466
|
+
person.preferences.find_or_create_by(name: 'Test')
|
|
2540
2467
|
end
|
|
2541
2468
|
|
|
2542
|
-
it
|
|
2543
|
-
expect(found.name).to eq(
|
|
2469
|
+
it 'sets the new document attributes' do
|
|
2470
|
+
expect(found.name).to eq('Test')
|
|
2544
2471
|
end
|
|
2545
2472
|
|
|
2546
|
-
it
|
|
2473
|
+
it 'returns a newly persisted document' do
|
|
2547
2474
|
expect(found).to be_persisted
|
|
2548
2475
|
end
|
|
2549
2476
|
end
|
|
2550
2477
|
end
|
|
2551
2478
|
end
|
|
2552
2479
|
|
|
2553
|
-
describe
|
|
2554
|
-
|
|
2555
|
-
context "when the relation is not polymorphic" do
|
|
2556
|
-
|
|
2480
|
+
describe '#find_or_create_by!' do
|
|
2481
|
+
context 'when the relation is not polymorphic' do
|
|
2557
2482
|
let(:person) do
|
|
2558
2483
|
Person.create!
|
|
2559
2484
|
end
|
|
2560
2485
|
|
|
2561
2486
|
let!(:preference) do
|
|
2562
|
-
person.preferences.create!(name:
|
|
2487
|
+
person.preferences.create!(name: 'Testing')
|
|
2563
2488
|
end
|
|
2564
2489
|
|
|
2565
|
-
context
|
|
2566
|
-
|
|
2490
|
+
context 'when the document exists' do
|
|
2567
2491
|
let(:found) do
|
|
2568
|
-
person.preferences.find_or_create_by!(name:
|
|
2492
|
+
person.preferences.find_or_create_by!(name: 'Testing')
|
|
2569
2493
|
end
|
|
2570
2494
|
|
|
2571
|
-
it
|
|
2495
|
+
it 'returns the document' do
|
|
2572
2496
|
expect(found).to eq(preference)
|
|
2573
2497
|
end
|
|
2574
2498
|
end
|
|
2575
2499
|
|
|
2576
|
-
context
|
|
2577
|
-
|
|
2500
|
+
context 'when the document does not exist' do
|
|
2578
2501
|
let(:found) do
|
|
2579
|
-
person.preferences.find_or_create_by!(name:
|
|
2502
|
+
person.preferences.find_or_create_by!(name: 'Test')
|
|
2580
2503
|
end
|
|
2581
2504
|
|
|
2582
|
-
it
|
|
2583
|
-
expect(found.name).to eq(
|
|
2505
|
+
it 'sets the new document attributes' do
|
|
2506
|
+
expect(found.name).to eq('Test')
|
|
2584
2507
|
end
|
|
2585
2508
|
|
|
2586
|
-
it
|
|
2509
|
+
it 'returns a newly persisted document' do
|
|
2587
2510
|
expect(found).to be_persisted
|
|
2588
2511
|
end
|
|
2589
2512
|
|
|
2590
|
-
context
|
|
2591
|
-
|
|
2592
|
-
|
|
2593
|
-
|
|
2594
|
-
|
|
2595
|
-
}.to raise_error(Mongoid::Errors::Validations)
|
|
2513
|
+
context 'when validation fails' do
|
|
2514
|
+
it 'raises an error' do
|
|
2515
|
+
expect do
|
|
2516
|
+
person.preferences.find_or_create_by!(name: 'A')
|
|
2517
|
+
end.to raise_error(Mongoid::Errors::Validations)
|
|
2596
2518
|
end
|
|
2597
2519
|
end
|
|
2598
2520
|
end
|
|
2599
2521
|
end
|
|
2600
2522
|
end
|
|
2601
2523
|
|
|
2602
|
-
describe
|
|
2603
|
-
|
|
2604
|
-
context "when the relation is not polymorphic" do
|
|
2605
|
-
|
|
2524
|
+
describe '#find_or_initialize_by' do
|
|
2525
|
+
context 'when the relation is not polymorphic' do
|
|
2606
2526
|
let(:person) do
|
|
2607
2527
|
Person.create!
|
|
2608
2528
|
end
|
|
2609
2529
|
|
|
2610
2530
|
let!(:preference) do
|
|
2611
|
-
person.preferences.create!(name:
|
|
2531
|
+
person.preferences.create!(name: 'Testing')
|
|
2612
2532
|
end
|
|
2613
2533
|
|
|
2614
|
-
context
|
|
2615
|
-
|
|
2534
|
+
context 'when the document exists' do
|
|
2616
2535
|
let(:found) do
|
|
2617
|
-
person.preferences.find_or_initialize_by(name:
|
|
2536
|
+
person.preferences.find_or_initialize_by(name: 'Testing')
|
|
2618
2537
|
end
|
|
2619
2538
|
|
|
2620
|
-
it
|
|
2539
|
+
it 'returns the document' do
|
|
2621
2540
|
expect(found).to eq(preference)
|
|
2622
2541
|
end
|
|
2623
2542
|
end
|
|
2624
2543
|
|
|
2625
|
-
context
|
|
2626
|
-
|
|
2544
|
+
context 'when the document does not exist' do
|
|
2627
2545
|
let(:found) do
|
|
2628
|
-
person.preferences.find_or_initialize_by(name:
|
|
2546
|
+
person.preferences.find_or_initialize_by(name: 'Test')
|
|
2629
2547
|
end
|
|
2630
2548
|
|
|
2631
|
-
it
|
|
2632
|
-
expect(found.name).to eq(
|
|
2549
|
+
it 'sets the new document attributes' do
|
|
2550
|
+
expect(found.name).to eq('Test')
|
|
2633
2551
|
end
|
|
2634
2552
|
|
|
2635
|
-
it
|
|
2636
|
-
expect(found).
|
|
2553
|
+
it 'returns a non persisted document' do
|
|
2554
|
+
expect(found).not_to be_persisted
|
|
2637
2555
|
end
|
|
2638
2556
|
end
|
|
2639
2557
|
end
|
|
2640
2558
|
end
|
|
2641
2559
|
|
|
2642
|
-
describe
|
|
2643
|
-
|
|
2560
|
+
describe '#max' do
|
|
2644
2561
|
let(:person) do
|
|
2645
2562
|
Person.create!
|
|
2646
2563
|
end
|
|
2564
|
+
let(:max) do
|
|
2565
|
+
person.preferences.max_by(&:ranking)
|
|
2566
|
+
end
|
|
2647
2567
|
|
|
2648
2568
|
let(:preference_one) do
|
|
2649
2569
|
Preference.create!(ranking: 5)
|
|
@@ -2657,22 +2577,18 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
2657
2577
|
person.preferences.push(preference_one, preference_two)
|
|
2658
2578
|
end
|
|
2659
2579
|
|
|
2660
|
-
|
|
2661
|
-
person.preferences.max do |a,b|
|
|
2662
|
-
a.ranking <=> b.ranking
|
|
2663
|
-
end
|
|
2664
|
-
end
|
|
2665
|
-
|
|
2666
|
-
it "returns the document with the max value of the supplied field" do
|
|
2580
|
+
it 'returns the document with the max value of the supplied field' do
|
|
2667
2581
|
expect(max).to eq(preference_two)
|
|
2668
2582
|
end
|
|
2669
2583
|
end
|
|
2670
2584
|
|
|
2671
|
-
describe
|
|
2672
|
-
|
|
2585
|
+
describe '#max_by' do
|
|
2673
2586
|
let(:person) do
|
|
2674
2587
|
Person.create!
|
|
2675
2588
|
end
|
|
2589
|
+
let(:max) do
|
|
2590
|
+
person.preferences.max_by(&:ranking)
|
|
2591
|
+
end
|
|
2676
2592
|
|
|
2677
2593
|
let(:preference_one) do
|
|
2678
2594
|
Preference.create!(ranking: 5)
|
|
@@ -2686,108 +2602,95 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
2686
2602
|
person.preferences.push(preference_one, preference_two)
|
|
2687
2603
|
end
|
|
2688
2604
|
|
|
2689
|
-
|
|
2690
|
-
person.preferences.max_by(&:ranking)
|
|
2691
|
-
end
|
|
2692
|
-
|
|
2693
|
-
it "returns the document with the max value of the supplied field" do
|
|
2605
|
+
it 'returns the document with the max value of the supplied field' do
|
|
2694
2606
|
expect(max).to eq(preference_two)
|
|
2695
2607
|
end
|
|
2696
2608
|
end
|
|
2697
2609
|
|
|
2698
|
-
describe
|
|
2699
|
-
|
|
2610
|
+
describe '#method_missing' do
|
|
2700
2611
|
let!(:person) do
|
|
2701
2612
|
Person.create!
|
|
2702
2613
|
end
|
|
2703
2614
|
|
|
2704
2615
|
let!(:preference_one) do
|
|
2705
|
-
person.preferences.create!(name:
|
|
2616
|
+
person.preferences.create!(name: 'First', value: 'Posting')
|
|
2706
2617
|
end
|
|
2707
2618
|
|
|
2708
2619
|
let!(:preference_two) do
|
|
2709
|
-
person.preferences.create!(name:
|
|
2620
|
+
person.preferences.create!(name: 'Second', value: 'Testing')
|
|
2710
2621
|
end
|
|
2711
2622
|
|
|
2712
2623
|
let!(:unrelated) do
|
|
2713
|
-
Preference.create!(name:
|
|
2624
|
+
Preference.create!(name: 'Third')
|
|
2714
2625
|
end
|
|
2715
2626
|
|
|
2716
|
-
context
|
|
2717
|
-
|
|
2627
|
+
context 'when providing a single criteria' do
|
|
2718
2628
|
let(:preferences) do
|
|
2719
|
-
person.preferences.where(name:
|
|
2629
|
+
person.preferences.where(name: 'First')
|
|
2720
2630
|
end
|
|
2721
2631
|
|
|
2722
|
-
it
|
|
2632
|
+
it 'applies the criteria to the documents' do
|
|
2723
2633
|
expect(preferences).to eq([ preference_one ])
|
|
2724
2634
|
end
|
|
2725
2635
|
|
|
2726
2636
|
context 'when providing a collation' do
|
|
2727
|
-
|
|
2728
2637
|
let(:preferences) do
|
|
2729
|
-
person.preferences.where(name:
|
|
2638
|
+
person.preferences.where(name: 'FIRST').collation(locale: 'en_US', strength: 2).to_a
|
|
2730
2639
|
end
|
|
2731
2640
|
|
|
2732
|
-
it
|
|
2641
|
+
it 'applies the collation option to the query' do
|
|
2733
2642
|
expect(preferences).to eq([ preference_one ])
|
|
2734
2643
|
end
|
|
2735
2644
|
end
|
|
2736
2645
|
end
|
|
2737
2646
|
|
|
2738
|
-
context
|
|
2739
|
-
|
|
2647
|
+
context 'when providing a criteria on id' do
|
|
2740
2648
|
let(:preferences) do
|
|
2741
2649
|
person.preferences.where(_id: unrelated.id)
|
|
2742
2650
|
end
|
|
2743
2651
|
|
|
2744
|
-
it
|
|
2652
|
+
it 'does not return unrelated documents' do
|
|
2745
2653
|
expect(preferences).to be_empty
|
|
2746
2654
|
end
|
|
2747
2655
|
end
|
|
2748
2656
|
|
|
2749
|
-
context
|
|
2750
|
-
|
|
2657
|
+
context 'when providing a criteria class method' do
|
|
2751
2658
|
let(:preferences) do
|
|
2752
2659
|
person.preferences.posting
|
|
2753
2660
|
end
|
|
2754
2661
|
|
|
2755
|
-
it
|
|
2662
|
+
it 'applies the criteria to the documents' do
|
|
2756
2663
|
expect(preferences).to eq([ preference_one ])
|
|
2757
2664
|
end
|
|
2758
2665
|
end
|
|
2759
2666
|
|
|
2760
|
-
context
|
|
2761
|
-
|
|
2667
|
+
context 'when chaining criteria' do
|
|
2762
2668
|
let(:preferences) do
|
|
2763
|
-
person.preferences.posting.where(:name.in => [
|
|
2669
|
+
person.preferences.posting.where(:name.in => [ 'First' ])
|
|
2764
2670
|
end
|
|
2765
2671
|
|
|
2766
|
-
it
|
|
2672
|
+
it 'applies the criteria to the documents' do
|
|
2767
2673
|
expect(preferences).to eq([ preference_one ])
|
|
2768
2674
|
end
|
|
2769
2675
|
end
|
|
2770
2676
|
|
|
2771
|
-
context
|
|
2772
|
-
|
|
2773
|
-
describe "#distinct" do
|
|
2774
|
-
|
|
2677
|
+
context 'when delegating methods' do
|
|
2678
|
+
describe '#distinct' do
|
|
2775
2679
|
let(:values) do
|
|
2776
2680
|
person.preferences.distinct(:name)
|
|
2777
2681
|
end
|
|
2778
2682
|
|
|
2779
|
-
it
|
|
2780
|
-
expect(values).to include(
|
|
2781
|
-
expect(values).to include(
|
|
2683
|
+
it 'returns the distinct values for the fields' do
|
|
2684
|
+
expect(values).to include('First')
|
|
2685
|
+
expect(values).to include('Second')
|
|
2782
2686
|
end
|
|
2783
2687
|
|
|
2784
|
-
context
|
|
2785
|
-
|
|
2688
|
+
context 'when the inverse relation is not defined' do
|
|
2786
2689
|
let!(:house) do
|
|
2787
|
-
person.houses.create!(name:
|
|
2690
|
+
person.houses.create!(name: 'Wayne Manor')
|
|
2788
2691
|
end
|
|
2789
2692
|
|
|
2790
|
-
it
|
|
2693
|
+
it 'returns the distinct values for the fields' do
|
|
2791
2694
|
expect(person.houses.distinct(:name)).to eq([ house.name ])
|
|
2792
2695
|
end
|
|
2793
2696
|
end
|
|
@@ -2795,11 +2698,13 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
2795
2698
|
end
|
|
2796
2699
|
end
|
|
2797
2700
|
|
|
2798
|
-
describe
|
|
2799
|
-
|
|
2701
|
+
describe '#min' do
|
|
2800
2702
|
let(:person) do
|
|
2801
2703
|
Person.create!
|
|
2802
2704
|
end
|
|
2705
|
+
let(:min) do
|
|
2706
|
+
person.preferences.min_by(&:ranking)
|
|
2707
|
+
end
|
|
2803
2708
|
|
|
2804
2709
|
let(:preference_one) do
|
|
2805
2710
|
Preference.create!(ranking: 5)
|
|
@@ -2813,22 +2718,18 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
2813
2718
|
person.preferences.push(preference_one, preference_two)
|
|
2814
2719
|
end
|
|
2815
2720
|
|
|
2816
|
-
|
|
2817
|
-
person.preferences.min do |a, b|
|
|
2818
|
-
a.ranking <=> b.ranking
|
|
2819
|
-
end
|
|
2820
|
-
end
|
|
2821
|
-
|
|
2822
|
-
it "returns the min value of the supplied field" do
|
|
2721
|
+
it 'returns the min value of the supplied field' do
|
|
2823
2722
|
expect(min).to eq(preference_one)
|
|
2824
2723
|
end
|
|
2825
2724
|
end
|
|
2826
2725
|
|
|
2827
|
-
describe
|
|
2828
|
-
|
|
2726
|
+
describe '#min_by' do
|
|
2829
2727
|
let(:person) do
|
|
2830
2728
|
Person.create!
|
|
2831
2729
|
end
|
|
2730
|
+
let(:min) do
|
|
2731
|
+
person.preferences.min_by(&:ranking)
|
|
2732
|
+
end
|
|
2832
2733
|
|
|
2833
2734
|
let(:preference_one) do
|
|
2834
2735
|
Preference.create!(ranking: 5)
|
|
@@ -2842,71 +2743,64 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
2842
2743
|
person.preferences.push(preference_one, preference_two)
|
|
2843
2744
|
end
|
|
2844
2745
|
|
|
2845
|
-
|
|
2846
|
-
person.preferences.min_by(&:ranking)
|
|
2847
|
-
end
|
|
2848
|
-
|
|
2849
|
-
it "returns the min value of the supplied field" do
|
|
2746
|
+
it 'returns the min value of the supplied field' do
|
|
2850
2747
|
expect(min).to eq(preference_one)
|
|
2851
2748
|
end
|
|
2852
2749
|
end
|
|
2853
2750
|
|
|
2854
|
-
describe
|
|
2855
|
-
|
|
2856
|
-
|
|
2857
|
-
expect(Person.new.preferences).to_not be_nil
|
|
2751
|
+
describe '#nil?' do
|
|
2752
|
+
it 'returns false' do
|
|
2753
|
+
expect(Person.new.preferences).not_to be_nil
|
|
2858
2754
|
end
|
|
2859
2755
|
end
|
|
2860
2756
|
|
|
2861
|
-
describe
|
|
2862
|
-
|
|
2757
|
+
describe '#nullify_all' do
|
|
2863
2758
|
let(:person) do
|
|
2864
2759
|
Person.create!
|
|
2865
2760
|
end
|
|
2866
2761
|
|
|
2867
2762
|
let!(:preference_one) do
|
|
2868
|
-
person.preferences.create!(name:
|
|
2763
|
+
person.preferences.create!(name: 'One')
|
|
2869
2764
|
end
|
|
2870
2765
|
|
|
2871
2766
|
let!(:preference_two) do
|
|
2872
|
-
person.preferences.create!(name:
|
|
2767
|
+
person.preferences.create!(name: 'Two')
|
|
2873
2768
|
end
|
|
2874
2769
|
|
|
2875
2770
|
before do
|
|
2876
2771
|
person.preferences.nullify_all
|
|
2877
2772
|
end
|
|
2878
2773
|
|
|
2879
|
-
it
|
|
2774
|
+
it 'removes the foreign key from the base document' do
|
|
2880
2775
|
[ preference_one, preference_two ].each do |preference|
|
|
2881
|
-
expect(person.preference_ids).
|
|
2776
|
+
expect(person.preference_ids).not_to include(preference.id)
|
|
2882
2777
|
end
|
|
2883
2778
|
end
|
|
2884
2779
|
|
|
2885
|
-
it
|
|
2780
|
+
it 'removes the foreign key from the target documents' do
|
|
2886
2781
|
[ preference_one, preference_two ].each do |preference|
|
|
2887
|
-
expect(preference.person_ids).
|
|
2782
|
+
expect(preference.person_ids).not_to include(person.id)
|
|
2888
2783
|
end
|
|
2889
2784
|
end
|
|
2890
2785
|
|
|
2891
|
-
it
|
|
2786
|
+
it 'removes the reference from the base document' do
|
|
2892
2787
|
[ preference_one, preference_two ].each do |preference|
|
|
2893
|
-
expect(person.preferences).
|
|
2788
|
+
expect(person.preferences).not_to include(preference)
|
|
2894
2789
|
end
|
|
2895
2790
|
end
|
|
2896
2791
|
|
|
2897
|
-
it
|
|
2792
|
+
it 'removes the reference from the target documents' do
|
|
2898
2793
|
[ preference_one, preference_two ].each do |preference|
|
|
2899
|
-
expect(preference.people).
|
|
2794
|
+
expect(preference.people).not_to include(person)
|
|
2900
2795
|
end
|
|
2901
2796
|
end
|
|
2902
2797
|
|
|
2903
|
-
it
|
|
2904
|
-
expect(preference_one.reload.people).
|
|
2798
|
+
it 'saves the documents' do
|
|
2799
|
+
expect(preference_one.reload.people).not_to include(person)
|
|
2905
2800
|
end
|
|
2906
2801
|
end
|
|
2907
2802
|
|
|
2908
|
-
describe
|
|
2909
|
-
|
|
2803
|
+
describe '#respond_to?' do
|
|
2910
2804
|
let(:person) do
|
|
2911
2805
|
Person.new
|
|
2912
2806
|
end
|
|
@@ -2916,90 +2810,78 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
2916
2810
|
end
|
|
2917
2811
|
|
|
2918
2812
|
Array.public_instance_methods.sort.each do |method|
|
|
2919
|
-
|
|
2920
2813
|
context "when checking Array##{method}" do
|
|
2921
|
-
|
|
2922
2814
|
before do
|
|
2923
2815
|
expect([].respond_to?(method)).to be true
|
|
2924
2816
|
end
|
|
2925
2817
|
|
|
2926
|
-
it
|
|
2818
|
+
it 'returns true' do
|
|
2927
2819
|
expect(preferences.respond_to?(method)).to be true
|
|
2928
2820
|
end
|
|
2929
2821
|
end
|
|
2930
2822
|
end
|
|
2931
2823
|
|
|
2932
2824
|
Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy.public_instance_methods.sort.each do |method|
|
|
2933
|
-
|
|
2934
2825
|
context "when checking Proxy##{method}" do
|
|
2935
|
-
|
|
2936
|
-
it "returns true" do
|
|
2826
|
+
it 'returns true' do
|
|
2937
2827
|
expect(preferences.respond_to?(method)).to be true
|
|
2938
2828
|
end
|
|
2939
2829
|
end
|
|
2940
2830
|
end
|
|
2941
2831
|
|
|
2942
2832
|
Preference.scopes.keys.sort.each do |method|
|
|
2943
|
-
|
|
2944
2833
|
context "when checking scope #{method}" do
|
|
2945
|
-
|
|
2946
|
-
it "returns true" do
|
|
2834
|
+
it 'returns true' do
|
|
2947
2835
|
expect(preferences.respond_to?(method)).to be true
|
|
2948
2836
|
end
|
|
2949
2837
|
end
|
|
2950
2838
|
end
|
|
2951
2839
|
end
|
|
2952
2840
|
|
|
2953
|
-
describe
|
|
2954
|
-
|
|
2841
|
+
describe '#scoped' do
|
|
2955
2842
|
let(:scoped) do
|
|
2956
2843
|
person.preferences.scoped
|
|
2957
2844
|
end
|
|
2958
2845
|
|
|
2959
2846
|
context 'when association is empty' do
|
|
2960
|
-
|
|
2961
2847
|
let(:person) do
|
|
2962
2848
|
Person.new
|
|
2963
2849
|
end
|
|
2964
2850
|
|
|
2965
|
-
it
|
|
2851
|
+
it 'returns the relation criteria' do
|
|
2966
2852
|
expect(scoped).to be_a(Mongoid::Criteria)
|
|
2967
2853
|
end
|
|
2968
2854
|
|
|
2969
|
-
it
|
|
2970
|
-
expect(scoped.selector).to eq(
|
|
2855
|
+
it 'returns with an empty selector' do
|
|
2856
|
+
expect(scoped.selector).to eq('_id' => { '$in' => [] })
|
|
2971
2857
|
end
|
|
2972
2858
|
end
|
|
2973
2859
|
|
|
2974
2860
|
context 'when association is not empty' do
|
|
2975
|
-
|
|
2976
2861
|
let(:person) do
|
|
2977
2862
|
Person.create!(preferences: [
|
|
2978
|
-
|
|
2979
|
-
|
|
2863
|
+
Preference.new(id: 123)
|
|
2864
|
+
])
|
|
2980
2865
|
end
|
|
2981
2866
|
|
|
2982
|
-
it
|
|
2983
|
-
expect(scoped.selector).to eq(
|
|
2867
|
+
it 'returns with a selector including association element ids' do
|
|
2868
|
+
expect(scoped.selector).to eq('_id' => { '$in' => [ 123 ] })
|
|
2984
2869
|
end
|
|
2985
2870
|
end
|
|
2986
2871
|
end
|
|
2987
2872
|
|
|
2988
|
-
[
|
|
2989
|
-
|
|
2873
|
+
%i[size length].each do |method|
|
|
2990
2874
|
describe "##{method}" do
|
|
2991
|
-
|
|
2992
2875
|
let(:person) do
|
|
2993
2876
|
Person.create!
|
|
2994
2877
|
end
|
|
2995
2878
|
|
|
2996
|
-
context
|
|
2997
|
-
|
|
2879
|
+
context 'when documents have been persisted' do
|
|
2998
2880
|
let!(:preference) do
|
|
2999
|
-
person.preferences.create!(name:
|
|
2881
|
+
person.preferences.create!(name: 'Testing')
|
|
3000
2882
|
end
|
|
3001
2883
|
|
|
3002
|
-
it
|
|
2884
|
+
it 'returns the number of documents' do
|
|
3003
2885
|
expect(person.preferences.send(method)).to eq(1)
|
|
3004
2886
|
end
|
|
3005
2887
|
end
|
|
@@ -3032,238 +2914,225 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
3032
2914
|
end
|
|
3033
2915
|
end
|
|
3034
2916
|
|
|
3035
|
-
context
|
|
3036
|
-
|
|
2917
|
+
context 'when documents have not been persisted' do
|
|
3037
2918
|
before do
|
|
3038
|
-
person.preferences.build(name:
|
|
3039
|
-
person.preferences.create!(name:
|
|
2919
|
+
person.preferences.build(name: 'Test')
|
|
2920
|
+
person.preferences.create!(name: 'Test 2')
|
|
3040
2921
|
end
|
|
3041
2922
|
|
|
3042
|
-
it
|
|
2923
|
+
it 'returns the total number of documents' do
|
|
3043
2924
|
expect(person.preferences.send(method)).to eq(2)
|
|
3044
2925
|
end
|
|
3045
2926
|
end
|
|
3046
2927
|
end
|
|
3047
2928
|
end
|
|
3048
2929
|
|
|
3049
|
-
describe
|
|
3050
|
-
|
|
3051
|
-
context "when the relation has no default scope" do
|
|
3052
|
-
|
|
2930
|
+
describe '#unscoped' do
|
|
2931
|
+
context 'when the relation has no default scope' do
|
|
3053
2932
|
let!(:person) do
|
|
3054
2933
|
Person.create!
|
|
3055
2934
|
end
|
|
3056
2935
|
|
|
3057
2936
|
let!(:preference_one) do
|
|
3058
|
-
person.preferences.create!(name:
|
|
2937
|
+
person.preferences.create!(name: 'first')
|
|
3059
2938
|
end
|
|
3060
2939
|
|
|
3061
2940
|
let!(:preference_two) do
|
|
3062
|
-
Preference.create!(name:
|
|
2941
|
+
Preference.create!(name: 'second')
|
|
3063
2942
|
end
|
|
3064
2943
|
|
|
3065
2944
|
let(:unscoped) do
|
|
3066
2945
|
person.preferences.unscoped
|
|
3067
2946
|
end
|
|
3068
2947
|
|
|
3069
|
-
it
|
|
2948
|
+
it 'returns only the associated documents' do
|
|
3070
2949
|
expect(unscoped).to eq([ preference_one ])
|
|
3071
2950
|
end
|
|
3072
2951
|
end
|
|
3073
2952
|
|
|
3074
|
-
context
|
|
3075
|
-
|
|
2953
|
+
context 'when the relation has a default scope' do
|
|
3076
2954
|
let!(:person) do
|
|
3077
2955
|
Person.create!
|
|
3078
2956
|
end
|
|
3079
2957
|
|
|
3080
2958
|
let!(:house_one) do
|
|
3081
|
-
person.houses.create!(name:
|
|
2959
|
+
person.houses.create!(name: 'first')
|
|
3082
2960
|
end
|
|
3083
2961
|
|
|
3084
2962
|
let!(:house_two) do
|
|
3085
|
-
House.create!(name:
|
|
2963
|
+
House.create!(name: 'second')
|
|
3086
2964
|
end
|
|
3087
2965
|
|
|
3088
2966
|
let(:unscoped) do
|
|
3089
2967
|
person.houses.unscoped
|
|
3090
2968
|
end
|
|
3091
2969
|
|
|
3092
|
-
it
|
|
2970
|
+
it 'only returns associated documents' do
|
|
3093
2971
|
expect(unscoped).to eq([ house_one ])
|
|
3094
2972
|
end
|
|
3095
2973
|
|
|
3096
|
-
it
|
|
2974
|
+
it 'removes the default scoping options' do
|
|
3097
2975
|
expect(unscoped.options).to eq({})
|
|
3098
2976
|
end
|
|
3099
2977
|
end
|
|
3100
2978
|
end
|
|
3101
2979
|
|
|
3102
|
-
context
|
|
3103
|
-
|
|
2980
|
+
context 'when setting the ids directly after the documents' do
|
|
3104
2981
|
let!(:person) do
|
|
3105
|
-
Person.create!(title:
|
|
2982
|
+
Person.create!(title: 'The Boss')
|
|
3106
2983
|
end
|
|
3107
2984
|
|
|
3108
2985
|
let!(:girlfriend_house) do
|
|
3109
|
-
House.create!(name:
|
|
2986
|
+
House.create!(name: 'Girlfriend')
|
|
3110
2987
|
end
|
|
3111
2988
|
|
|
3112
2989
|
let!(:wife_house) do
|
|
3113
|
-
House.create!(name:
|
|
2990
|
+
House.create!(name: 'Wife')
|
|
3114
2991
|
end
|
|
3115
2992
|
|
|
3116
2993
|
let!(:exwife_house) do
|
|
3117
|
-
House.create!(name:
|
|
2994
|
+
House.create!(name: 'Ex-Wife')
|
|
3118
2995
|
end
|
|
3119
2996
|
|
|
3120
2997
|
before do
|
|
3121
2998
|
person.update_attributes!(
|
|
3122
|
-
|
|
2999
|
+
houses: [ wife_house, exwife_house, girlfriend_house ]
|
|
3123
3000
|
)
|
|
3124
3001
|
person.update_attributes!(house_ids: [ girlfriend_house.id ])
|
|
3125
3002
|
end
|
|
3126
3003
|
|
|
3127
|
-
context
|
|
3128
|
-
|
|
3129
|
-
it "properly sets the references" do
|
|
3004
|
+
context 'when reloading' do
|
|
3005
|
+
it 'properly sets the references' do
|
|
3130
3006
|
expect(person.houses(true)).to eq([ girlfriend_house ])
|
|
3131
3007
|
end
|
|
3132
3008
|
end
|
|
3133
3009
|
end
|
|
3134
3010
|
|
|
3135
|
-
context
|
|
3136
|
-
|
|
3137
|
-
context "when the documents are new" do
|
|
3138
|
-
|
|
3011
|
+
context 'when setting both sides in a single call' do
|
|
3012
|
+
context 'when the documents are new' do
|
|
3139
3013
|
let(:user) do
|
|
3140
|
-
User.new(name:
|
|
3014
|
+
User.new(name: 'testing')
|
|
3141
3015
|
end
|
|
3142
3016
|
|
|
3143
3017
|
let(:business) do
|
|
3144
|
-
Business.new(name:
|
|
3018
|
+
Business.new(name: 'serious', owners: [ user ])
|
|
3145
3019
|
end
|
|
3146
3020
|
|
|
3147
3021
|
before do
|
|
3148
3022
|
user.businesses = [ business ]
|
|
3149
3023
|
end
|
|
3150
3024
|
|
|
3151
|
-
it
|
|
3025
|
+
it 'sets the businesses' do
|
|
3152
3026
|
expect(user.businesses).to eq([ business ])
|
|
3153
3027
|
end
|
|
3154
3028
|
|
|
3155
|
-
it
|
|
3029
|
+
it 'sets the inverse users' do
|
|
3156
3030
|
expect(user.businesses.first.owners.first).to eq(user)
|
|
3157
3031
|
end
|
|
3158
3032
|
|
|
3159
|
-
it
|
|
3033
|
+
it 'sets the inverse businesses' do
|
|
3160
3034
|
expect(business.owners).to eq([ user ])
|
|
3161
3035
|
end
|
|
3162
3036
|
end
|
|
3163
3037
|
|
|
3164
|
-
context
|
|
3165
|
-
|
|
3038
|
+
context 'when one side is persisted' do
|
|
3166
3039
|
let!(:user) do
|
|
3167
|
-
User.new(name:
|
|
3040
|
+
User.new(name: 'testing')
|
|
3168
3041
|
end
|
|
3169
3042
|
|
|
3170
3043
|
let!(:business) do
|
|
3171
|
-
Business.create!(name:
|
|
3044
|
+
Business.create!(name: 'serious', owners: [ user ])
|
|
3172
3045
|
end
|
|
3173
3046
|
|
|
3174
3047
|
before do
|
|
3175
3048
|
user.businesses = [ business ]
|
|
3176
3049
|
end
|
|
3177
3050
|
|
|
3178
|
-
it
|
|
3051
|
+
it 'sets the businesses' do
|
|
3179
3052
|
expect(user.businesses).to eq([ business ])
|
|
3180
3053
|
end
|
|
3181
3054
|
|
|
3182
|
-
it
|
|
3055
|
+
it 'sets the inverse users' do
|
|
3183
3056
|
expect(user.businesses.first.owners.first).to eq(user)
|
|
3184
3057
|
end
|
|
3185
3058
|
|
|
3186
|
-
it
|
|
3059
|
+
it 'sets the inverse businesses' do
|
|
3187
3060
|
expect(business.owners).to eq([ user ])
|
|
3188
3061
|
end
|
|
3189
3062
|
|
|
3190
|
-
context
|
|
3191
|
-
|
|
3063
|
+
context 'when reloading' do
|
|
3192
3064
|
before do
|
|
3193
3065
|
user.reload
|
|
3194
3066
|
business.reload
|
|
3195
3067
|
end
|
|
3196
3068
|
|
|
3197
|
-
it
|
|
3069
|
+
it 'persists the businesses' do
|
|
3198
3070
|
expect(user.businesses).to eq([ business ])
|
|
3199
3071
|
end
|
|
3200
3072
|
|
|
3201
|
-
it
|
|
3073
|
+
it 'persists the inverse users' do
|
|
3202
3074
|
expect(user.businesses.first.owners.first).to eq(user)
|
|
3203
3075
|
end
|
|
3204
3076
|
|
|
3205
|
-
it
|
|
3077
|
+
it 'persists the inverse businesses' do
|
|
3206
3078
|
expect(business.owners).to eq([ user ])
|
|
3207
3079
|
end
|
|
3208
3080
|
end
|
|
3209
3081
|
end
|
|
3210
3082
|
|
|
3211
|
-
context
|
|
3212
|
-
|
|
3083
|
+
context 'when the documents are persisted' do
|
|
3213
3084
|
let(:user) do
|
|
3214
|
-
User.create!(name:
|
|
3085
|
+
User.create!(name: 'tst')
|
|
3215
3086
|
end
|
|
3216
3087
|
|
|
3217
3088
|
let(:business) do
|
|
3218
|
-
Business.create!(name:
|
|
3089
|
+
Business.create!(name: 'srs', owners: [ user ])
|
|
3219
3090
|
end
|
|
3220
3091
|
|
|
3221
3092
|
before do
|
|
3222
3093
|
user.businesses = [ business ]
|
|
3223
3094
|
end
|
|
3224
3095
|
|
|
3225
|
-
it
|
|
3096
|
+
it 'sets the businesses' do
|
|
3226
3097
|
expect(user.businesses).to eq([ business ])
|
|
3227
3098
|
end
|
|
3228
3099
|
|
|
3229
|
-
it
|
|
3100
|
+
it 'sets the inverse users' do
|
|
3230
3101
|
expect(user.businesses.first.owners.first).to eq(user)
|
|
3231
3102
|
end
|
|
3232
3103
|
|
|
3233
|
-
it
|
|
3104
|
+
it 'sets the inverse businesses' do
|
|
3234
3105
|
expect(business.owners).to eq([ user ])
|
|
3235
3106
|
end
|
|
3236
3107
|
|
|
3237
|
-
context
|
|
3238
|
-
|
|
3108
|
+
context 'when reloading' do
|
|
3239
3109
|
before do
|
|
3240
3110
|
user.reload
|
|
3241
3111
|
business.reload
|
|
3242
3112
|
end
|
|
3243
3113
|
|
|
3244
|
-
it
|
|
3114
|
+
it 'persists the businesses' do
|
|
3245
3115
|
expect(user.businesses).to eq([ business ])
|
|
3246
3116
|
end
|
|
3247
3117
|
|
|
3248
|
-
it
|
|
3118
|
+
it 'persists the inverse users' do
|
|
3249
3119
|
expect(user.businesses.first.owners.first).to eq(user)
|
|
3250
3120
|
end
|
|
3251
3121
|
|
|
3252
|
-
it
|
|
3122
|
+
it 'persists the inverse businesses' do
|
|
3253
3123
|
expect(business.owners).to eq([ user ])
|
|
3254
3124
|
end
|
|
3255
3125
|
end
|
|
3256
3126
|
end
|
|
3257
3127
|
end
|
|
3258
3128
|
|
|
3259
|
-
context
|
|
3260
|
-
|
|
3129
|
+
context 'when binding the relation multiple times' do
|
|
3261
3130
|
let(:person) do
|
|
3262
3131
|
Person.create!
|
|
3263
3132
|
end
|
|
3264
3133
|
|
|
3265
3134
|
let(:preference) do
|
|
3266
|
-
person.preferences.create!(name:
|
|
3135
|
+
person.preferences.create!(name: 'testing')
|
|
3267
3136
|
end
|
|
3268
3137
|
|
|
3269
3138
|
before do
|
|
@@ -3274,13 +3143,12 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
3274
3143
|
end
|
|
3275
3144
|
end
|
|
3276
3145
|
|
|
3277
|
-
it
|
|
3146
|
+
it 'does not duplicate foreign keys' do
|
|
3278
3147
|
expect(person.preference_ids).to eq([ preference.id ])
|
|
3279
3148
|
end
|
|
3280
3149
|
end
|
|
3281
3150
|
|
|
3282
|
-
context
|
|
3283
|
-
|
|
3151
|
+
context 'when the association has order criteria' do
|
|
3284
3152
|
let(:person) do
|
|
3285
3153
|
Person.create!
|
|
3286
3154
|
end
|
|
@@ -3302,21 +3170,21 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
3302
3170
|
person.ordered_preferences.push(preference_one, preference_two, preference_three)
|
|
3303
3171
|
end
|
|
3304
3172
|
|
|
3305
|
-
it
|
|
3173
|
+
it 'orders the documents' do
|
|
3306
3174
|
expect(person.ordered_preferences(true)).to eq(
|
|
3307
|
-
|
|
3308
|
-
|
|
3175
|
+
[ preference_two, preference_three, preference_one ]
|
|
3176
|
+
)
|
|
3309
3177
|
end
|
|
3310
3178
|
|
|
3311
|
-
it
|
|
3179
|
+
it 'chains default criteria with additional' do
|
|
3312
3180
|
expect(person.ordered_preferences.order_by(:name.desc).to_a).to eq(
|
|
3313
|
-
|
|
3314
|
-
|
|
3181
|
+
[ preference_three, preference_two,
|
|
3182
|
+
preference_one ]
|
|
3183
|
+
)
|
|
3315
3184
|
end
|
|
3316
3185
|
end
|
|
3317
3186
|
|
|
3318
|
-
context
|
|
3319
|
-
|
|
3187
|
+
context 'when the parent is not a new record and freshly loaded' do
|
|
3320
3188
|
let(:person) do
|
|
3321
3189
|
Person.create!
|
|
3322
3190
|
end
|
|
@@ -3332,57 +3200,54 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
3332
3200
|
person.preferences = nil
|
|
3333
3201
|
end
|
|
3334
3202
|
|
|
3335
|
-
it
|
|
3203
|
+
it 'sets the relation to an empty array' do
|
|
3336
3204
|
expect(person.preferences).to be_empty
|
|
3337
3205
|
end
|
|
3338
3206
|
|
|
3339
|
-
it
|
|
3207
|
+
it 'removes the foreign key values' do
|
|
3340
3208
|
expect(person.preference_ids).to be_empty
|
|
3341
3209
|
end
|
|
3342
3210
|
|
|
3343
|
-
it
|
|
3344
|
-
expect
|
|
3211
|
+
it 'does not delete the target from the database' do
|
|
3212
|
+
expect do
|
|
3345
3213
|
preference.reload
|
|
3346
|
-
|
|
3214
|
+
end.not_to raise_error
|
|
3347
3215
|
end
|
|
3348
3216
|
end
|
|
3349
3217
|
|
|
3350
|
-
context
|
|
3351
|
-
|
|
3218
|
+
context 'when reloading the relation' do
|
|
3352
3219
|
let!(:person) do
|
|
3353
3220
|
Person.create!
|
|
3354
3221
|
end
|
|
3355
3222
|
|
|
3356
3223
|
let!(:preference_one) do
|
|
3357
|
-
Preference.create!(name:
|
|
3224
|
+
Preference.create!(name: 'one')
|
|
3358
3225
|
end
|
|
3359
3226
|
|
|
3360
3227
|
let!(:preference_two) do
|
|
3361
|
-
Preference.create!(name:
|
|
3228
|
+
Preference.create!(name: 'two')
|
|
3362
3229
|
end
|
|
3363
3230
|
|
|
3364
3231
|
before do
|
|
3365
3232
|
person.preferences << preference_one
|
|
3366
3233
|
end
|
|
3367
3234
|
|
|
3368
|
-
context
|
|
3369
|
-
|
|
3235
|
+
context 'when the relation references the same documents' do
|
|
3370
3236
|
before do
|
|
3371
|
-
Preference.collection.find({ _id: preference_one.id })
|
|
3372
|
-
|
|
3237
|
+
Preference.collection.find({ _id: preference_one.id })
|
|
3238
|
+
.update_one({ '$set' => { name: 'reloaded' } })
|
|
3373
3239
|
end
|
|
3374
3240
|
|
|
3375
3241
|
let(:reloaded) do
|
|
3376
3242
|
person.preferences(true)
|
|
3377
3243
|
end
|
|
3378
3244
|
|
|
3379
|
-
it
|
|
3380
|
-
expect(reloaded.first.name).to eq(
|
|
3245
|
+
it 'reloads the document from the database' do
|
|
3246
|
+
expect(reloaded.first.name).to eq('reloaded')
|
|
3381
3247
|
end
|
|
3382
3248
|
end
|
|
3383
3249
|
|
|
3384
|
-
context
|
|
3385
|
-
|
|
3250
|
+
context 'when the relation references different documents' do
|
|
3386
3251
|
before do
|
|
3387
3252
|
person.preferences << preference_two
|
|
3388
3253
|
end
|
|
@@ -3391,18 +3256,16 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
3391
3256
|
person.preferences(true)
|
|
3392
3257
|
end
|
|
3393
3258
|
|
|
3394
|
-
it
|
|
3259
|
+
it 'reloads the new document from the database' do
|
|
3395
3260
|
expect(reloaded).to eq([ preference_one, preference_two ])
|
|
3396
3261
|
end
|
|
3397
3262
|
end
|
|
3398
3263
|
end
|
|
3399
3264
|
|
|
3400
|
-
context
|
|
3401
|
-
|
|
3402
|
-
context "when the document is new" do
|
|
3403
|
-
|
|
3265
|
+
context 'when adding to a relation via a field setter' do
|
|
3266
|
+
context 'when the document is new' do
|
|
3404
3267
|
let!(:person) do
|
|
3405
|
-
Person.create!(preference_names:
|
|
3268
|
+
Person.create!(preference_names: 'one, two')
|
|
3406
3269
|
end
|
|
3407
3270
|
|
|
3408
3271
|
let(:preference_one) do
|
|
@@ -3413,36 +3276,35 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
3413
3276
|
person.reload.preferences.last
|
|
3414
3277
|
end
|
|
3415
3278
|
|
|
3416
|
-
it
|
|
3417
|
-
expect(preference_one).
|
|
3279
|
+
it 'persists the first preference' do
|
|
3280
|
+
expect(preference_one).not_to be_nil
|
|
3418
3281
|
end
|
|
3419
3282
|
|
|
3420
|
-
it
|
|
3283
|
+
it 'sets the first inverse' do
|
|
3421
3284
|
expect(preference_one.people).to eq([ person ])
|
|
3422
3285
|
end
|
|
3423
3286
|
|
|
3424
|
-
it
|
|
3425
|
-
expect(preference_two).
|
|
3287
|
+
it 'persists the second preference' do
|
|
3288
|
+
expect(preference_two).not_to be_nil
|
|
3426
3289
|
end
|
|
3427
3290
|
|
|
3428
|
-
it
|
|
3291
|
+
it 'sets the second inverse keys' do
|
|
3429
3292
|
expect(preference_two.people).to eq([ person ])
|
|
3430
3293
|
end
|
|
3431
3294
|
end
|
|
3432
3295
|
end
|
|
3433
3296
|
|
|
3434
|
-
context
|
|
3435
|
-
|
|
3297
|
+
context 'when changing the order of existing ids' do
|
|
3436
3298
|
let(:person) do
|
|
3437
3299
|
Person.new
|
|
3438
3300
|
end
|
|
3439
3301
|
|
|
3440
3302
|
let(:preference_one) do
|
|
3441
|
-
Preference.create!(name:
|
|
3303
|
+
Preference.create!(name: 'one')
|
|
3442
3304
|
end
|
|
3443
3305
|
|
|
3444
3306
|
let(:preference_two) do
|
|
3445
|
-
Preference.create!(name:
|
|
3307
|
+
Preference.create!(name: 'two')
|
|
3446
3308
|
end
|
|
3447
3309
|
|
|
3448
3310
|
before do
|
|
@@ -3450,8 +3312,7 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
3450
3312
|
person.save!
|
|
3451
3313
|
end
|
|
3452
3314
|
|
|
3453
|
-
context
|
|
3454
|
-
|
|
3315
|
+
context 'and the order is changed' do
|
|
3455
3316
|
before do
|
|
3456
3317
|
person.preference_ids = [ preference_two.id, preference_one.id ]
|
|
3457
3318
|
person.save!
|
|
@@ -3461,65 +3322,60 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
3461
3322
|
Person.find(person.id)
|
|
3462
3323
|
end
|
|
3463
3324
|
|
|
3464
|
-
it
|
|
3325
|
+
it 'persists the change in id order' do
|
|
3465
3326
|
expect(reloaded.preference_ids).to eq(
|
|
3466
|
-
|
|
3467
|
-
|
|
3327
|
+
[ preference_two.id, preference_one.id ]
|
|
3328
|
+
)
|
|
3468
3329
|
end
|
|
3469
3330
|
end
|
|
3470
3331
|
|
|
3471
|
-
context
|
|
3472
|
-
|
|
3332
|
+
context 'and the order is changed and an element is added' do
|
|
3473
3333
|
let(:preference_three) do
|
|
3474
|
-
Preference.create!(name:
|
|
3334
|
+
Preference.create!(name: 'three')
|
|
3335
|
+
end
|
|
3336
|
+
let(:reloaded) do
|
|
3337
|
+
Person.find(person.id)
|
|
3475
3338
|
end
|
|
3476
3339
|
|
|
3477
3340
|
before do
|
|
3478
3341
|
person.preference_ids =
|
|
3479
|
-
|
|
3342
|
+
[ preference_two.id, preference_one.id, preference_three.id ]
|
|
3480
3343
|
person.save!
|
|
3481
3344
|
end
|
|
3482
3345
|
|
|
3483
|
-
|
|
3484
|
-
Person.find(person.id)
|
|
3485
|
-
end
|
|
3486
|
-
|
|
3487
|
-
it "also persists the change in id order" do
|
|
3346
|
+
it 'also persists the change in id order' do
|
|
3488
3347
|
expect(reloaded.preference_ids).to eq(
|
|
3489
|
-
|
|
3490
|
-
|
|
3348
|
+
[ preference_two.id, preference_one.id, preference_three.id ]
|
|
3349
|
+
)
|
|
3491
3350
|
end
|
|
3492
3351
|
end
|
|
3493
3352
|
|
|
3494
|
-
context
|
|
3495
|
-
|
|
3353
|
+
context 'and the order is changed and an element is removed' do
|
|
3496
3354
|
let(:preference_three) do
|
|
3497
|
-
Preference.create!(name:
|
|
3355
|
+
Preference.create!(name: 'three')
|
|
3356
|
+
end
|
|
3357
|
+
let(:reloaded) do
|
|
3358
|
+
Person.find(person.id)
|
|
3498
3359
|
end
|
|
3499
3360
|
|
|
3500
3361
|
before do
|
|
3501
3362
|
person.preference_ids =
|
|
3502
|
-
|
|
3363
|
+
[ preference_one.id, preference_two.id, preference_three.id ]
|
|
3503
3364
|
person.save!
|
|
3504
3365
|
person.preference_ids =
|
|
3505
|
-
|
|
3366
|
+
[ preference_three.id, preference_two.id ]
|
|
3506
3367
|
person.save!
|
|
3507
3368
|
end
|
|
3508
3369
|
|
|
3509
|
-
|
|
3510
|
-
Person.find(person.id)
|
|
3511
|
-
end
|
|
3512
|
-
|
|
3513
|
-
it "also persists the change in id order" do
|
|
3370
|
+
it 'also persists the change in id order' do
|
|
3514
3371
|
expect(reloaded.preference_ids).to eq(
|
|
3515
|
-
|
|
3516
|
-
|
|
3372
|
+
[ preference_three.id, preference_two.id ]
|
|
3373
|
+
)
|
|
3517
3374
|
end
|
|
3518
3375
|
end
|
|
3519
3376
|
end
|
|
3520
3377
|
|
|
3521
|
-
context
|
|
3522
|
-
|
|
3378
|
+
context 'when adding a document' do
|
|
3523
3379
|
let(:person) do
|
|
3524
3380
|
Person.new
|
|
3525
3381
|
end
|
|
@@ -3532,8 +3388,7 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
3532
3388
|
person.preferences.push(preference_one)
|
|
3533
3389
|
end
|
|
3534
3390
|
|
|
3535
|
-
context
|
|
3536
|
-
|
|
3391
|
+
context 'when chaining a second add' do
|
|
3537
3392
|
let(:preference_two) do
|
|
3538
3393
|
Preference.new
|
|
3539
3394
|
end
|
|
@@ -3542,16 +3397,14 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
3542
3397
|
first_add.push(preference_two)
|
|
3543
3398
|
end
|
|
3544
3399
|
|
|
3545
|
-
it
|
|
3400
|
+
it 'adds both documents' do
|
|
3546
3401
|
expect(result).to eq([ preference_one, preference_two ])
|
|
3547
3402
|
end
|
|
3548
3403
|
end
|
|
3549
3404
|
end
|
|
3550
3405
|
|
|
3551
|
-
context
|
|
3552
|
-
|
|
3553
|
-
context "when the relation exists" do
|
|
3554
|
-
|
|
3406
|
+
context 'when setting the relation via the foreign key' do
|
|
3407
|
+
context 'when the relation exists' do
|
|
3555
3408
|
let!(:person) do
|
|
3556
3409
|
Person.create!
|
|
3557
3410
|
end
|
|
@@ -3568,14 +3421,13 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
3568
3421
|
person.preference_ids = [ pref_two.id ]
|
|
3569
3422
|
end
|
|
3570
3423
|
|
|
3571
|
-
it
|
|
3424
|
+
it 'sets the new documents on the relation' do
|
|
3572
3425
|
expect(person.preferences).to eq([ pref_two ])
|
|
3573
3426
|
end
|
|
3574
3427
|
end
|
|
3575
3428
|
end
|
|
3576
3429
|
|
|
3577
|
-
context
|
|
3578
|
-
|
|
3430
|
+
context 'when using a different primary key' do
|
|
3579
3431
|
let(:dog) do
|
|
3580
3432
|
Dog.create!(name: 'Doggie')
|
|
3581
3433
|
end
|
|
@@ -3592,72 +3444,67 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
3592
3444
|
FireHydrant.create!(location: '221B Baker Street')
|
|
3593
3445
|
end
|
|
3594
3446
|
|
|
3595
|
-
context
|
|
3596
|
-
|
|
3447
|
+
context 'when adding to a one-way many to many' do
|
|
3597
3448
|
before do
|
|
3598
3449
|
fire_hydrant.cats.push(cat)
|
|
3599
3450
|
end
|
|
3600
3451
|
|
|
3601
|
-
it
|
|
3602
|
-
expect(fire_hydrant.cat_ids).to eq([cat.name])
|
|
3452
|
+
it 'adds the pk value to the fk set' do
|
|
3453
|
+
expect(fire_hydrant.cat_ids).to eq([ cat.name ])
|
|
3603
3454
|
end
|
|
3604
3455
|
end
|
|
3605
3456
|
|
|
3606
|
-
context
|
|
3607
|
-
|
|
3457
|
+
context 'when adding multiple documents to a one-way many to many' do
|
|
3608
3458
|
before do
|
|
3609
|
-
fire_hydrant.cats.push([cat, another_cat])
|
|
3459
|
+
fire_hydrant.cats.push([ cat, another_cat ])
|
|
3610
3460
|
end
|
|
3611
3461
|
|
|
3612
|
-
it
|
|
3613
|
-
expect(fire_hydrant.cat_ids).to eq([cat.name, another_cat.name])
|
|
3462
|
+
it 'adds the pk values to the fk set' do
|
|
3463
|
+
expect(fire_hydrant.cat_ids).to eq([ cat.name, another_cat.name ])
|
|
3614
3464
|
end
|
|
3615
3465
|
end
|
|
3616
3466
|
|
|
3617
|
-
context
|
|
3618
|
-
|
|
3467
|
+
context 'when adding a new document to a one-way many to many' do
|
|
3619
3468
|
before do
|
|
3620
3469
|
fire_hydrant.cats.build name: 'Kitten'
|
|
3621
3470
|
end
|
|
3622
3471
|
|
|
3623
|
-
it
|
|
3624
|
-
expect(fire_hydrant.cat_ids).to eq(['Kitten'])
|
|
3472
|
+
it 'adds the pk values to the fk set' do
|
|
3473
|
+
expect(fire_hydrant.cat_ids).to eq([ 'Kitten' ])
|
|
3625
3474
|
end
|
|
3626
3475
|
end
|
|
3627
3476
|
|
|
3628
|
-
context
|
|
3629
|
-
|
|
3477
|
+
context 'when adding to a two-way many to many' do
|
|
3630
3478
|
before do
|
|
3631
3479
|
fire_hydrant.dogs.push(dog)
|
|
3632
3480
|
end
|
|
3633
3481
|
|
|
3634
|
-
it
|
|
3635
|
-
expect(fire_hydrant.dog_ids).to eq([dog.name])
|
|
3482
|
+
it 'adds the pk value to the fk set' do
|
|
3483
|
+
expect(fire_hydrant.dog_ids).to eq([ dog.name ])
|
|
3636
3484
|
end
|
|
3637
3485
|
|
|
3638
|
-
it
|
|
3639
|
-
expect(dog.fire_hydrant_ids).to eq([fire_hydrant.location])
|
|
3486
|
+
it 'adds the base pk value to the inverse fk set' do
|
|
3487
|
+
expect(dog.fire_hydrant_ids).to eq([ fire_hydrant.location ])
|
|
3640
3488
|
end
|
|
3641
3489
|
end
|
|
3642
3490
|
|
|
3643
|
-
context
|
|
3644
|
-
|
|
3491
|
+
context 'when deleting from a two-way many to many' do
|
|
3645
3492
|
before do
|
|
3646
3493
|
dog.fire_hydrants.push(fire_hydrant)
|
|
3647
3494
|
fire_hydrant.dogs.delete(dog)
|
|
3648
3495
|
end
|
|
3649
3496
|
|
|
3650
|
-
it
|
|
3497
|
+
it 'removes the pk value from the fk set' do
|
|
3651
3498
|
expect(fire_hydrant.dog_ids).to eq([])
|
|
3652
3499
|
end
|
|
3653
3500
|
|
|
3654
|
-
it
|
|
3501
|
+
it 'removes the base pk value from the inverse fk set' do
|
|
3655
3502
|
expect(dog.fire_hydrant_ids).to eq([])
|
|
3656
3503
|
end
|
|
3657
3504
|
end
|
|
3658
3505
|
end
|
|
3659
3506
|
|
|
3660
|
-
context
|
|
3507
|
+
context 'HABTM' do
|
|
3661
3508
|
before do
|
|
3662
3509
|
class Project
|
|
3663
3510
|
include Mongoid::Document
|
|
@@ -3680,18 +3527,18 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
3680
3527
|
end
|
|
3681
3528
|
end
|
|
3682
3529
|
|
|
3683
|
-
it
|
|
3530
|
+
it 'assigns relation from both sides' do
|
|
3684
3531
|
p1 = Project.create! name: 'Foo'
|
|
3685
3532
|
p2 = Project.create! name: 'Bar'
|
|
3686
3533
|
d1 = Distributor.create! name: 'Rock'
|
|
3687
3534
|
d2 = Distributor.create! name: 'Soul'
|
|
3688
3535
|
|
|
3689
3536
|
p1.distributors << d1
|
|
3690
|
-
expect(p1.d_ids).to
|
|
3691
|
-
expect(d1.p_ids).to
|
|
3537
|
+
expect(p1.d_ids).to contain_exactly(d1.id)
|
|
3538
|
+
expect(d1.p_ids).to contain_exactly(p1.id)
|
|
3692
3539
|
d2.projects << p2
|
|
3693
|
-
expect(d2.p_ids).to
|
|
3694
|
-
expect(p2.d_ids).to
|
|
3540
|
+
expect(d2.p_ids).to contain_exactly(p2.id)
|
|
3541
|
+
expect(p2.d_ids).to contain_exactly(d2.id)
|
|
3695
3542
|
end
|
|
3696
3543
|
end
|
|
3697
3544
|
|
|
@@ -3699,24 +3546,24 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
3699
3546
|
let!(:contract) { HabtmmContract.create! }
|
|
3700
3547
|
let!(:signature) { contract.signatures.create! }
|
|
3701
3548
|
|
|
3702
|
-
it
|
|
3703
|
-
expect(signature.favorite_signature).to
|
|
3549
|
+
it 'is nil' do
|
|
3550
|
+
expect(signature.favorite_signature).to be_nil
|
|
3704
3551
|
end
|
|
3705
3552
|
end
|
|
3706
3553
|
|
|
3707
|
-
context
|
|
3554
|
+
context 'when setting an association on a model that uses the class_name option' do
|
|
3708
3555
|
let!(:contract) { HabtmmContract.create! }
|
|
3709
|
-
let!(:signature) { HabtmmSignature.create!(contracts: [contract]) }
|
|
3556
|
+
let!(:signature) { HabtmmSignature.create!(contracts: [ contract ]) }
|
|
3710
3557
|
|
|
3711
|
-
it
|
|
3712
|
-
expect(signature.contracts.first.signature_ids).to eq([signature.id])
|
|
3558
|
+
it 'populates the inverse foreign key' do
|
|
3559
|
+
expect(signature.contracts.first.signature_ids).to eq([ signature.id ])
|
|
3713
3560
|
end
|
|
3714
3561
|
end
|
|
3715
3562
|
|
|
3716
|
-
context
|
|
3717
|
-
it
|
|
3718
|
-
expect(Dog.aliased_associations[
|
|
3719
|
-
expect(Breed.aliased_associations[
|
|
3563
|
+
context 'when there is a foreign key in the aliased associations' do
|
|
3564
|
+
it 'has the correct aliases' do
|
|
3565
|
+
expect(Dog.aliased_associations['breed_ids']).to eq('breeds')
|
|
3566
|
+
expect(Breed.aliased_associations['dog_ids']).to eq('dogs')
|
|
3720
3567
|
end
|
|
3721
3568
|
end
|
|
3722
3569
|
|
|
@@ -3729,21 +3576,18 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
3729
3576
|
# when retrieving a document from the database, the attributes hash is actually
|
|
3730
3577
|
# a BSON::Document, which applies a transformation to the array before
|
|
3731
3578
|
# storing it.
|
|
3732
|
-
context
|
|
3579
|
+
context 'when executing concat on foreign key array from the db' do
|
|
3733
3580
|
before do
|
|
3734
3581
|
HabtmmContract.create!
|
|
3735
3582
|
HabtmmSignature.create!
|
|
3583
|
+
contract.signature_ids.concat([ signature.id ])
|
|
3736
3584
|
end
|
|
3737
3585
|
|
|
3738
3586
|
let!(:contract) { HabtmmContract.first }
|
|
3739
3587
|
let!(:signature) { HabtmmSignature.first }
|
|
3740
3588
|
|
|
3741
|
-
|
|
3742
|
-
contract.signature_ids.
|
|
3743
|
-
end
|
|
3744
|
-
|
|
3745
|
-
it "works on the first attempt" do
|
|
3746
|
-
expect(contract.signature_ids).to eq([signature.id])
|
|
3589
|
+
it 'works on the first attempt' do
|
|
3590
|
+
expect(contract.signature_ids).to eq([ signature.id ])
|
|
3747
3591
|
end
|
|
3748
3592
|
end
|
|
3749
3593
|
end
|