mongoid 9.0.12 → 9.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -8
- data/README.md +5 -11
- data/Rakefile +35 -38
- data/lib/config/locales/en.yml +11 -45
- data/lib/mongoid/association/accessors.rb +19 -26
- data/lib/mongoid/association/bindable.rb +43 -45
- data/lib/mongoid/association/builders.rb +18 -15
- data/lib/mongoid/association/constrainable.rb +3 -5
- data/lib/mongoid/association/depending.rb +38 -46
- data/lib/mongoid/association/eager.rb +47 -13
- data/lib/mongoid/association/eager_loadable.rb +152 -24
- data/lib/mongoid/association/embedded/batchable.rb +28 -30
- data/lib/mongoid/association/embedded/cyclic.rb +8 -11
- data/lib/mongoid/association/embedded/eager.rb +1 -6
- data/lib/mongoid/association/embedded/embedded_in/binding.rb +6 -9
- data/lib/mongoid/association/embedded/embedded_in/buildable.rb +2 -3
- data/lib/mongoid/association/embedded/embedded_in/proxy.rb +2 -0
- data/lib/mongoid/association/embedded/embedded_in.rb +25 -22
- data/lib/mongoid/association/embedded/embeds_many/binding.rb +0 -2
- data/lib/mongoid/association/embedded/embeds_many/buildable.rb +2 -3
- data/lib/mongoid/association/embedded/embeds_many/proxy.rb +1 -1
- data/lib/mongoid/association/embedded/embeds_many.rb +24 -21
- data/lib/mongoid/association/embedded/embeds_one/binding.rb +0 -2
- data/lib/mongoid/association/embedded/embeds_one/buildable.rb +2 -6
- data/lib/mongoid/association/embedded/embeds_one.rb +20 -18
- data/lib/mongoid/association/embedded.rb +0 -1
- data/lib/mongoid/association/macros.rb +3 -7
- data/lib/mongoid/association/many.rb +75 -3
- data/lib/mongoid/association/marshalable.rb +0 -3
- data/lib/mongoid/association/nested/many.rb +23 -47
- data/lib/mongoid/association/nested/nested_buildable.rb +6 -23
- data/lib/mongoid/association/nested/one.rb +4 -7
- data/lib/mongoid/association/nested.rb +1 -3
- data/lib/mongoid/association/one.rb +0 -3
- data/lib/mongoid/association/options.rb +8 -7
- data/lib/mongoid/association/referenced/auto_save.rb +0 -2
- data/lib/mongoid/association/referenced/belongs_to/binding.rb +11 -15
- data/lib/mongoid/association/referenced/belongs_to/buildable.rb +23 -5
- data/lib/mongoid/association/referenced/belongs_to/eager.rb +1 -3
- data/lib/mongoid/association/referenced/belongs_to.rb +48 -43
- data/lib/mongoid/association/referenced/counter_cache.rb +5 -10
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/binding.rb +0 -2
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb +8 -4
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb +0 -3
- data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +52 -47
- data/lib/mongoid/association/referenced/has_many/binding.rb +0 -2
- data/lib/mongoid/association/referenced/has_many/buildable.rb +11 -5
- data/lib/mongoid/association/referenced/has_many/eager.rb +0 -3
- data/lib/mongoid/association/referenced/has_many/enumerable.rb +107 -38
- data/lib/mongoid/association/referenced/has_many/proxy.rb +11 -106
- data/lib/mongoid/association/referenced/has_many.rb +30 -24
- data/lib/mongoid/association/referenced/has_one/binding.rb +0 -2
- data/lib/mongoid/association/referenced/has_one/buildable.rb +11 -9
- data/lib/mongoid/association/referenced/has_one/eager.rb +0 -3
- data/lib/mongoid/association/referenced/has_one.rb +26 -21
- data/lib/mongoid/association/referenced/syncable.rb +30 -34
- data/lib/mongoid/association/referenced.rb +0 -1
- data/lib/mongoid/association/reflections.rb +0 -3
- data/lib/mongoid/association/relatable.rb +69 -83
- data/lib/mongoid/association.rb +12 -15
- data/lib/mongoid/atomic/modifiers.rb +65 -25
- data/lib/mongoid/atomic/paths/embedded/many.rb +4 -7
- data/lib/mongoid/atomic/paths/embedded/one.rb +2 -4
- data/lib/mongoid/atomic/paths/embedded.rb +3 -6
- data/lib/mongoid/atomic/paths/root.rb +1 -4
- data/lib/mongoid/atomic/paths.rb +2 -3
- data/lib/mongoid/atomic.rb +0 -3
- data/lib/mongoid/attributes/dynamic.rb +6 -7
- data/lib/mongoid/attributes/nested.rb +8 -10
- data/lib/mongoid/attributes/projector.rb +7 -20
- data/lib/mongoid/attributes/readonly.rb +6 -11
- data/lib/mongoid/attributes.rb +18 -34
- data/lib/mongoid/cacheable.rb +24 -6
- data/lib/mongoid/clients/factory.rb +12 -15
- data/lib/mongoid/clients/options.rb +2 -5
- data/lib/mongoid/clients/sessions.rb +50 -60
- data/lib/mongoid/clients/storage_options.rb +1 -4
- data/lib/mongoid/clients/validators/storage.rb +2 -3
- data/lib/mongoid/clients/validators.rb +1 -2
- data/lib/mongoid/clients.rb +8 -11
- data/lib/mongoid/collection_configurable.rb +10 -22
- data/lib/mongoid/composable.rb +30 -34
- data/lib/mongoid/config/defaults.rb +7 -4
- data/lib/mongoid/config/encryption.rb +19 -39
- 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 +127 -84
- data/lib/mongoid/contextual/aggregable/memory.rb +4 -9
- 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 +76 -80
- data/lib/mongoid/contextual/mongo/documents_loader.rb +2 -3
- data/lib/mongoid/contextual/mongo.rb +169 -85
- data/lib/mongoid/contextual/none.rb +9 -9
- data/lib/mongoid/contextual/queryable.rb +1 -4
- data/lib/mongoid/contextual.rb +18 -7
- data/lib/mongoid/copyable.rb +7 -9
- data/lib/mongoid/criteria/findable.rb +5 -7
- data/lib/mongoid/criteria/includable.rb +37 -8
- data/lib/mongoid/criteria/inspectable.rb +2 -5
- data/lib/mongoid/criteria/marshalable.rb +4 -8
- data/lib/mongoid/criteria/modifiable.rb +3 -11
- data/lib/mongoid/criteria/options.rb +0 -3
- data/lib/mongoid/criteria/permission.rb +34 -37
- data/lib/mongoid/criteria/queryable/aggregable.rb +9 -10
- data/lib/mongoid/criteria/queryable/expandable.rb +4 -9
- data/lib/mongoid/criteria/queryable/extensions/array.rb +7 -10
- data/lib/mongoid/criteria/queryable/extensions/big_decimal.rb +3 -5
- data/lib/mongoid/criteria/queryable/extensions/boolean.rb +1 -4
- data/lib/mongoid/criteria/queryable/extensions/date.rb +2 -6
- data/lib/mongoid/criteria/queryable/extensions/date_time.rb +3 -7
- data/lib/mongoid/criteria/queryable/extensions/hash.rb +1 -4
- data/lib/mongoid/criteria/queryable/extensions/nil_class.rb +20 -11
- data/lib/mongoid/criteria/queryable/extensions/numeric.rb +15 -11
- data/lib/mongoid/criteria/queryable/extensions/object.rb +7 -8
- data/lib/mongoid/criteria/queryable/extensions/range.rb +6 -9
- data/lib/mongoid/criteria/queryable/extensions/regexp.rb +10 -12
- data/lib/mongoid/criteria/queryable/extensions/set.rb +4 -6
- data/lib/mongoid/criteria/queryable/extensions/string.rb +5 -9
- data/lib/mongoid/criteria/queryable/extensions/symbol.rb +3 -7
- data/lib/mongoid/criteria/queryable/extensions/time.rb +2 -6
- data/lib/mongoid/criteria/queryable/extensions/time_with_zone.rb +2 -12
- data/lib/mongoid/criteria/queryable/extensions.rb +18 -21
- data/lib/mongoid/criteria/queryable/key.rb +12 -18
- data/lib/mongoid/criteria/queryable/macroable.rb +0 -3
- data/lib/mongoid/criteria/queryable/mergeable.rb +31 -43
- 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 +130 -301
- data/lib/mongoid/criteria/queryable/selector.rb +20 -29
- data/lib/mongoid/criteria/queryable/smash.rb +1 -6
- data/lib/mongoid/criteria/queryable/storable.rb +20 -39
- data/lib/mongoid/criteria/queryable.rb +25 -15
- data/lib/mongoid/criteria/scopable.rb +5 -11
- data/lib/mongoid/criteria/translator.rb +5 -8
- data/lib/mongoid/criteria.rb +109 -54
- data/lib/mongoid/deprecable.rb +1 -4
- data/lib/mongoid/deprecation.rb +2 -6
- data/lib/mongoid/document.rb +1 -1
- data/lib/mongoid/encryptable.rb +1 -48
- data/lib/mongoid/equality.rb +4 -5
- data/lib/mongoid/errors/ambiguous_relationship.rb +2 -5
- data/lib/mongoid/errors/attribute_not_loaded.rb +1 -4
- data/lib/mongoid/errors/callback.rb +1 -4
- data/lib/mongoid/errors/create_collection_failure.rb +8 -11
- data/lib/mongoid/errors/criteria_argument_required.rb +2 -5
- data/lib/mongoid/errors/delete_restriction.rb +1 -4
- data/lib/mongoid/errors/document_not_destroyed.rb +1 -4
- data/lib/mongoid/errors/document_not_found.rb +16 -21
- data/lib/mongoid/errors/drop_collection_failure.rb +7 -10
- data/lib/mongoid/errors/empty_config_file.rb +1 -4
- data/lib/mongoid/errors/immutable_attribute.rb +1 -4
- data/lib/mongoid/errors/in_memory_collation_not_supported.rb +1 -4
- data/lib/mongoid/errors/invalid_async_query_executor.rb +2 -5
- data/lib/mongoid/errors/invalid_auto_encryption_configuration.rb +2 -5
- data/lib/mongoid/errors/invalid_collection.rb +1 -3
- data/lib/mongoid/errors/invalid_config_file.rb +1 -4
- data/lib/mongoid/errors/invalid_config_option.rb +2 -5
- data/lib/mongoid/errors/invalid_dependent_strategy.rb +6 -10
- data/lib/mongoid/errors/invalid_discriminator_key_target.rb +2 -4
- data/lib/mongoid/errors/invalid_dot_dollar_assignment.rb +1 -4
- data/lib/mongoid/errors/invalid_elem_match_operator.rb +5 -9
- data/lib/mongoid/errors/invalid_estimated_count_criteria.rb +1 -4
- data/lib/mongoid/errors/invalid_estimated_count_scoping.rb +1 -4
- data/lib/mongoid/errors/invalid_expression_operator.rb +3 -7
- data/lib/mongoid/errors/invalid_field.rb +2 -5
- data/lib/mongoid/errors/invalid_field_operator.rb +5 -9
- data/lib/mongoid/errors/invalid_field_option.rb +2 -5
- data/lib/mongoid/errors/invalid_field_type.rb +1 -4
- data/lib/mongoid/errors/invalid_find.rb +1 -4
- data/lib/mongoid/errors/invalid_global_executor_concurrency.rb +1 -4
- data/lib/mongoid/errors/invalid_includes.rb +3 -6
- data/lib/mongoid/errors/invalid_index.rb +1 -4
- data/lib/mongoid/errors/invalid_options.rb +1 -4
- data/lib/mongoid/errors/invalid_path.rb +1 -4
- data/lib/mongoid/errors/invalid_persistence_option.rb +4 -7
- data/lib/mongoid/errors/invalid_query.rb +6 -11
- data/lib/mongoid/errors/invalid_relation.rb +10 -13
- data/lib/mongoid/errors/invalid_relation_option.rb +4 -7
- data/lib/mongoid/errors/invalid_scope.rb +1 -4
- data/lib/mongoid/errors/invalid_session_nesting.rb +0 -3
- data/lib/mongoid/errors/invalid_set_polymorphic_relation.rb +2 -4
- data/lib/mongoid/errors/invalid_storage_options.rb +1 -4
- data/lib/mongoid/errors/invalid_time.rb +1 -4
- data/lib/mongoid/errors/invalid_transaction_nesting.rb +0 -3
- data/lib/mongoid/errors/inverse_not_found.rb +1 -4
- data/lib/mongoid/errors/mixed_client_configuration.rb +1 -4
- data/lib/mongoid/errors/mixed_relations.rb +1 -3
- data/lib/mongoid/errors/mongoid_error.rb +7 -11
- data/lib/mongoid/errors/nested_attributes_metadata_not_found.rb +1 -4
- data/lib/mongoid/errors/no_client_config.rb +1 -4
- data/lib/mongoid/errors/no_client_database.rb +1 -4
- data/lib/mongoid/errors/no_client_hosts.rb +1 -4
- data/lib/mongoid/errors/no_clients_config.rb +1 -4
- data/lib/mongoid/errors/no_default_client.rb +1 -4
- data/lib/mongoid/errors/no_environment.rb +1 -4
- data/lib/mongoid/errors/no_map_reduce_output.rb +1 -4
- data/lib/mongoid/errors/no_metadata.rb +1 -4
- data/lib/mongoid/errors/no_parent.rb +1 -4
- data/lib/mongoid/errors/readonly_attribute.rb +1 -4
- data/lib/mongoid/errors/readonly_document.rb +1 -4
- data/lib/mongoid/errors/rollback.rb +0 -2
- data/lib/mongoid/errors/scope_overwrite.rb +2 -4
- data/lib/mongoid/errors/sessions_not_supported.rb +0 -3
- data/lib/mongoid/errors/too_many_nested_attribute_records.rb +1 -3
- data/lib/mongoid/errors/transaction_error.rb +0 -3
- data/lib/mongoid/errors/transactions_not_supported.rb +0 -3
- data/lib/mongoid/errors/unknown_attribute.rb +1 -4
- data/lib/mongoid/errors/unknown_model.rb +1 -4
- data/lib/mongoid/errors/unregistered_class.rb +1 -1
- data/lib/mongoid/errors/unsaved_document.rb +1 -3
- data/lib/mongoid/errors/unsupported_isolation_level.rb +22 -0
- data/lib/mongoid/errors/unsupported_javascript.rb +1 -4
- data/lib/mongoid/errors/validations.rb +3 -5
- data/lib/mongoid/errors.rb +72 -74
- data/lib/mongoid/evolvable.rb +0 -3
- data/lib/mongoid/extensions/array.rb +5 -7
- data/lib/mongoid/extensions/big_decimal.rb +6 -7
- data/lib/mongoid/extensions/binary.rb +3 -6
- data/lib/mongoid/extensions/boolean.rb +4 -7
- data/lib/mongoid/extensions/bson_document.rb +28 -0
- data/lib/mongoid/extensions/date.rb +10 -14
- data/lib/mongoid/extensions/date_time.rb +2 -6
- data/lib/mongoid/extensions/decimal128.rb +1 -5
- data/lib/mongoid/extensions/false_class.rb +3 -5
- data/lib/mongoid/extensions/float.rb +5 -10
- data/lib/mongoid/extensions/hash.rb +5 -10
- data/lib/mongoid/extensions/integer.rb +5 -10
- data/lib/mongoid/extensions/module.rb +1 -4
- data/lib/mongoid/extensions/nil_class.rb +0 -1
- data/lib/mongoid/extensions/object.rb +9 -14
- data/lib/mongoid/extensions/object_id.rb +2 -6
- data/lib/mongoid/extensions/range.rb +14 -15
- data/lib/mongoid/extensions/raw_value.rb +0 -3
- data/lib/mongoid/extensions/regexp.rb +3 -6
- data/lib/mongoid/extensions/set.rb +3 -4
- data/lib/mongoid/extensions/string.rb +8 -12
- data/lib/mongoid/extensions/symbol.rb +3 -7
- data/lib/mongoid/extensions/time.rb +24 -26
- data/lib/mongoid/extensions/time_with_zone.rb +4 -7
- data/lib/mongoid/extensions/true_class.rb +3 -5
- data/lib/mongoid/extensions.rb +26 -26
- data/lib/mongoid/factory.rb +5 -9
- data/lib/mongoid/fields/encrypted.rb +0 -1
- data/lib/mongoid/fields/foreign_key.rb +9 -10
- data/lib/mongoid/fields/localized.rb +12 -12
- data/lib/mongoid/fields/standard.rb +5 -7
- data/lib/mongoid/fields/validators/macro.rb +23 -34
- data/lib/mongoid/fields/validators.rb +1 -2
- data/lib/mongoid/fields.rb +151 -58
- data/lib/mongoid/findable.rb +82 -53
- data/lib/mongoid/indexable/specification.rb +20 -6
- data/lib/mongoid/indexable/validators/options.rb +33 -40
- data/lib/mongoid/indexable.rb +41 -52
- 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 +1 -8
- 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 -2
- 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 +12 -11
- data/lib/mongoid/matcher/size.rb +2 -5
- data/lib/mongoid/matcher/type.rb +32 -37
- data/lib/mongoid/matcher.rb +7 -16
- data/lib/mongoid/model_resolver.rb +1 -0
- data/lib/mongoid/persistable/creatable.rb +23 -7
- data/lib/mongoid/persistable/deletable.rb +6 -7
- data/lib/mongoid/persistable/destroyable.rb +2 -6
- data/lib/mongoid/persistable/incrementable.rb +1 -3
- data/lib/mongoid/persistable/logical.rb +3 -5
- data/lib/mongoid/persistable/maxable.rb +2 -4
- data/lib/mongoid/persistable/minable.rb +2 -4
- data/lib/mongoid/persistable/multipliable.rb +1 -3
- data/lib/mongoid/persistable/poppable.rb +2 -4
- data/lib/mongoid/persistable/pullable.rb +3 -5
- data/lib/mongoid/persistable/pushable.rb +5 -7
- data/lib/mongoid/persistable/renamable.rb +1 -3
- data/lib/mongoid/persistable/savable.rb +0 -3
- data/lib/mongoid/persistable/settable.rb +4 -13
- data/lib/mongoid/persistable/unsettable.rb +1 -3
- data/lib/mongoid/persistable/updatable.rb +16 -24
- data/lib/mongoid/persistable/upsertable.rb +8 -10
- data/lib/mongoid/persistable.rb +28 -27
- data/lib/mongoid/persistence_context.rb +34 -76
- 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 +337 -14
- data/lib/mongoid/selectable.rb +1 -3
- data/lib/mongoid/serializable.rb +6 -8
- data/lib/mongoid/shardable.rb +8 -17
- data/lib/mongoid/stateful.rb +5 -10
- data/lib/mongoid/stringified_symbol.rb +5 -9
- data/lib/mongoid/tasks/database.rake +31 -32
- data/lib/mongoid/tasks/database.rb +37 -53
- data/lib/mongoid/tasks/encryption.rb +10 -15
- data/lib/mongoid/threaded/lifecycle.rb +6 -9
- data/lib/mongoid/threaded.rb +132 -69
- data/lib/mongoid/timestamps/created/short.rb +2 -3
- data/lib/mongoid/timestamps/created.rb +1 -2
- data/lib/mongoid/timestamps/short.rb +0 -1
- data/lib/mongoid/timestamps/timeless.rb +8 -13
- data/lib/mongoid/timestamps/updated/short.rb +2 -3
- data/lib/mongoid/timestamps/updated.rb +2 -5
- data/lib/mongoid/timestamps.rb +4 -6
- data/lib/mongoid/touchable.rb +19 -11
- data/lib/mongoid/traversable.rb +3 -2
- data/lib/mongoid/validatable/associated.rb +1 -5
- data/lib/mongoid/validatable/format.rb +0 -2
- data/lib/mongoid/validatable/length.rb +0 -2
- data/lib/mongoid/validatable/localizable.rb +0 -3
- data/lib/mongoid/validatable/macros.rb +0 -2
- data/lib/mongoid/validatable/numericality.rb +24 -3
- data/lib/mongoid/validatable/presence.rb +8 -10
- data/lib/mongoid/validatable/queryable.rb +0 -3
- data/lib/mongoid/validatable/uniqueness.rb +12 -14
- data/lib/mongoid/validatable.rb +15 -20
- data/lib/mongoid/version.rb +1 -1
- data/lib/mongoid/warnings.rb +10 -11
- data/lib/mongoid.rb +30 -34
- data/lib/rails/generators/mongoid/config/config_generator.rb +2 -4
- data/lib/rails/generators/mongoid/config/templates/mongoid.rb +4 -5
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +2 -2
- data/lib/rails/generators/mongoid/model/model_generator.rb +5 -8
- data/lib/rails/generators/mongoid_generator.rb +6 -10
- data/lib/rails/mongoid.rb +1 -3
- data/spec/integration/active_job_spec.rb +1 -2
- data/spec/integration/app_spec.rb +142 -185
- data/spec/integration/associations/belongs_to_spec.rb +30 -28
- data/spec/integration/associations/embedded_dirty_spec.rb +0 -1
- data/spec/integration/associations/embedded_spec.rb +41 -43
- data/spec/integration/associations/embeds_many_spec.rb +46 -29
- data/spec/integration/associations/embeds_one_spec.rb +2 -1
- data/spec/integration/associations/foreign_key_spec.rb +10 -11
- data/spec/integration/associations/foreign_key_spec_models.rb +8 -9
- data/spec/integration/associations/has_and_belongs_to_many_spec.rb +18 -30
- 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 +105 -130
- data/spec/integration/encryption_spec.rb +8 -160
- 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 +0 -21
- data/spec/integration/matcher_operator_spec.rb +16 -26
- data/spec/integration/matcher_spec.rb +31 -32
- data/spec/integration/persistence/collection_options_spec.rb +0 -2
- data/spec/integration/persistence/range_field_spec.rb +24 -2
- data/spec/integration/server_query_spec.rb +43 -37
- data/spec/integration/shardable_spec.rb +10 -11
- data/spec/integration/stringified_symbol_field_spec.rb +28 -43
- data/spec/lite_spec_helper.rb +11 -12
- data/spec/mongoid/association/accessors_spec.rb +197 -285
- data/spec/mongoid/association/auto_save_spec.rb +62 -93
- data/spec/mongoid/association/builders_spec.rb +121 -74
- data/spec/mongoid/association/constrainable_spec.rb +9 -22
- data/spec/mongoid/association/counter_cache_spec.rb +71 -104
- data/spec/mongoid/association/depending_spec.rb +166 -208
- data/spec/mongoid/association/eager_spec.rb +416 -80
- data/spec/mongoid/association/embedded/cyclic_spec.rb +36 -48
- data/spec/mongoid/association/embedded/dirty_spec.rb +15 -20
- data/spec/mongoid/association/embedded/embedded_in/binding_spec.rb +36 -54
- data/spec/mongoid/association/embedded/embedded_in/buildable_spec.rb +8 -15
- data/spec/mongoid/association/embedded/embedded_in/proxy_spec.rb +135 -175
- data/spec/mongoid/association/embedded/embedded_in_spec.rb +25 -127
- data/spec/mongoid/association/embedded/embeds_many/binding_spec.rb +9 -14
- data/spec/mongoid/association/embedded/embeds_many/buildable_spec.rb +38 -51
- data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +1151 -1340
- data/spec/mongoid/association/embedded/embeds_many_models.rb +17 -17
- data/spec/mongoid/association/embedded/embeds_many_query_spec.rb +11 -11
- data/spec/mongoid/association/embedded/embeds_many_spec.rb +25 -138
- data/spec/mongoid/association/embedded/embeds_one/binding_spec.rb +15 -23
- data/spec/mongoid/association/embedded/embeds_one/buildable_spec.rb +18 -26
- data/spec/mongoid/association/embedded/embeds_one/proxy_spec.rb +204 -273
- data/spec/mongoid/association/embedded/embeds_one_dnl_models.rb +0 -1
- data/spec/mongoid/association/embedded/embeds_one_models.rb +0 -1
- data/spec/mongoid/association/embedded/embeds_one_query_spec.rb +5 -6
- data/spec/mongoid/association/embedded/embeds_one_spec.rb +28 -143
- data/spec/mongoid/association/macros_spec.rb +213 -300
- data/spec/mongoid/association/nested/many_spec.rb +51 -73
- data/spec/mongoid/association/nested/one_spec.rb +56 -80
- data/spec/mongoid/association/options_spec.rb +187 -310
- data/spec/mongoid/association/polymorphic_spec.rb +35 -53
- data/spec/mongoid/association/referenced/belongs_to/binding_spec.rb +55 -76
- data/spec/mongoid/association/referenced/belongs_to/buildable_spec.rb +36 -41
- data/spec/mongoid/association/referenced/belongs_to/eager_spec.rb +81 -101
- data/spec/mongoid/association/referenced/belongs_to/proxy_spec.rb +258 -345
- data/spec/mongoid/association/referenced/belongs_to_models.rb +4 -3
- data/spec/mongoid/association/referenced/belongs_to_query_spec.rb +3 -4
- data/spec/mongoid/association/referenced/belongs_to_spec.rb +91 -294
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/binding_spec.rb +42 -56
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/buildable_spec.rb +41 -32
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/eager_spec.rb +36 -42
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_persistence_spec.rb +8 -9
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +771 -1023
- data/spec/mongoid/association/referenced/has_and_belongs_to_many_models.rb +9 -9
- data/spec/mongoid/association/referenced/has_and_belongs_to_many_query_spec.rb +5 -6
- data/spec/mongoid/association/referenced/has_and_belongs_to_many_spec.rb +59 -177
- data/spec/mongoid/association/referenced/has_many/binding_spec.rb +29 -43
- data/spec/mongoid/association/referenced/has_many/buildable_spec.rb +53 -53
- data/spec/mongoid/association/referenced/has_many/eager_spec.rb +104 -67
- data/spec/mongoid/association/referenced/has_many/enumerable_spec.rb +431 -675
- data/spec/mongoid/association/referenced/has_many/proxy_query_spec.rb +1 -2
- data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +173 -156
- data/spec/mongoid/association/referenced/has_many_models.rb +1 -2
- data/spec/mongoid/association/referenced/has_many_query_spec.rb +3 -4
- data/spec/mongoid/association/referenced/has_many_spec.rb +44 -188
- data/spec/mongoid/association/referenced/has_one/binding_spec.rb +26 -38
- data/spec/mongoid/association/referenced/has_one/buildable_spec.rb +28 -29
- data/spec/mongoid/association/referenced/has_one/eager_spec.rb +29 -42
- data/spec/mongoid/association/referenced/has_one/proxy_spec.rb +270 -332
- data/spec/mongoid/association/referenced/has_one_models.rb +1 -2
- data/spec/mongoid/association/referenced/has_one_query_spec.rb +3 -4
- data/spec/mongoid/association/referenced/has_one_spec.rb +40 -188
- data/spec/mongoid/association/reflections_spec.rb +14 -24
- data/spec/mongoid/association/syncable_spec.rb +102 -125
- data/spec/mongoid/association_spec.rb +47 -70
- data/spec/mongoid/atomic/modifiers_spec.rb +184 -197
- data/spec/mongoid/atomic/paths/embedded/many_spec.rb +39 -52
- data/spec/mongoid/atomic/paths/embedded/one_spec.rb +35 -49
- data/spec/mongoid/atomic/paths/root_spec.rb +9 -15
- data/spec/mongoid/atomic/paths_spec.rb +81 -117
- data/spec/mongoid/atomic_spec.rb +156 -171
- data/spec/mongoid/attributes/dynamic_spec.rb +5 -6
- data/spec/mongoid/attributes/nested_spec.rb +1221 -1888
- data/spec/mongoid/attributes/nested_spec_models.rb +8 -9
- data/spec/mongoid/attributes/projector_spec.rb +2 -6
- data/spec/mongoid/attributes/readonly_spec.rb +73 -97
- data/spec/mongoid/attributes_spec.rb +669 -826
- data/spec/mongoid/cacheable_spec.rb +74 -44
- data/spec/mongoid/changeable_spec.rb +591 -733
- data/spec/mongoid/clients/factory_spec.rb +122 -154
- data/spec/mongoid/clients/options_spec.rb +40 -59
- data/spec/mongoid/clients/sessions_spec.rb +25 -44
- data/spec/mongoid/clients/transactions_spec.rb +33 -50
- data/spec/mongoid/clients/transactions_spec_models.rb +3 -4
- data/spec/mongoid/clients_spec.rb +265 -355
- data/spec/mongoid/collection_configurable_spec.rb +9 -12
- data/spec/mongoid/composable_spec.rb +2 -9
- data/spec/mongoid/config/defaults_spec.rb +24 -49
- data/spec/mongoid/config/encryption_spec.rb +53 -284
- data/spec/mongoid/config/environment_spec.rb +30 -40
- data/spec/mongoid/config/introspection_spec.rb +42 -37
- data/spec/mongoid/config/options_spec.rb +14 -21
- data/spec/mongoid/config_spec.rb +145 -195
- data/spec/mongoid/contextual/aggregable/memory_spec.rb +98 -226
- data/spec/mongoid/contextual/aggregable/memory_table_spec.rb +4 -7
- data/spec/mongoid/contextual/aggregable/mongo_spec.rb +149 -187
- data/spec/mongoid/contextual/aggregable/none_spec.rb +14 -16
- data/spec/mongoid/contextual/atomic_spec.rb +223 -271
- data/spec/mongoid/contextual/map_reduce_spec.rb +110 -138
- data/spec/mongoid/contextual/memory_spec.rb +706 -1031
- data/spec/mongoid/contextual/mongo/documents_loader_spec.rb +12 -13
- data/spec/mongoid/contextual/mongo_spec.rb +1008 -1213
- data/spec/mongoid/contextual/none_spec.rb +54 -59
- data/spec/mongoid/contextual/short_circuit_spec.rb +139 -0
- data/spec/mongoid/copyable_spec.rb +254 -300
- data/spec/mongoid/copyable_spec_models.rb +0 -1
- data/spec/mongoid/criteria/findable_spec.rb +322 -387
- data/spec/mongoid/criteria/includable_spec.rb +421 -372
- data/spec/mongoid/criteria/includable_spec_models.rb +16 -7
- data/spec/mongoid/criteria/inspectable_spec.rb +11 -14
- data/spec/mongoid/criteria/marshalable_spec.rb +11 -14
- data/spec/mongoid/criteria/modifiable_spec.rb +366 -512
- data/spec/mongoid/criteria/options_spec.rb +2 -7
- data/spec/mongoid/criteria/queryable/aggregable_spec.rb +112 -141
- data/spec/mongoid/criteria/queryable/expandable_spec.rb +8 -11
- data/spec/mongoid/criteria/queryable/extensions/array_spec.rb +127 -184
- data/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb +123 -60
- data/spec/mongoid/criteria/queryable/extensions/boolean_spec.rb +84 -117
- data/spec/mongoid/criteria/queryable/extensions/date_spec.rb +44 -65
- data/spec/mongoid/criteria/queryable/extensions/date_time_spec.rb +86 -108
- data/spec/mongoid/criteria/queryable/extensions/float_spec.rb +94 -33
- data/spec/mongoid/criteria/queryable/extensions/hash_spec.rb +103 -138
- data/spec/mongoid/criteria/queryable/extensions/integer_spec.rb +95 -34
- data/spec/mongoid/criteria/queryable/extensions/nil_class_spec.rb +25 -38
- data/spec/mongoid/criteria/queryable/extensions/numeric_spec.rb +26 -28
- data/spec/mongoid/criteria/queryable/extensions/object_spec.rb +32 -54
- data/spec/mongoid/criteria/queryable/extensions/range_spec.rb +136 -130
- data/spec/mongoid/criteria/queryable/extensions/regexp_raw_spec.rb +16 -24
- data/spec/mongoid/criteria/queryable/extensions/regexp_spec.rb +15 -24
- data/spec/mongoid/criteria/queryable/extensions/set_spec.rb +12 -19
- data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +46 -63
- data/spec/mongoid/criteria/queryable/extensions/symbol_spec.rb +28 -42
- data/spec/mongoid/criteria/queryable/extensions/time_spec.rb +73 -96
- data/spec/mongoid/criteria/queryable/extensions/time_with_zone_spec.rb +71 -92
- data/spec/mongoid/criteria/queryable/key_spec.rb +32 -36
- data/spec/mongoid/criteria/queryable/mergeable_spec.rb +44 -49
- data/spec/mongoid/criteria/queryable/optional_spec.rb +373 -486
- data/spec/mongoid/criteria/queryable/options_spec.rb +172 -123
- data/spec/mongoid/criteria/queryable/pipeline_spec.rb +65 -87
- data/spec/mongoid/criteria/queryable/queryable_spec.rb +41 -53
- data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +576 -682
- data/spec/mongoid/criteria/queryable/selectable_shared_examples.rb +7 -11
- data/spec/mongoid/criteria/queryable/selectable_spec.rb +807 -998
- data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +154 -391
- data/spec/mongoid/criteria/queryable/selector_spec.rb +294 -390
- data/spec/mongoid/criteria/queryable/smash_spec.rb +6 -11
- data/spec/mongoid/criteria/queryable/storable_spec.rb +54 -81
- data/spec/mongoid/criteria/queryable_mql_spec.rb +92 -0
- data/spec/mongoid/criteria/scopable_spec.rb +102 -138
- data/spec/mongoid/criteria/translator_spec.rb +59 -60
- data/spec/mongoid/criteria_projection_spec.rb +81 -99
- data/spec/mongoid/criteria_spec.rb +935 -952
- data/spec/mongoid/document_fields_spec.rb +28 -30
- data/spec/mongoid/document_persistence_context_spec.rb +13 -15
- data/spec/mongoid/document_query_spec.rb +9 -12
- data/spec/mongoid/document_spec.rb +317 -409
- data/spec/mongoid/equality_spec.rb +69 -102
- data/spec/mongoid/errors/ambiguous_relationship_spec.rb +8 -11
- data/spec/mongoid/errors/attribute_not_loaded_spec.rb +7 -10
- data/spec/mongoid/errors/callback_spec.rb +8 -11
- data/spec/mongoid/errors/delete_restriction_spec.rb +5 -8
- data/spec/mongoid/errors/document_not_destroyed_spec.rb +7 -10
- data/spec/mongoid/errors/document_not_found_spec.rb +51 -61
- data/spec/mongoid/errors/invalid_collection_spec.rb +9 -13
- data/spec/mongoid/errors/invalid_config_file_spec.rb +8 -11
- data/spec/mongoid/errors/invalid_config_option_spec.rb +8 -11
- data/spec/mongoid/errors/invalid_field_option_spec.rb +7 -10
- data/spec/mongoid/errors/invalid_field_spec.rb +10 -11
- data/spec/mongoid/errors/invalid_field_type_spec.rb +9 -12
- data/spec/mongoid/errors/invalid_find_spec.rb +8 -11
- data/spec/mongoid/errors/invalid_includes_spec.rb +9 -13
- data/spec/mongoid/errors/invalid_index_spec.rb +8 -11
- data/spec/mongoid/errors/invalid_options_spec.rb +8 -11
- data/spec/mongoid/errors/invalid_path_spec.rb +7 -10
- data/spec/mongoid/errors/invalid_relation_spec.rb +15 -16
- data/spec/mongoid/errors/invalid_scope_spec.rb +8 -11
- data/spec/mongoid/errors/invalid_set_polymorphic_relation_spec.rb +3 -6
- data/spec/mongoid/errors/invalid_storage_options_spec.rb +8 -11
- data/spec/mongoid/errors/invalid_time_spec.rb +8 -11
- data/spec/mongoid/errors/inverse_not_found_spec.rb +9 -12
- data/spec/mongoid/errors/mixed_client_configuration_spec.rb +9 -12
- data/spec/mongoid/errors/mixed_relations_spec.rb +8 -11
- data/spec/mongoid/errors/mongoid_error_spec.rb +21 -24
- data/spec/mongoid/errors/nested_attributes_metadata_not_found_spec.rb +6 -9
- data/spec/mongoid/errors/no_client_config_spec.rb +7 -10
- data/spec/mongoid/errors/no_client_database_spec.rb +9 -12
- data/spec/mongoid/errors/no_client_hosts_spec.rb +9 -12
- data/spec/mongoid/errors/no_clients_config_spec.rb +7 -10
- data/spec/mongoid/errors/no_environment_spec.rb +8 -11
- data/spec/mongoid/errors/no_map_reduce_output_spec.rb +8 -11
- data/spec/mongoid/errors/no_metadata_spec.rb +7 -10
- data/spec/mongoid/errors/no_parent_spec.rb +7 -10
- data/spec/mongoid/errors/readonly_attribute_spec.rb +7 -10
- data/spec/mongoid/errors/readonly_document_spec.rb +6 -9
- data/spec/mongoid/errors/scope_overwrite_spec.rb +9 -12
- data/spec/mongoid/errors/too_many_nested_attribute_records_spec.rb +9 -12
- data/spec/mongoid/errors/unknown_attribute_spec.rb +5 -8
- data/spec/mongoid/errors/unsaved_document_spec.rb +8 -11
- data/spec/mongoid/errors/unsupported_javascript_spec.rb +9 -12
- data/spec/mongoid/errors/validations_spec.rb +11 -14
- data/spec/mongoid/extensions/array_spec.rb +70 -92
- data/spec/mongoid/extensions/big_decimal_spec.rb +233 -313
- data/spec/mongoid/extensions/binary_spec.rb +21 -32
- data/spec/mongoid/extensions/boolean_spec.rb +52 -75
- data/spec/mongoid/extensions/date_class_mongoize_spec.rb +43 -54
- data/spec/mongoid/extensions/date_spec.rb +26 -38
- data/spec/mongoid/extensions/date_time_spec.rb +22 -32
- data/spec/mongoid/extensions/decimal128_spec.rb +12 -18
- data/spec/mongoid/extensions/false_class_spec.rb +10 -17
- data/spec/mongoid/extensions/float_spec.rb +40 -61
- data/spec/mongoid/extensions/hash_spec.rb +46 -64
- data/spec/mongoid/extensions/integer_spec.rb +40 -61
- data/spec/mongoid/extensions/module_spec.rb +13 -18
- data/spec/mongoid/extensions/nil_class_spec.rb +2 -5
- data/spec/mongoid/extensions/object_id_spec.rb +159 -247
- data/spec/mongoid/extensions/object_spec.rb +31 -47
- data/spec/mongoid/extensions/range_spec.rb +112 -115
- data/spec/mongoid/extensions/raw_value_spec.rb +10 -12
- data/spec/mongoid/extensions/regexp_spec.rb +23 -36
- data/spec/mongoid/extensions/set_spec.rb +38 -49
- data/spec/mongoid/extensions/string_spec.rb +118 -167
- data/spec/mongoid/extensions/stringified_symbol_spec.rb +29 -43
- data/spec/mongoid/extensions/symbol_spec.rb +12 -20
- data/spec/mongoid/extensions/time_spec.rb +160 -202
- data/spec/mongoid/extensions/time_with_zone_spec.rb +79 -105
- data/spec/mongoid/extensions/true_class_spec.rb +10 -17
- data/spec/mongoid/factory_spec.rb +125 -148
- data/spec/mongoid/fields/foreign_key_spec.rb +124 -169
- data/spec/mongoid/fields/localized_spec.rb +140 -185
- data/spec/mongoid/fields/standard_spec.rb +43 -59
- data/spec/mongoid/fields_spec.rb +751 -620
- data/spec/mongoid/findable_spec.rb +260 -228
- data/spec/mongoid/indexable/specification_spec.rb +42 -58
- data/spec/mongoid/indexable_spec.rb +260 -264
- data/spec/mongoid/inspectable_spec.rb +45 -55
- data/spec/mongoid/interceptable_spec.rb +784 -922
- data/spec/mongoid/interceptable_spec_models.rb +35 -35
- data/spec/mongoid/loading_spec.rb +47 -43
- data/spec/mongoid/loggable_spec.rb +3 -6
- data/spec/mongoid/matcher/extract_attribute_spec.rb +2 -6
- data/spec/mongoid/mongoizable_spec.rb +81 -90
- data/spec/mongoid/monkey_patches_spec.rb +4 -0
- data/spec/mongoid/persistable/creatable_spec.rb +181 -220
- data/spec/mongoid/persistable/deletable_spec.rb +139 -179
- data/spec/mongoid/persistable/destroyable_spec.rb +135 -164
- data/spec/mongoid/persistable/incrementable_spec.rb +65 -84
- data/spec/mongoid/persistable/logical_spec.rb +48 -61
- data/spec/mongoid/persistable/maxable_spec.rb +45 -44
- data/spec/mongoid/persistable/minable_spec.rb +45 -44
- data/spec/mongoid/persistable/multipliable_spec.rb +54 -69
- data/spec/mongoid/persistable/poppable_spec.rb +47 -60
- data/spec/mongoid/persistable/pullable_spec.rb +79 -103
- data/spec/mongoid/persistable/pushable_spec.rb +106 -131
- data/spec/mongoid/persistable/renamable_spec.rb +52 -65
- data/spec/mongoid/persistable/savable_spec.rb +152 -189
- data/spec/mongoid/persistable/settable_spec.rb +121 -145
- data/spec/mongoid/persistable/unsettable_spec.rb +56 -73
- data/spec/mongoid/persistable/updatable_spec.rb +185 -232
- data/spec/mongoid/persistable/upsertable_spec.rb +66 -77
- data/spec/mongoid/persistable_spec.rb +132 -139
- data/spec/mongoid/persistence_context_spec.rb +31 -89
- data/spec/mongoid/positional_spec.rb +67 -79
- data/spec/mongoid/railties/bson_object_id_serializer_spec.rb +0 -3
- data/spec/mongoid/railties/console_sandbox_spec.rb +0 -1
- data/spec/mongoid/relations/proxy_spec.rb +0 -1
- data/spec/mongoid/reloadable_spec.rb +250 -145
- data/spec/mongoid/scopable_spec.rb +319 -398
- data/spec/mongoid/search_indexable_spec.rb +446 -41
- data/spec/mongoid/selectable_spec.rb +36 -51
- data/spec/mongoid/serializable_spec.rb +269 -312
- data/spec/mongoid/shardable_models.rb +5 -5
- data/spec/mongoid/shardable_spec.rb +44 -46
- data/spec/mongoid/stateful_spec.rb +55 -75
- data/spec/mongoid/tasks/database_rake_spec.rb +138 -141
- data/spec/mongoid/tasks/database_spec.rb +80 -98
- data/spec/mongoid/tasks/encryption_spec.rb +22 -20
- data/spec/mongoid/threaded_spec.rb +57 -83
- data/spec/mongoid/timestamps/created/short_spec.rb +12 -16
- data/spec/mongoid/timestamps/created_spec.rb +16 -20
- data/spec/mongoid/timestamps/timeless_spec.rb +31 -43
- data/spec/mongoid/timestamps/updated/short_spec.rb +21 -28
- data/spec/mongoid/timestamps/updated_spec.rb +18 -25
- data/spec/mongoid/timestamps_spec.rb +171 -188
- data/spec/mongoid/timestamps_spec_models.rb +12 -13
- data/spec/mongoid/touchable_spec.rb +330 -294
- data/spec/mongoid/touchable_spec_models.rb +35 -37
- data/spec/mongoid/traversable_spec.rb +435 -471
- data/spec/mongoid/validatable/associated_spec.rb +42 -60
- data/spec/mongoid/validatable/format_spec.rb +24 -34
- data/spec/mongoid/validatable/length_spec.rb +62 -86
- data/spec/mongoid/validatable/numericality_spec.rb +114 -23
- data/spec/mongoid/validatable/presence_spec.rb +149 -203
- data/spec/mongoid/validatable/uniqueness_spec.rb +632 -830
- data/spec/mongoid/validatable_spec.rb +71 -103
- data/spec/mongoid/warnings_spec.rb +5 -9
- data/spec/mongoid_spec.rb +25 -41
- data/spec/rails/controller_extension/controller_runtime_spec.rb +45 -49
- data/spec/rails/mongoid_spec.rb +15 -15
- data/spec/shared/lib/mrss/cluster_config.rb +5 -40
- data/spec/shared/lib/mrss/constraints.rb +7 -46
- data/spec/shared/lib/mrss/docker_runner.rb +10 -21
- data/spec/shared/lib/mrss/eg_config_utils.rb +0 -31
- data/spec/shared/lib/mrss/release/candidate.rb +18 -12
- data/spec/shared/lib/mrss/server_version_registry.rb +1 -5
- data/spec/shared/share/Dockerfile.erb +6 -8
- data/spec/shared/shlib/server.sh +0 -50
- data/spec/shared/shlib/set_env.sh +4 -10
- data/spec/spec_helper.rb +54 -31
- data/spec/support/authorization.rb +0 -1
- data/spec/support/client_registry.rb +1 -2
- data/spec/support/constraints.rb +8 -25
- data/spec/support/crypt/models.rb +7 -165
- data/spec/support/crypt.rb +6 -9
- data/spec/support/expectations.rb +21 -7
- data/spec/support/feature_sandbox.rb +1 -4
- data/spec/support/helpers.rb +0 -1
- data/spec/support/immutable_ids.rb +20 -21
- data/spec/support/macros.rb +1 -3
- data/spec/support/models/account.rb +4 -5
- data/spec/support/models/acolyte.rb +5 -5
- data/spec/support/models/actor.rb +3 -3
- data/spec/support/models/actress.rb +0 -1
- data/spec/support/models/address.rb +11 -11
- data/spec/support/models/address_component.rb +1 -1
- data/spec/support/models/address_number.rb +1 -1
- data/spec/support/models/agency.rb +1 -1
- data/spec/support/models/agent.rb +1 -1
- data/spec/support/models/album.rb +1 -2
- data/spec/support/models/alert.rb +1 -1
- data/spec/support/models/animal.rb +4 -5
- data/spec/support/models/answer.rb +1 -1
- data/spec/support/models/appointment.rb +2 -2
- data/spec/support/models/armrest.rb +0 -1
- data/spec/support/models/array_field.rb +0 -1
- data/spec/support/models/article.rb +0 -1
- data/spec/support/models/artist.rb +16 -12
- data/spec/support/models/artwork.rb +1 -1
- data/spec/support/models/audible_sound.rb +0 -1
- data/spec/support/models/audio.rb +2 -2
- data/spec/support/models/augmentation.rb +1 -2
- data/spec/support/models/author.rb +1 -1
- data/spec/support/models/baby.rb +1 -1
- data/spec/support/models/band.rb +0 -1
- data/spec/support/models/bar.rb +2 -2
- data/spec/support/models/basic.rb +0 -1
- data/spec/support/models/bed.rb +0 -1
- data/spec/support/models/big_palette.rb +0 -1
- data/spec/support/models/birthday.rb +3 -5
- data/spec/support/models/bolt.rb +0 -1
- data/spec/support/models/bomb.rb +1 -1
- data/spec/support/models/book.rb +1 -1
- data/spec/support/models/breed.rb +1 -1
- data/spec/support/models/browser.rb +1 -2
- data/spec/support/models/building.rb +0 -1
- data/spec/support/models/building_address.rb +1 -1
- data/spec/support/models/bus.rb +1 -1
- data/spec/support/models/business.rb +2 -2
- data/spec/support/models/callback_test.rb +1 -1
- data/spec/support/models/canvas.rb +5 -6
- data/spec/support/models/car.rb +0 -1
- data/spec/support/models/cat.rb +0 -2
- data/spec/support/models/catalog.rb +0 -1
- data/spec/support/models/category.rb +1 -1
- data/spec/support/models/child.rb +1 -1
- data/spec/support/models/child_doc.rb +5 -6
- data/spec/support/models/church.rb +1 -1
- data/spec/support/models/circle.rb +0 -1
- data/spec/support/models/circuit.rb +2 -2
- data/spec/support/models/circus.rb +0 -1
- data/spec/support/models/code.rb +2 -1
- data/spec/support/models/coding/pull_request.rb +0 -1
- data/spec/support/models/coding.rb +0 -1
- data/spec/support/models/comment.rb +0 -1
- data/spec/support/models/company.rb +0 -1
- data/spec/support/models/consumption_period.rb +0 -1
- data/spec/support/models/contextable_item.rb +1 -1
- data/spec/support/models/contractor.rb +1 -1
- data/spec/support/models/cookie.rb +0 -1
- data/spec/support/models/country_code.rb +2 -3
- data/spec/support/models/courier_job.rb +2 -2
- data/spec/support/models/cover.rb +0 -1
- data/spec/support/models/crate.rb +0 -1
- data/spec/support/models/customer.rb +0 -1
- data/spec/support/models/customer_address.rb +0 -1
- data/spec/support/models/deed.rb +1 -1
- data/spec/support/models/definition.rb +1 -1
- data/spec/support/models/delegating_patient.rb +0 -1
- data/spec/support/models/description.rb +0 -1
- data/spec/support/models/dictionary.rb +1 -1
- data/spec/support/models/division.rb +2 -2
- data/spec/support/models/doctor.rb +0 -1
- data/spec/support/models/dog.rb +2 -2
- data/spec/support/models/dokument.rb +1 -1
- data/spec/support/models/draft.rb +0 -1
- data/spec/support/models/dragon.rb +1 -1
- data/spec/support/models/driver.rb +2 -2
- data/spec/support/models/drug.rb +1 -1
- data/spec/support/models/dungeon.rb +1 -1
- data/spec/support/models/edit.rb +1 -1
- data/spec/support/models/email.rb +1 -1
- data/spec/support/models/employer.rb +1 -2
- data/spec/support/models/entry.rb +1 -1
- data/spec/support/models/eraser.rb +0 -1
- data/spec/support/models/even.rb +1 -1
- data/spec/support/models/event.rb +3 -4
- data/spec/support/models/exhibition.rb +1 -1
- data/spec/support/models/exhibitor.rb +1 -1
- data/spec/support/models/explosion.rb +1 -1
- data/spec/support/models/eye.rb +0 -1
- data/spec/support/models/eye_bowl.rb +2 -3
- data/spec/support/models/face.rb +2 -3
- data/spec/support/models/fanatic.rb +1 -1
- data/spec/support/models/favorite.rb +1 -1
- data/spec/support/models/filesystem.rb +1 -1
- data/spec/support/models/fire_hydrant.rb +1 -1
- data/spec/support/models/firefox.rb +0 -1
- data/spec/support/models/fish.rb +0 -1
- data/spec/support/models/folder.rb +0 -2
- data/spec/support/models/folder_item.rb +1 -3
- data/spec/support/models/fruits.rb +9 -5
- data/spec/support/models/game.rb +3 -4
- data/spec/support/models/ghost.rb +0 -1
- data/spec/support/models/guitar.rb +0 -1
- data/spec/support/models/hole.rb +0 -1
- data/spec/support/models/home.rb +1 -1
- data/spec/support/models/house.rb +2 -2
- data/spec/support/models/html_writer.rb +0 -1
- data/spec/support/models/id_key.rb +3 -3
- data/spec/support/models/idnodef.rb +0 -1
- data/spec/support/models/image.rb +0 -1
- data/spec/support/models/implant.rb +1 -2
- data/spec/support/models/instrument.rb +2 -3
- data/spec/support/models/item.rb +2 -2
- data/spec/support/models/jar.rb +1 -2
- data/spec/support/models/kaleidoscope.rb +1 -1
- data/spec/support/models/kangaroo.rb +1 -1
- data/spec/support/models/label.rb +2 -2
- data/spec/support/models/language.rb +1 -1
- data/spec/support/models/lat_lng.rb +0 -1
- data/spec/support/models/league.rb +1 -1
- data/spec/support/models/learner.rb +0 -1
- data/spec/support/models/line_item.rb +1 -1
- data/spec/support/models/location.rb +1 -1
- data/spec/support/models/login.rb +1 -2
- data/spec/support/models/manufacturer.rb +0 -1
- data/spec/support/models/meat.rb +1 -1
- data/spec/support/models/membership.rb +1 -1
- data/spec/support/models/message.rb +1 -2
- data/spec/support/models/minim.rb +0 -1
- data/spec/support/models/mixed_drink.rb +1 -1
- data/spec/support/models/modifier_models.rb +25 -0
- data/spec/support/models/mop.rb +0 -1
- data/spec/support/models/movie.rb +1 -1
- data/spec/support/models/my_hash.rb +1 -2
- data/spec/support/models/name.rb +4 -5
- data/spec/support/models/name_only.rb +0 -1
- data/spec/support/models/node.rb +1 -1
- data/spec/support/models/note.rb +3 -3
- data/spec/support/models/nut.rb +0 -1
- data/spec/support/models/odd.rb +1 -1
- data/spec/support/models/order.rb +1 -1
- data/spec/support/models/ordered_post.rb +1 -1
- data/spec/support/models/ordered_preference.rb +1 -1
- data/spec/support/models/oscar.rb +1 -1
- data/spec/support/models/other_owner_object.rb +0 -1
- data/spec/support/models/override.rb +3 -11
- data/spec/support/models/ownable.rb +1 -1
- data/spec/support/models/owner.rb +1 -1
- data/spec/support/models/pack.rb +0 -1
- data/spec/support/models/page.rb +3 -3
- data/spec/support/models/page_question.rb +1 -1
- data/spec/support/models/palette.rb +2 -2
- data/spec/support/models/parent.rb +2 -2
- data/spec/support/models/parent_doc.rb +1 -1
- data/spec/support/models/passport.rb +0 -1
- data/spec/support/models/patient.rb +2 -2
- data/spec/support/models/pdf_writer.rb +0 -1
- data/spec/support/models/pencil.rb +0 -1
- data/spec/support/models/person.rb +32 -28
- data/spec/support/models/pet.rb +1 -1
- data/spec/support/models/pet_owner.rb +1 -1
- data/spec/support/models/phone.rb +1 -2
- data/spec/support/models/piano.rb +0 -1
- data/spec/support/models/pizza.rb +1 -1
- data/spec/support/models/player.rb +5 -5
- data/spec/support/models/post.rb +12 -12
- data/spec/support/models/post_genre.rb +1 -1
- data/spec/support/models/powerup.rb +1 -2
- data/spec/support/models/preference.rb +2 -2
- data/spec/support/models/princess.rb +1 -1
- data/spec/support/models/product.rb +3 -3
- data/spec/support/models/profile.rb +2 -1
- data/spec/support/models/pronunciation.rb +1 -1
- data/spec/support/models/pub.rb +2 -2
- data/spec/support/models/publication/encyclopedia.rb +0 -1
- data/spec/support/models/publication/review.rb +0 -1
- data/spec/support/models/publication.rb +0 -1
- data/spec/support/models/purchase.rb +1 -1
- data/spec/support/models/purchased_item.rb +1 -1
- data/spec/support/models/question.rb +2 -2
- data/spec/support/models/quiz.rb +1 -1
- data/spec/support/models/rating.rb +1 -1
- data/spec/support/models/record.rb +2 -3
- data/spec/support/models/registry.rb +1 -1
- data/spec/support/models/role.rb +1 -1
- data/spec/support/models/root_category.rb +1 -1
- data/spec/support/models/sandbox/app/models/app_models_message.rb +0 -1
- data/spec/support/models/sandbox/lib/models/lib_models_message.rb +0 -1
- data/spec/support/models/sandbox/sandbox_message.rb +0 -1
- data/spec/support/models/sandbox/sandbox_user.rb +0 -1
- data/spec/support/models/sandbox/subdir/sandbox_comment.rb +0 -1
- data/spec/support/models/sandwich.rb +1 -1
- data/spec/support/models/scheduler.rb +0 -1
- data/spec/support/models/school.rb +0 -1
- data/spec/support/models/scribe.rb +1 -1
- data/spec/support/models/sealer.rb +0 -1
- data/spec/support/models/seat.rb +0 -1
- data/spec/support/models/seo.rb +1 -1
- data/spec/support/models/series.rb +1 -1
- data/spec/support/models/server.rb +1 -1
- data/spec/support/models/service.rb +2 -2
- data/spec/support/models/shape.rb +3 -3
- data/spec/support/models/shelf.rb +1 -1
- data/spec/support/models/shield.rb +2 -2
- data/spec/support/models/shipment_address.rb +0 -1
- data/spec/support/models/shipping_container.rb +1 -1
- data/spec/support/models/shipping_pack.rb +0 -1
- data/spec/support/models/shirt.rb +0 -1
- data/spec/support/models/shop.rb +2 -2
- data/spec/support/models/short_agent.rb +0 -1
- data/spec/support/models/short_quiz.rb +1 -1
- data/spec/support/models/simple.rb +1 -1
- data/spec/support/models/slave.rb +1 -1
- data/spec/support/models/song.rb +1 -2
- data/spec/support/models/sound.rb +2 -2
- data/spec/support/models/spacer.rb +0 -1
- data/spec/support/models/square.rb +0 -1
- data/spec/support/models/staff.rb +0 -1
- data/spec/support/models/store_as_dup_test1.rb +2 -2
- data/spec/support/models/store_as_dup_test2.rb +1 -1
- data/spec/support/models/store_as_dup_test3.rb +2 -2
- data/spec/support/models/store_as_dup_test4.rb +1 -1
- data/spec/support/models/strategy.rb +0 -1
- data/spec/support/models/student.rb +1 -2
- data/spec/support/models/sub_item.rb +0 -1
- data/spec/support/models/subscription.rb +2 -2
- data/spec/support/models/survey.rb +2 -2
- data/spec/support/models/symptom.rb +2 -2
- data/spec/support/models/system_role.rb +2 -3
- data/spec/support/models/tag.rb +2 -2
- data/spec/support/models/target.rb +1 -1
- data/spec/support/models/template.rb +1 -1
- data/spec/support/models/thing.rb +1 -1
- data/spec/support/models/threadlocker.rb +0 -1
- data/spec/support/models/title.rb +0 -1
- data/spec/support/models/tool.rb +3 -3
- data/spec/support/models/topping.rb +1 -1
- data/spec/support/models/toy.rb +0 -1
- data/spec/support/models/track.rb +1 -1
- data/spec/support/models/translation.rb +1 -1
- data/spec/support/models/tree.rb +2 -3
- data/spec/support/models/truck.rb +0 -1
- data/spec/support/models/updatable.rb +0 -1
- data/spec/support/models/user.rb +3 -4
- data/spec/support/models/user_account.rb +3 -3
- data/spec/support/models/validation_callback.rb +4 -4
- data/spec/support/models/vehicle.rb +2 -3
- data/spec/support/models/version.rb +1 -1
- data/spec/support/models/vertex.rb +0 -1
- data/spec/support/models/vet_visit.rb +1 -1
- data/spec/support/models/video.rb +2 -2
- data/spec/support/models/video_game.rb +0 -1
- data/spec/support/models/washer.rb +0 -1
- data/spec/support/models/weapon.rb +1 -2
- data/spec/support/models/wiki_page.rb +2 -3
- data/spec/support/models/word.rb +2 -2
- data/spec/support/models/word_origin.rb +1 -2
- data/spec/support/models/writer.rb +3 -3
- data/spec/support/rails_mock.rb +7 -12
- data/spec/support/shared/time.rb +0 -1
- data/spec/support/sinatra_mock.rb +4 -2
- data/spec/support/spec_config.rb +19 -18
- metadata +14 -14
- data/lib/mongoid/errors/in_memory_regexp_timeout.rb +0 -26
- data/lib/mongoid/errors/no_encryption_schema.rb +0 -28
- data/lib/mongoid/field_readable.rb +0 -70
- data/lib/mongoid/matcher/regexp_budget.rb +0 -383
- data/spec/integration/matcher_regexp_timeout_spec.rb +0 -215
- data/spec/integration/query_operator_guard_spec.rb +0 -89
- data/spec/mongoid/encryptable_spec.rb +0 -61
- data/spec/mongoid/matcher/regexp_budget_spec.rb +0 -570
|
@@ -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,61 +2161,53 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
2267
2161
|
end
|
|
2268
2162
|
end
|
|
2269
2163
|
|
|
2270
|
-
# Whether the conditions carry a pattern decides which branch of remove_all
|
|
2271
|
-
# runs, so anything that has to behave the same on both is exercised twice.
|
|
2272
|
-
conditions_variants = [
|
|
2273
|
-
[ 'no regular expression', nil ],
|
|
2274
|
-
[ 'a regular expression', { name: /Test/ } ]
|
|
2275
|
-
].freeze
|
|
2276
|
-
|
|
2277
2164
|
%i[ delete_all destroy_all ].each do |method|
|
|
2278
|
-
describe "
|
|
2165
|
+
describe "##{method}" do
|
|
2279
2166
|
context 'when the relation is not polymorphic' do
|
|
2280
2167
|
context 'when conditions are provided' do
|
|
2281
2168
|
let(:person) { Person.create! }
|
|
2282
2169
|
|
|
2283
2170
|
let!(:preference_one) do
|
|
2284
|
-
person.preferences.create!(name:
|
|
2171
|
+
person.preferences.create!(name: 'Testing')
|
|
2285
2172
|
end
|
|
2286
2173
|
|
|
2287
2174
|
let!(:preference_two) do
|
|
2288
|
-
person.preferences.create!(name:
|
|
2175
|
+
person.preferences.create!(name: 'Test')
|
|
2289
2176
|
end
|
|
2290
2177
|
|
|
2291
2178
|
let!(:deleted) do
|
|
2292
2179
|
person.preferences.send(
|
|
2293
|
-
|
|
2294
|
-
|
|
2180
|
+
method,
|
|
2181
|
+
{ name: 'Testing' }
|
|
2295
2182
|
)
|
|
2296
2183
|
end
|
|
2297
2184
|
|
|
2298
|
-
it
|
|
2185
|
+
it 'removes the correct preferences' do
|
|
2299
2186
|
expect(person.preferences.count).to eq(1)
|
|
2300
2187
|
end
|
|
2301
2188
|
|
|
2302
|
-
it
|
|
2303
|
-
expect(Preference.where(name:
|
|
2189
|
+
it 'deletes the documents from the database' do
|
|
2190
|
+
expect(Preference.where(name: 'Testing').count).to eq(0)
|
|
2304
2191
|
end
|
|
2305
2192
|
|
|
2306
|
-
it
|
|
2193
|
+
it 'returns the number of documents deleted' do
|
|
2307
2194
|
expect(deleted).to eq(1)
|
|
2308
2195
|
end
|
|
2309
2196
|
|
|
2310
|
-
it
|
|
2197
|
+
it 'removes the ids from the foreign key' do
|
|
2311
2198
|
expect(person.preference_ids).to eq([ preference_two.id ])
|
|
2312
2199
|
end
|
|
2313
2200
|
|
|
2314
|
-
it
|
|
2315
|
-
expect(person.preferences).to eq([preference_two])
|
|
2201
|
+
it 'sets the association locally' do
|
|
2202
|
+
expect(person.preferences).to eq([ preference_two ])
|
|
2316
2203
|
end
|
|
2317
2204
|
end
|
|
2318
2205
|
|
|
2319
|
-
context
|
|
2320
|
-
|
|
2206
|
+
context 'when conditions are not provided' do
|
|
2321
2207
|
let(:person) do
|
|
2322
2208
|
Person.create!.tap do |person|
|
|
2323
|
-
person.preferences.create!(name:
|
|
2324
|
-
person.preferences.create!(name:
|
|
2209
|
+
person.preferences.create!(name: 'Testing')
|
|
2210
|
+
person.preferences.create!(name: 'Test')
|
|
2325
2211
|
end
|
|
2326
2212
|
end
|
|
2327
2213
|
|
|
@@ -2329,227 +2215,176 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
2329
2215
|
person.preferences.send(method)
|
|
2330
2216
|
end
|
|
2331
2217
|
|
|
2332
|
-
it
|
|
2218
|
+
it 'removes the correct preferences' do
|
|
2333
2219
|
expect(person.preferences.count).to eq(0)
|
|
2334
2220
|
end
|
|
2335
2221
|
|
|
2336
|
-
it
|
|
2222
|
+
it 'deletes the documents from the database' do
|
|
2337
2223
|
expect(Preference.count).to eq(0)
|
|
2338
2224
|
end
|
|
2339
2225
|
|
|
2340
|
-
it
|
|
2226
|
+
it 'returns the number of documents deleted' do
|
|
2341
2227
|
expect(deleted).to eq(2)
|
|
2342
2228
|
end
|
|
2343
2229
|
|
|
2344
|
-
it
|
|
2230
|
+
it 'sets the association locally' do
|
|
2345
2231
|
expect(person.preferences).to eq([])
|
|
2346
2232
|
end
|
|
2347
2233
|
end
|
|
2348
|
-
|
|
2349
|
-
context 'when the base holds the foreign keys' do
|
|
2350
|
-
# unbind_one pulls ids out of the base's foreign key array and marks
|
|
2351
|
-
# the base dirty, so which documents get unbound is visible here.
|
|
2352
|
-
# Only what the association already held is unbound, and that has to
|
|
2353
|
-
# hold whether or not the conditions carry a regular expression:
|
|
2354
|
-
# scanning under a regexp budget loads the association first, and
|
|
2355
|
-
# that load must not turn into an unbind of its own. See the note in
|
|
2356
|
-
# HasMany::Proxy#remove_all_bounded.
|
|
2357
|
-
let!(:person) do
|
|
2358
|
-
Person.create!.tap do |base|
|
|
2359
|
-
base.preferences.create!(name: 'Testing')
|
|
2360
|
-
base.preferences.create!(name: 'Test')
|
|
2361
|
-
end
|
|
2362
|
-
end
|
|
2363
|
-
|
|
2364
|
-
let(:ids) { person.preference_ids.dup }
|
|
2365
|
-
|
|
2366
|
-
conditions_variants.each do |description, conditions|
|
|
2367
|
-
context "when the conditions carry #{description}" do
|
|
2368
|
-
context 'when the association is already in memory' do
|
|
2369
|
-
it 'unbinds the deleted documents' do
|
|
2370
|
-
person.preferences.send(method, conditions)
|
|
2371
|
-
expect(person.preference_ids).to eq([])
|
|
2372
|
-
expect(person).to be_changed
|
|
2373
|
-
end
|
|
2374
|
-
end
|
|
2375
|
-
|
|
2376
|
-
context 'when the association has not been loaded' do
|
|
2377
|
-
let(:cold) { Person.find(person._id) }
|
|
2378
|
-
|
|
2379
|
-
it 'leaves the foreign keys alone' do
|
|
2380
|
-
expect(ids.length).to eq(2)
|
|
2381
|
-
cold.preferences.send(method, conditions)
|
|
2382
|
-
expect(cold.preference_ids).to eq(ids)
|
|
2383
|
-
expect(cold).not_to be_changed
|
|
2384
|
-
end
|
|
2385
|
-
end
|
|
2386
|
-
end
|
|
2387
|
-
end
|
|
2388
|
-
end
|
|
2389
2234
|
end
|
|
2390
2235
|
end
|
|
2391
2236
|
end
|
|
2392
2237
|
|
|
2393
|
-
describe
|
|
2394
|
-
|
|
2395
|
-
|
|
2396
|
-
expect(described_class).to_not be_embedded
|
|
2238
|
+
describe '.embedded?' do
|
|
2239
|
+
it 'returns false' do
|
|
2240
|
+
expect(described_class).not_to be_embedded
|
|
2397
2241
|
end
|
|
2398
2242
|
end
|
|
2399
2243
|
|
|
2400
|
-
describe
|
|
2401
|
-
|
|
2244
|
+
describe '#exists?' do
|
|
2402
2245
|
let!(:person) do
|
|
2403
2246
|
Person.create!
|
|
2404
2247
|
end
|
|
2405
2248
|
|
|
2406
|
-
context
|
|
2407
|
-
|
|
2249
|
+
context 'when documents exist in the database' do
|
|
2408
2250
|
before do
|
|
2409
2251
|
person.preferences.create!
|
|
2410
2252
|
end
|
|
2411
2253
|
|
|
2412
|
-
it
|
|
2254
|
+
it 'returns true' do
|
|
2413
2255
|
expect(person.preferences.exists?).to be true
|
|
2414
2256
|
end
|
|
2415
2257
|
end
|
|
2416
2258
|
|
|
2417
|
-
context
|
|
2418
|
-
|
|
2259
|
+
context 'when no documents exist in the database' do
|
|
2419
2260
|
before do
|
|
2420
2261
|
person.preferences.build
|
|
2421
2262
|
end
|
|
2422
2263
|
|
|
2423
|
-
it
|
|
2264
|
+
it 'returns false' do
|
|
2424
2265
|
expect(person.preferences.exists?).to be false
|
|
2425
2266
|
end
|
|
2426
2267
|
end
|
|
2427
2268
|
end
|
|
2428
2269
|
|
|
2429
|
-
describe
|
|
2430
|
-
|
|
2431
|
-
context "when the relation is not polymorphic" do
|
|
2432
|
-
|
|
2270
|
+
describe '#find' do
|
|
2271
|
+
context 'when the relation is not polymorphic' do
|
|
2433
2272
|
let(:person) do
|
|
2434
2273
|
Person.create!
|
|
2435
2274
|
end
|
|
2436
2275
|
|
|
2437
2276
|
let!(:preference_one) do
|
|
2438
|
-
person.preferences.create!(name:
|
|
2277
|
+
person.preferences.create!(name: 'Test')
|
|
2439
2278
|
end
|
|
2440
2279
|
|
|
2441
2280
|
let!(:preference_two) do
|
|
2442
|
-
person.preferences.create!(name:
|
|
2281
|
+
person.preferences.create!(name: 'OMG I has relations')
|
|
2443
2282
|
end
|
|
2444
2283
|
|
|
2445
2284
|
let!(:unrelated_pref) do
|
|
2446
|
-
Preference.create!(name:
|
|
2285
|
+
Preference.create!(name: 'orphan annie')
|
|
2447
2286
|
end
|
|
2448
2287
|
|
|
2449
2288
|
let!(:unrelated_pref_two) do
|
|
2450
|
-
Preference.create!(name:
|
|
2289
|
+
Preference.create!(name: 'orphan two')
|
|
2451
2290
|
end
|
|
2452
2291
|
|
|
2453
|
-
context
|
|
2454
|
-
|
|
2455
|
-
context "when the id matches" do
|
|
2456
|
-
|
|
2292
|
+
context 'when providing an id' do
|
|
2293
|
+
context 'when the id matches' do
|
|
2457
2294
|
let(:preference) do
|
|
2458
2295
|
person.preferences.find(preference_one.id)
|
|
2459
2296
|
end
|
|
2460
2297
|
|
|
2461
|
-
it
|
|
2298
|
+
it 'returns the matching document' do
|
|
2462
2299
|
expect(preference).to eq(preference_one)
|
|
2463
2300
|
end
|
|
2464
2301
|
end
|
|
2465
2302
|
|
|
2466
|
-
context
|
|
2467
|
-
|
|
2303
|
+
context 'when the id matches an unreferenced document' do
|
|
2468
2304
|
let(:preference) do
|
|
2469
2305
|
person.preferences.find(unrelated_pref.id)
|
|
2470
2306
|
end
|
|
2471
2307
|
|
|
2472
|
-
it
|
|
2473
|
-
expect
|
|
2308
|
+
it 'raises an error' do
|
|
2309
|
+
expect do
|
|
2474
2310
|
preference
|
|
2475
|
-
|
|
2311
|
+
end.to raise_error(Mongoid::Errors::DocumentNotFound,
|
|
2312
|
+
/Document\(s\) not found for class Preference with id\(s\)/)
|
|
2476
2313
|
end
|
|
2477
2314
|
end
|
|
2478
2315
|
|
|
2479
|
-
context
|
|
2480
|
-
|
|
2481
|
-
context "when config set to raise error" do
|
|
2316
|
+
context 'when the id does not match' do
|
|
2317
|
+
context 'when config set to raise error' do
|
|
2482
2318
|
config_override :raise_not_found_error, true
|
|
2483
2319
|
|
|
2484
|
-
it
|
|
2485
|
-
expect
|
|
2320
|
+
it 'raises an error' do
|
|
2321
|
+
expect do
|
|
2486
2322
|
person.preferences.find(BSON::ObjectId.new)
|
|
2487
|
-
|
|
2323
|
+
end.to raise_error(Mongoid::Errors::DocumentNotFound,
|
|
2324
|
+
/Document\(s\) not found for class Preference with id\(s\)/)
|
|
2488
2325
|
end
|
|
2489
2326
|
end
|
|
2490
2327
|
|
|
2491
|
-
context
|
|
2328
|
+
context 'when config set not to raise error' do
|
|
2492
2329
|
config_override :raise_not_found_error, false
|
|
2493
2330
|
|
|
2494
2331
|
let(:preference) do
|
|
2495
2332
|
person.preferences.find(BSON::ObjectId.new)
|
|
2496
2333
|
end
|
|
2497
2334
|
|
|
2498
|
-
it
|
|
2335
|
+
it 'returns nil' do
|
|
2499
2336
|
expect(preference).to be_nil
|
|
2500
2337
|
end
|
|
2501
2338
|
end
|
|
2502
2339
|
end
|
|
2503
2340
|
end
|
|
2504
2341
|
|
|
2505
|
-
context
|
|
2506
|
-
|
|
2507
|
-
context "when the ids match" do
|
|
2508
|
-
|
|
2342
|
+
context 'when providing an array of ids' do
|
|
2343
|
+
context 'when the ids match' do
|
|
2509
2344
|
let(:preferences) do
|
|
2510
2345
|
person.preferences.find([ preference_one.id, preference_two.id ])
|
|
2511
2346
|
end
|
|
2512
2347
|
|
|
2513
|
-
it
|
|
2348
|
+
it 'returns the matching documents' do
|
|
2514
2349
|
expect(preferences).to eq([ preference_one, preference_two ])
|
|
2515
2350
|
end
|
|
2516
2351
|
end
|
|
2517
2352
|
|
|
2518
|
-
context
|
|
2519
|
-
|
|
2353
|
+
context 'when the ids match unreferenced documents' do
|
|
2520
2354
|
let(:preferences) do
|
|
2521
2355
|
person.preferences.find(
|
|
2522
|
-
|
|
2356
|
+
[ unrelated_pref.id, unrelated_pref_two.id ]
|
|
2523
2357
|
)
|
|
2524
2358
|
end
|
|
2525
2359
|
|
|
2526
|
-
it
|
|
2527
|
-
expect
|
|
2360
|
+
it 'raises an error' do
|
|
2361
|
+
expect do
|
|
2528
2362
|
preferences
|
|
2529
|
-
|
|
2363
|
+
end.to raise_error(Mongoid::Errors::DocumentNotFound,
|
|
2364
|
+
/Document\(s\) not found for class Preference with id\(s\)/)
|
|
2530
2365
|
end
|
|
2531
2366
|
end
|
|
2532
2367
|
|
|
2533
|
-
context
|
|
2534
|
-
|
|
2535
|
-
context "when config set to raise error" do
|
|
2368
|
+
context 'when the ids do not match' do
|
|
2369
|
+
context 'when config set to raise error' do
|
|
2536
2370
|
config_override :raise_not_found_error, true
|
|
2537
2371
|
|
|
2538
|
-
it
|
|
2539
|
-
expect
|
|
2372
|
+
it 'raises an error' do
|
|
2373
|
+
expect do
|
|
2540
2374
|
person.preferences.find([ BSON::ObjectId.new ])
|
|
2541
|
-
|
|
2375
|
+
end.to raise_error(Mongoid::Errors::DocumentNotFound,
|
|
2376
|
+
/Document\(s\) not found for class Preference with id\(s\)/)
|
|
2542
2377
|
end
|
|
2543
2378
|
end
|
|
2544
2379
|
|
|
2545
|
-
context
|
|
2380
|
+
context 'when config set not to raise error' do
|
|
2546
2381
|
config_override :raise_not_found_error, false
|
|
2547
2382
|
|
|
2548
2383
|
let(:preferences) do
|
|
2549
2384
|
person.preferences.find([ BSON::ObjectId.new ])
|
|
2550
2385
|
end
|
|
2551
2386
|
|
|
2552
|
-
it
|
|
2387
|
+
it 'returns an empty array' do
|
|
2553
2388
|
expect(preferences).to be_empty
|
|
2554
2389
|
end
|
|
2555
2390
|
end
|
|
@@ -2558,140 +2393,129 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
2558
2393
|
end
|
|
2559
2394
|
end
|
|
2560
2395
|
|
|
2561
|
-
describe
|
|
2562
|
-
|
|
2563
|
-
context "when the relation is not polymorphic" do
|
|
2564
|
-
|
|
2396
|
+
describe '#find_or_create_by' do
|
|
2397
|
+
context 'when the relation is not polymorphic' do
|
|
2565
2398
|
let(:person) do
|
|
2566
2399
|
Person.create!
|
|
2567
2400
|
end
|
|
2568
2401
|
|
|
2569
2402
|
let!(:preference) do
|
|
2570
|
-
person.preferences.create!(name:
|
|
2403
|
+
person.preferences.create!(name: 'Testing')
|
|
2571
2404
|
end
|
|
2572
2405
|
|
|
2573
|
-
context
|
|
2574
|
-
|
|
2406
|
+
context 'when the document exists' do
|
|
2575
2407
|
let(:found) do
|
|
2576
|
-
person.preferences.find_or_create_by(name:
|
|
2408
|
+
person.preferences.find_or_create_by(name: 'Testing')
|
|
2577
2409
|
end
|
|
2578
2410
|
|
|
2579
|
-
it
|
|
2411
|
+
it 'returns the document' do
|
|
2580
2412
|
expect(found).to eq(preference)
|
|
2581
2413
|
end
|
|
2582
2414
|
end
|
|
2583
2415
|
|
|
2584
|
-
context
|
|
2585
|
-
|
|
2416
|
+
context 'when the document does not exist' do
|
|
2586
2417
|
let(:found) do
|
|
2587
|
-
person.preferences.find_or_create_by(name:
|
|
2418
|
+
person.preferences.find_or_create_by(name: 'Test')
|
|
2588
2419
|
end
|
|
2589
2420
|
|
|
2590
|
-
it
|
|
2591
|
-
expect(found.name).to eq(
|
|
2421
|
+
it 'sets the new document attributes' do
|
|
2422
|
+
expect(found.name).to eq('Test')
|
|
2592
2423
|
end
|
|
2593
2424
|
|
|
2594
|
-
it
|
|
2425
|
+
it 'returns a newly persisted document' do
|
|
2595
2426
|
expect(found).to be_persisted
|
|
2596
2427
|
end
|
|
2597
2428
|
end
|
|
2598
2429
|
end
|
|
2599
2430
|
end
|
|
2600
2431
|
|
|
2601
|
-
describe
|
|
2602
|
-
|
|
2603
|
-
context "when the relation is not polymorphic" do
|
|
2604
|
-
|
|
2432
|
+
describe '#find_or_create_by!' do
|
|
2433
|
+
context 'when the relation is not polymorphic' do
|
|
2605
2434
|
let(:person) do
|
|
2606
2435
|
Person.create!
|
|
2607
2436
|
end
|
|
2608
2437
|
|
|
2609
2438
|
let!(:preference) do
|
|
2610
|
-
person.preferences.create!(name:
|
|
2439
|
+
person.preferences.create!(name: 'Testing')
|
|
2611
2440
|
end
|
|
2612
2441
|
|
|
2613
|
-
context
|
|
2614
|
-
|
|
2442
|
+
context 'when the document exists' do
|
|
2615
2443
|
let(:found) do
|
|
2616
|
-
person.preferences.find_or_create_by!(name:
|
|
2444
|
+
person.preferences.find_or_create_by!(name: 'Testing')
|
|
2617
2445
|
end
|
|
2618
2446
|
|
|
2619
|
-
it
|
|
2447
|
+
it 'returns the document' do
|
|
2620
2448
|
expect(found).to eq(preference)
|
|
2621
2449
|
end
|
|
2622
2450
|
end
|
|
2623
2451
|
|
|
2624
|
-
context
|
|
2625
|
-
|
|
2452
|
+
context 'when the document does not exist' do
|
|
2626
2453
|
let(:found) do
|
|
2627
|
-
person.preferences.find_or_create_by!(name:
|
|
2454
|
+
person.preferences.find_or_create_by!(name: 'Test')
|
|
2628
2455
|
end
|
|
2629
2456
|
|
|
2630
|
-
it
|
|
2631
|
-
expect(found.name).to eq(
|
|
2457
|
+
it 'sets the new document attributes' do
|
|
2458
|
+
expect(found.name).to eq('Test')
|
|
2632
2459
|
end
|
|
2633
2460
|
|
|
2634
|
-
it
|
|
2461
|
+
it 'returns a newly persisted document' do
|
|
2635
2462
|
expect(found).to be_persisted
|
|
2636
2463
|
end
|
|
2637
2464
|
|
|
2638
|
-
context
|
|
2639
|
-
|
|
2640
|
-
|
|
2641
|
-
|
|
2642
|
-
|
|
2643
|
-
}.to raise_error(Mongoid::Errors::Validations)
|
|
2465
|
+
context 'when validation fails' do
|
|
2466
|
+
it 'raises an error' do
|
|
2467
|
+
expect do
|
|
2468
|
+
person.preferences.find_or_create_by!(name: 'A')
|
|
2469
|
+
end.to raise_error(Mongoid::Errors::Validations)
|
|
2644
2470
|
end
|
|
2645
2471
|
end
|
|
2646
2472
|
end
|
|
2647
2473
|
end
|
|
2648
2474
|
end
|
|
2649
2475
|
|
|
2650
|
-
describe
|
|
2651
|
-
|
|
2652
|
-
context "when the relation is not polymorphic" do
|
|
2653
|
-
|
|
2476
|
+
describe '#find_or_initialize_by' do
|
|
2477
|
+
context 'when the relation is not polymorphic' do
|
|
2654
2478
|
let(:person) do
|
|
2655
2479
|
Person.create!
|
|
2656
2480
|
end
|
|
2657
2481
|
|
|
2658
2482
|
let!(:preference) do
|
|
2659
|
-
person.preferences.create!(name:
|
|
2483
|
+
person.preferences.create!(name: 'Testing')
|
|
2660
2484
|
end
|
|
2661
2485
|
|
|
2662
|
-
context
|
|
2663
|
-
|
|
2486
|
+
context 'when the document exists' do
|
|
2664
2487
|
let(:found) do
|
|
2665
|
-
person.preferences.find_or_initialize_by(name:
|
|
2488
|
+
person.preferences.find_or_initialize_by(name: 'Testing')
|
|
2666
2489
|
end
|
|
2667
2490
|
|
|
2668
|
-
it
|
|
2491
|
+
it 'returns the document' do
|
|
2669
2492
|
expect(found).to eq(preference)
|
|
2670
2493
|
end
|
|
2671
2494
|
end
|
|
2672
2495
|
|
|
2673
|
-
context
|
|
2674
|
-
|
|
2496
|
+
context 'when the document does not exist' do
|
|
2675
2497
|
let(:found) do
|
|
2676
|
-
person.preferences.find_or_initialize_by(name:
|
|
2498
|
+
person.preferences.find_or_initialize_by(name: 'Test')
|
|
2677
2499
|
end
|
|
2678
2500
|
|
|
2679
|
-
it
|
|
2680
|
-
expect(found.name).to eq(
|
|
2501
|
+
it 'sets the new document attributes' do
|
|
2502
|
+
expect(found.name).to eq('Test')
|
|
2681
2503
|
end
|
|
2682
2504
|
|
|
2683
|
-
it
|
|
2684
|
-
expect(found).
|
|
2505
|
+
it 'returns a non persisted document' do
|
|
2506
|
+
expect(found).not_to be_persisted
|
|
2685
2507
|
end
|
|
2686
2508
|
end
|
|
2687
2509
|
end
|
|
2688
2510
|
end
|
|
2689
2511
|
|
|
2690
|
-
describe
|
|
2691
|
-
|
|
2512
|
+
describe '#max' do
|
|
2692
2513
|
let(:person) do
|
|
2693
2514
|
Person.create!
|
|
2694
2515
|
end
|
|
2516
|
+
let(:max) do
|
|
2517
|
+
person.preferences.max_by(&:ranking)
|
|
2518
|
+
end
|
|
2695
2519
|
|
|
2696
2520
|
let(:preference_one) do
|
|
2697
2521
|
Preference.create!(ranking: 5)
|
|
@@ -2705,22 +2529,18 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
2705
2529
|
person.preferences.push(preference_one, preference_two)
|
|
2706
2530
|
end
|
|
2707
2531
|
|
|
2708
|
-
|
|
2709
|
-
person.preferences.max do |a,b|
|
|
2710
|
-
a.ranking <=> b.ranking
|
|
2711
|
-
end
|
|
2712
|
-
end
|
|
2713
|
-
|
|
2714
|
-
it "returns the document with the max value of the supplied field" do
|
|
2532
|
+
it 'returns the document with the max value of the supplied field' do
|
|
2715
2533
|
expect(max).to eq(preference_two)
|
|
2716
2534
|
end
|
|
2717
2535
|
end
|
|
2718
2536
|
|
|
2719
|
-
describe
|
|
2720
|
-
|
|
2537
|
+
describe '#max_by' do
|
|
2721
2538
|
let(:person) do
|
|
2722
2539
|
Person.create!
|
|
2723
2540
|
end
|
|
2541
|
+
let(:max) do
|
|
2542
|
+
person.preferences.max_by(&:ranking)
|
|
2543
|
+
end
|
|
2724
2544
|
|
|
2725
2545
|
let(:preference_one) do
|
|
2726
2546
|
Preference.create!(ranking: 5)
|
|
@@ -2734,108 +2554,95 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
2734
2554
|
person.preferences.push(preference_one, preference_two)
|
|
2735
2555
|
end
|
|
2736
2556
|
|
|
2737
|
-
|
|
2738
|
-
person.preferences.max_by(&:ranking)
|
|
2739
|
-
end
|
|
2740
|
-
|
|
2741
|
-
it "returns the document with the max value of the supplied field" do
|
|
2557
|
+
it 'returns the document with the max value of the supplied field' do
|
|
2742
2558
|
expect(max).to eq(preference_two)
|
|
2743
2559
|
end
|
|
2744
2560
|
end
|
|
2745
2561
|
|
|
2746
|
-
describe
|
|
2747
|
-
|
|
2562
|
+
describe '#method_missing' do
|
|
2748
2563
|
let!(:person) do
|
|
2749
2564
|
Person.create!
|
|
2750
2565
|
end
|
|
2751
2566
|
|
|
2752
2567
|
let!(:preference_one) do
|
|
2753
|
-
person.preferences.create!(name:
|
|
2568
|
+
person.preferences.create!(name: 'First', value: 'Posting')
|
|
2754
2569
|
end
|
|
2755
2570
|
|
|
2756
2571
|
let!(:preference_two) do
|
|
2757
|
-
person.preferences.create!(name:
|
|
2572
|
+
person.preferences.create!(name: 'Second', value: 'Testing')
|
|
2758
2573
|
end
|
|
2759
2574
|
|
|
2760
2575
|
let!(:unrelated) do
|
|
2761
|
-
Preference.create!(name:
|
|
2576
|
+
Preference.create!(name: 'Third')
|
|
2762
2577
|
end
|
|
2763
2578
|
|
|
2764
|
-
context
|
|
2765
|
-
|
|
2579
|
+
context 'when providing a single criteria' do
|
|
2766
2580
|
let(:preferences) do
|
|
2767
|
-
person.preferences.where(name:
|
|
2581
|
+
person.preferences.where(name: 'First')
|
|
2768
2582
|
end
|
|
2769
2583
|
|
|
2770
|
-
it
|
|
2584
|
+
it 'applies the criteria to the documents' do
|
|
2771
2585
|
expect(preferences).to eq([ preference_one ])
|
|
2772
2586
|
end
|
|
2773
2587
|
|
|
2774
2588
|
context 'when providing a collation' do
|
|
2775
|
-
|
|
2776
2589
|
let(:preferences) do
|
|
2777
|
-
person.preferences.where(name:
|
|
2590
|
+
person.preferences.where(name: 'FIRST').collation(locale: 'en_US', strength: 2).to_a
|
|
2778
2591
|
end
|
|
2779
2592
|
|
|
2780
|
-
it
|
|
2593
|
+
it 'applies the collation option to the query' do
|
|
2781
2594
|
expect(preferences).to eq([ preference_one ])
|
|
2782
2595
|
end
|
|
2783
2596
|
end
|
|
2784
2597
|
end
|
|
2785
2598
|
|
|
2786
|
-
context
|
|
2787
|
-
|
|
2599
|
+
context 'when providing a criteria on id' do
|
|
2788
2600
|
let(:preferences) do
|
|
2789
2601
|
person.preferences.where(_id: unrelated.id)
|
|
2790
2602
|
end
|
|
2791
2603
|
|
|
2792
|
-
it
|
|
2604
|
+
it 'does not return unrelated documents' do
|
|
2793
2605
|
expect(preferences).to be_empty
|
|
2794
2606
|
end
|
|
2795
2607
|
end
|
|
2796
2608
|
|
|
2797
|
-
context
|
|
2798
|
-
|
|
2609
|
+
context 'when providing a criteria class method' do
|
|
2799
2610
|
let(:preferences) do
|
|
2800
2611
|
person.preferences.posting
|
|
2801
2612
|
end
|
|
2802
2613
|
|
|
2803
|
-
it
|
|
2614
|
+
it 'applies the criteria to the documents' do
|
|
2804
2615
|
expect(preferences).to eq([ preference_one ])
|
|
2805
2616
|
end
|
|
2806
2617
|
end
|
|
2807
2618
|
|
|
2808
|
-
context
|
|
2809
|
-
|
|
2619
|
+
context 'when chaining criteria' do
|
|
2810
2620
|
let(:preferences) do
|
|
2811
|
-
person.preferences.posting.where(:name.in => [
|
|
2621
|
+
person.preferences.posting.where(:name.in => [ 'First' ])
|
|
2812
2622
|
end
|
|
2813
2623
|
|
|
2814
|
-
it
|
|
2624
|
+
it 'applies the criteria to the documents' do
|
|
2815
2625
|
expect(preferences).to eq([ preference_one ])
|
|
2816
2626
|
end
|
|
2817
2627
|
end
|
|
2818
2628
|
|
|
2819
|
-
context
|
|
2820
|
-
|
|
2821
|
-
describe "#distinct" do
|
|
2822
|
-
|
|
2629
|
+
context 'when delegating methods' do
|
|
2630
|
+
describe '#distinct' do
|
|
2823
2631
|
let(:values) do
|
|
2824
2632
|
person.preferences.distinct(:name)
|
|
2825
2633
|
end
|
|
2826
2634
|
|
|
2827
|
-
it
|
|
2828
|
-
expect(values).to include(
|
|
2829
|
-
expect(values).to include(
|
|
2635
|
+
it 'returns the distinct values for the fields' do
|
|
2636
|
+
expect(values).to include('First')
|
|
2637
|
+
expect(values).to include('Second')
|
|
2830
2638
|
end
|
|
2831
2639
|
|
|
2832
|
-
context
|
|
2833
|
-
|
|
2640
|
+
context 'when the inverse relation is not defined' do
|
|
2834
2641
|
let!(:house) do
|
|
2835
|
-
person.houses.create!(name:
|
|
2642
|
+
person.houses.create!(name: 'Wayne Manor')
|
|
2836
2643
|
end
|
|
2837
2644
|
|
|
2838
|
-
it
|
|
2645
|
+
it 'returns the distinct values for the fields' do
|
|
2839
2646
|
expect(person.houses.distinct(:name)).to eq([ house.name ])
|
|
2840
2647
|
end
|
|
2841
2648
|
end
|
|
@@ -2843,11 +2650,13 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
2843
2650
|
end
|
|
2844
2651
|
end
|
|
2845
2652
|
|
|
2846
|
-
describe
|
|
2847
|
-
|
|
2653
|
+
describe '#min' do
|
|
2848
2654
|
let(:person) do
|
|
2849
2655
|
Person.create!
|
|
2850
2656
|
end
|
|
2657
|
+
let(:min) do
|
|
2658
|
+
person.preferences.min_by(&:ranking)
|
|
2659
|
+
end
|
|
2851
2660
|
|
|
2852
2661
|
let(:preference_one) do
|
|
2853
2662
|
Preference.create!(ranking: 5)
|
|
@@ -2861,22 +2670,18 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
2861
2670
|
person.preferences.push(preference_one, preference_two)
|
|
2862
2671
|
end
|
|
2863
2672
|
|
|
2864
|
-
|
|
2865
|
-
person.preferences.min do |a, b|
|
|
2866
|
-
a.ranking <=> b.ranking
|
|
2867
|
-
end
|
|
2868
|
-
end
|
|
2869
|
-
|
|
2870
|
-
it "returns the min value of the supplied field" do
|
|
2673
|
+
it 'returns the min value of the supplied field' do
|
|
2871
2674
|
expect(min).to eq(preference_one)
|
|
2872
2675
|
end
|
|
2873
2676
|
end
|
|
2874
2677
|
|
|
2875
|
-
describe
|
|
2876
|
-
|
|
2678
|
+
describe '#min_by' do
|
|
2877
2679
|
let(:person) do
|
|
2878
2680
|
Person.create!
|
|
2879
2681
|
end
|
|
2682
|
+
let(:min) do
|
|
2683
|
+
person.preferences.min_by(&:ranking)
|
|
2684
|
+
end
|
|
2880
2685
|
|
|
2881
2686
|
let(:preference_one) do
|
|
2882
2687
|
Preference.create!(ranking: 5)
|
|
@@ -2890,71 +2695,64 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
2890
2695
|
person.preferences.push(preference_one, preference_two)
|
|
2891
2696
|
end
|
|
2892
2697
|
|
|
2893
|
-
|
|
2894
|
-
person.preferences.min_by(&:ranking)
|
|
2895
|
-
end
|
|
2896
|
-
|
|
2897
|
-
it "returns the min value of the supplied field" do
|
|
2698
|
+
it 'returns the min value of the supplied field' do
|
|
2898
2699
|
expect(min).to eq(preference_one)
|
|
2899
2700
|
end
|
|
2900
2701
|
end
|
|
2901
2702
|
|
|
2902
|
-
describe
|
|
2903
|
-
|
|
2904
|
-
|
|
2905
|
-
expect(Person.new.preferences).to_not be_nil
|
|
2703
|
+
describe '#nil?' do
|
|
2704
|
+
it 'returns false' do
|
|
2705
|
+
expect(Person.new.preferences).not_to be_nil
|
|
2906
2706
|
end
|
|
2907
2707
|
end
|
|
2908
2708
|
|
|
2909
|
-
describe
|
|
2910
|
-
|
|
2709
|
+
describe '#nullify_all' do
|
|
2911
2710
|
let(:person) do
|
|
2912
2711
|
Person.create!
|
|
2913
2712
|
end
|
|
2914
2713
|
|
|
2915
2714
|
let!(:preference_one) do
|
|
2916
|
-
person.preferences.create!(name:
|
|
2715
|
+
person.preferences.create!(name: 'One')
|
|
2917
2716
|
end
|
|
2918
2717
|
|
|
2919
2718
|
let!(:preference_two) do
|
|
2920
|
-
person.preferences.create!(name:
|
|
2719
|
+
person.preferences.create!(name: 'Two')
|
|
2921
2720
|
end
|
|
2922
2721
|
|
|
2923
2722
|
before do
|
|
2924
2723
|
person.preferences.nullify_all
|
|
2925
2724
|
end
|
|
2926
2725
|
|
|
2927
|
-
it
|
|
2726
|
+
it 'removes the foreign key from the base document' do
|
|
2928
2727
|
[ preference_one, preference_two ].each do |preference|
|
|
2929
|
-
expect(person.preference_ids).
|
|
2728
|
+
expect(person.preference_ids).not_to include(preference.id)
|
|
2930
2729
|
end
|
|
2931
2730
|
end
|
|
2932
2731
|
|
|
2933
|
-
it
|
|
2732
|
+
it 'removes the foreign key from the target documents' do
|
|
2934
2733
|
[ preference_one, preference_two ].each do |preference|
|
|
2935
|
-
expect(preference.person_ids).
|
|
2734
|
+
expect(preference.person_ids).not_to include(person.id)
|
|
2936
2735
|
end
|
|
2937
2736
|
end
|
|
2938
2737
|
|
|
2939
|
-
it
|
|
2738
|
+
it 'removes the reference from the base document' do
|
|
2940
2739
|
[ preference_one, preference_two ].each do |preference|
|
|
2941
|
-
expect(person.preferences).
|
|
2740
|
+
expect(person.preferences).not_to include(preference)
|
|
2942
2741
|
end
|
|
2943
2742
|
end
|
|
2944
2743
|
|
|
2945
|
-
it
|
|
2744
|
+
it 'removes the reference from the target documents' do
|
|
2946
2745
|
[ preference_one, preference_two ].each do |preference|
|
|
2947
|
-
expect(preference.people).
|
|
2746
|
+
expect(preference.people).not_to include(person)
|
|
2948
2747
|
end
|
|
2949
2748
|
end
|
|
2950
2749
|
|
|
2951
|
-
it
|
|
2952
|
-
expect(preference_one.reload.people).
|
|
2750
|
+
it 'saves the documents' do
|
|
2751
|
+
expect(preference_one.reload.people).not_to include(person)
|
|
2953
2752
|
end
|
|
2954
2753
|
end
|
|
2955
2754
|
|
|
2956
|
-
describe
|
|
2957
|
-
|
|
2755
|
+
describe '#respond_to?' do
|
|
2958
2756
|
let(:person) do
|
|
2959
2757
|
Person.new
|
|
2960
2758
|
end
|
|
@@ -2964,90 +2762,78 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
2964
2762
|
end
|
|
2965
2763
|
|
|
2966
2764
|
Array.public_instance_methods.sort.each do |method|
|
|
2967
|
-
|
|
2968
2765
|
context "when checking Array##{method}" do
|
|
2969
|
-
|
|
2970
2766
|
before do
|
|
2971
2767
|
expect([].respond_to?(method)).to be true
|
|
2972
2768
|
end
|
|
2973
2769
|
|
|
2974
|
-
it
|
|
2770
|
+
it 'returns true' do
|
|
2975
2771
|
expect(preferences.respond_to?(method)).to be true
|
|
2976
2772
|
end
|
|
2977
2773
|
end
|
|
2978
2774
|
end
|
|
2979
2775
|
|
|
2980
2776
|
Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy.public_instance_methods.sort.each do |method|
|
|
2981
|
-
|
|
2982
2777
|
context "when checking Proxy##{method}" do
|
|
2983
|
-
|
|
2984
|
-
it "returns true" do
|
|
2778
|
+
it 'returns true' do
|
|
2985
2779
|
expect(preferences.respond_to?(method)).to be true
|
|
2986
2780
|
end
|
|
2987
2781
|
end
|
|
2988
2782
|
end
|
|
2989
2783
|
|
|
2990
2784
|
Preference.scopes.keys.sort.each do |method|
|
|
2991
|
-
|
|
2992
2785
|
context "when checking scope #{method}" do
|
|
2993
|
-
|
|
2994
|
-
it "returns true" do
|
|
2786
|
+
it 'returns true' do
|
|
2995
2787
|
expect(preferences.respond_to?(method)).to be true
|
|
2996
2788
|
end
|
|
2997
2789
|
end
|
|
2998
2790
|
end
|
|
2999
2791
|
end
|
|
3000
2792
|
|
|
3001
|
-
describe
|
|
3002
|
-
|
|
2793
|
+
describe '#scoped' do
|
|
3003
2794
|
let(:scoped) do
|
|
3004
2795
|
person.preferences.scoped
|
|
3005
2796
|
end
|
|
3006
2797
|
|
|
3007
2798
|
context 'when association is empty' do
|
|
3008
|
-
|
|
3009
2799
|
let(:person) do
|
|
3010
2800
|
Person.new
|
|
3011
2801
|
end
|
|
3012
2802
|
|
|
3013
|
-
it
|
|
2803
|
+
it 'returns the relation criteria' do
|
|
3014
2804
|
expect(scoped).to be_a(Mongoid::Criteria)
|
|
3015
2805
|
end
|
|
3016
2806
|
|
|
3017
|
-
it
|
|
3018
|
-
expect(scoped.selector).to eq(
|
|
2807
|
+
it 'returns with an empty selector' do
|
|
2808
|
+
expect(scoped.selector).to eq('_id' => { '$in' => [] })
|
|
3019
2809
|
end
|
|
3020
2810
|
end
|
|
3021
2811
|
|
|
3022
2812
|
context 'when association is not empty' do
|
|
3023
|
-
|
|
3024
2813
|
let(:person) do
|
|
3025
2814
|
Person.create!(preferences: [
|
|
3026
|
-
|
|
3027
|
-
|
|
2815
|
+
Preference.new(id: 123)
|
|
2816
|
+
])
|
|
3028
2817
|
end
|
|
3029
2818
|
|
|
3030
|
-
it
|
|
3031
|
-
expect(scoped.selector).to eq(
|
|
2819
|
+
it 'returns with a selector including association element ids' do
|
|
2820
|
+
expect(scoped.selector).to eq('_id' => { '$in' => [ 123 ] })
|
|
3032
2821
|
end
|
|
3033
2822
|
end
|
|
3034
2823
|
end
|
|
3035
2824
|
|
|
3036
|
-
[
|
|
3037
|
-
|
|
2825
|
+
%i[size length].each do |method|
|
|
3038
2826
|
describe "##{method}" do
|
|
3039
|
-
|
|
3040
2827
|
let(:person) do
|
|
3041
2828
|
Person.create!
|
|
3042
2829
|
end
|
|
3043
2830
|
|
|
3044
|
-
context
|
|
3045
|
-
|
|
2831
|
+
context 'when documents have been persisted' do
|
|
3046
2832
|
let!(:preference) do
|
|
3047
|
-
person.preferences.create!(name:
|
|
2833
|
+
person.preferences.create!(name: 'Testing')
|
|
3048
2834
|
end
|
|
3049
2835
|
|
|
3050
|
-
it
|
|
2836
|
+
it 'returns the number of documents' do
|
|
3051
2837
|
expect(person.preferences.send(method)).to eq(1)
|
|
3052
2838
|
end
|
|
3053
2839
|
end
|
|
@@ -3080,238 +2866,225 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
3080
2866
|
end
|
|
3081
2867
|
end
|
|
3082
2868
|
|
|
3083
|
-
context
|
|
3084
|
-
|
|
2869
|
+
context 'when documents have not been persisted' do
|
|
3085
2870
|
before do
|
|
3086
|
-
person.preferences.build(name:
|
|
3087
|
-
person.preferences.create!(name:
|
|
2871
|
+
person.preferences.build(name: 'Test')
|
|
2872
|
+
person.preferences.create!(name: 'Test 2')
|
|
3088
2873
|
end
|
|
3089
2874
|
|
|
3090
|
-
it
|
|
2875
|
+
it 'returns the total number of documents' do
|
|
3091
2876
|
expect(person.preferences.send(method)).to eq(2)
|
|
3092
2877
|
end
|
|
3093
2878
|
end
|
|
3094
2879
|
end
|
|
3095
2880
|
end
|
|
3096
2881
|
|
|
3097
|
-
describe
|
|
3098
|
-
|
|
3099
|
-
context "when the relation has no default scope" do
|
|
3100
|
-
|
|
2882
|
+
describe '#unscoped' do
|
|
2883
|
+
context 'when the relation has no default scope' do
|
|
3101
2884
|
let!(:person) do
|
|
3102
2885
|
Person.create!
|
|
3103
2886
|
end
|
|
3104
2887
|
|
|
3105
2888
|
let!(:preference_one) do
|
|
3106
|
-
person.preferences.create!(name:
|
|
2889
|
+
person.preferences.create!(name: 'first')
|
|
3107
2890
|
end
|
|
3108
2891
|
|
|
3109
2892
|
let!(:preference_two) do
|
|
3110
|
-
Preference.create!(name:
|
|
2893
|
+
Preference.create!(name: 'second')
|
|
3111
2894
|
end
|
|
3112
2895
|
|
|
3113
2896
|
let(:unscoped) do
|
|
3114
2897
|
person.preferences.unscoped
|
|
3115
2898
|
end
|
|
3116
2899
|
|
|
3117
|
-
it
|
|
2900
|
+
it 'returns only the associated documents' do
|
|
3118
2901
|
expect(unscoped).to eq([ preference_one ])
|
|
3119
2902
|
end
|
|
3120
2903
|
end
|
|
3121
2904
|
|
|
3122
|
-
context
|
|
3123
|
-
|
|
2905
|
+
context 'when the relation has a default scope' do
|
|
3124
2906
|
let!(:person) do
|
|
3125
2907
|
Person.create!
|
|
3126
2908
|
end
|
|
3127
2909
|
|
|
3128
2910
|
let!(:house_one) do
|
|
3129
|
-
person.houses.create!(name:
|
|
2911
|
+
person.houses.create!(name: 'first')
|
|
3130
2912
|
end
|
|
3131
2913
|
|
|
3132
2914
|
let!(:house_two) do
|
|
3133
|
-
House.create!(name:
|
|
2915
|
+
House.create!(name: 'second')
|
|
3134
2916
|
end
|
|
3135
2917
|
|
|
3136
2918
|
let(:unscoped) do
|
|
3137
2919
|
person.houses.unscoped
|
|
3138
2920
|
end
|
|
3139
2921
|
|
|
3140
|
-
it
|
|
2922
|
+
it 'only returns associated documents' do
|
|
3141
2923
|
expect(unscoped).to eq([ house_one ])
|
|
3142
2924
|
end
|
|
3143
2925
|
|
|
3144
|
-
it
|
|
2926
|
+
it 'removes the default scoping options' do
|
|
3145
2927
|
expect(unscoped.options).to eq({})
|
|
3146
2928
|
end
|
|
3147
2929
|
end
|
|
3148
2930
|
end
|
|
3149
2931
|
|
|
3150
|
-
context
|
|
3151
|
-
|
|
2932
|
+
context 'when setting the ids directly after the documents' do
|
|
3152
2933
|
let!(:person) do
|
|
3153
|
-
Person.create!(title:
|
|
2934
|
+
Person.create!(title: 'The Boss')
|
|
3154
2935
|
end
|
|
3155
2936
|
|
|
3156
2937
|
let!(:girlfriend_house) do
|
|
3157
|
-
House.create!(name:
|
|
2938
|
+
House.create!(name: 'Girlfriend')
|
|
3158
2939
|
end
|
|
3159
2940
|
|
|
3160
2941
|
let!(:wife_house) do
|
|
3161
|
-
House.create!(name:
|
|
2942
|
+
House.create!(name: 'Wife')
|
|
3162
2943
|
end
|
|
3163
2944
|
|
|
3164
2945
|
let!(:exwife_house) do
|
|
3165
|
-
House.create!(name:
|
|
2946
|
+
House.create!(name: 'Ex-Wife')
|
|
3166
2947
|
end
|
|
3167
2948
|
|
|
3168
2949
|
before do
|
|
3169
2950
|
person.update_attributes!(
|
|
3170
|
-
|
|
2951
|
+
houses: [ wife_house, exwife_house, girlfriend_house ]
|
|
3171
2952
|
)
|
|
3172
2953
|
person.update_attributes!(house_ids: [ girlfriend_house.id ])
|
|
3173
2954
|
end
|
|
3174
2955
|
|
|
3175
|
-
context
|
|
3176
|
-
|
|
3177
|
-
it "properly sets the references" do
|
|
2956
|
+
context 'when reloading' do
|
|
2957
|
+
it 'properly sets the references' do
|
|
3178
2958
|
expect(person.houses(true)).to eq([ girlfriend_house ])
|
|
3179
2959
|
end
|
|
3180
2960
|
end
|
|
3181
2961
|
end
|
|
3182
2962
|
|
|
3183
|
-
context
|
|
3184
|
-
|
|
3185
|
-
context "when the documents are new" do
|
|
3186
|
-
|
|
2963
|
+
context 'when setting both sides in a single call' do
|
|
2964
|
+
context 'when the documents are new' do
|
|
3187
2965
|
let(:user) do
|
|
3188
|
-
User.new(name:
|
|
2966
|
+
User.new(name: 'testing')
|
|
3189
2967
|
end
|
|
3190
2968
|
|
|
3191
2969
|
let(:business) do
|
|
3192
|
-
Business.new(name:
|
|
2970
|
+
Business.new(name: 'serious', owners: [ user ])
|
|
3193
2971
|
end
|
|
3194
2972
|
|
|
3195
2973
|
before do
|
|
3196
2974
|
user.businesses = [ business ]
|
|
3197
2975
|
end
|
|
3198
2976
|
|
|
3199
|
-
it
|
|
2977
|
+
it 'sets the businesses' do
|
|
3200
2978
|
expect(user.businesses).to eq([ business ])
|
|
3201
2979
|
end
|
|
3202
2980
|
|
|
3203
|
-
it
|
|
2981
|
+
it 'sets the inverse users' do
|
|
3204
2982
|
expect(user.businesses.first.owners.first).to eq(user)
|
|
3205
2983
|
end
|
|
3206
2984
|
|
|
3207
|
-
it
|
|
2985
|
+
it 'sets the inverse businesses' do
|
|
3208
2986
|
expect(business.owners).to eq([ user ])
|
|
3209
2987
|
end
|
|
3210
2988
|
end
|
|
3211
2989
|
|
|
3212
|
-
context
|
|
3213
|
-
|
|
2990
|
+
context 'when one side is persisted' do
|
|
3214
2991
|
let!(:user) do
|
|
3215
|
-
User.new(name:
|
|
2992
|
+
User.new(name: 'testing')
|
|
3216
2993
|
end
|
|
3217
2994
|
|
|
3218
2995
|
let!(:business) do
|
|
3219
|
-
Business.create!(name:
|
|
2996
|
+
Business.create!(name: 'serious', owners: [ user ])
|
|
3220
2997
|
end
|
|
3221
2998
|
|
|
3222
2999
|
before do
|
|
3223
3000
|
user.businesses = [ business ]
|
|
3224
3001
|
end
|
|
3225
3002
|
|
|
3226
|
-
it
|
|
3003
|
+
it 'sets the businesses' do
|
|
3227
3004
|
expect(user.businesses).to eq([ business ])
|
|
3228
3005
|
end
|
|
3229
3006
|
|
|
3230
|
-
it
|
|
3007
|
+
it 'sets the inverse users' do
|
|
3231
3008
|
expect(user.businesses.first.owners.first).to eq(user)
|
|
3232
3009
|
end
|
|
3233
3010
|
|
|
3234
|
-
it
|
|
3011
|
+
it 'sets the inverse businesses' do
|
|
3235
3012
|
expect(business.owners).to eq([ user ])
|
|
3236
3013
|
end
|
|
3237
3014
|
|
|
3238
|
-
context
|
|
3239
|
-
|
|
3015
|
+
context 'when reloading' do
|
|
3240
3016
|
before do
|
|
3241
3017
|
user.reload
|
|
3242
3018
|
business.reload
|
|
3243
3019
|
end
|
|
3244
3020
|
|
|
3245
|
-
it
|
|
3021
|
+
it 'persists the businesses' do
|
|
3246
3022
|
expect(user.businesses).to eq([ business ])
|
|
3247
3023
|
end
|
|
3248
3024
|
|
|
3249
|
-
it
|
|
3025
|
+
it 'persists the inverse users' do
|
|
3250
3026
|
expect(user.businesses.first.owners.first).to eq(user)
|
|
3251
3027
|
end
|
|
3252
3028
|
|
|
3253
|
-
it
|
|
3029
|
+
it 'persists the inverse businesses' do
|
|
3254
3030
|
expect(business.owners).to eq([ user ])
|
|
3255
3031
|
end
|
|
3256
3032
|
end
|
|
3257
3033
|
end
|
|
3258
3034
|
|
|
3259
|
-
context
|
|
3260
|
-
|
|
3035
|
+
context 'when the documents are persisted' do
|
|
3261
3036
|
let(:user) do
|
|
3262
|
-
User.create!(name:
|
|
3037
|
+
User.create!(name: 'tst')
|
|
3263
3038
|
end
|
|
3264
3039
|
|
|
3265
3040
|
let(:business) do
|
|
3266
|
-
Business.create!(name:
|
|
3041
|
+
Business.create!(name: 'srs', owners: [ user ])
|
|
3267
3042
|
end
|
|
3268
3043
|
|
|
3269
3044
|
before do
|
|
3270
3045
|
user.businesses = [ business ]
|
|
3271
3046
|
end
|
|
3272
3047
|
|
|
3273
|
-
it
|
|
3048
|
+
it 'sets the businesses' do
|
|
3274
3049
|
expect(user.businesses).to eq([ business ])
|
|
3275
3050
|
end
|
|
3276
3051
|
|
|
3277
|
-
it
|
|
3052
|
+
it 'sets the inverse users' do
|
|
3278
3053
|
expect(user.businesses.first.owners.first).to eq(user)
|
|
3279
3054
|
end
|
|
3280
3055
|
|
|
3281
|
-
it
|
|
3056
|
+
it 'sets the inverse businesses' do
|
|
3282
3057
|
expect(business.owners).to eq([ user ])
|
|
3283
3058
|
end
|
|
3284
3059
|
|
|
3285
|
-
context
|
|
3286
|
-
|
|
3060
|
+
context 'when reloading' do
|
|
3287
3061
|
before do
|
|
3288
3062
|
user.reload
|
|
3289
3063
|
business.reload
|
|
3290
3064
|
end
|
|
3291
3065
|
|
|
3292
|
-
it
|
|
3066
|
+
it 'persists the businesses' do
|
|
3293
3067
|
expect(user.businesses).to eq([ business ])
|
|
3294
3068
|
end
|
|
3295
3069
|
|
|
3296
|
-
it
|
|
3070
|
+
it 'persists the inverse users' do
|
|
3297
3071
|
expect(user.businesses.first.owners.first).to eq(user)
|
|
3298
3072
|
end
|
|
3299
3073
|
|
|
3300
|
-
it
|
|
3074
|
+
it 'persists the inverse businesses' do
|
|
3301
3075
|
expect(business.owners).to eq([ user ])
|
|
3302
3076
|
end
|
|
3303
3077
|
end
|
|
3304
3078
|
end
|
|
3305
3079
|
end
|
|
3306
3080
|
|
|
3307
|
-
context
|
|
3308
|
-
|
|
3081
|
+
context 'when binding the relation multiple times' do
|
|
3309
3082
|
let(:person) do
|
|
3310
3083
|
Person.create!
|
|
3311
3084
|
end
|
|
3312
3085
|
|
|
3313
3086
|
let(:preference) do
|
|
3314
|
-
person.preferences.create!(name:
|
|
3087
|
+
person.preferences.create!(name: 'testing')
|
|
3315
3088
|
end
|
|
3316
3089
|
|
|
3317
3090
|
before do
|
|
@@ -3322,13 +3095,12 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
3322
3095
|
end
|
|
3323
3096
|
end
|
|
3324
3097
|
|
|
3325
|
-
it
|
|
3098
|
+
it 'does not duplicate foreign keys' do
|
|
3326
3099
|
expect(person.preference_ids).to eq([ preference.id ])
|
|
3327
3100
|
end
|
|
3328
3101
|
end
|
|
3329
3102
|
|
|
3330
|
-
context
|
|
3331
|
-
|
|
3103
|
+
context 'when the association has order criteria' do
|
|
3332
3104
|
let(:person) do
|
|
3333
3105
|
Person.create!
|
|
3334
3106
|
end
|
|
@@ -3350,21 +3122,21 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
3350
3122
|
person.ordered_preferences.push(preference_one, preference_two, preference_three)
|
|
3351
3123
|
end
|
|
3352
3124
|
|
|
3353
|
-
it
|
|
3125
|
+
it 'orders the documents' do
|
|
3354
3126
|
expect(person.ordered_preferences(true)).to eq(
|
|
3355
|
-
|
|
3356
|
-
|
|
3127
|
+
[ preference_two, preference_three, preference_one ]
|
|
3128
|
+
)
|
|
3357
3129
|
end
|
|
3358
3130
|
|
|
3359
|
-
it
|
|
3131
|
+
it 'chains default criteria with additional' do
|
|
3360
3132
|
expect(person.ordered_preferences.order_by(:name.desc).to_a).to eq(
|
|
3361
|
-
|
|
3362
|
-
|
|
3133
|
+
[ preference_three, preference_two,
|
|
3134
|
+
preference_one ]
|
|
3135
|
+
)
|
|
3363
3136
|
end
|
|
3364
3137
|
end
|
|
3365
3138
|
|
|
3366
|
-
context
|
|
3367
|
-
|
|
3139
|
+
context 'when the parent is not a new record and freshly loaded' do
|
|
3368
3140
|
let(:person) do
|
|
3369
3141
|
Person.create!
|
|
3370
3142
|
end
|
|
@@ -3380,57 +3152,54 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
3380
3152
|
person.preferences = nil
|
|
3381
3153
|
end
|
|
3382
3154
|
|
|
3383
|
-
it
|
|
3155
|
+
it 'sets the relation to an empty array' do
|
|
3384
3156
|
expect(person.preferences).to be_empty
|
|
3385
3157
|
end
|
|
3386
3158
|
|
|
3387
|
-
it
|
|
3159
|
+
it 'removes the foreign key values' do
|
|
3388
3160
|
expect(person.preference_ids).to be_empty
|
|
3389
3161
|
end
|
|
3390
3162
|
|
|
3391
|
-
it
|
|
3392
|
-
expect
|
|
3163
|
+
it 'does not delete the target from the database' do
|
|
3164
|
+
expect do
|
|
3393
3165
|
preference.reload
|
|
3394
|
-
|
|
3166
|
+
end.not_to raise_error
|
|
3395
3167
|
end
|
|
3396
3168
|
end
|
|
3397
3169
|
|
|
3398
|
-
context
|
|
3399
|
-
|
|
3170
|
+
context 'when reloading the relation' do
|
|
3400
3171
|
let!(:person) do
|
|
3401
3172
|
Person.create!
|
|
3402
3173
|
end
|
|
3403
3174
|
|
|
3404
3175
|
let!(:preference_one) do
|
|
3405
|
-
Preference.create!(name:
|
|
3176
|
+
Preference.create!(name: 'one')
|
|
3406
3177
|
end
|
|
3407
3178
|
|
|
3408
3179
|
let!(:preference_two) do
|
|
3409
|
-
Preference.create!(name:
|
|
3180
|
+
Preference.create!(name: 'two')
|
|
3410
3181
|
end
|
|
3411
3182
|
|
|
3412
3183
|
before do
|
|
3413
3184
|
person.preferences << preference_one
|
|
3414
3185
|
end
|
|
3415
3186
|
|
|
3416
|
-
context
|
|
3417
|
-
|
|
3187
|
+
context 'when the relation references the same documents' do
|
|
3418
3188
|
before do
|
|
3419
|
-
Preference.collection.find({ _id: preference_one.id })
|
|
3420
|
-
|
|
3189
|
+
Preference.collection.find({ _id: preference_one.id })
|
|
3190
|
+
.update_one({ '$set' => { name: 'reloaded' } })
|
|
3421
3191
|
end
|
|
3422
3192
|
|
|
3423
3193
|
let(:reloaded) do
|
|
3424
3194
|
person.preferences(true)
|
|
3425
3195
|
end
|
|
3426
3196
|
|
|
3427
|
-
it
|
|
3428
|
-
expect(reloaded.first.name).to eq(
|
|
3197
|
+
it 'reloads the document from the database' do
|
|
3198
|
+
expect(reloaded.first.name).to eq('reloaded')
|
|
3429
3199
|
end
|
|
3430
3200
|
end
|
|
3431
3201
|
|
|
3432
|
-
context
|
|
3433
|
-
|
|
3202
|
+
context 'when the relation references different documents' do
|
|
3434
3203
|
before do
|
|
3435
3204
|
person.preferences << preference_two
|
|
3436
3205
|
end
|
|
@@ -3439,18 +3208,16 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
3439
3208
|
person.preferences(true)
|
|
3440
3209
|
end
|
|
3441
3210
|
|
|
3442
|
-
it
|
|
3211
|
+
it 'reloads the new document from the database' do
|
|
3443
3212
|
expect(reloaded).to eq([ preference_one, preference_two ])
|
|
3444
3213
|
end
|
|
3445
3214
|
end
|
|
3446
3215
|
end
|
|
3447
3216
|
|
|
3448
|
-
context
|
|
3449
|
-
|
|
3450
|
-
context "when the document is new" do
|
|
3451
|
-
|
|
3217
|
+
context 'when adding to a relation via a field setter' do
|
|
3218
|
+
context 'when the document is new' do
|
|
3452
3219
|
let!(:person) do
|
|
3453
|
-
Person.create!(preference_names:
|
|
3220
|
+
Person.create!(preference_names: 'one, two')
|
|
3454
3221
|
end
|
|
3455
3222
|
|
|
3456
3223
|
let(:preference_one) do
|
|
@@ -3461,36 +3228,35 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
3461
3228
|
person.reload.preferences.last
|
|
3462
3229
|
end
|
|
3463
3230
|
|
|
3464
|
-
it
|
|
3465
|
-
expect(preference_one).
|
|
3231
|
+
it 'persists the first preference' do
|
|
3232
|
+
expect(preference_one).not_to be_nil
|
|
3466
3233
|
end
|
|
3467
3234
|
|
|
3468
|
-
it
|
|
3235
|
+
it 'sets the first inverse' do
|
|
3469
3236
|
expect(preference_one.people).to eq([ person ])
|
|
3470
3237
|
end
|
|
3471
3238
|
|
|
3472
|
-
it
|
|
3473
|
-
expect(preference_two).
|
|
3239
|
+
it 'persists the second preference' do
|
|
3240
|
+
expect(preference_two).not_to be_nil
|
|
3474
3241
|
end
|
|
3475
3242
|
|
|
3476
|
-
it
|
|
3243
|
+
it 'sets the second inverse keys' do
|
|
3477
3244
|
expect(preference_two.people).to eq([ person ])
|
|
3478
3245
|
end
|
|
3479
3246
|
end
|
|
3480
3247
|
end
|
|
3481
3248
|
|
|
3482
|
-
context
|
|
3483
|
-
|
|
3249
|
+
context 'when changing the order of existing ids' do
|
|
3484
3250
|
let(:person) do
|
|
3485
3251
|
Person.new
|
|
3486
3252
|
end
|
|
3487
3253
|
|
|
3488
3254
|
let(:preference_one) do
|
|
3489
|
-
Preference.create!(name:
|
|
3255
|
+
Preference.create!(name: 'one')
|
|
3490
3256
|
end
|
|
3491
3257
|
|
|
3492
3258
|
let(:preference_two) do
|
|
3493
|
-
Preference.create!(name:
|
|
3259
|
+
Preference.create!(name: 'two')
|
|
3494
3260
|
end
|
|
3495
3261
|
|
|
3496
3262
|
before do
|
|
@@ -3498,8 +3264,7 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
3498
3264
|
person.save!
|
|
3499
3265
|
end
|
|
3500
3266
|
|
|
3501
|
-
context
|
|
3502
|
-
|
|
3267
|
+
context 'and the order is changed' do
|
|
3503
3268
|
before do
|
|
3504
3269
|
person.preference_ids = [ preference_two.id, preference_one.id ]
|
|
3505
3270
|
person.save!
|
|
@@ -3509,65 +3274,60 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
3509
3274
|
Person.find(person.id)
|
|
3510
3275
|
end
|
|
3511
3276
|
|
|
3512
|
-
it
|
|
3277
|
+
it 'persists the change in id order' do
|
|
3513
3278
|
expect(reloaded.preference_ids).to eq(
|
|
3514
|
-
|
|
3515
|
-
|
|
3279
|
+
[ preference_two.id, preference_one.id ]
|
|
3280
|
+
)
|
|
3516
3281
|
end
|
|
3517
3282
|
end
|
|
3518
3283
|
|
|
3519
|
-
context
|
|
3520
|
-
|
|
3284
|
+
context 'and the order is changed and an element is added' do
|
|
3521
3285
|
let(:preference_three) do
|
|
3522
|
-
Preference.create!(name:
|
|
3286
|
+
Preference.create!(name: 'three')
|
|
3287
|
+
end
|
|
3288
|
+
let(:reloaded) do
|
|
3289
|
+
Person.find(person.id)
|
|
3523
3290
|
end
|
|
3524
3291
|
|
|
3525
3292
|
before do
|
|
3526
3293
|
person.preference_ids =
|
|
3527
|
-
|
|
3294
|
+
[ preference_two.id, preference_one.id, preference_three.id ]
|
|
3528
3295
|
person.save!
|
|
3529
3296
|
end
|
|
3530
3297
|
|
|
3531
|
-
|
|
3532
|
-
Person.find(person.id)
|
|
3533
|
-
end
|
|
3534
|
-
|
|
3535
|
-
it "also persists the change in id order" do
|
|
3298
|
+
it 'also persists the change in id order' do
|
|
3536
3299
|
expect(reloaded.preference_ids).to eq(
|
|
3537
|
-
|
|
3538
|
-
|
|
3300
|
+
[ preference_two.id, preference_one.id, preference_three.id ]
|
|
3301
|
+
)
|
|
3539
3302
|
end
|
|
3540
3303
|
end
|
|
3541
3304
|
|
|
3542
|
-
context
|
|
3543
|
-
|
|
3305
|
+
context 'and the order is changed and an element is removed' do
|
|
3544
3306
|
let(:preference_three) do
|
|
3545
|
-
Preference.create!(name:
|
|
3307
|
+
Preference.create!(name: 'three')
|
|
3308
|
+
end
|
|
3309
|
+
let(:reloaded) do
|
|
3310
|
+
Person.find(person.id)
|
|
3546
3311
|
end
|
|
3547
3312
|
|
|
3548
3313
|
before do
|
|
3549
3314
|
person.preference_ids =
|
|
3550
|
-
|
|
3315
|
+
[ preference_one.id, preference_two.id, preference_three.id ]
|
|
3551
3316
|
person.save!
|
|
3552
3317
|
person.preference_ids =
|
|
3553
|
-
|
|
3318
|
+
[ preference_three.id, preference_two.id ]
|
|
3554
3319
|
person.save!
|
|
3555
3320
|
end
|
|
3556
3321
|
|
|
3557
|
-
|
|
3558
|
-
Person.find(person.id)
|
|
3559
|
-
end
|
|
3560
|
-
|
|
3561
|
-
it "also persists the change in id order" do
|
|
3322
|
+
it 'also persists the change in id order' do
|
|
3562
3323
|
expect(reloaded.preference_ids).to eq(
|
|
3563
|
-
|
|
3564
|
-
|
|
3324
|
+
[ preference_three.id, preference_two.id ]
|
|
3325
|
+
)
|
|
3565
3326
|
end
|
|
3566
3327
|
end
|
|
3567
3328
|
end
|
|
3568
3329
|
|
|
3569
|
-
context
|
|
3570
|
-
|
|
3330
|
+
context 'when adding a document' do
|
|
3571
3331
|
let(:person) do
|
|
3572
3332
|
Person.new
|
|
3573
3333
|
end
|
|
@@ -3580,8 +3340,7 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
3580
3340
|
person.preferences.push(preference_one)
|
|
3581
3341
|
end
|
|
3582
3342
|
|
|
3583
|
-
context
|
|
3584
|
-
|
|
3343
|
+
context 'when chaining a second add' do
|
|
3585
3344
|
let(:preference_two) do
|
|
3586
3345
|
Preference.new
|
|
3587
3346
|
end
|
|
@@ -3590,16 +3349,14 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
3590
3349
|
first_add.push(preference_two)
|
|
3591
3350
|
end
|
|
3592
3351
|
|
|
3593
|
-
it
|
|
3352
|
+
it 'adds both documents' do
|
|
3594
3353
|
expect(result).to eq([ preference_one, preference_two ])
|
|
3595
3354
|
end
|
|
3596
3355
|
end
|
|
3597
3356
|
end
|
|
3598
3357
|
|
|
3599
|
-
context
|
|
3600
|
-
|
|
3601
|
-
context "when the relation exists" do
|
|
3602
|
-
|
|
3358
|
+
context 'when setting the relation via the foreign key' do
|
|
3359
|
+
context 'when the relation exists' do
|
|
3603
3360
|
let!(:person) do
|
|
3604
3361
|
Person.create!
|
|
3605
3362
|
end
|
|
@@ -3616,14 +3373,13 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
3616
3373
|
person.preference_ids = [ pref_two.id ]
|
|
3617
3374
|
end
|
|
3618
3375
|
|
|
3619
|
-
it
|
|
3376
|
+
it 'sets the new documents on the relation' do
|
|
3620
3377
|
expect(person.preferences).to eq([ pref_two ])
|
|
3621
3378
|
end
|
|
3622
3379
|
end
|
|
3623
3380
|
end
|
|
3624
3381
|
|
|
3625
|
-
context
|
|
3626
|
-
|
|
3382
|
+
context 'when using a different primary key' do
|
|
3627
3383
|
let(:dog) do
|
|
3628
3384
|
Dog.create!(name: 'Doggie')
|
|
3629
3385
|
end
|
|
@@ -3640,72 +3396,67 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
3640
3396
|
FireHydrant.create!(location: '221B Baker Street')
|
|
3641
3397
|
end
|
|
3642
3398
|
|
|
3643
|
-
context
|
|
3644
|
-
|
|
3399
|
+
context 'when adding to a one-way many to many' do
|
|
3645
3400
|
before do
|
|
3646
3401
|
fire_hydrant.cats.push(cat)
|
|
3647
3402
|
end
|
|
3648
3403
|
|
|
3649
|
-
it
|
|
3650
|
-
expect(fire_hydrant.cat_ids).to eq([cat.name])
|
|
3404
|
+
it 'adds the pk value to the fk set' do
|
|
3405
|
+
expect(fire_hydrant.cat_ids).to eq([ cat.name ])
|
|
3651
3406
|
end
|
|
3652
3407
|
end
|
|
3653
3408
|
|
|
3654
|
-
context
|
|
3655
|
-
|
|
3409
|
+
context 'when adding multiple documents to a one-way many to many' do
|
|
3656
3410
|
before do
|
|
3657
|
-
fire_hydrant.cats.push([cat, another_cat])
|
|
3411
|
+
fire_hydrant.cats.push([ cat, another_cat ])
|
|
3658
3412
|
end
|
|
3659
3413
|
|
|
3660
|
-
it
|
|
3661
|
-
expect(fire_hydrant.cat_ids).to eq([cat.name, another_cat.name])
|
|
3414
|
+
it 'adds the pk values to the fk set' do
|
|
3415
|
+
expect(fire_hydrant.cat_ids).to eq([ cat.name, another_cat.name ])
|
|
3662
3416
|
end
|
|
3663
3417
|
end
|
|
3664
3418
|
|
|
3665
|
-
context
|
|
3666
|
-
|
|
3419
|
+
context 'when adding a new document to a one-way many to many' do
|
|
3667
3420
|
before do
|
|
3668
3421
|
fire_hydrant.cats.build name: 'Kitten'
|
|
3669
3422
|
end
|
|
3670
3423
|
|
|
3671
|
-
it
|
|
3672
|
-
expect(fire_hydrant.cat_ids).to eq(['Kitten'])
|
|
3424
|
+
it 'adds the pk values to the fk set' do
|
|
3425
|
+
expect(fire_hydrant.cat_ids).to eq([ 'Kitten' ])
|
|
3673
3426
|
end
|
|
3674
3427
|
end
|
|
3675
3428
|
|
|
3676
|
-
context
|
|
3677
|
-
|
|
3429
|
+
context 'when adding to a two-way many to many' do
|
|
3678
3430
|
before do
|
|
3679
3431
|
fire_hydrant.dogs.push(dog)
|
|
3680
3432
|
end
|
|
3681
3433
|
|
|
3682
|
-
it
|
|
3683
|
-
expect(fire_hydrant.dog_ids).to eq([dog.name])
|
|
3434
|
+
it 'adds the pk value to the fk set' do
|
|
3435
|
+
expect(fire_hydrant.dog_ids).to eq([ dog.name ])
|
|
3684
3436
|
end
|
|
3685
3437
|
|
|
3686
|
-
it
|
|
3687
|
-
expect(dog.fire_hydrant_ids).to eq([fire_hydrant.location])
|
|
3438
|
+
it 'adds the base pk value to the inverse fk set' do
|
|
3439
|
+
expect(dog.fire_hydrant_ids).to eq([ fire_hydrant.location ])
|
|
3688
3440
|
end
|
|
3689
3441
|
end
|
|
3690
3442
|
|
|
3691
|
-
context
|
|
3692
|
-
|
|
3443
|
+
context 'when deleting from a two-way many to many' do
|
|
3693
3444
|
before do
|
|
3694
3445
|
dog.fire_hydrants.push(fire_hydrant)
|
|
3695
3446
|
fire_hydrant.dogs.delete(dog)
|
|
3696
3447
|
end
|
|
3697
3448
|
|
|
3698
|
-
it
|
|
3449
|
+
it 'removes the pk value from the fk set' do
|
|
3699
3450
|
expect(fire_hydrant.dog_ids).to eq([])
|
|
3700
3451
|
end
|
|
3701
3452
|
|
|
3702
|
-
it
|
|
3453
|
+
it 'removes the base pk value from the inverse fk set' do
|
|
3703
3454
|
expect(dog.fire_hydrant_ids).to eq([])
|
|
3704
3455
|
end
|
|
3705
3456
|
end
|
|
3706
3457
|
end
|
|
3707
3458
|
|
|
3708
|
-
context
|
|
3459
|
+
context 'HABTM' do
|
|
3709
3460
|
before do
|
|
3710
3461
|
class Project
|
|
3711
3462
|
include Mongoid::Document
|
|
@@ -3728,18 +3479,18 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
3728
3479
|
end
|
|
3729
3480
|
end
|
|
3730
3481
|
|
|
3731
|
-
it
|
|
3482
|
+
it 'assigns relation from both sides' do
|
|
3732
3483
|
p1 = Project.create! name: 'Foo'
|
|
3733
3484
|
p2 = Project.create! name: 'Bar'
|
|
3734
3485
|
d1 = Distributor.create! name: 'Rock'
|
|
3735
3486
|
d2 = Distributor.create! name: 'Soul'
|
|
3736
3487
|
|
|
3737
3488
|
p1.distributors << d1
|
|
3738
|
-
expect(p1.d_ids).to
|
|
3739
|
-
expect(d1.p_ids).to
|
|
3489
|
+
expect(p1.d_ids).to contain_exactly(d1.id)
|
|
3490
|
+
expect(d1.p_ids).to contain_exactly(p1.id)
|
|
3740
3491
|
d2.projects << p2
|
|
3741
|
-
expect(d2.p_ids).to
|
|
3742
|
-
expect(p2.d_ids).to
|
|
3492
|
+
expect(d2.p_ids).to contain_exactly(p2.id)
|
|
3493
|
+
expect(p2.d_ids).to contain_exactly(d2.id)
|
|
3743
3494
|
end
|
|
3744
3495
|
end
|
|
3745
3496
|
|
|
@@ -3747,24 +3498,24 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
3747
3498
|
let!(:contract) { HabtmmContract.create! }
|
|
3748
3499
|
let!(:signature) { contract.signatures.create! }
|
|
3749
3500
|
|
|
3750
|
-
it
|
|
3751
|
-
expect(signature.favorite_signature).to
|
|
3501
|
+
it 'is nil' do
|
|
3502
|
+
expect(signature.favorite_signature).to be_nil
|
|
3752
3503
|
end
|
|
3753
3504
|
end
|
|
3754
3505
|
|
|
3755
|
-
context
|
|
3506
|
+
context 'when setting an association on a model that uses the class_name option' do
|
|
3756
3507
|
let!(:contract) { HabtmmContract.create! }
|
|
3757
|
-
let!(:signature) { HabtmmSignature.create!(contracts: [contract]) }
|
|
3508
|
+
let!(:signature) { HabtmmSignature.create!(contracts: [ contract ]) }
|
|
3758
3509
|
|
|
3759
|
-
it
|
|
3760
|
-
expect(signature.contracts.first.signature_ids).to eq([signature.id])
|
|
3510
|
+
it 'populates the inverse foreign key' do
|
|
3511
|
+
expect(signature.contracts.first.signature_ids).to eq([ signature.id ])
|
|
3761
3512
|
end
|
|
3762
3513
|
end
|
|
3763
3514
|
|
|
3764
|
-
context
|
|
3765
|
-
it
|
|
3766
|
-
expect(Dog.aliased_associations[
|
|
3767
|
-
expect(Breed.aliased_associations[
|
|
3515
|
+
context 'when there is a foreign key in the aliased associations' do
|
|
3516
|
+
it 'has the correct aliases' do
|
|
3517
|
+
expect(Dog.aliased_associations['breed_ids']).to eq('breeds')
|
|
3518
|
+
expect(Breed.aliased_associations['dog_ids']).to eq('dogs')
|
|
3768
3519
|
end
|
|
3769
3520
|
end
|
|
3770
3521
|
|
|
@@ -3777,21 +3528,18 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
3777
3528
|
# when retrieving a document from the database, the attributes hash is actually
|
|
3778
3529
|
# a BSON::Document, which applies a transformation to the array before
|
|
3779
3530
|
# storing it.
|
|
3780
|
-
context
|
|
3531
|
+
context 'when executing concat on foreign key array from the db' do
|
|
3781
3532
|
before do
|
|
3782
3533
|
HabtmmContract.create!
|
|
3783
3534
|
HabtmmSignature.create!
|
|
3535
|
+
contract.signature_ids.concat([ signature.id ])
|
|
3784
3536
|
end
|
|
3785
3537
|
|
|
3786
3538
|
let!(:contract) { HabtmmContract.first }
|
|
3787
3539
|
let!(:signature) { HabtmmSignature.first }
|
|
3788
3540
|
|
|
3789
|
-
|
|
3790
|
-
contract.signature_ids.
|
|
3791
|
-
end
|
|
3792
|
-
|
|
3793
|
-
it "works on the first attempt" do
|
|
3794
|
-
expect(contract.signature_ids).to eq([signature.id])
|
|
3541
|
+
it 'works on the first attempt' do
|
|
3542
|
+
expect(contract.signature_ids).to eq([ signature.id ])
|
|
3795
3543
|
end
|
|
3796
3544
|
end
|
|
3797
3545
|
end
|