mongoid 7.0.13 → 9.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +41 -41
- data/LICENSE +1 -1
- data/README.md +29 -25
- data/Rakefile +150 -40
- data/lib/config/locales/en.yml +364 -108
- data/lib/mongoid/association/accessors.rb +205 -107
- data/lib/mongoid/association/bindable.rb +102 -56
- data/lib/mongoid/association/builders.rb +24 -26
- data/lib/mongoid/association/constrainable.rb +8 -11
- data/lib/mongoid/association/depending.rb +65 -59
- data/lib/mongoid/association/eager.rb +203 -0
- data/lib/mongoid/association/eager_load/discriminated_inclusion.rb +88 -0
- data/lib/mongoid/association/eager_load/embedded_distributor.rb +151 -0
- data/lib/mongoid/association/eager_load/inclusion.rb +173 -0
- data/lib/mongoid/association/eager_load/inclusion_tree.rb +96 -0
- data/lib/mongoid/association/eager_load/lookup_pipeline.rb +121 -0
- data/lib/mongoid/association/eager_load/polymorphic_preloader.rb +61 -0
- data/lib/mongoid/association/eager_load/polymorphic_targets.rb +151 -0
- data/lib/mongoid/association/eager_loadable.rb +135 -15
- data/lib/mongoid/association/embedded/batchable.rb +87 -75
- data/lib/mongoid/association/embedded/cyclic.rb +16 -25
- data/lib/mongoid/association/embedded/eager.rb +18 -0
- data/lib/mongoid/association/embedded/embedded_in/binding.rb +30 -14
- data/lib/mongoid/association/embedded/embedded_in/buildable.rb +10 -7
- data/lib/mongoid/association/embedded/embedded_in/proxy.rb +39 -32
- data/lib/mongoid/association/embedded/embedded_in.rb +37 -46
- data/lib/mongoid/association/embedded/embeds_many/binding.rb +7 -11
- data/lib/mongoid/association/embedded/embeds_many/buildable.rb +11 -7
- data/lib/mongoid/association/embedded/embeds_many/proxy.rb +309 -243
- data/lib/mongoid/association/embedded/embeds_many.rb +32 -56
- data/lib/mongoid/association/embedded/embeds_one/binding.rb +6 -11
- data/lib/mongoid/association/embedded/embeds_one/buildable.rb +25 -11
- data/lib/mongoid/association/embedded/embeds_one/proxy.rb +145 -53
- data/lib/mongoid/association/embedded/embeds_one.rb +30 -50
- data/lib/mongoid/association/embedded.rb +3 -0
- data/lib/mongoid/association/macros.rb +97 -56
- data/lib/mongoid/association/many.rb +99 -36
- data/lib/mongoid/association/marshalable.rb +5 -7
- data/lib/mongoid/association/nested/many.rb +86 -47
- data/lib/mongoid/association/nested/nested_buildable.rb +58 -21
- data/lib/mongoid/association/nested/one.rb +81 -36
- data/lib/mongoid/association/nested.rb +4 -5
- data/lib/mongoid/association/one.rb +5 -14
- data/lib/mongoid/association/options.rb +48 -53
- data/lib/mongoid/association/proxy.rb +65 -54
- data/lib/mongoid/association/referenced/auto_save.rb +59 -20
- data/lib/mongoid/association/referenced/belongs_to/binding.rb +21 -22
- data/lib/mongoid/association/referenced/belongs_to/buildable.rb +32 -11
- data/lib/mongoid/association/referenced/belongs_to/eager.rb +47 -6
- data/lib/mongoid/association/referenced/belongs_to/proxy.rb +35 -42
- data/lib/mongoid/association/referenced/belongs_to.rb +77 -77
- data/lib/mongoid/association/referenced/counter_cache.rb +24 -35
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/binding.rb +26 -15
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb +12 -7
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb +3 -4
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb +202 -133
- data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +81 -83
- data/lib/mongoid/association/referenced/has_many/binding.rb +3 -7
- data/lib/mongoid/association/referenced/has_many/buildable.rb +15 -8
- data/lib/mongoid/association/referenced/has_many/eager.rb +3 -4
- data/lib/mongoid/association/referenced/has_many/enumerable.rb +601 -456
- data/lib/mongoid/association/referenced/has_many/proxy.rb +344 -216
- data/lib/mongoid/association/referenced/has_many.rb +58 -74
- data/lib/mongoid/association/referenced/has_many_through/eager.rb +99 -0
- data/lib/mongoid/association/referenced/has_many_through/proxy.rb +68 -0
- data/lib/mongoid/association/referenced/has_many_through.rb +201 -0
- data/lib/mongoid/association/referenced/has_one/binding.rb +4 -10
- data/lib/mongoid/association/referenced/has_one/buildable.rb +29 -17
- data/lib/mongoid/association/referenced/has_one/eager.rb +4 -6
- data/lib/mongoid/association/referenced/has_one/proxy.rb +51 -47
- data/lib/mongoid/association/referenced/has_one.rb +45 -55
- data/lib/mongoid/association/referenced/has_one_through/eager.rb +65 -0
- data/lib/mongoid/association/referenced/has_one_through/proxy.rb +30 -0
- data/lib/mongoid/association/referenced/has_one_through.rb +167 -0
- data/lib/mongoid/association/referenced/syncable.rb +44 -62
- data/lib/mongoid/association/referenced/with_polymorphic_criteria.rb +41 -0
- data/lib/mongoid/association/referenced.rb +4 -0
- data/lib/mongoid/association/reflections.rb +11 -12
- data/lib/mongoid/association/relatable.rb +173 -146
- data/lib/mongoid/association.rb +35 -40
- data/lib/mongoid/atomic/modifiers.rb +91 -74
- data/lib/mongoid/atomic/paths/embedded/many.rb +25 -11
- data/lib/mongoid/atomic/paths/embedded/one.rb +6 -10
- data/lib/mongoid/atomic/paths/embedded.rb +5 -8
- data/lib/mongoid/atomic/paths/root.rb +5 -10
- data/lib/mongoid/atomic/paths.rb +4 -3
- data/lib/mongoid/atomic.rb +41 -89
- data/lib/mongoid/atomic_update_preparer.rb +89 -0
- data/lib/mongoid/attributes/dynamic.rb +12 -27
- data/lib/mongoid/attributes/embedded.rb +34 -0
- data/lib/mongoid/attributes/nested.rb +22 -25
- data/lib/mongoid/attributes/processing.rb +51 -34
- data/lib/mongoid/attributes/projector.rb +107 -0
- data/lib/mongoid/attributes/readonly.rb +15 -17
- data/lib/mongoid/attributes.rb +119 -126
- data/lib/mongoid/cacheable.rb +30 -14
- data/lib/mongoid/changeable.rb +196 -99
- data/lib/mongoid/clients/factory.rb +73 -17
- data/lib/mongoid/clients/options.rb +140 -19
- data/lib/mongoid/clients/sessions.rb +239 -87
- data/lib/mongoid/clients/storage_options.rb +42 -17
- data/lib/mongoid/clients/validators/storage.rb +6 -25
- data/lib/mongoid/clients/validators.rb +3 -2
- data/lib/mongoid/clients.rb +44 -22
- data/lib/mongoid/collection_configurable.rb +63 -0
- data/lib/mongoid/composable.rb +40 -39
- data/lib/mongoid/config/defaults.rb +46 -0
- data/lib/mongoid/config/encryption.rb +228 -0
- data/lib/mongoid/config/environment.rb +26 -13
- data/lib/mongoid/config/introspection.rb +150 -0
- data/lib/mongoid/config/options.rb +37 -26
- data/lib/mongoid/config/validators/async_query_executor.rb +31 -0
- data/lib/mongoid/config/validators/client.rb +20 -37
- data/lib/mongoid/config/validators/option.rb +5 -7
- data/lib/mongoid/config/validators.rb +5 -3
- data/lib/mongoid/config.rb +418 -60
- data/lib/mongoid/contextual/aggregable/memory.rb +45 -28
- data/lib/mongoid/contextual/aggregable/mongo.rb +49 -47
- data/lib/mongoid/contextual/aggregable/none.rb +64 -0
- data/lib/mongoid/contextual/aggregable.rb +16 -0
- data/lib/mongoid/contextual/atomic.rb +126 -45
- data/lib/mongoid/contextual/command.rb +3 -6
- data/lib/mongoid/contextual/map_reduce.rb +23 -49
- data/lib/mongoid/contextual/memory.rb +448 -121
- data/lib/mongoid/contextual/mongo/documents_loader.rb +177 -0
- data/lib/mongoid/contextual/mongo.rb +707 -351
- data/lib/mongoid/contextual/none.rb +249 -33
- data/lib/mongoid/contextual/queryable.rb +6 -6
- data/lib/mongoid/contextual.rb +40 -15
- data/lib/mongoid/copyable.rb +45 -21
- data/lib/mongoid/criteria/findable.rb +65 -27
- data/lib/mongoid/criteria/includable.rb +97 -45
- data/lib/mongoid/criteria/inspectable.rb +6 -6
- data/lib/mongoid/criteria/marshalable.rb +15 -10
- data/lib/mongoid/criteria/modifiable.rb +8 -28
- data/lib/mongoid/criteria/options.rb +2 -5
- data/lib/mongoid/criteria/permission.rb +40 -37
- data/lib/mongoid/criteria/queryable/aggregable.rb +13 -24
- data/lib/mongoid/criteria/queryable/expandable.rb +64 -0
- data/lib/mongoid/criteria/queryable/extensions/array.rb +14 -50
- data/lib/mongoid/criteria/queryable/extensions/big_decimal.rb +29 -13
- data/lib/mongoid/criteria/queryable/extensions/boolean.rb +6 -9
- data/lib/mongoid/criteria/queryable/extensions/date.rb +13 -16
- data/lib/mongoid/criteria/queryable/extensions/date_time.rb +14 -18
- data/lib/mongoid/criteria/queryable/extensions/hash.rb +5 -44
- data/lib/mongoid/criteria/queryable/extensions/nil_class.rb +23 -24
- data/lib/mongoid/criteria/queryable/extensions/numeric.rb +31 -29
- data/lib/mongoid/criteria/queryable/extensions/object.rb +15 -37
- data/lib/mongoid/criteria/queryable/extensions/range.rb +52 -22
- data/lib/mongoid/criteria/queryable/extensions/regexp.rb +22 -25
- data/lib/mongoid/criteria/queryable/extensions/set.rb +8 -10
- data/lib/mongoid/criteria/queryable/extensions/string.rb +24 -41
- data/lib/mongoid/criteria/queryable/extensions/symbol.rb +10 -28
- data/lib/mongoid/criteria/queryable/extensions/time.rb +11 -14
- data/lib/mongoid/criteria/queryable/extensions/time_with_zone.rb +12 -20
- data/lib/mongoid/criteria/queryable/extensions.rb +19 -24
- data/lib/mongoid/criteria/queryable/key.rb +49 -30
- data/lib/mongoid/criteria/queryable/macroable.rb +3 -6
- data/lib/mongoid/criteria/queryable/mergeable.rb +203 -46
- data/lib/mongoid/criteria/queryable/optional.rb +32 -81
- data/lib/mongoid/criteria/queryable/options.rb +24 -29
- data/lib/mongoid/criteria/queryable/pipeline.rb +11 -23
- data/lib/mongoid/criteria/queryable/selectable.rb +558 -242
- data/lib/mongoid/criteria/queryable/selector.rb +134 -43
- data/lib/mongoid/criteria/queryable/smash.rb +41 -21
- data/lib/mongoid/criteria/queryable/storable.rb +219 -0
- data/lib/mongoid/criteria/queryable.rb +41 -27
- data/lib/mongoid/criteria/scopable.rb +37 -30
- data/lib/mongoid/criteria/translator.rb +43 -0
- data/lib/mongoid/criteria.rb +257 -152
- data/lib/mongoid/deprecable.rb +78 -0
- data/lib/mongoid/deprecation.rb +25 -0
- data/lib/mongoid/document.rb +277 -171
- data/lib/mongoid/encryptable.rb +98 -0
- data/lib/mongoid/equality.rb +29 -31
- data/lib/mongoid/errors/ambiguous_relationship.rb +6 -9
- data/lib/mongoid/errors/attribute_not_loaded.rb +31 -0
- data/lib/mongoid/errors/callback.rb +3 -6
- data/lib/mongoid/errors/config_redactor.rb +43 -0
- data/lib/mongoid/errors/create_collection_failure.rb +31 -0
- data/lib/mongoid/errors/criteria_argument_required.rb +16 -0
- data/lib/mongoid/errors/delete_restriction.rb +11 -16
- data/lib/mongoid/errors/document_not_destroyed.rb +5 -10
- data/lib/mongoid/errors/document_not_found.rb +37 -27
- data/lib/mongoid/errors/drop_collection_failure.rb +25 -0
- data/lib/mongoid/errors/empty_config_file.rb +23 -0
- data/lib/mongoid/errors/immutable_attribute.rb +24 -0
- data/lib/mongoid/errors/in_memory_collation_not_supported.rb +3 -6
- data/lib/mongoid/errors/in_memory_regexp_timeout.rb +26 -0
- data/lib/mongoid/errors/invalid_around_callback.rb +16 -0
- data/lib/mongoid/errors/invalid_async_query_executor.rb +23 -0
- data/lib/mongoid/errors/invalid_auto_encryption_configuration.rb +30 -0
- data/lib/mongoid/errors/invalid_collection.rb +3 -3
- data/lib/mongoid/errors/invalid_config_file.rb +23 -0
- data/lib/mongoid/errors/invalid_config_option.rb +5 -8
- data/lib/mongoid/errors/invalid_dependent_strategy.rb +9 -13
- data/lib/mongoid/errors/invalid_discriminator_key_target.rb +23 -0
- data/lib/mongoid/errors/invalid_dot_dollar_assignment.rb +21 -0
- data/lib/mongoid/errors/invalid_elem_match_operator.rb +29 -0
- data/lib/mongoid/errors/invalid_estimated_count_criteria.rb +24 -0
- data/lib/mongoid/errors/invalid_estimated_count_scoping.rb +24 -0
- data/lib/mongoid/errors/invalid_expression_operator.rb +24 -0
- data/lib/mongoid/errors/invalid_field.rb +10 -11
- data/lib/mongoid/errors/invalid_field_operator.rb +29 -0
- data/lib/mongoid/errors/invalid_field_option.rb +4 -7
- data/lib/mongoid/errors/invalid_field_type.rb +24 -0
- data/lib/mongoid/errors/invalid_find.rb +3 -6
- data/lib/mongoid/errors/invalid_global_executor_concurrency.rb +20 -0
- data/lib/mongoid/errors/invalid_includes.rb +5 -8
- data/lib/mongoid/errors/invalid_index.rb +3 -6
- data/lib/mongoid/errors/invalid_options.rb +5 -8
- data/lib/mongoid/errors/invalid_path.rb +3 -6
- data/lib/mongoid/errors/invalid_persistence_option.rb +6 -11
- data/lib/mongoid/errors/invalid_query.rb +36 -0
- data/lib/mongoid/errors/invalid_relation.rb +15 -22
- data/lib/mongoid/errors/invalid_relation_option.rb +8 -11
- data/lib/mongoid/errors/invalid_scope.rb +3 -6
- data/lib/mongoid/errors/invalid_session_nesting.rb +14 -0
- data/lib/mongoid/errors/invalid_set_polymorphic_relation.rb +8 -9
- data/lib/mongoid/errors/invalid_storage_options.rb +4 -7
- data/lib/mongoid/errors/invalid_time.rb +3 -6
- data/lib/mongoid/errors/invalid_transaction_nesting.rb +14 -0
- data/lib/mongoid/errors/inverse_not_found.rb +4 -7
- data/lib/mongoid/errors/mixed_client_configuration.rb +4 -7
- data/lib/mongoid/errors/mixed_relations.rb +3 -5
- data/lib/mongoid/errors/mongoid_error.rb +13 -23
- data/lib/mongoid/errors/nested_attributes_metadata_not_found.rb +4 -7
- data/lib/mongoid/errors/no_client_config.rb +5 -8
- data/lib/mongoid/errors/no_client_database.rb +5 -8
- data/lib/mongoid/errors/no_client_hosts.rb +5 -8
- data/lib/mongoid/errors/no_clients_config.rb +3 -6
- data/lib/mongoid/errors/no_default_client.rb +4 -7
- data/lib/mongoid/errors/no_encryption_schema.rb +28 -0
- data/lib/mongoid/errors/no_environment.rb +3 -6
- data/lib/mongoid/errors/no_map_reduce_output.rb +3 -6
- data/lib/mongoid/errors/no_metadata.rb +3 -6
- data/lib/mongoid/errors/no_parent.rb +3 -6
- data/lib/mongoid/errors/readonly_association.rb +29 -0
- data/lib/mongoid/errors/readonly_attribute.rb +4 -7
- data/lib/mongoid/errors/readonly_document.rb +4 -9
- data/lib/mongoid/errors/rollback.rb +13 -0
- data/lib/mongoid/errors/scope_overwrite.rb +4 -4
- data/lib/mongoid/errors/sessions_not_supported.rb +14 -0
- data/lib/mongoid/errors/too_many_nested_attribute_records.rb +4 -3
- data/lib/mongoid/errors/transaction_error.rb +22 -0
- data/lib/mongoid/errors/transactions_not_supported.rb +14 -0
- data/lib/mongoid/errors/unknown_attribute.rb +4 -7
- data/lib/mongoid/errors/unknown_model.rb +3 -6
- data/lib/mongoid/errors/unrecognized_model_alias.rb +53 -0
- data/lib/mongoid/errors/unrecognized_resolver.rb +27 -0
- data/lib/mongoid/errors/unregistered_class.rb +47 -0
- data/lib/mongoid/errors/unsaved_document.rb +4 -4
- data/lib/mongoid/errors/unsupported_isolation_level.rb +22 -0
- data/lib/mongoid/errors/unsupported_javascript.rb +3 -6
- data/lib/mongoid/errors/validations.rb +5 -5
- data/lib/mongoid/errors.rb +82 -51
- data/lib/mongoid/evolvable.rb +4 -7
- data/lib/mongoid/extensions/array.rb +32 -55
- data/lib/mongoid/extensions/big_decimal.rb +45 -24
- data/lib/mongoid/extensions/binary.rb +42 -0
- data/lib/mongoid/extensions/boolean.rb +12 -8
- data/lib/mongoid/extensions/bson_document.rb +28 -0
- data/lib/mongoid/extensions/date.rb +38 -30
- data/lib/mongoid/extensions/date_time.rb +8 -22
- data/lib/mongoid/extensions/decimal128.rb +4 -8
- data/lib/mongoid/extensions/false_class.rb +11 -13
- data/lib/mongoid/extensions/float.rb +15 -19
- data/lib/mongoid/extensions/hash.rb +31 -116
- data/lib/mongoid/extensions/integer.rb +17 -21
- data/lib/mongoid/extensions/module.rb +6 -7
- data/lib/mongoid/extensions/nil_class.rb +6 -7
- data/lib/mongoid/extensions/object.rb +42 -84
- data/lib/mongoid/extensions/object_id.rb +5 -11
- data/lib/mongoid/extensions/range.rb +49 -25
- data/lib/mongoid/extensions/raw_value.rb +31 -0
- data/lib/mongoid/extensions/regexp.rb +16 -10
- data/lib/mongoid/extensions/set.rb +27 -15
- data/lib/mongoid/extensions/string.rb +40 -67
- data/lib/mongoid/extensions/symbol.rb +11 -26
- data/lib/mongoid/extensions/time.rb +49 -47
- data/lib/mongoid/extensions/time_with_zone.rb +30 -12
- data/lib/mongoid/extensions/true_class.rb +11 -13
- data/lib/mongoid/extensions/vector.rb +64 -0
- data/lib/mongoid/extensions.rb +28 -41
- data/lib/mongoid/factory.rb +205 -28
- data/lib/mongoid/field_readable.rb +69 -0
- data/lib/mongoid/fields/encrypted.rb +47 -0
- data/lib/mongoid/fields/foreign_key.rb +48 -35
- data/lib/mongoid/fields/localized.rb +33 -21
- data/lib/mongoid/fields/standard.rb +31 -48
- data/lib/mongoid/fields/validators/macro.rb +48 -45
- data/lib/mongoid/fields/validators.rb +3 -2
- data/lib/mongoid/fields.rb +499 -117
- data/lib/mongoid/findable.rb +174 -64
- data/lib/mongoid/identifiable.rb +28 -0
- data/lib/mongoid/indexable/specification.rb +76 -39
- data/lib/mongoid/indexable/validators/options.rb +36 -43
- data/lib/mongoid/indexable.rb +57 -58
- data/lib/mongoid/inspectable.rb +37 -15
- data/lib/mongoid/interceptable.rb +235 -66
- data/lib/mongoid/loadable.rb +146 -0
- data/lib/mongoid/loggable.rb +12 -18
- data/lib/mongoid/matchable.rb +7 -154
- data/lib/mongoid/matcher/all.rb +34 -0
- data/lib/mongoid/matcher/and.rb +32 -0
- data/lib/mongoid/matcher/bits.rb +65 -0
- data/lib/mongoid/matcher/bits_all_clear.rb +39 -0
- data/lib/mongoid/matcher/bits_all_set.rb +39 -0
- data/lib/mongoid/matcher/bits_any_clear.rb +39 -0
- data/lib/mongoid/matcher/bits_any_set.rb +39 -0
- data/lib/mongoid/matcher/elem_match.rb +49 -0
- data/lib/mongoid/matcher/elem_match_expression.rb +29 -0
- data/lib/mongoid/matcher/eq.rb +23 -0
- data/lib/mongoid/matcher/eq_impl.rb +79 -0
- data/lib/mongoid/matcher/eq_impl_with_regexp.rb +33 -0
- data/lib/mongoid/matcher/exists.rb +27 -0
- data/lib/mongoid/matcher/expression.rb +43 -0
- data/lib/mongoid/matcher/expression_operator.rb +31 -0
- data/lib/mongoid/matcher/field_expression.rb +74 -0
- data/lib/mongoid/matcher/field_operator.rb +76 -0
- data/lib/mongoid/matcher/gt.rb +29 -0
- data/lib/mongoid/matcher/gte.rb +29 -0
- data/lib/mongoid/matcher/in.rb +37 -0
- data/lib/mongoid/matcher/lt.rb +29 -0
- data/lib/mongoid/matcher/lte.rb +29 -0
- data/lib/mongoid/matcher/mod.rb +29 -0
- data/lib/mongoid/matcher/ne.rb +28 -0
- data/lib/mongoid/matcher/nin.rb +23 -0
- data/lib/mongoid/matcher/nor.rb +34 -0
- data/lib/mongoid/matcher/not.rb +42 -0
- data/lib/mongoid/matcher/or.rb +32 -0
- data/lib/mongoid/matcher/regex.rb +61 -0
- data/lib/mongoid/matcher/regexp_budget.rb +372 -0
- data/lib/mongoid/matcher/size.rb +39 -0
- data/lib/mongoid/matcher/type.rb +119 -0
- data/lib/mongoid/matcher.rb +141 -0
- data/lib/mongoid/model_resolver.rb +155 -0
- data/lib/mongoid/persistable/creatable.rb +50 -36
- data/lib/mongoid/persistable/deletable.rb +18 -30
- data/lib/mongoid/persistable/destroyable.rb +38 -14
- data/lib/mongoid/persistable/incrementable.rb +8 -10
- data/lib/mongoid/persistable/logical.rb +7 -10
- data/lib/mongoid/persistable/maxable.rb +35 -0
- data/lib/mongoid/persistable/minable.rb +35 -0
- data/lib/mongoid/persistable/multipliable.rb +34 -0
- data/lib/mongoid/persistable/poppable.rb +5 -9
- data/lib/mongoid/persistable/pullable.rb +6 -12
- data/lib/mongoid/persistable/pushable.rb +8 -14
- data/lib/mongoid/persistable/renamable.rb +11 -10
- data/lib/mongoid/persistable/savable.rb +16 -11
- data/lib/mongoid/persistable/settable.rb +7 -18
- data/lib/mongoid/persistable/unsettable.rb +11 -12
- data/lib/mongoid/persistable/updatable.rb +113 -35
- data/lib/mongoid/persistable/upsertable.rb +37 -12
- data/lib/mongoid/persistable.rb +169 -60
- data/lib/mongoid/persistence_context.rb +189 -75
- data/lib/mongoid/pluckable.rb +130 -0
- data/lib/mongoid/positional.rb +7 -15
- data/lib/mongoid/railtie.rb +36 -32
- data/lib/mongoid/railties/bson_object_id_serializer.rb +46 -0
- data/lib/mongoid/railties/console_sandbox.rb +40 -0
- data/lib/mongoid/railties/controller_runtime.rb +34 -20
- data/lib/mongoid/railties/database.rake +53 -29
- data/lib/mongoid/reloadable.rb +56 -45
- data/lib/mongoid/scopable.rb +50 -79
- data/lib/mongoid/search_indexable.rb +538 -0
- data/lib/mongoid/selectable.rb +8 -19
- data/lib/mongoid/serializable.rb +46 -40
- data/lib/mongoid/shardable.rb +102 -23
- data/lib/mongoid/stateful.rb +67 -26
- data/lib/mongoid/stringified_symbol.rb +61 -0
- data/lib/mongoid/tasks/database.rake +46 -15
- data/lib/mongoid/tasks/database.rb +204 -31
- data/lib/mongoid/tasks/encryption.rake +59 -0
- data/lib/mongoid/tasks/encryption.rb +103 -0
- data/lib/mongoid/threaded/lifecycle.rb +13 -34
- data/lib/mongoid/threaded.rb +353 -108
- data/lib/mongoid/timestamps/created/short.rb +4 -3
- data/lib/mongoid/timestamps/created.rb +15 -7
- data/lib/mongoid/timestamps/short.rb +2 -1
- data/lib/mongoid/timestamps/timeless.rb +127 -29
- data/lib/mongoid/timestamps/updated/short.rb +4 -3
- data/lib/mongoid/timestamps/updated.rb +6 -9
- data/lib/mongoid/timestamps.rb +7 -7
- data/lib/mongoid/touchable.rb +176 -40
- data/lib/mongoid/traversable.rb +309 -106
- data/lib/mongoid/utils.rb +52 -0
- data/lib/mongoid/validatable/associated.rb +115 -22
- data/lib/mongoid/validatable/format.rb +2 -2
- data/lib/mongoid/validatable/length.rb +2 -2
- data/lib/mongoid/validatable/localizable.rb +3 -6
- data/lib/mongoid/validatable/macros.rb +25 -14
- data/lib/mongoid/validatable/numericality.rb +40 -0
- data/lib/mongoid/validatable/presence.rb +15 -21
- data/lib/mongoid/validatable/queryable.rb +9 -4
- data/lib/mongoid/validatable/uniqueness.rb +40 -53
- data/lib/mongoid/validatable.rb +41 -52
- data/lib/mongoid/version.rb +6 -2
- data/lib/mongoid/warnings.rb +40 -0
- data/lib/mongoid.rb +105 -44
- data/lib/rails/generators/mongoid/config/config_generator.rb +18 -5
- data/lib/rails/generators/mongoid/config/templates/mongoid.rb +24 -0
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +51 -56
- data/lib/rails/generators/mongoid/model/model_generator.rb +10 -8
- data/lib/rails/generators/mongoid_generator.rb +17 -7
- data/lib/rails/mongoid.rb +4 -32
- data/spec/README.md +19 -4
- data/spec/config/mongoid.yml +17 -1
- data/spec/config/mongoid_with_schema_map_uuid.yml +27 -0
- data/spec/integration/active_job_spec.rb +35 -0
- data/spec/integration/app_spec.rb +327 -144
- data/spec/integration/associations/belongs_to_spec.rb +165 -0
- data/spec/integration/associations/embedded_dirty_spec.rb +57 -0
- data/spec/integration/associations/embedded_spec.rb +241 -67
- data/spec/integration/associations/embeds_many_spec.rb +347 -0
- data/spec/integration/associations/embeds_one_spec.rb +62 -0
- data/spec/integration/associations/foreign_key_spec.rb +107 -0
- data/spec/integration/associations/foreign_key_spec_models.rb +64 -0
- data/spec/integration/associations/has_and_belongs_to_many_spec.rb +161 -0
- data/spec/integration/associations/has_many_spec.rb +184 -0
- data/spec/integration/associations/has_one_spec.rb +322 -0
- data/spec/integration/associations/nested_attributes_assignment_spec.rb +113 -0
- data/spec/integration/associations/reverse_population_spec.rb +34 -0
- data/spec/integration/associations/reverse_population_spec_models.rb +36 -0
- data/spec/integration/associations/scope_option_spec.rb +100 -0
- data/spec/integration/atomic/modifiers_spec.rb +137 -0
- data/spec/integration/bson_regexp_raw_spec.rb +19 -0
- data/spec/integration/caching_spec.rb +68 -0
- data/spec/integration/callbacks_models.rb +149 -3
- data/spec/integration/callbacks_spec.rb +422 -34
- data/spec/integration/contextual/empty_spec.rb +141 -0
- data/spec/integration/criteria/alias_query_spec.rb +91 -0
- data/spec/integration/criteria/date_field_spec.rb +1 -2
- data/spec/integration/criteria/default_scope_spec.rb +78 -0
- data/spec/integration/criteria/logical_spec.rb +105 -0
- data/spec/integration/criteria/range_spec.rb +349 -0
- data/spec/integration/criteria/raw_value_spec.rb +538 -0
- data/spec/integration/criteria/time_with_zone_spec.rb +135 -15
- data/spec/integration/discriminator_key_spec.rb +392 -0
- data/spec/integration/discriminator_value_spec.rb +204 -0
- data/spec/integration/document_spec.rb +32 -3
- data/spec/integration/dots_and_dollars_spec.rb +273 -0
- data/spec/integration/encryption_spec.rb +248 -0
- data/spec/integration/i18n_fallbacks_spec.rb +47 -0
- data/spec/integration/isolation_state_spec.rb +241 -0
- data/spec/integration/matcher_examples_spec.rb +743 -0
- data/spec/integration/matcher_operator_data/all.yml +140 -0
- data/spec/integration/matcher_operator_data/and.yml +93 -0
- data/spec/integration/matcher_operator_data/bits_all_clear.yml +144 -0
- data/spec/integration/matcher_operator_data/bits_all_set.yml +144 -0
- data/spec/integration/matcher_operator_data/bits_any_clear.yml +144 -0
- data/spec/integration/matcher_operator_data/bits_any_set.yml +144 -0
- data/spec/integration/matcher_operator_data/comment.yml +22 -0
- data/spec/integration/matcher_operator_data/elem_match.yml +409 -0
- data/spec/integration/matcher_operator_data/elem_match_expr.yml +213 -0
- data/spec/integration/matcher_operator_data/eq.yml +191 -0
- data/spec/integration/matcher_operator_data/exists.yml +213 -0
- data/spec/integration/matcher_operator_data/generic_op.yml +17 -0
- data/spec/integration/matcher_operator_data/gt.yml +132 -0
- data/spec/integration/matcher_operator_data/gt_types.yml +63 -0
- data/spec/integration/matcher_operator_data/gte.yml +132 -0
- data/spec/integration/matcher_operator_data/gte_types.yml +15 -0
- data/spec/integration/matcher_operator_data/implicit.yml +331 -0
- data/spec/integration/matcher_operator_data/implicit_traversal.yml +112 -0
- data/spec/integration/matcher_operator_data/in.yml +210 -0
- data/spec/integration/matcher_operator_data/invalid_op.yml +59 -0
- data/spec/integration/matcher_operator_data/invalid_syntax.yml +39 -0
- data/spec/integration/matcher_operator_data/lt.yml +132 -0
- data/spec/integration/matcher_operator_data/lt_types.yml +15 -0
- data/spec/integration/matcher_operator_data/lte.yml +132 -0
- data/spec/integration/matcher_operator_data/lte_types.yml +15 -0
- data/spec/integration/matcher_operator_data/mod.yml +55 -0
- data/spec/integration/matcher_operator_data/multiple.yml +29 -0
- data/spec/integration/matcher_operator_data/ne.yml +150 -0
- data/spec/integration/matcher_operator_data/ne_types.yml +15 -0
- data/spec/integration/matcher_operator_data/nin.yml +114 -0
- data/spec/integration/matcher_operator_data/nor.yml +126 -0
- data/spec/integration/matcher_operator_data/not.yml +196 -0
- data/spec/integration/matcher_operator_data/or.yml +137 -0
- data/spec/integration/matcher_operator_data/regex.yml +195 -0
- data/spec/integration/matcher_operator_data/regex_options.yml +72 -0
- data/spec/integration/matcher_operator_data/size.yml +171 -0
- data/spec/integration/matcher_operator_data/type.yml +64 -0
- data/spec/integration/matcher_operator_data/type_array.yml +15 -0
- data/spec/integration/matcher_operator_data/type_binary.yml +18 -0
- data/spec/integration/matcher_operator_data/type_boolean.yml +39 -0
- data/spec/integration/matcher_operator_data/type_code.yml +26 -0
- data/spec/integration/matcher_operator_data/type_code_with_scope.yml +26 -0
- data/spec/integration/matcher_operator_data/type_date.yml +39 -0
- data/spec/integration/matcher_operator_data/type_db_pointer.yml +19 -0
- data/spec/integration/matcher_operator_data/type_decimal.yml +37 -0
- data/spec/integration/matcher_operator_data/type_double.yml +15 -0
- data/spec/integration/matcher_operator_data/type_int32.yml +33 -0
- data/spec/integration/matcher_operator_data/type_int64.yml +33 -0
- data/spec/integration/matcher_operator_data/type_max_key.yml +17 -0
- data/spec/integration/matcher_operator_data/type_min_key.yml +17 -0
- data/spec/integration/matcher_operator_data/type_null.yml +23 -0
- data/spec/integration/matcher_operator_data/type_object.yml +23 -0
- data/spec/integration/matcher_operator_data/type_object_id.yml +25 -0
- data/spec/integration/matcher_operator_data/type_regex.yml +44 -0
- data/spec/integration/matcher_operator_data/type_string.yml +15 -0
- data/spec/integration/matcher_operator_data/type_symbol.yml +32 -0
- data/spec/integration/matcher_operator_data/type_timestamp.yml +25 -0
- data/spec/integration/matcher_operator_data/type_undefined.yml +17 -0
- data/spec/integration/matcher_operator_spec.rb +113 -0
- data/spec/integration/matcher_regexp_timeout_spec.rb +215 -0
- data/spec/integration/matcher_spec.rb +237 -0
- data/spec/integration/persistence/collection_options_spec.rb +34 -0
- data/spec/integration/persistence/range_field_spec.rb +373 -0
- data/spec/integration/query_operator_guard_spec.rb +89 -0
- data/spec/integration/server_query_spec.rb +148 -0
- data/spec/integration/shardable_spec.rb +148 -0
- data/spec/integration/stringified_symbol_field_spec.rb +189 -0
- data/spec/lite_spec_helper.rb +56 -19
- data/spec/mongoid/association/accessors_spec.rb +375 -273
- data/spec/mongoid/association/auto_save_spec.rb +215 -106
- data/spec/mongoid/association/builders_spec.rb +124 -74
- data/spec/mongoid/association/constrainable_spec.rb +11 -21
- data/spec/mongoid/association/counter_cache_spec.rb +101 -131
- data/spec/mongoid/association/depending_spec.rb +454 -402
- data/spec/mongoid/association/eager_loadable_spec.rb +47 -0
- data/spec/mongoid/association/eager_spec.rb +471 -60
- data/spec/mongoid/association/embedded/cyclic_spec.rb +40 -49
- data/spec/mongoid/association/embedded/dirty_spec.rb +19 -21
- data/spec/mongoid/association/embedded/embedded_in/binding_spec.rb +39 -53
- data/spec/mongoid/association/embedded/embedded_in/buildable_spec.rb +58 -8
- data/spec/mongoid/association/embedded/embedded_in/proxy_spec.rb +208 -158
- data/spec/mongoid/association/embedded/embedded_in_spec.rb +46 -134
- data/spec/mongoid/association/embedded/embeds_many/binding_spec.rb +11 -13
- data/spec/mongoid/association/embedded/embeds_many/buildable_spec.rb +137 -35
- data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +1716 -1327
- data/spec/mongoid/association/embedded/embeds_many_models.rb +191 -2
- data/spec/mongoid/association/embedded/embeds_many_query_spec.rb +68 -0
- data/spec/mongoid/association/embedded/embeds_many_spec.rb +77 -119
- data/spec/mongoid/association/embedded/embeds_one/binding_spec.rb +17 -22
- data/spec/mongoid/association/embedded/embeds_one/buildable_spec.rb +44 -24
- data/spec/mongoid/association/embedded/embeds_one/proxy_spec.rb +234 -287
- data/spec/mongoid/association/embedded/embeds_one_dnl_models.rb +2 -0
- data/spec/mongoid/association/embedded/embeds_one_models.rb +27 -1
- data/spec/mongoid/association/embedded/embeds_one_query_spec.rb +28 -0
- data/spec/mongoid/association/embedded/embeds_one_spec.rb +50 -134
- data/spec/mongoid/association/fallback_spec.rb +425 -0
- data/spec/mongoid/association/macros_spec.rb +250 -308
- data/spec/mongoid/association/nested/many_spec.rb +53 -72
- data/spec/mongoid/association/nested/one_spec.rb +68 -85
- data/spec/mongoid/association/options_spec.rb +189 -309
- data/spec/mongoid/association/polymorphic_spec.rb +36 -51
- data/spec/mongoid/association/referenced/belongs_to/binding_spec.rb +58 -75
- data/spec/mongoid/association/referenced/belongs_to/buildable_spec.rb +131 -40
- data/spec/mongoid/association/referenced/belongs_to/eager_spec.rb +280 -52
- data/spec/mongoid/association/referenced/belongs_to/proxy_spec.rb +345 -370
- data/spec/mongoid/association/referenced/belongs_to_models.rb +13 -0
- data/spec/mongoid/association/referenced/belongs_to_query_spec.rb +57 -0
- data/spec/mongoid/association/referenced/belongs_to_spec.rb +171 -329
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/binding_spec.rb +45 -56
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/buildable_spec.rb +66 -29
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/eager_spec.rb +75 -36
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_persistence_spec.rb +28 -8
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +1151 -1283
- data/spec/mongoid/association/referenced/has_and_belongs_to_many_models.rb +69 -2
- data/spec/mongoid/association/referenced/has_and_belongs_to_many_query_spec.rb +39 -0
- data/spec/mongoid/association/referenced/has_and_belongs_to_many_spec.rb +176 -166
- data/spec/mongoid/association/referenced/has_many/binding_spec.rb +32 -43
- data/spec/mongoid/association/referenced/has_many/buildable_spec.rb +161 -24
- data/spec/mongoid/association/referenced/has_many/eager_spec.rb +169 -67
- data/spec/mongoid/association/referenced/has_many/enumerable_spec.rb +1021 -689
- data/spec/mongoid/association/referenced/has_many/proxy_query_spec.rb +1 -1
- data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +1743 -1955
- data/spec/mongoid/association/referenced/has_many_models.rb +85 -1
- data/spec/mongoid/association/referenced/has_many_query_spec.rb +37 -0
- data/spec/mongoid/association/referenced/has_many_spec.rb +83 -178
- data/spec/mongoid/association/referenced/has_many_through/eager_spec.rb +58 -0
- data/spec/mongoid/association/referenced/has_many_through/proxy_spec.rb +62 -0
- data/spec/mongoid/association/referenced/has_many_through_spec.rb +399 -0
- data/spec/mongoid/association/referenced/has_one/binding_spec.rb +28 -37
- data/spec/mongoid/association/referenced/has_one/buildable_spec.rb +74 -28
- data/spec/mongoid/association/referenced/has_one/eager_spec.rb +60 -42
- data/spec/mongoid/association/referenced/has_one/proxy_spec.rb +380 -333
- data/spec/mongoid/association/referenced/has_one_models.rb +73 -0
- data/spec/mongoid/association/referenced/has_one_query_spec.rb +37 -0
- data/spec/mongoid/association/referenced/has_one_spec.rb +61 -185
- data/spec/mongoid/association/referenced/has_one_through/eager_spec.rb +58 -0
- data/spec/mongoid/association/referenced/has_one_through/proxy_spec.rb +49 -0
- data/spec/mongoid/association/referenced/has_one_through_spec.rb +211 -0
- data/spec/mongoid/association/reflections_spec.rb +16 -23
- data/spec/mongoid/association/syncable_spec.rb +132 -138
- data/spec/mongoid/association_spec.rb +59 -65
- data/spec/mongoid/atomic/modifiers_spec.rb +217 -180
- data/spec/mongoid/atomic/paths/embedded/many_spec.rb +41 -51
- data/spec/mongoid/atomic/paths/embedded/one_spec.rb +37 -48
- data/spec/mongoid/atomic/paths/root_spec.rb +11 -14
- data/spec/mongoid/atomic/paths_spec.rb +133 -128
- data/spec/mongoid/atomic_spec.rb +187 -154
- data/spec/mongoid/atomic_update_preparer_spec.rb +83 -0
- data/spec/mongoid/attributes/dynamic_spec.rb +5 -6
- data/spec/mongoid/attributes/embedded_spec.rb +118 -0
- data/spec/mongoid/attributes/nested_spec.rb +1604 -1737
- data/spec/mongoid/attributes/nested_spec_models.rb +48 -0
- data/spec/mongoid/attributes/projector_data/embedded.yml +105 -0
- data/spec/mongoid/attributes/projector_data/fields.yml +93 -0
- data/spec/mongoid/attributes/projector_spec.rb +37 -0
- data/spec/mongoid/attributes/readonly_spec.rb +106 -108
- data/spec/mongoid/attributes_spec.rb +1217 -715
- data/spec/mongoid/cacheable_spec.rb +78 -45
- data/spec/mongoid/changeable_spec.rb +914 -638
- data/spec/mongoid/clients/factory_spec.rb +314 -137
- data/spec/mongoid/clients/options_spec.rb +205 -76
- data/spec/mongoid/clients/sessions_spec.rb +73 -116
- data/spec/mongoid/clients/transactions_spec.rb +1128 -186
- data/spec/mongoid/clients/transactions_spec_models.rb +251 -0
- data/spec/mongoid/clients_spec.rb +433 -312
- data/spec/mongoid/collection_configurable_spec.rb +156 -0
- data/spec/mongoid/composable_spec.rb +4 -8
- data/spec/mongoid/config/defaults_spec.rb +125 -0
- data/spec/mongoid/config/encryption_spec.rb +377 -0
- data/spec/mongoid/config/environment_spec.rb +129 -49
- data/spec/mongoid/config/introspection_spec.rb +119 -0
- data/spec/mongoid/config/options_spec.rb +36 -23
- data/spec/mongoid/config_spec.rb +497 -148
- data/spec/mongoid/contextual/aggregable/memory_spec.rb +507 -161
- data/spec/mongoid/contextual/aggregable/memory_table.yml +88 -0
- data/spec/mongoid/contextual/aggregable/memory_table_spec.rb +60 -0
- data/spec/mongoid/contextual/aggregable/mongo_spec.rb +242 -158
- data/spec/mongoid/contextual/aggregable/none_spec.rb +47 -0
- data/spec/mongoid/contextual/atomic_spec.rb +510 -229
- data/spec/mongoid/contextual/map_reduce_spec.rb +114 -153
- data/spec/mongoid/contextual/memory_spec.rb +1947 -563
- data/spec/mongoid/contextual/mongo/documents_loader_spec.rb +193 -0
- data/spec/mongoid/contextual/mongo_spec.rb +3317 -1268
- data/spec/mongoid/contextual/none_spec.rb +98 -89
- data/spec/mongoid/contextual/short_circuit_spec.rb +139 -0
- data/spec/mongoid/copyable_spec.rb +769 -191
- data/spec/mongoid/copyable_spec_models.rb +30 -0
- data/spec/mongoid/criteria/findable_spec.rb +565 -506
- data/spec/mongoid/criteria/includable_spec.rb +2417 -0
- data/spec/mongoid/criteria/includable_spec_models.rb +64 -0
- data/spec/mongoid/criteria/inspectable_spec.rb +13 -13
- data/spec/mongoid/criteria/marshalable_spec.rb +29 -12
- data/spec/mongoid/criteria/modifiable_spec.rb +388 -530
- data/spec/mongoid/criteria/options_spec.rb +4 -6
- data/spec/mongoid/criteria/queryable/aggregable_spec.rb +114 -140
- data/spec/mongoid/criteria/queryable/expandable_spec.rb +59 -0
- data/spec/mongoid/criteria/queryable/extensions/array_spec.rb +132 -198
- data/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb +199 -25
- data/spec/mongoid/criteria/queryable/extensions/boolean_spec.rb +86 -116
- data/spec/mongoid/criteria/queryable/extensions/date_spec.rb +69 -67
- data/spec/mongoid/criteria/queryable/extensions/date_time_spec.rb +121 -113
- data/spec/mongoid/criteria/queryable/extensions/float_spec.rb +96 -32
- data/spec/mongoid/criteria/queryable/extensions/hash_spec.rb +105 -152
- data/spec/mongoid/criteria/queryable/extensions/integer_spec.rb +97 -33
- data/spec/mongoid/criteria/queryable/extensions/nil_class_spec.rb +27 -37
- data/spec/mongoid/criteria/queryable/extensions/numeric_spec.rb +79 -15
- data/spec/mongoid/criteria/queryable/extensions/object_spec.rb +34 -53
- data/spec/mongoid/criteria/queryable/extensions/range_spec.rb +259 -191
- data/spec/mongoid/criteria/queryable/extensions/regexp_raw_spec.rb +18 -34
- data/spec/mongoid/criteria/queryable/extensions/regexp_spec.rb +16 -33
- data/spec/mongoid/criteria/queryable/extensions/set_spec.rb +14 -18
- data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +91 -189
- data/spec/mongoid/criteria/queryable/extensions/symbol_spec.rb +34 -100
- data/spec/mongoid/criteria/queryable/extensions/time_spec.rb +99 -103
- data/spec/mongoid/criteria/queryable/extensions/time_with_zone_spec.rb +78 -85
- data/spec/mongoid/criteria/queryable/key_spec.rb +34 -35
- data/spec/mongoid/criteria/queryable/mergeable_spec.rb +150 -21
- data/spec/mongoid/criteria/queryable/optional_spec.rb +375 -954
- data/spec/mongoid/criteria/queryable/options_spec.rb +175 -123
- data/spec/mongoid/criteria/queryable/pipeline_spec.rb +67 -86
- data/spec/mongoid/criteria/queryable/queryable_spec.rb +43 -52
- data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +2101 -414
- data/spec/mongoid/criteria/queryable/selectable_shared_examples.rb +35 -0
- data/spec/mongoid/criteria/queryable/selectable_spec.rb +1283 -3140
- data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +754 -0
- data/spec/mongoid/criteria/queryable/selector_spec.rb +404 -317
- data/spec/mongoid/criteria/queryable/smash_spec.rb +8 -10
- data/spec/mongoid/criteria/queryable/storable_spec.rb +249 -0
- data/spec/mongoid/criteria/queryable_mql_spec.rb +92 -0
- data/spec/mongoid/criteria/scopable_spec.rb +212 -118
- data/spec/mongoid/criteria/translator_spec.rb +132 -0
- data/spec/mongoid/criteria_projection_spec.rb +389 -0
- data/spec/mongoid/criteria_spec.rb +1942 -2301
- data/spec/mongoid/document_fields_spec.rb +187 -14
- data/spec/mongoid/document_persistence_context_spec.rb +80 -6
- data/spec/mongoid/document_query_spec.rb +87 -0
- data/spec/mongoid/document_spec.rb +394 -538
- data/spec/mongoid/encryptable_spec.rb +61 -0
- data/spec/mongoid/equality_spec.rb +77 -103
- data/spec/mongoid/errors/ambiguous_relationship_spec.rb +10 -10
- data/spec/mongoid/errors/attribute_not_loaded_spec.rb +29 -0
- data/spec/mongoid/errors/callback_spec.rb +10 -10
- data/spec/mongoid/errors/config_redactor_spec.rb +112 -0
- data/spec/mongoid/errors/delete_restriction_spec.rb +9 -9
- data/spec/mongoid/errors/document_not_destroyed_spec.rb +9 -9
- data/spec/mongoid/errors/document_not_found_spec.rb +108 -32
- data/spec/mongoid/errors/invalid_collection_spec.rb +11 -12
- data/spec/mongoid/errors/invalid_config_file_spec.rb +29 -0
- data/spec/mongoid/errors/invalid_config_option_spec.rb +10 -10
- data/spec/mongoid/errors/invalid_field_option_spec.rb +9 -9
- data/spec/mongoid/errors/invalid_field_spec.rb +13 -11
- data/spec/mongoid/errors/invalid_field_type_spec.rb +53 -0
- data/spec/mongoid/errors/invalid_find_spec.rb +10 -10
- data/spec/mongoid/errors/invalid_includes_spec.rb +11 -12
- data/spec/mongoid/errors/invalid_index_spec.rb +10 -10
- data/spec/mongoid/errors/invalid_options_spec.rb +10 -10
- data/spec/mongoid/errors/invalid_path_spec.rb +9 -9
- data/spec/mongoid/errors/invalid_relation_spec.rb +17 -15
- data/spec/mongoid/errors/invalid_scope_spec.rb +10 -10
- data/spec/mongoid/errors/invalid_set_polymorphic_relation_spec.rb +5 -5
- data/spec/mongoid/errors/invalid_storage_options_spec.rb +10 -10
- data/spec/mongoid/errors/invalid_time_spec.rb +10 -10
- data/spec/mongoid/errors/inverse_not_found_spec.rb +11 -11
- data/spec/mongoid/errors/mixed_client_configuration_spec.rb +42 -11
- data/spec/mongoid/errors/mixed_relations_spec.rb +10 -10
- data/spec/mongoid/errors/mongoid_error_spec.rb +23 -23
- data/spec/mongoid/errors/nested_attributes_metadata_not_found_spec.rb +10 -10
- data/spec/mongoid/errors/no_client_config_spec.rb +11 -11
- data/spec/mongoid/errors/no_client_database_spec.rb +40 -11
- data/spec/mongoid/errors/no_client_hosts_spec.rb +37 -11
- data/spec/mongoid/errors/no_clients_config_spec.rb +9 -9
- data/spec/mongoid/errors/no_environment_spec.rb +10 -10
- data/spec/mongoid/errors/no_map_reduce_output_spec.rb +10 -10
- data/spec/mongoid/errors/no_metadata_spec.rb +9 -9
- data/spec/mongoid/errors/no_parent_spec.rb +10 -10
- data/spec/mongoid/errors/readonly_association_spec.rb +34 -0
- data/spec/mongoid/errors/readonly_attribute_spec.rb +9 -9
- data/spec/mongoid/errors/readonly_document_spec.rb +9 -9
- data/spec/mongoid/errors/scope_overwrite_spec.rb +11 -11
- data/spec/mongoid/errors/too_many_nested_attribute_records_spec.rb +11 -11
- data/spec/mongoid/errors/unknown_attribute_spec.rb +9 -9
- data/spec/mongoid/errors/unsaved_document_spec.rb +10 -10
- data/spec/mongoid/errors/unsupported_javascript_spec.rb +11 -11
- data/spec/mongoid/errors/validations_spec.rb +13 -13
- data/spec/mongoid/extensions/array_spec.rb +92 -377
- data/spec/mongoid/extensions/big_decimal_spec.rb +640 -217
- data/spec/mongoid/extensions/binary_spec.rb +50 -23
- data/spec/mongoid/extensions/boolean_spec.rb +73 -107
- data/spec/mongoid/extensions/date_class_mongoize_spec.rb +329 -0
- data/spec/mongoid/extensions/date_spec.rb +62 -173
- data/spec/mongoid/extensions/date_time_spec.rb +42 -95
- data/spec/mongoid/extensions/decimal128_spec.rb +14 -17
- data/spec/mongoid/extensions/false_class_spec.rb +13 -24
- data/spec/mongoid/extensions/float_spec.rb +66 -100
- data/spec/mongoid/extensions/hash_spec.rb +79 -165
- data/spec/mongoid/extensions/integer_spec.rb +63 -91
- data/spec/mongoid/extensions/module_spec.rb +15 -17
- data/spec/mongoid/extensions/nil_class_spec.rb +4 -4
- data/spec/mongoid/extensions/object_id_spec.rb +161 -246
- data/spec/mongoid/extensions/object_spec.rb +36 -238
- data/spec/mongoid/extensions/range_spec.rb +259 -69
- data/spec/mongoid/extensions/raw_value_spec.rb +65 -0
- data/spec/mongoid/extensions/regexp_spec.rb +56 -41
- data/spec/mongoid/extensions/set_spec.rb +110 -14
- data/spec/mongoid/extensions/string_spec.rb +139 -237
- data/spec/mongoid/extensions/stringified_symbol_spec.rb +71 -0
- data/spec/mongoid/extensions/symbol_spec.rb +19 -62
- data/spec/mongoid/extensions/time_spec.rb +394 -192
- data/spec/mongoid/extensions/time_with_zone_spec.rb +111 -176
- data/spec/mongoid/extensions/true_class_spec.rb +13 -24
- data/spec/mongoid/extensions/vector_spec.rb +175 -0
- data/spec/mongoid/factory_spec.rb +378 -80
- data/spec/mongoid/fields/foreign_key_spec.rb +387 -267
- data/spec/mongoid/fields/localized_spec.rb +197 -196
- data/spec/mongoid/fields/standard_spec.rb +45 -58
- data/spec/mongoid/fields_spec.rb +1448 -525
- data/spec/mongoid/findable_spec.rb +659 -230
- data/spec/mongoid/indexable/specification_spec.rb +145 -40
- data/spec/mongoid/indexable_spec.rb +551 -188
- data/spec/mongoid/inspectable_spec.rb +126 -18
- data/spec/mongoid/interceptable_spec.rb +1431 -573
- data/spec/mongoid/interceptable_spec_models.rb +244 -0
- data/spec/mongoid/loadable_spec.rb +86 -0
- data/spec/mongoid/loading_spec.rb +114 -0
- data/spec/mongoid/loggable_spec.rb +5 -5
- data/spec/mongoid/matcher/extract_attribute_data/numeric_keys.yml +104 -0
- data/spec/mongoid/matcher/extract_attribute_data/traversal.yml +239 -0
- data/spec/mongoid/matcher/extract_attribute_spec.rb +33 -0
- data/spec/mongoid/matcher/regexp_budget_spec.rb +570 -0
- data/spec/mongoid/model_resolver_spec.rb +167 -0
- data/spec/mongoid/mongoizable_spec.rb +277 -0
- data/spec/mongoid/monkey_patches_spec.rb +215 -0
- data/spec/mongoid/persistable/creatable_spec.rb +256 -209
- data/spec/mongoid/persistable/deletable_spec.rb +369 -80
- data/spec/mongoid/persistable/destroyable_spec.rb +439 -80
- data/spec/mongoid/persistable/incrementable_spec.rb +112 -77
- data/spec/mongoid/persistable/logical_spec.rb +92 -51
- data/spec/mongoid/persistable/maxable_spec.rb +148 -0
- data/spec/mongoid/persistable/minable_spec.rb +148 -0
- data/spec/mongoid/persistable/multipliable_spec.rb +212 -0
- data/spec/mongoid/persistable/poppable_spec.rb +87 -47
- data/spec/mongoid/persistable/pullable_spec.rb +161 -82
- data/spec/mongoid/persistable/pushable_spec.rb +188 -110
- data/spec/mongoid/persistable/renamable_spec.rb +91 -51
- data/spec/mongoid/persistable/savable_spec.rb +373 -155
- data/spec/mongoid/persistable/settable_spec.rb +164 -134
- data/spec/mongoid/persistable/unsettable_spec.rb +94 -58
- data/spec/mongoid/persistable/updatable_spec.rb +235 -267
- data/spec/mongoid/persistable/upsertable_spec.rb +150 -38
- data/spec/mongoid/persistable_spec.rb +198 -128
- data/spec/mongoid/persistence_context_spec.rb +133 -137
- data/spec/mongoid/positional_spec.rb +69 -78
- data/spec/mongoid/railties/bson_object_id_serializer_spec.rb +27 -0
- data/spec/mongoid/railties/console_sandbox_spec.rb +43 -0
- data/spec/mongoid/relations/proxy_spec.rb +8 -6
- data/spec/mongoid/reloadable_spec.rb +552 -123
- data/spec/mongoid/scopable_spec.rb +522 -455
- data/spec/mongoid/search_indexable_spec.rb +838 -0
- data/spec/mongoid/selectable_spec.rb +41 -53
- data/spec/mongoid/serializable_spec.rb +394 -319
- data/spec/mongoid/shardable_models.rb +76 -0
- data/spec/mongoid/shardable_spec.rb +245 -41
- data/spec/mongoid/stateful_spec.rb +170 -45
- data/spec/mongoid/tasks/database_rake_spec.rb +284 -133
- data/spec/mongoid/tasks/database_spec.rb +245 -51
- data/spec/mongoid/tasks/encryption_spec.rb +189 -0
- data/spec/mongoid/threaded_spec.rb +118 -85
- data/spec/mongoid/timestamps/created/short_spec.rb +15 -16
- data/spec/mongoid/timestamps/created_spec.rb +36 -14
- data/spec/mongoid/timestamps/timeless_spec.rb +230 -47
- data/spec/mongoid/timestamps/updated/short_spec.rb +26 -30
- data/spec/mongoid/timestamps/updated_spec.rb +23 -27
- data/spec/mongoid/timestamps_spec.rb +408 -34
- data/spec/mongoid/timestamps_spec_models.rb +67 -0
- data/spec/mongoid/touchable_spec.rb +1235 -136
- data/spec/mongoid/touchable_spec_models.rb +178 -0
- data/spec/mongoid/traversable_spec.rb +1188 -79
- data/spec/mongoid/validatable/associated_spec.rb +86 -89
- data/spec/mongoid/validatable/format_spec.rb +26 -33
- data/spec/mongoid/validatable/length_spec.rb +64 -85
- data/spec/mongoid/validatable/numericality_spec.rb +124 -14
- data/spec/mongoid/validatable/presence_spec.rb +168 -215
- data/spec/mongoid/validatable/uniqueness_spec.rb +702 -826
- data/spec/mongoid/validatable_spec.rb +76 -105
- data/spec/mongoid/warnings_spec.rb +32 -0
- data/spec/mongoid_spec.rb +67 -30
- data/spec/rails/controller_extension/controller_runtime_spec.rb +59 -60
- data/spec/rails/mongoid_spec.rb +38 -139
- data/spec/shared/CANDIDATE.md +28 -0
- data/spec/shared/bin/s3-copy +45 -0
- data/spec/shared/bin/s3-upload +69 -0
- data/spec/shared/lib/mrss/cluster_config.rb +16 -41
- data/spec/shared/lib/mrss/constraints.rb +44 -59
- data/spec/shared/lib/mrss/docker_runner.rb +42 -20
- data/spec/shared/lib/mrss/eg_config_utils.rb +20 -0
- data/spec/shared/lib/mrss/event_subscriber.rb +210 -0
- data/spec/shared/lib/mrss/lite_constraints.rb +48 -1
- data/spec/shared/lib/mrss/release/candidate.rb +287 -0
- data/spec/shared/lib/mrss/release/product_data.rb +144 -0
- data/spec/shared/lib/mrss/server_version_registry.rb +18 -24
- data/spec/shared/lib/mrss/session_registry.rb +69 -0
- data/spec/shared/lib/mrss/session_registry_legacy.rb +60 -0
- data/spec/shared/lib/mrss/spec_organizer.rb +48 -2
- data/spec/shared/lib/mrss/utils.rb +28 -6
- data/spec/shared/lib/tasks/candidate.rake +64 -0
- data/spec/shared/share/Dockerfile.erb +114 -96
- data/spec/shared/share/haproxy-1.conf +16 -0
- data/spec/shared/share/haproxy-2.conf +17 -0
- data/spec/shared/shlib/config.sh +27 -0
- data/spec/shared/shlib/distro.sh +12 -1
- data/spec/shared/shlib/server.sh +148 -65
- data/spec/shared/shlib/set_env.sh +56 -80
- data/spec/spec_helper.rb +104 -72
- data/spec/support/authorization.rb +2 -0
- data/spec/support/client_registry.rb +9 -0
- data/spec/support/constraints.rb +37 -22
- data/spec/support/crypt/models.rb +206 -0
- data/spec/support/crypt.rb +77 -0
- data/spec/support/expectations.rb +38 -17
- data/spec/support/feature_sandbox.rb +69 -0
- data/spec/support/helpers.rb +11 -0
- data/spec/support/immutable_ids.rb +118 -0
- data/spec/support/macros.rb +106 -1
- data/spec/support/models/account.rb +38 -0
- data/spec/support/models/acolyte.rb +20 -0
- data/spec/support/models/actor.rb +21 -0
- data/spec/support/models/actress.rb +4 -0
- data/spec/support/models/address.rb +88 -0
- data/spec/support/models/address_component.rb +8 -0
- data/spec/support/models/address_number.rb +9 -0
- data/spec/support/models/agency.rb +8 -0
- data/spec/support/models/agent.rb +19 -0
- data/spec/support/models/album.rb +20 -0
- data/spec/support/models/alert.rb +10 -0
- data/spec/support/models/animal.rb +28 -0
- data/spec/support/models/answer.rb +9 -0
- data/spec/support/models/appointment.rb +10 -0
- data/spec/support/models/armrest.rb +9 -0
- data/spec/support/models/article.rb +14 -0
- data/spec/support/models/artist.rb +95 -0
- data/spec/support/models/artwork.rb +7 -0
- data/spec/support/models/audible_sound.rb +3 -0
- data/spec/support/models/audio.rb +8 -0
- data/spec/support/models/augmentation.rb +25 -0
- data/spec/support/models/author.rb +9 -0
- data/spec/support/models/baby.rb +7 -0
- data/spec/support/models/band.rb +46 -0
- data/spec/support/models/bar.rb +13 -0
- data/spec/support/models/basic.rb +8 -0
- data/spec/support/models/bed.rb +3 -0
- data/spec/support/models/big_palette.rb +4 -0
- data/spec/support/models/birthday.rb +14 -0
- data/spec/support/models/bolt.rb +7 -0
- data/spec/support/models/bomb.rb +7 -0
- data/spec/support/models/book.rb +20 -0
- data/spec/support/models/breed.rb +7 -0
- data/spec/support/models/browser.rb +8 -0
- data/spec/support/models/building.rb +10 -0
- data/spec/support/models/building_address.rb +10 -0
- data/spec/support/models/bus.rb +10 -0
- data/spec/support/models/business.rb +8 -0
- data/spec/support/models/callback_test.rb +12 -0
- data/spec/support/models/canvas.rb +27 -0
- data/spec/support/models/car.rb +3 -0
- data/spec/support/models/cat.rb +9 -0
- data/spec/support/models/catalog.rb +24 -0
- data/spec/support/models/category.rb +11 -0
- data/spec/support/models/child.rb +7 -0
- data/spec/support/models/child_doc.rb +24 -0
- data/spec/support/models/church.rb +9 -0
- data/spec/support/models/circle.rb +5 -0
- data/spec/support/models/circuit.rb +7 -0
- data/spec/support/models/circus.rb +12 -0
- data/spec/support/models/code.rb +17 -0
- data/spec/support/models/coding/pull_request.rb +11 -0
- data/spec/support/models/coding.rb +3 -0
- data/spec/support/models/comment.rb +18 -0
- data/spec/support/models/company.rb +9 -0
- data/spec/support/models/consumption_period.rb +9 -0
- data/spec/support/models/contextable_item.rb +8 -0
- data/spec/support/models/contractor.rb +8 -0
- data/spec/support/models/cookie.rb +8 -0
- data/spec/support/models/country_code.rb +12 -0
- data/spec/support/models/courier_job.rb +7 -0
- data/spec/support/models/cover.rb +10 -0
- data/spec/support/models/crate.rb +12 -0
- data/spec/support/models/customer.rb +10 -0
- data/spec/support/models/customer_address.rb +11 -0
- data/spec/support/models/deed.rb +8 -0
- data/spec/support/models/definition.rb +12 -0
- data/spec/support/models/delegating_patient.rb +15 -0
- data/spec/support/models/description.rb +13 -0
- data/spec/support/models/dictionary.rb +19 -0
- data/spec/support/models/division.rb +13 -0
- data/spec/support/models/doctor.rb +14 -0
- data/spec/support/models/dog.rb +10 -0
- data/spec/support/models/dokument.rb +9 -0
- data/spec/support/models/draft.rb +11 -0
- data/spec/support/models/dragon.rb +7 -0
- data/spec/support/models/driver.rb +10 -0
- data/spec/support/models/drug.rb +9 -0
- data/spec/support/models/dungeon.rb +7 -0
- data/spec/support/models/edit.rb +8 -0
- data/spec/support/models/email.rb +9 -0
- data/spec/support/models/employer.rb +7 -0
- data/spec/support/models/entry.rb +9 -0
- data/spec/support/models/eraser.rb +3 -0
- data/spec/support/models/even.rb +10 -0
- data/spec/support/models/event.rb +24 -0
- data/spec/support/models/exhibition.rb +7 -0
- data/spec/support/models/exhibitor.rb +9 -0
- data/spec/support/models/explosion.rb +7 -0
- data/spec/support/models/eye.rb +11 -0
- data/spec/support/models/eye_bowl.rb +11 -0
- data/spec/support/models/face.rb +10 -0
- data/spec/support/models/fanatic.rb +9 -0
- data/spec/support/models/favorite.rb +15 -0
- data/spec/support/models/filesystem.rb +8 -0
- data/spec/support/models/fire_hydrant.rb +9 -0
- data/spec/support/models/firefox.rb +6 -0
- data/spec/support/models/fish.rb +9 -0
- data/spec/support/models/folder.rb +8 -0
- data/spec/support/models/folder_item.rb +10 -0
- data/spec/support/models/fruits.rb +41 -0
- data/spec/support/models/game.rb +21 -0
- data/spec/support/models/ghost.rb +9 -0
- data/spec/support/models/guitar.rb +4 -0
- data/spec/support/models/hole.rb +12 -0
- data/spec/support/models/home.rb +7 -0
- data/spec/support/models/house.rb +9 -0
- data/spec/support/models/html_writer.rb +5 -0
- data/spec/support/models/id_key.rb +9 -0
- data/spec/support/models/idnodef.rb +7 -0
- data/spec/support/models/image.rb +24 -0
- data/spec/support/models/implant.rb +27 -0
- data/spec/support/models/instrument.rb +8 -0
- data/spec/support/models/item.rb +11 -0
- data/spec/support/models/jar.rb +9 -0
- data/spec/support/models/kaleidoscope.rb +9 -0
- data/spec/support/models/kangaroo.rb +7 -0
- data/spec/support/models/label.rb +53 -0
- data/spec/support/models/language.rb +8 -0
- data/spec/support/models/lat_lng.rb +23 -0
- data/spec/support/models/league.rb +14 -0
- data/spec/support/models/learner.rb +4 -0
- data/spec/support/models/line_item.rb +9 -0
- data/spec/support/models/location.rb +11 -0
- data/spec/support/models/login.rb +10 -0
- data/spec/support/models/manufacturer.rb +9 -0
- data/spec/support/models/meat.rb +7 -0
- data/spec/support/models/membership.rb +8 -0
- data/spec/support/models/message.rb +13 -0
- data/spec/support/models/minim.rb +6 -0
- data/spec/support/models/mixed_drink.rb +7 -0
- data/spec/support/models/modifier_models.rb +25 -0
- data/spec/support/models/mop.rb +24 -0
- data/spec/support/models/movie.rb +16 -0
- data/spec/support/models/my_hash.rb +4 -0
- data/spec/support/models/name.rb +36 -0
- data/spec/support/models/name_only.rb +8 -0
- data/spec/support/models/node.rb +8 -0
- data/spec/support/models/note.rb +20 -0
- data/spec/support/models/nut.rb +7 -0
- data/spec/support/models/odd.rb +10 -0
- data/spec/support/models/order.rb +13 -0
- data/spec/support/models/ordered_post.rb +14 -0
- data/spec/support/models/ordered_preference.rb +9 -0
- data/spec/support/models/oscar.rb +17 -0
- data/spec/support/models/other_owner_object.rb +4 -0
- data/spec/support/models/override.rb +11 -0
- data/spec/support/models/ownable.rb +9 -0
- data/spec/support/models/owner.rb +11 -0
- data/spec/support/models/pack.rb +5 -0
- data/spec/support/models/page.rb +19 -0
- data/spec/support/models/page_question.rb +7 -0
- data/spec/support/models/palette.rb +10 -0
- data/spec/support/models/parent.rb +8 -0
- data/spec/support/models/parent_doc.rb +9 -0
- data/spec/support/models/passport.rb +23 -0
- data/spec/support/models/patient.rb +12 -0
- data/spec/support/models/pdf_writer.rb +5 -0
- data/spec/support/models/pencil.rb +3 -0
- data/spec/support/models/person.rb +237 -0
- data/spec/support/models/pet.rb +26 -0
- data/spec/support/models/pet_owner.rb +9 -0
- data/spec/support/models/phone.rb +13 -0
- data/spec/support/models/piano.rb +4 -0
- data/spec/support/models/pizza.rb +10 -0
- data/spec/support/models/player.rb +40 -0
- data/spec/support/models/post.rb +53 -0
- data/spec/support/models/post_genre.rb +9 -0
- data/spec/support/models/powerup.rb +25 -0
- data/spec/support/models/preference.rb +12 -0
- data/spec/support/models/princess.rb +13 -0
- data/spec/support/models/product.rb +23 -0
- data/spec/support/models/profile.rb +19 -0
- data/spec/support/models/pronunciation.rb +8 -0
- data/spec/support/models/pub.rb +9 -0
- data/spec/support/models/publication/encyclopedia.rb +11 -0
- data/spec/support/models/publication/review.rb +13 -0
- data/spec/support/models/publication.rb +4 -0
- data/spec/support/models/purchase.rb +7 -0
- data/spec/support/models/purchased_item.rb +11 -0
- data/spec/support/models/purse.rb +9 -0
- data/spec/support/models/question.rb +11 -0
- data/spec/support/models/quiz.rb +10 -0
- data/spec/support/models/rating.rb +11 -0
- data/spec/support/models/record.rb +54 -0
- data/spec/support/models/registry.rb +8 -0
- data/spec/support/models/role.rb +10 -0
- data/spec/support/models/root_category.rb +7 -0
- data/spec/support/models/sandbox/app/models/app_models_message.rb +3 -0
- data/spec/support/models/sandbox/lib/models/lib_models_message.rb +3 -0
- data/spec/support/models/sandbox/sandbox_message.rb +3 -0
- data/spec/support/models/sandbox/sandbox_user.rb +3 -0
- data/spec/support/models/sandbox/subdir/sandbox_comment.rb +3 -0
- data/spec/support/models/sandwich.rb +12 -0
- data/spec/support/models/scheduler.rb +9 -0
- data/spec/support/models/school.rb +14 -0
- data/spec/support/models/scribe.rb +8 -0
- data/spec/support/models/sealer.rb +7 -0
- data/spec/support/models/seat.rb +24 -0
- data/spec/support/models/seo.rb +12 -0
- data/spec/support/models/series.rb +8 -0
- data/spec/support/models/server.rb +16 -0
- data/spec/support/models/service.rb +25 -0
- data/spec/support/models/shape.rb +15 -0
- data/spec/support/models/shelf.rb +8 -0
- data/spec/support/models/shield.rb +19 -0
- data/spec/support/models/shipment_address.rb +5 -0
- data/spec/support/models/shipping_container.rb +8 -0
- data/spec/support/models/shipping_pack.rb +5 -0
- data/spec/support/models/shirt.rb +11 -0
- data/spec/support/models/shop.rb +9 -0
- data/spec/support/models/short_agent.rb +6 -0
- data/spec/support/models/short_quiz.rb +8 -0
- data/spec/support/models/simple.rb +8 -0
- data/spec/support/models/slave.rb +9 -0
- data/spec/support/models/song.rb +10 -0
- data/spec/support/models/sound.rb +8 -0
- data/spec/support/models/spacer.rb +7 -0
- data/spec/support/models/square.rb +6 -0
- data/spec/support/models/staff.rb +9 -0
- data/spec/support/models/store_as_dup_test1.rb +8 -0
- data/spec/support/models/store_as_dup_test2.rb +8 -0
- data/spec/support/models/store_as_dup_test3.rb +8 -0
- data/spec/support/models/store_as_dup_test4.rb +8 -0
- data/spec/support/models/strategy.rb +5 -0
- data/spec/support/models/student.rb +14 -0
- data/spec/support/models/sub_item.rb +5 -0
- data/spec/support/models/subscription.rb +8 -0
- data/spec/support/models/survey.rb +8 -0
- data/spec/support/models/symptom.rb +9 -0
- data/spec/support/models/system_role.rb +7 -0
- data/spec/support/models/tag.rb +11 -0
- data/spec/support/models/target.rb +8 -0
- data/spec/support/models/template.rb +8 -0
- data/spec/support/models/thing.rb +12 -0
- data/spec/support/models/threadlocker.rb +7 -0
- data/spec/support/models/title.rb +5 -0
- data/spec/support/models/tool.rb +11 -0
- data/spec/support/models/topping.rb +8 -0
- data/spec/support/models/toy.rb +9 -0
- data/spec/support/models/track.rb +41 -0
- data/spec/support/models/translation.rb +8 -0
- data/spec/support/models/tree.rb +11 -0
- data/spec/support/models/truck.rb +7 -0
- data/spec/support/models/user.rb +23 -0
- data/spec/support/models/user_account.rb +13 -0
- data/spec/support/models/validation_callback.rb +13 -0
- data/spec/support/models/vehicle.rb +18 -0
- data/spec/support/models/version.rb +8 -0
- data/spec/support/models/vertex.rb +8 -0
- data/spec/support/models/vet_visit.rb +8 -0
- data/spec/support/models/video.rb +16 -0
- data/spec/support/models/video_game.rb +3 -0
- data/spec/support/models/washer.rb +7 -0
- data/spec/support/models/weapon.rb +25 -0
- data/spec/support/models/wiki_page.rb +17 -0
- data/spec/support/models/word.rb +18 -0
- data/spec/support/models/word_origin.rb +13 -0
- data/spec/support/models/writer.rb +14 -0
- data/spec/support/rails_mock.rb +27 -0
- data/spec/support/schema_maps/schema_map_aws.json +17 -0
- data/spec/support/schema_maps/schema_map_aws_key_alt_names.json +12 -0
- data/spec/support/schema_maps/schema_map_azure.json +17 -0
- data/spec/support/schema_maps/schema_map_azure_key_alt_names.json +12 -0
- data/spec/support/schema_maps/schema_map_gcp.json +17 -0
- data/spec/support/schema_maps/schema_map_gcp_key_alt_names.json +12 -0
- data/spec/support/schema_maps/schema_map_kmip.json +17 -0
- data/spec/support/schema_maps/schema_map_kmip_key_alt_names.json +12 -0
- data/spec/support/schema_maps/schema_map_local.json +18 -0
- data/spec/support/schema_maps/schema_map_local_key_alt_names.json +12 -0
- data/spec/support/shared/time.rb +34 -0
- data/spec/support/sinatra_mock.rb +9 -0
- data/spec/support/spec_config.rb +45 -14
- metadata +1341 -780
- checksums.yaml.gz.sig +0 -0
- data/lib/mongoid/association/referenced/eager.rb +0 -162
- data/lib/mongoid/association/referenced/has_one/nested_builder.rb +0 -126
- data/lib/mongoid/contextual/geo_near.rb +0 -267
- data/lib/mongoid/criteria/queryable/forwardable.rb +0 -65
- data/lib/mongoid/errors/eager_load.rb +0 -22
- data/lib/mongoid/errors/invalid_session_use.rb +0 -24
- data/lib/mongoid/errors/invalid_storage_parent.rb +0 -26
- data/lib/mongoid/errors/invalid_value.rb +0 -16
- data/lib/mongoid/matchable/all.rb +0 -28
- data/lib/mongoid/matchable/and.rb +0 -30
- data/lib/mongoid/matchable/default.rb +0 -119
- data/lib/mongoid/matchable/elem_match.rb +0 -34
- data/lib/mongoid/matchable/eq.rb +0 -22
- data/lib/mongoid/matchable/exists.rb +0 -23
- data/lib/mongoid/matchable/gt.rb +0 -23
- data/lib/mongoid/matchable/gte.rb +0 -23
- data/lib/mongoid/matchable/in.rb +0 -24
- data/lib/mongoid/matchable/lt.rb +0 -23
- data/lib/mongoid/matchable/lte.rb +0 -23
- data/lib/mongoid/matchable/ne.rb +0 -21
- data/lib/mongoid/matchable/nin.rb +0 -22
- data/lib/mongoid/matchable/nor.rb +0 -37
- data/lib/mongoid/matchable/or.rb +0 -33
- data/lib/mongoid/matchable/regexp.rb +0 -27
- data/lib/mongoid/matchable/size.rb +0 -21
- data/lib/mongoid/query_cache.rb +0 -331
- data/lib/support/ruby_version.rb +0 -26
- data/spec/app/models/account.rb +0 -36
- data/spec/app/models/acolyte.rb +0 -17
- data/spec/app/models/actor.rb +0 -18
- data/spec/app/models/actress.rb +0 -2
- data/spec/app/models/address.rb +0 -79
- data/spec/app/models/address_component.rb +0 -5
- data/spec/app/models/address_number.rb +0 -6
- data/spec/app/models/agency.rb +0 -5
- data/spec/app/models/agent.rb +0 -16
- data/spec/app/models/album.rb +0 -18
- data/spec/app/models/alert.rb +0 -7
- data/spec/app/models/animal.rb +0 -26
- data/spec/app/models/answer.rb +0 -6
- data/spec/app/models/appointment.rb +0 -7
- data/spec/app/models/article.rb +0 -12
- data/spec/app/models/artist.rb +0 -87
- data/spec/app/models/artwork.rb +0 -4
- data/spec/app/models/audio.rb +0 -5
- data/spec/app/models/augmentation.rb +0 -11
- data/spec/app/models/author.rb +0 -6
- data/spec/app/models/baby.rb +0 -4
- data/spec/app/models/band.rb +0 -31
- data/spec/app/models/bar.rb +0 -10
- data/spec/app/models/basic.rb +0 -6
- data/spec/app/models/bed.rb +0 -1
- data/spec/app/models/big_palette.rb +0 -2
- data/spec/app/models/birthday.rb +0 -13
- data/spec/app/models/bomb.rb +0 -4
- data/spec/app/models/book.rb +0 -16
- data/spec/app/models/breed.rb +0 -4
- data/spec/app/models/browser.rb +0 -6
- data/spec/app/models/building.rb +0 -6
- data/spec/app/models/building_address.rb +0 -7
- data/spec/app/models/bus.rb +0 -7
- data/spec/app/models/business.rb +0 -5
- data/spec/app/models/callback_test.rb +0 -9
- data/spec/app/models/canvas.rb +0 -25
- data/spec/app/models/car.rb +0 -1
- data/spec/app/models/cat.rb +0 -8
- data/spec/app/models/category.rb +0 -8
- data/spec/app/models/child.rb +0 -4
- data/spec/app/models/child_doc.rb +0 -22
- data/spec/app/models/church.rb +0 -6
- data/spec/app/models/circle.rb +0 -3
- data/spec/app/models/circuit.rb +0 -4
- data/spec/app/models/circus.rb +0 -7
- data/spec/app/models/code.rb +0 -11
- data/spec/app/models/comment.rb +0 -16
- data/spec/app/models/company.rb +0 -5
- data/spec/app/models/consumption_period.rb +0 -7
- data/spec/app/models/contextable_item.rb +0 -5
- data/spec/app/models/contractor.rb +0 -5
- data/spec/app/models/cookie.rb +0 -6
- data/spec/app/models/country_code.rb +0 -8
- data/spec/app/models/courier_job.rb +0 -4
- data/spec/app/models/customer.rb +0 -11
- data/spec/app/models/customer_address.rb +0 -12
- data/spec/app/models/definition.rb +0 -9
- data/spec/app/models/delegating_patient.rb +0 -16
- data/spec/app/models/description.rb +0 -11
- data/spec/app/models/dictionary.rb +0 -10
- data/spec/app/models/division.rb +0 -10
- data/spec/app/models/doctor.rb +0 -12
- data/spec/app/models/dog.rb +0 -7
- data/spec/app/models/dokument.rb +0 -6
- data/spec/app/models/draft.rb +0 -9
- data/spec/app/models/dragon.rb +0 -4
- data/spec/app/models/driver.rb +0 -7
- data/spec/app/models/drug.rb +0 -6
- data/spec/app/models/dungeon.rb +0 -4
- data/spec/app/models/edit.rb +0 -5
- data/spec/app/models/email.rb +0 -6
- data/spec/app/models/employer.rb +0 -5
- data/spec/app/models/entry.rb +0 -6
- data/spec/app/models/eraser.rb +0 -1
- data/spec/app/models/even.rb +0 -7
- data/spec/app/models/event.rb +0 -22
- data/spec/app/models/exhibition.rb +0 -4
- data/spec/app/models/exhibitor.rb +0 -6
- data/spec/app/models/explosion.rb +0 -4
- data/spec/app/models/eye.rb +0 -9
- data/spec/app/models/eye_bowl.rb +0 -9
- data/spec/app/models/face.rb +0 -8
- data/spec/app/models/favorite.rb +0 -6
- data/spec/app/models/filesystem.rb +0 -5
- data/spec/app/models/fire_hydrant.rb +0 -6
- data/spec/app/models/firefox.rb +0 -4
- data/spec/app/models/fish.rb +0 -7
- data/spec/app/models/folder.rb +0 -7
- data/spec/app/models/folder_item.rb +0 -9
- data/spec/app/models/fruits.rb +0 -34
- data/spec/app/models/game.rb +0 -19
- data/spec/app/models/ghost.rb +0 -7
- data/spec/app/models/home.rb +0 -4
- data/spec/app/models/house.rb +0 -6
- data/spec/app/models/html_writer.rb +0 -3
- data/spec/app/models/id_key.rb +0 -6
- data/spec/app/models/image.rb +0 -22
- data/spec/app/models/implant.rb +0 -16
- data/spec/app/models/item.rb +0 -8
- data/spec/app/models/jar.rb +0 -7
- data/spec/app/models/kaleidoscope.rb +0 -6
- data/spec/app/models/kangaroo.rb +0 -4
- data/spec/app/models/label.rb +0 -47
- data/spec/app/models/language.rb +0 -5
- data/spec/app/models/lat_lng.rb +0 -15
- data/spec/app/models/league.rb +0 -11
- data/spec/app/models/learner.rb +0 -2
- data/spec/app/models/line_item.rb +0 -6
- data/spec/app/models/location.rb +0 -8
- data/spec/app/models/login.rb +0 -8
- data/spec/app/models/manufacturer.rb +0 -7
- data/spec/app/models/meat.rb +0 -4
- data/spec/app/models/membership.rb +0 -4
- data/spec/app/models/message.rb +0 -11
- data/spec/app/models/minim.rb +0 -7
- data/spec/app/models/mixed_drink.rb +0 -4
- data/spec/app/models/movie.rb +0 -13
- data/spec/app/models/my_hash.rb +0 -2
- data/spec/app/models/name.rb +0 -24
- data/spec/app/models/node.rb +0 -5
- data/spec/app/models/note.rb +0 -17
- data/spec/app/models/odd.rb +0 -7
- data/spec/app/models/ordered_post.rb +0 -11
- data/spec/app/models/ordered_preference.rb +0 -6
- data/spec/app/models/oscar.rb +0 -14
- data/spec/app/models/other_owner_object.rb +0 -2
- data/spec/app/models/override.rb +0 -16
- data/spec/app/models/ownable.rb +0 -6
- data/spec/app/models/owner.rb +0 -6
- data/spec/app/models/pack.rb +0 -3
- data/spec/app/models/page.rb +0 -16
- data/spec/app/models/page_question.rb +0 -4
- data/spec/app/models/palette.rb +0 -7
- data/spec/app/models/parent.rb +0 -5
- data/spec/app/models/parent_doc.rb +0 -6
- data/spec/app/models/passport.rb +0 -5
- data/spec/app/models/patient.rb +0 -9
- data/spec/app/models/pdf_writer.rb +0 -3
- data/spec/app/models/pencil.rb +0 -1
- data/spec/app/models/person.rb +0 -210
- data/spec/app/models/pet.rb +0 -23
- data/spec/app/models/pet_owner.rb +0 -6
- data/spec/app/models/phone.rb +0 -9
- data/spec/app/models/pizza.rb +0 -7
- data/spec/app/models/player.rb +0 -35
- data/spec/app/models/post.rb +0 -50
- data/spec/app/models/post_genre.rb +0 -6
- data/spec/app/models/powerup.rb +0 -11
- data/spec/app/models/preference.rb +0 -9
- data/spec/app/models/princess.rb +0 -10
- data/spec/app/models/product.rb +0 -17
- data/spec/app/models/profile.rb +0 -5
- data/spec/app/models/pronunciation.rb +0 -5
- data/spec/app/models/pub.rb +0 -6
- data/spec/app/models/purchase.rb +0 -4
- data/spec/app/models/question.rb +0 -8
- data/spec/app/models/quiz.rb +0 -7
- data/spec/app/models/rating.rb +0 -8
- data/spec/app/models/record.rb +0 -52
- data/spec/app/models/registry.rb +0 -4
- data/spec/app/models/role.rb +0 -7
- data/spec/app/models/root_category.rb +0 -4
- data/spec/app/models/sandwich.rb +0 -9
- data/spec/app/models/scheduler.rb +0 -7
- data/spec/app/models/seo.rb +0 -7
- data/spec/app/models/series.rb +0 -4
- data/spec/app/models/server.rb +0 -13
- data/spec/app/models/service.rb +0 -22
- data/spec/app/models/shape.rb +0 -12
- data/spec/app/models/shelf.rb +0 -5
- data/spec/app/models/shipment_address.rb +0 -3
- data/spec/app/models/shipping_container.rb +0 -5
- data/spec/app/models/shipping_pack.rb +0 -3
- data/spec/app/models/shop.rb +0 -6
- data/spec/app/models/short_agent.rb +0 -4
- data/spec/app/models/short_quiz.rb +0 -5
- data/spec/app/models/simple.rb +0 -5
- data/spec/app/models/slave.rb +0 -6
- data/spec/app/models/song.rb +0 -8
- data/spec/app/models/sound.rb +0 -5
- data/spec/app/models/square.rb +0 -4
- data/spec/app/models/staff.rb +0 -7
- data/spec/app/models/store_as_dup_test1.rb +0 -5
- data/spec/app/models/store_as_dup_test2.rb +0 -5
- data/spec/app/models/store_as_dup_test3.rb +0 -7
- data/spec/app/models/store_as_dup_test4.rb +0 -7
- data/spec/app/models/strategy.rb +0 -3
- data/spec/app/models/sub_item.rb +0 -3
- data/spec/app/models/subscription.rb +0 -5
- data/spec/app/models/survey.rb +0 -5
- data/spec/app/models/symptom.rb +0 -6
- data/spec/app/models/tag.rb +0 -8
- data/spec/app/models/target.rb +0 -5
- data/spec/app/models/template.rb +0 -5
- data/spec/app/models/thing.rb +0 -9
- data/spec/app/models/title.rb +0 -3
- data/spec/app/models/tool.rb +0 -8
- data/spec/app/models/topping.rb +0 -5
- data/spec/app/models/track.rb +0 -38
- data/spec/app/models/translation.rb +0 -5
- data/spec/app/models/tree.rb +0 -9
- data/spec/app/models/truck.rb +0 -3
- data/spec/app/models/user.rb +0 -21
- data/spec/app/models/user_account.rb +0 -10
- data/spec/app/models/validation_callback.rb +0 -10
- data/spec/app/models/vehicle.rb +0 -11
- data/spec/app/models/version.rb +0 -5
- data/spec/app/models/vertex.rb +0 -6
- data/spec/app/models/vet_visit.rb +0 -5
- data/spec/app/models/video.rb +0 -13
- data/spec/app/models/video_game.rb +0 -1
- data/spec/app/models/weapon.rb +0 -11
- data/spec/app/models/wiki_page.rb +0 -14
- data/spec/app/models/word.rb +0 -15
- data/spec/app/models/word_origin.rb +0 -11
- data/spec/app/models/writer.rb +0 -11
- data/spec/integration/matchable_spec.rb +0 -680
- data/spec/mongoid/contextual/geo_near_spec.rb +0 -469
- data/spec/mongoid/criteria/queryable/extensions/bignum_spec.rb +0 -58
- data/spec/mongoid/criteria/queryable/extensions/fixnum_spec.rb +0 -58
- data/spec/mongoid/criteria/queryable/forwardable_spec.rb +0 -87
- data/spec/mongoid/errors/eager_load_spec.rb +0 -29
- data/spec/mongoid/extensions_spec.rb +0 -29
- data/spec/mongoid/matchable/all_spec.rb +0 -31
- data/spec/mongoid/matchable/and_spec.rb +0 -187
- data/spec/mongoid/matchable/default_spec.rb +0 -137
- data/spec/mongoid/matchable/elem_match_spec.rb +0 -106
- data/spec/mongoid/matchable/eq_spec.rb +0 -48
- data/spec/mongoid/matchable/exists_spec.rb +0 -57
- data/spec/mongoid/matchable/gt_spec.rb +0 -86
- data/spec/mongoid/matchable/gte_spec.rb +0 -84
- data/spec/mongoid/matchable/in_spec.rb +0 -49
- data/spec/mongoid/matchable/lt_spec.rb +0 -85
- data/spec/mongoid/matchable/lte_spec.rb +0 -85
- data/spec/mongoid/matchable/ne_spec.rb +0 -46
- data/spec/mongoid/matchable/nin_spec.rb +0 -48
- data/spec/mongoid/matchable/nor_spec.rb +0 -209
- data/spec/mongoid/matchable/or_spec.rb +0 -131
- data/spec/mongoid/matchable/regexp_spec.rb +0 -59
- data/spec/mongoid/matchable/size_spec.rb +0 -25
- data/spec/mongoid/matchable_spec.rb +0 -853
- data/spec/mongoid/query_cache_spec.rb +0 -618
- data/spec/support/cluster_config.rb +0 -158
- data/spec/support/session_registry.rb +0 -50
- data.tar.gz.sig +0 -0
- metadata.gz.sig +0 -1
- /data/spec/{app → support}/models/array_field.rb +0 -0
- /data/spec/{app → support}/models/updatable.rb +0 -0
|
@@ -1,83 +1,74 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
require_relative 'attributes/nested_spec_models'
|
|
4
5
|
|
|
6
|
+
describe Mongoid::Attributes do
|
|
5
7
|
describe "\#{attribute}" do
|
|
6
|
-
|
|
7
|
-
context "when setting the value in the getter" do
|
|
8
|
-
|
|
8
|
+
context 'when setting the value in the getter' do
|
|
9
9
|
let(:account) do
|
|
10
10
|
Account.new
|
|
11
11
|
end
|
|
12
12
|
|
|
13
|
-
it
|
|
14
|
-
expect(account.overridden).to eq(
|
|
13
|
+
it 'does not cause an infinite loop' do
|
|
14
|
+
expect(account.overridden).to eq('not recommended')
|
|
15
15
|
end
|
|
16
16
|
end
|
|
17
17
|
|
|
18
|
-
context
|
|
19
|
-
|
|
18
|
+
context 'when the attribute was excluded in a criteria' do
|
|
20
19
|
let!(:person) do
|
|
21
|
-
Person.create(title:
|
|
20
|
+
Person.create!(title: 'sir')
|
|
22
21
|
end
|
|
23
22
|
|
|
24
|
-
context
|
|
25
|
-
|
|
23
|
+
context 'when the attribute is localized' do
|
|
26
24
|
before do
|
|
27
|
-
person.update_attribute(:desc,
|
|
25
|
+
person.update_attribute(:desc, 'test')
|
|
28
26
|
end
|
|
29
27
|
|
|
30
|
-
context
|
|
31
|
-
|
|
32
|
-
context "when the attribute exists" do
|
|
33
|
-
|
|
28
|
+
context 'when the context includes' do
|
|
29
|
+
context 'when the attribute exists' do
|
|
34
30
|
let(:from_db) do
|
|
35
31
|
Person.only(:desc).first
|
|
36
32
|
end
|
|
37
33
|
|
|
38
|
-
it
|
|
39
|
-
expect(from_db.desc).to eq(
|
|
34
|
+
it 'does not raise an error' do
|
|
35
|
+
expect(from_db.desc).to eq('test')
|
|
40
36
|
end
|
|
41
37
|
|
|
42
|
-
context
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
expect(from_db["desc"]).to eq("test")
|
|
38
|
+
context 'accessing via []' do
|
|
39
|
+
it 'does not raise an error' do
|
|
40
|
+
expect(from_db['desc']).to eq('test')
|
|
46
41
|
end
|
|
47
42
|
end
|
|
48
43
|
|
|
49
|
-
context
|
|
50
|
-
|
|
51
|
-
let(:
|
|
52
|
-
let(:city) {"NYC"}
|
|
44
|
+
context 'when calling only on a sub-document' do
|
|
45
|
+
let(:title) { 'Executive' }
|
|
46
|
+
let(:city) { 'NYC' }
|
|
53
47
|
let!(:agent) do
|
|
54
|
-
agent = Agent.new(:
|
|
55
|
-
agent.build_address(:
|
|
56
|
-
agent.save
|
|
48
|
+
agent = Agent.new(title: title)
|
|
49
|
+
agent.build_address(city: city)
|
|
50
|
+
agent.save!
|
|
57
51
|
agent
|
|
58
52
|
end
|
|
59
53
|
let(:from_db) do
|
|
60
|
-
Agent.only(:title,
|
|
54
|
+
Agent.only(:title, 'address.city').first
|
|
61
55
|
end
|
|
62
56
|
|
|
63
|
-
context
|
|
64
|
-
|
|
65
|
-
it "does not raise an error" do
|
|
57
|
+
context 'when the field is in the only' do
|
|
58
|
+
it 'does not raise an error' do
|
|
66
59
|
expect(from_db.address.city).to eq(city)
|
|
67
60
|
end
|
|
68
61
|
end
|
|
69
62
|
|
|
70
|
-
context
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
expect(from_db["address.city"]).to eq(city)
|
|
63
|
+
context 'accessing via []' do
|
|
64
|
+
it 'does not raise an error' do
|
|
65
|
+
expect(from_db['address.city']).to eq(city)
|
|
74
66
|
end
|
|
75
67
|
end
|
|
76
68
|
end
|
|
77
69
|
end
|
|
78
70
|
|
|
79
71
|
context 'when the attribute is a hash field' do
|
|
80
|
-
|
|
81
72
|
before do
|
|
82
73
|
person.update_attribute(:map, map)
|
|
83
74
|
end
|
|
@@ -90,70 +81,107 @@ describe Mongoid::Attributes do
|
|
|
90
81
|
Person.only('map.dates.y.2016').first
|
|
91
82
|
end
|
|
92
83
|
|
|
93
|
-
it
|
|
84
|
+
it 'does not raise an error' do
|
|
94
85
|
expect(from_db.map).to eq(map)
|
|
95
86
|
end
|
|
96
|
-
end
|
|
97
|
-
end
|
|
98
87
|
|
|
99
|
-
|
|
88
|
+
context 'when only one of the hash fields is projected' do
|
|
89
|
+
let(:map) do
|
|
90
|
+
{ 'dates' => { 'y' => { '2016' => 'Berlin', '2017' => 'Munich' } } }
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
let(:expected) do
|
|
94
|
+
{ 'dates' => { 'y' => {
|
|
95
|
+
'2016' => 'Berlin'
|
|
96
|
+
} } }
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
it 'retrieves only the projected fields' do
|
|
100
|
+
expect(from_db.map).to eq(expected)
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
context 'when several of the hash fields is projected' do
|
|
105
|
+
let(:map) do
|
|
106
|
+
{ 'dates' => { 'y' => {
|
|
107
|
+
'2016' => 'Berlin',
|
|
108
|
+
'2017' => 'Munich',
|
|
109
|
+
'2018' => 'Krakow'
|
|
110
|
+
} } }
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
let(:expected) do
|
|
114
|
+
{ 'dates' => { 'y' => {
|
|
115
|
+
'2016' => 'Berlin',
|
|
116
|
+
'2018' => 'Krakow'
|
|
117
|
+
} } }
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
let(:from_db) do
|
|
121
|
+
Person.only('map.dates.y.2016', 'map.dates.y.2018').first
|
|
122
|
+
end
|
|
100
123
|
|
|
101
|
-
|
|
124
|
+
it 'retrieves only the projected fields' do
|
|
125
|
+
expect(from_db.map).to eq(expected)
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
end
|
|
102
130
|
|
|
131
|
+
context 'when the context excludes' do
|
|
132
|
+
context 'when the attribute exists' do
|
|
103
133
|
let(:from_db) do
|
|
104
134
|
Person.without(:pets).first
|
|
105
135
|
end
|
|
106
136
|
|
|
107
|
-
it
|
|
108
|
-
expect(from_db.desc).to eq(
|
|
137
|
+
it 'does not raise an error' do
|
|
138
|
+
expect(from_db.desc).to eq('test')
|
|
109
139
|
end
|
|
110
140
|
end
|
|
111
141
|
end
|
|
112
142
|
end
|
|
113
143
|
|
|
114
|
-
context
|
|
115
|
-
|
|
144
|
+
context 'when excluding with only' do
|
|
116
145
|
let(:from_db) do
|
|
117
146
|
Person.only(:_id).first
|
|
118
147
|
end
|
|
119
148
|
|
|
120
|
-
it
|
|
121
|
-
expect
|
|
149
|
+
it 'raises an error' do
|
|
150
|
+
expect do
|
|
122
151
|
from_db.title
|
|
123
|
-
|
|
152
|
+
end.to raise_error(Mongoid::Errors::AttributeNotLoaded,
|
|
153
|
+
/Attempted to access attribute 'title' on Person which was not loaded/)
|
|
124
154
|
end
|
|
125
155
|
|
|
126
|
-
context
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
156
|
+
context 'accessing via []' do
|
|
157
|
+
it 'raises an error' do
|
|
158
|
+
expect do
|
|
159
|
+
from_db['title']
|
|
160
|
+
end.to raise_error(Mongoid::Errors::AttributeNotLoaded,
|
|
161
|
+
/Attempted to access attribute 'title' on Person which was not loaded/)
|
|
132
162
|
end
|
|
133
163
|
end
|
|
134
164
|
end
|
|
135
165
|
|
|
136
|
-
context
|
|
137
|
-
|
|
166
|
+
context 'when excluding with without' do
|
|
138
167
|
let(:from_db) do
|
|
139
168
|
Person.without(:title).first
|
|
140
169
|
end
|
|
141
170
|
|
|
142
|
-
it
|
|
143
|
-
expect
|
|
171
|
+
it 'raises an error' do
|
|
172
|
+
expect do
|
|
144
173
|
from_db.title
|
|
145
|
-
|
|
174
|
+
end.to raise_error(Mongoid::Errors::AttributeNotLoaded,
|
|
175
|
+
/Attempted to access attribute 'title' on Person which was not loaded/)
|
|
146
176
|
end
|
|
147
177
|
end
|
|
148
178
|
end
|
|
149
179
|
end
|
|
150
180
|
|
|
151
|
-
describe
|
|
152
|
-
|
|
181
|
+
describe '#[]' do
|
|
153
182
|
context 'when the document has a custom attribute type' do
|
|
154
|
-
|
|
155
183
|
let(:bar) do
|
|
156
|
-
Bar.create(lat_lng: LatLng.new(52.30, 13.25))
|
|
184
|
+
Bar.create!(lat_lng: LatLng.new(52.30, 13.25))
|
|
157
185
|
end
|
|
158
186
|
|
|
159
187
|
it 'returns the demongoized version of the attribute' do
|
|
@@ -161,130 +189,132 @@ describe Mongoid::Attributes do
|
|
|
161
189
|
end
|
|
162
190
|
end
|
|
163
191
|
|
|
164
|
-
context
|
|
165
|
-
|
|
192
|
+
context 'when the document is a new record' do
|
|
166
193
|
let(:person) do
|
|
167
194
|
Person.new
|
|
168
195
|
end
|
|
169
196
|
|
|
170
|
-
context
|
|
171
|
-
|
|
197
|
+
context 'when accessing a localized field' do
|
|
172
198
|
before do
|
|
173
|
-
person.desc =
|
|
199
|
+
person.desc = 'testing'
|
|
174
200
|
end
|
|
175
201
|
|
|
176
|
-
context
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
expect(person[:desc]).to eq("testing")
|
|
202
|
+
context 'when passing just the name' do
|
|
203
|
+
it 'returns the full value' do
|
|
204
|
+
expect(person[:desc]).to eq('testing')
|
|
180
205
|
end
|
|
181
206
|
end
|
|
182
207
|
|
|
183
|
-
context
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
expect(person["desc.en"]).to eq("testing")
|
|
208
|
+
context 'when passing the name with locale' do
|
|
209
|
+
it 'returns the value for the locale' do
|
|
210
|
+
expect(person['desc.en']).to eq('testing')
|
|
187
211
|
end
|
|
188
212
|
end
|
|
189
213
|
end
|
|
190
214
|
|
|
191
|
-
context
|
|
192
|
-
|
|
193
|
-
it "returns the default value" do
|
|
215
|
+
context 'when attribute does not exist' do
|
|
216
|
+
it 'returns the default value' do
|
|
194
217
|
expect(person[:age]).to eq(100)
|
|
195
218
|
end
|
|
196
219
|
end
|
|
197
220
|
|
|
198
|
-
context
|
|
199
|
-
|
|
221
|
+
context 'when attribute is not accessible' do
|
|
200
222
|
before do
|
|
201
223
|
person.owner_id = 5
|
|
202
224
|
end
|
|
203
225
|
|
|
204
|
-
it
|
|
226
|
+
it 'returns the value' do
|
|
205
227
|
expect(person[:owner_id]).to eq(5)
|
|
206
228
|
end
|
|
207
229
|
end
|
|
208
230
|
end
|
|
209
231
|
|
|
210
|
-
context
|
|
211
|
-
|
|
232
|
+
context 'when the document is an existing record' do
|
|
212
233
|
let!(:person) do
|
|
213
|
-
Person.create(title:
|
|
234
|
+
Person.create!(title: 'sir')
|
|
214
235
|
end
|
|
215
236
|
|
|
216
|
-
context
|
|
217
|
-
|
|
218
|
-
context "when excluding with only" do
|
|
219
|
-
|
|
237
|
+
context 'when the attribute was excluded in a criteria' do
|
|
238
|
+
context 'when excluding with only' do
|
|
220
239
|
let(:from_db) do
|
|
221
240
|
Person.only(:_id).first
|
|
222
241
|
end
|
|
223
242
|
|
|
224
|
-
it
|
|
225
|
-
expect
|
|
243
|
+
it 'raises an error' do
|
|
244
|
+
expect do
|
|
226
245
|
from_db[:title]
|
|
227
|
-
|
|
246
|
+
end.to raise_error(Mongoid::Errors::AttributeNotLoaded,
|
|
247
|
+
/Attempted to access attribute 'title' on Person which was not loaded/)
|
|
228
248
|
end
|
|
229
249
|
end
|
|
230
250
|
|
|
231
|
-
context
|
|
232
|
-
|
|
251
|
+
context 'when excluding with without' do
|
|
233
252
|
let(:from_db) do
|
|
234
253
|
Person.without(:title).first
|
|
235
254
|
end
|
|
236
255
|
|
|
237
|
-
it
|
|
238
|
-
expect
|
|
256
|
+
it 'raises an error' do
|
|
257
|
+
expect do
|
|
239
258
|
from_db[:title]
|
|
240
|
-
|
|
259
|
+
end.to raise_error(Mongoid::Errors::AttributeNotLoaded,
|
|
260
|
+
/Attempted to access attribute 'title' on Person which was not loaded/)
|
|
241
261
|
end
|
|
242
262
|
end
|
|
243
263
|
end
|
|
244
264
|
|
|
245
|
-
context
|
|
265
|
+
context 'when given nil' do
|
|
266
|
+
it 'returns nil' do
|
|
267
|
+
expect(person[nil]).to be_nil
|
|
268
|
+
end
|
|
269
|
+
end
|
|
246
270
|
|
|
247
|
-
|
|
271
|
+
context 'when given an empty string' do
|
|
272
|
+
it 'returns nil' do
|
|
273
|
+
expect(person['']).to be_nil
|
|
274
|
+
end
|
|
275
|
+
end
|
|
248
276
|
|
|
277
|
+
context 'when the field was not explicitly defined' do
|
|
278
|
+
context 'when excluding with only and the field was not excluded' do
|
|
249
279
|
let(:from_db) do
|
|
250
280
|
Person.only(:_id).first
|
|
251
281
|
end
|
|
252
282
|
|
|
253
|
-
it
|
|
254
|
-
expect
|
|
283
|
+
it 'raises an error' do
|
|
284
|
+
expect do
|
|
255
285
|
from_db[:undefined_field]
|
|
256
|
-
|
|
286
|
+
end.to raise_error(Mongoid::Errors::AttributeNotLoaded,
|
|
287
|
+
/Attempted to access attribute 'undefined_field' on Person which was not loaded/)
|
|
257
288
|
end
|
|
258
289
|
end
|
|
259
290
|
|
|
260
|
-
context
|
|
261
|
-
|
|
291
|
+
context 'when excluding with without and the field was excluded' do
|
|
262
292
|
let(:from_db) do
|
|
263
293
|
Person.without(:title).first
|
|
264
294
|
end
|
|
265
295
|
|
|
266
|
-
it
|
|
267
|
-
expect
|
|
296
|
+
it 'raises an error' do
|
|
297
|
+
expect do
|
|
268
298
|
from_db[:title]
|
|
269
|
-
|
|
299
|
+
end.to raise_error(Mongoid::Errors::AttributeNotLoaded,
|
|
300
|
+
/Attempted to access attribute 'title' on Person which was not loaded/)
|
|
270
301
|
end
|
|
271
302
|
end
|
|
272
303
|
|
|
273
|
-
context
|
|
274
|
-
|
|
304
|
+
context 'when excluding with without and the field was not excluded' do
|
|
275
305
|
let(:from_db) do
|
|
276
306
|
Person.without(:title).first
|
|
277
307
|
end
|
|
278
308
|
|
|
279
|
-
it
|
|
280
|
-
from_db[:undefined_field].should
|
|
309
|
+
it 'returns nil' do
|
|
310
|
+
from_db[:undefined_field].should be_nil
|
|
281
311
|
end
|
|
282
312
|
end
|
|
283
313
|
end
|
|
284
314
|
|
|
285
315
|
context 'when projecting with #only' do
|
|
286
316
|
let!(:person) do
|
|
287
|
-
Person.create(title: 'sir', name: { first_name: 'Jose', language: { name: 'es' } })
|
|
317
|
+
Person.create!(title: 'sir', name: { first_name: 'Jose', language: { name: 'es' } })
|
|
288
318
|
end
|
|
289
319
|
|
|
290
320
|
context 'when projecting an embedded association' do
|
|
@@ -293,18 +323,13 @@ describe Mongoid::Attributes do
|
|
|
293
323
|
end
|
|
294
324
|
|
|
295
325
|
context 'when retrieving a field of the association using the dot notation' do
|
|
296
|
-
|
|
297
326
|
it 'retrieves the field' do
|
|
298
|
-
pending 'MONGOID-5032, fixed in 7.3'
|
|
299
|
-
|
|
300
327
|
expect(from_db['name.first_name']).to eq 'Jose'
|
|
301
328
|
end
|
|
302
329
|
end
|
|
303
330
|
|
|
304
331
|
context 'when retrieving a field of a nested association using the dot notation' do
|
|
305
332
|
it 'retrieves the field' do
|
|
306
|
-
pending 'MONGOID-5032, fixed in 7.3'
|
|
307
|
-
|
|
308
333
|
expect(from_db['name.language.name']).to eq 'es'
|
|
309
334
|
end
|
|
310
335
|
end
|
|
@@ -317,50 +342,46 @@ describe Mongoid::Attributes do
|
|
|
317
342
|
|
|
318
343
|
context 'when retrieving a field under the projected sub-association' do
|
|
319
344
|
it 'retrieves the field' do
|
|
320
|
-
pending 'MONGOID-5032, fixed in 7.3'
|
|
321
|
-
|
|
322
345
|
expect(from_db['name.language.name']).to eq 'es'
|
|
323
346
|
end
|
|
324
347
|
end
|
|
325
348
|
|
|
326
349
|
context 'when retrieving a non-projected field' do
|
|
327
|
-
it 'raises
|
|
350
|
+
it 'raises AttributeNotLoaded' do
|
|
328
351
|
expect do
|
|
329
352
|
from_db['name.first_name']
|
|
330
|
-
end.to raise_error(
|
|
353
|
+
end.to raise_error(Mongoid::Errors::AttributeNotLoaded,
|
|
354
|
+
/Attempted to access attribute 'name.first_name' on Person which was not loaded/)
|
|
331
355
|
end
|
|
332
356
|
end
|
|
333
357
|
end
|
|
334
358
|
end
|
|
335
359
|
|
|
336
|
-
context
|
|
337
|
-
|
|
360
|
+
context 'when the attribute does not exist' do
|
|
338
361
|
before do
|
|
339
362
|
person.collection
|
|
340
|
-
|
|
341
|
-
|
|
363
|
+
.find({ _id: person.id })
|
|
364
|
+
.update_one({ '$unset' => { age: 1 } })
|
|
342
365
|
end
|
|
343
366
|
|
|
344
|
-
context
|
|
345
|
-
|
|
367
|
+
context 'when found' do
|
|
346
368
|
let(:found) do
|
|
347
369
|
Person.find(person.id)
|
|
348
370
|
end
|
|
349
371
|
|
|
350
|
-
it
|
|
372
|
+
it 'returns the default value' do
|
|
351
373
|
expect(found[:age]).to eq(100)
|
|
352
374
|
end
|
|
353
375
|
end
|
|
354
376
|
|
|
355
|
-
context
|
|
377
|
+
context 'when reloaded' do
|
|
378
|
+
config_override :raise_not_found_error, false
|
|
356
379
|
|
|
357
380
|
before do
|
|
358
|
-
Mongoid.raise_not_found_error = false
|
|
359
381
|
person.reload
|
|
360
|
-
Mongoid.raise_not_found_error = true
|
|
361
382
|
end
|
|
362
383
|
|
|
363
|
-
it
|
|
384
|
+
it 'returns the default value' do
|
|
364
385
|
expect(person[:age]).to eq(100)
|
|
365
386
|
end
|
|
366
387
|
end
|
|
@@ -368,14 +389,16 @@ describe Mongoid::Attributes do
|
|
|
368
389
|
end
|
|
369
390
|
end
|
|
370
391
|
|
|
371
|
-
describe
|
|
392
|
+
describe '#[]=' do
|
|
393
|
+
let(:person) do
|
|
394
|
+
Person.new
|
|
395
|
+
end
|
|
372
396
|
|
|
373
397
|
context 'when the document has a custom attribute type' do
|
|
374
|
-
|
|
375
398
|
let(:bar) do
|
|
376
399
|
Bar.new.tap do |b|
|
|
377
400
|
b[:lat_lng] = LatLng.new(52.30, 13.25)
|
|
378
|
-
b.save
|
|
401
|
+
b.save!
|
|
379
402
|
end
|
|
380
403
|
end
|
|
381
404
|
|
|
@@ -384,36 +407,30 @@ describe Mongoid::Attributes do
|
|
|
384
407
|
end
|
|
385
408
|
end
|
|
386
409
|
|
|
387
|
-
|
|
388
|
-
Person.new
|
|
389
|
-
end
|
|
390
|
-
|
|
391
|
-
context "when setting the attribute to nil" do
|
|
392
|
-
|
|
410
|
+
context 'when setting the attribute to nil' do
|
|
393
411
|
let!(:age) do
|
|
394
412
|
person[:age] = nil
|
|
395
413
|
end
|
|
396
414
|
|
|
397
|
-
it
|
|
415
|
+
it 'does not use the default value' do
|
|
398
416
|
expect(person.age).to be_nil
|
|
399
417
|
end
|
|
400
418
|
|
|
401
|
-
it
|
|
419
|
+
it 'returns the set value' do
|
|
402
420
|
expect(age).to be_nil
|
|
403
421
|
end
|
|
404
422
|
end
|
|
405
423
|
|
|
406
|
-
context
|
|
407
|
-
|
|
424
|
+
context 'when field has a default value' do
|
|
408
425
|
let!(:terms) do
|
|
409
426
|
person[:terms] = true
|
|
410
427
|
end
|
|
411
428
|
|
|
412
|
-
it
|
|
429
|
+
it 'allows overwriting of the default value' do
|
|
413
430
|
expect(person.terms).to be true
|
|
414
431
|
end
|
|
415
432
|
|
|
416
|
-
it
|
|
433
|
+
it 'returns the set value' do
|
|
417
434
|
expect(terms).to eq(true)
|
|
418
435
|
end
|
|
419
436
|
end
|
|
@@ -426,102 +443,96 @@ describe Mongoid::Attributes do
|
|
|
426
443
|
end
|
|
427
444
|
|
|
428
445
|
it 'writes the value into attributes' do
|
|
429
|
-
bar.attributes.should
|
|
446
|
+
bar.attributes.should eq({ '_id' => bar.id, 'missing_field' => 42 })
|
|
430
447
|
end
|
|
431
448
|
|
|
432
449
|
it 'makes the attribute accessible via []' do
|
|
433
|
-
bar['missing_field'].should
|
|
450
|
+
bar['missing_field'].should eq 42
|
|
434
451
|
end
|
|
435
452
|
|
|
436
453
|
context 'when writing fields on a document with projection' do
|
|
437
|
-
|
|
438
454
|
let!(:person) do
|
|
439
|
-
Person.create(title:
|
|
455
|
+
Person.create!(title: 'sir')
|
|
440
456
|
end
|
|
441
457
|
|
|
442
|
-
context
|
|
443
|
-
|
|
458
|
+
context 'when excluding with only and the field was not excluded' do
|
|
444
459
|
let(:from_db) do
|
|
445
460
|
Person.only(:_id).first
|
|
446
461
|
end
|
|
447
462
|
|
|
448
|
-
it
|
|
449
|
-
expect
|
|
463
|
+
it 'raises an error' do
|
|
464
|
+
expect do
|
|
450
465
|
from_db[:undefined_field] = 'x'
|
|
451
|
-
|
|
466
|
+
end.to raise_error(Mongoid::Errors::AttributeNotLoaded,
|
|
467
|
+
/Attempted to access attribute 'undefined_field' on Person which was not loaded/)
|
|
452
468
|
end
|
|
453
469
|
end
|
|
454
470
|
|
|
455
|
-
context
|
|
456
|
-
|
|
471
|
+
context 'when excluding with without and the field was excluded' do
|
|
457
472
|
let(:from_db) do
|
|
458
473
|
Person.without(:title).first
|
|
459
474
|
end
|
|
460
475
|
|
|
461
|
-
it
|
|
462
|
-
expect
|
|
476
|
+
it 'raises an error' do
|
|
477
|
+
expect do
|
|
463
478
|
from_db[:title] = 'x'
|
|
464
|
-
|
|
479
|
+
end.to raise_error(Mongoid::Errors::AttributeNotLoaded,
|
|
480
|
+
/Attempted to access attribute 'title' on Person which was not loaded/)
|
|
465
481
|
end
|
|
466
482
|
end
|
|
467
483
|
|
|
468
|
-
context
|
|
469
|
-
|
|
484
|
+
context 'when excluding with without and the field was not excluded' do
|
|
470
485
|
let(:from_db) do
|
|
471
486
|
Person.without(:title).first
|
|
472
487
|
end
|
|
473
488
|
|
|
474
|
-
it
|
|
489
|
+
it 'writes the value' do
|
|
475
490
|
from_db[:undefined_field] = 'x'
|
|
476
|
-
from_db[:undefined_field].should
|
|
491
|
+
from_db[:undefined_field].should eq 'x'
|
|
477
492
|
end
|
|
478
493
|
end
|
|
479
494
|
end
|
|
480
495
|
end
|
|
481
496
|
end
|
|
482
497
|
|
|
483
|
-
describe
|
|
484
|
-
|
|
498
|
+
describe '#_id' do
|
|
485
499
|
let(:person) do
|
|
486
500
|
Person.new
|
|
487
501
|
end
|
|
488
502
|
|
|
489
|
-
it
|
|
503
|
+
it 'delegates to #id' do
|
|
490
504
|
expect(person._id).to eq(person.id)
|
|
491
505
|
end
|
|
492
506
|
|
|
493
|
-
context
|
|
494
|
-
|
|
507
|
+
context 'when #id alias is overridden' do
|
|
495
508
|
let(:object) do
|
|
496
509
|
IdKey.new(key: 'foo')
|
|
497
510
|
end
|
|
498
511
|
|
|
499
|
-
it
|
|
512
|
+
it 'delegates to another method' do
|
|
500
513
|
expect(object.id).to eq(object.key)
|
|
501
514
|
end
|
|
502
515
|
end
|
|
503
516
|
end
|
|
504
517
|
|
|
505
|
-
describe
|
|
506
|
-
|
|
518
|
+
describe '#_id=' do
|
|
507
519
|
after(:all) do
|
|
508
520
|
Person.field(
|
|
509
521
|
:_id,
|
|
510
522
|
type: BSON::ObjectId,
|
|
511
523
|
pre_processed: true,
|
|
512
|
-
default: ->{ BSON::ObjectId.new },
|
|
524
|
+
default: -> { BSON::ObjectId.new },
|
|
513
525
|
overwrite: true
|
|
514
526
|
)
|
|
515
527
|
end
|
|
516
528
|
|
|
517
|
-
context
|
|
518
|
-
|
|
529
|
+
context 'when using object ids' do
|
|
519
530
|
before(:all) do
|
|
520
531
|
Person.field(
|
|
521
532
|
:_id,
|
|
522
533
|
type: BSON::ObjectId,
|
|
523
534
|
pre_processed: true,
|
|
524
|
-
default: ->{ BSON::ObjectId.new },
|
|
535
|
+
default: -> { BSON::ObjectId.new },
|
|
525
536
|
overwrite: true
|
|
526
537
|
)
|
|
527
538
|
end
|
|
@@ -534,61 +545,55 @@ describe Mongoid::Attributes do
|
|
|
534
545
|
BSON::ObjectId.new
|
|
535
546
|
end
|
|
536
547
|
|
|
537
|
-
context
|
|
538
|
-
|
|
548
|
+
context 'when providing an object id' do
|
|
539
549
|
before do
|
|
540
550
|
person._id = bson_id
|
|
541
551
|
end
|
|
542
552
|
|
|
543
|
-
it
|
|
553
|
+
it 'sets the id as the object id' do
|
|
544
554
|
expect(person.id).to eq(bson_id)
|
|
545
555
|
end
|
|
546
556
|
end
|
|
547
557
|
|
|
548
|
-
context
|
|
549
|
-
|
|
558
|
+
context 'when providing a string' do
|
|
550
559
|
before do
|
|
551
560
|
person._id = bson_id.to_s
|
|
552
561
|
end
|
|
553
562
|
|
|
554
|
-
it
|
|
563
|
+
it 'sets the id as the object id' do
|
|
555
564
|
expect(person.id).to eq(bson_id)
|
|
556
565
|
end
|
|
557
566
|
end
|
|
558
567
|
|
|
559
|
-
context
|
|
560
|
-
|
|
568
|
+
context 'when providing an integer' do
|
|
561
569
|
before do
|
|
562
570
|
person._id = 2
|
|
563
571
|
end
|
|
564
572
|
|
|
565
|
-
it
|
|
573
|
+
it 'sets the id as the supplied value to_s' do
|
|
566
574
|
expect(person.id).to eq(2)
|
|
567
575
|
end
|
|
568
576
|
end
|
|
569
577
|
|
|
570
|
-
context
|
|
571
|
-
|
|
578
|
+
context 'when #id= alias is overridden' do
|
|
572
579
|
let(:object) do
|
|
573
580
|
IdKey.new(key: 'foo')
|
|
574
581
|
end
|
|
575
582
|
|
|
576
|
-
it
|
|
583
|
+
it 'delegates to another method' do
|
|
577
584
|
object.id = 'bar'
|
|
578
585
|
expect(object.id).to eq('bar')
|
|
579
586
|
end
|
|
580
587
|
end
|
|
581
|
-
|
|
582
588
|
end
|
|
583
589
|
|
|
584
|
-
context
|
|
585
|
-
|
|
590
|
+
context 'when using string ids' do
|
|
586
591
|
before(:all) do
|
|
587
592
|
Person.field(
|
|
588
593
|
:_id,
|
|
589
594
|
type: String,
|
|
590
595
|
pre_processed: true,
|
|
591
|
-
default: ->{ BSON::ObjectId.new.to_s },
|
|
596
|
+
default: -> { BSON::ObjectId.new.to_s },
|
|
592
597
|
overwrite: true
|
|
593
598
|
)
|
|
594
599
|
end
|
|
@@ -601,42 +606,38 @@ describe Mongoid::Attributes do
|
|
|
601
606
|
BSON::ObjectId.new
|
|
602
607
|
end
|
|
603
608
|
|
|
604
|
-
context
|
|
605
|
-
|
|
609
|
+
context 'when providing an object id' do
|
|
606
610
|
before do
|
|
607
611
|
person._id = bson_id
|
|
608
612
|
end
|
|
609
613
|
|
|
610
|
-
it
|
|
614
|
+
it 'sets the id as the string of the object id' do
|
|
611
615
|
expect(person.id).to eq(bson_id.to_s)
|
|
612
616
|
end
|
|
613
617
|
end
|
|
614
618
|
|
|
615
|
-
context
|
|
616
|
-
|
|
619
|
+
context 'when providing a string' do
|
|
617
620
|
before do
|
|
618
621
|
person._id = bson_id.to_s
|
|
619
622
|
end
|
|
620
623
|
|
|
621
|
-
it
|
|
624
|
+
it 'sets the id as the string' do
|
|
622
625
|
expect(person.id).to eq(bson_id.to_s)
|
|
623
626
|
end
|
|
624
627
|
end
|
|
625
628
|
|
|
626
|
-
context
|
|
627
|
-
|
|
629
|
+
context 'when providing an integer' do
|
|
628
630
|
before do
|
|
629
631
|
person._id = 2
|
|
630
632
|
end
|
|
631
633
|
|
|
632
|
-
it
|
|
633
|
-
expect(person.id).to eq(
|
|
634
|
+
it 'sets the id as the supplied value to_s' do
|
|
635
|
+
expect(person.id).to eq('2')
|
|
634
636
|
end
|
|
635
637
|
end
|
|
636
638
|
end
|
|
637
639
|
|
|
638
|
-
context
|
|
639
|
-
|
|
640
|
+
context 'when using integer ids' do
|
|
640
641
|
before(:all) do
|
|
641
642
|
Person.field(:_id, type: Integer, overwrite: true)
|
|
642
643
|
end
|
|
@@ -645,62 +646,57 @@ describe Mongoid::Attributes do
|
|
|
645
646
|
Person.new
|
|
646
647
|
end
|
|
647
648
|
|
|
648
|
-
context
|
|
649
|
-
|
|
649
|
+
context 'when providing a string' do
|
|
650
650
|
before do
|
|
651
651
|
person._id = 1.to_s
|
|
652
652
|
end
|
|
653
653
|
|
|
654
|
-
it
|
|
654
|
+
it 'sets the id as the integer' do
|
|
655
655
|
expect(person.id).to eq(1)
|
|
656
656
|
end
|
|
657
657
|
end
|
|
658
658
|
|
|
659
|
-
context
|
|
660
|
-
|
|
659
|
+
context 'when providing an integer' do
|
|
661
660
|
before do
|
|
662
661
|
person._id = 2
|
|
663
662
|
end
|
|
664
663
|
|
|
665
|
-
it
|
|
664
|
+
it 'sets the id as the supplied value' do
|
|
666
665
|
expect(person.id).to eq(2)
|
|
667
666
|
end
|
|
668
667
|
end
|
|
669
668
|
end
|
|
670
669
|
end
|
|
671
670
|
|
|
672
|
-
describe
|
|
673
|
-
|
|
671
|
+
describe '#method_missing' do
|
|
674
672
|
let(:attributes) do
|
|
675
|
-
{ testing:
|
|
673
|
+
{ testing: 'Testing' }
|
|
676
674
|
end
|
|
677
675
|
|
|
678
676
|
let(:person) do
|
|
679
677
|
Person.new(attributes)
|
|
680
678
|
end
|
|
681
679
|
|
|
682
|
-
context
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
expect(person.testing).to eq("Testing")
|
|
680
|
+
context 'when an attribute exists' do
|
|
681
|
+
it 'allows the getter' do
|
|
682
|
+
expect(person.testing).to eq('Testing')
|
|
686
683
|
end
|
|
687
684
|
|
|
688
|
-
it
|
|
689
|
-
person.testing =
|
|
690
|
-
expect(person.testing).to eq(
|
|
685
|
+
it 'allows the setter' do
|
|
686
|
+
person.testing = 'Test'
|
|
687
|
+
expect(person.testing).to eq('Test')
|
|
691
688
|
end
|
|
692
689
|
|
|
693
|
-
it
|
|
694
|
-
expect(person.testing_before_type_cast).to eq(
|
|
690
|
+
it 'allows the getter before_type_cast' do
|
|
691
|
+
expect(person.testing_before_type_cast).to eq('Testing')
|
|
695
692
|
end
|
|
696
693
|
|
|
697
|
-
it
|
|
694
|
+
it 'returns true for respond_to?' do
|
|
698
695
|
expect(person.respond_to?(:testing)).to be true
|
|
699
696
|
end
|
|
700
697
|
end
|
|
701
698
|
|
|
702
|
-
context
|
|
703
|
-
|
|
699
|
+
context 'when the provided value needs mongoization' do
|
|
704
700
|
let(:new_years) do
|
|
705
701
|
DateTime.new(2013, 1, 1, 0, 0, 0)
|
|
706
702
|
end
|
|
@@ -709,77 +705,70 @@ describe Mongoid::Attributes do
|
|
|
709
705
|
person[:new_years] = new_years
|
|
710
706
|
end
|
|
711
707
|
|
|
712
|
-
it
|
|
708
|
+
it 'mongoizes the dynamic field' do
|
|
713
709
|
expect(person.new_years).to be_a(Time)
|
|
714
710
|
end
|
|
715
711
|
|
|
716
|
-
it
|
|
712
|
+
it 'keeps the same value' do
|
|
717
713
|
expect(person.new_years).to eq(new_years)
|
|
718
714
|
end
|
|
719
715
|
end
|
|
720
716
|
end
|
|
721
717
|
|
|
722
|
-
describe
|
|
723
|
-
|
|
724
|
-
context "when attributes dont have fields defined" do
|
|
725
|
-
|
|
718
|
+
describe '#process' do
|
|
719
|
+
context 'when attributes dont have fields defined' do
|
|
726
720
|
let(:attributes) do
|
|
727
721
|
{
|
|
728
|
-
nofieldstring:
|
|
722
|
+
nofieldstring: 'Testing',
|
|
729
723
|
nofieldint: 5,
|
|
730
724
|
employer: Employer.new
|
|
731
725
|
}
|
|
732
726
|
end
|
|
733
727
|
|
|
734
|
-
context
|
|
735
|
-
|
|
728
|
+
context 'when allowing dynamic fields' do
|
|
736
729
|
let!(:person) do
|
|
737
730
|
Person.new(attributes)
|
|
738
731
|
end
|
|
739
732
|
|
|
740
|
-
context
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
expect(person.attributes["nofieldstring"]).to eq("Testing")
|
|
733
|
+
context 'when attribute is a string' do
|
|
734
|
+
it 'adds the string to the attributes' do
|
|
735
|
+
expect(person.attributes['nofieldstring']).to eq('Testing')
|
|
744
736
|
end
|
|
745
737
|
end
|
|
746
738
|
|
|
747
|
-
context
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
expect(person.attributes["nofieldint"]).to eq(5)
|
|
739
|
+
context 'when attribute is not a string' do
|
|
740
|
+
it 'adds a cast value to the attributes' do
|
|
741
|
+
expect(person.attributes['nofieldint']).to eq(5)
|
|
751
742
|
end
|
|
752
743
|
end
|
|
753
744
|
end
|
|
754
745
|
|
|
755
|
-
context
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
}.to raise_error(Mongoid::Errors::UnknownAttribute)
|
|
746
|
+
context 'when not allowing dynamic fields' do
|
|
747
|
+
it 'raises an unknown attribute error on instantiation' do
|
|
748
|
+
expect do
|
|
749
|
+
Account.new({ anothernew: 'Test' })
|
|
750
|
+
end.to raise_error(Mongoid::Errors::UnknownAttribute)
|
|
761
751
|
end
|
|
762
752
|
end
|
|
763
753
|
end
|
|
764
754
|
|
|
765
|
-
context
|
|
766
|
-
|
|
755
|
+
context 'when supplied hash has string values' do
|
|
767
756
|
let(:bson_id) do
|
|
768
757
|
BSON::ObjectId.new
|
|
769
758
|
end
|
|
770
759
|
|
|
771
760
|
let!(:attributes) do
|
|
772
761
|
{
|
|
773
|
-
title:
|
|
774
|
-
age:
|
|
775
|
-
terms:
|
|
776
|
-
score:
|
|
762
|
+
title: 'value',
|
|
763
|
+
age: '30',
|
|
764
|
+
terms: 'true',
|
|
765
|
+
score: '',
|
|
777
766
|
name: {
|
|
778
|
-
_id:
|
|
767
|
+
_id: '2', first_name: 'Test', last_name: 'User'
|
|
779
768
|
},
|
|
780
769
|
addresses: [
|
|
781
|
-
{ _id:
|
|
782
|
-
{ _id:
|
|
770
|
+
{ _id: '3', street: 'First Street' },
|
|
771
|
+
{ _id: '4', street: 'Second Street' }
|
|
783
772
|
]
|
|
784
773
|
}
|
|
785
774
|
end
|
|
@@ -788,25 +777,23 @@ describe Mongoid::Attributes do
|
|
|
788
777
|
Person.new(attributes)
|
|
789
778
|
end
|
|
790
779
|
|
|
791
|
-
it
|
|
780
|
+
it 'casts integers' do
|
|
792
781
|
expect(person[:age]).to eq(30)
|
|
793
782
|
end
|
|
794
783
|
|
|
795
|
-
it
|
|
784
|
+
it 'casts booleans' do
|
|
796
785
|
expect(person[:terms]).to be true
|
|
797
786
|
end
|
|
798
787
|
|
|
799
|
-
it
|
|
788
|
+
it 'sets empty strings to nil' do
|
|
800
789
|
expect(person[:score]).to be_nil
|
|
801
790
|
end
|
|
802
791
|
end
|
|
803
792
|
|
|
804
|
-
context
|
|
805
|
-
|
|
806
|
-
context "when association is a has_one" do
|
|
807
|
-
|
|
793
|
+
context 'when associations provided in the attributes' do
|
|
794
|
+
context 'when association is a has_one' do
|
|
808
795
|
let(:name) do
|
|
809
|
-
Name.new(first_name:
|
|
796
|
+
Name.new(first_name: 'Testy')
|
|
810
797
|
end
|
|
811
798
|
|
|
812
799
|
let(:attributes) do
|
|
@@ -817,13 +804,12 @@ describe Mongoid::Attributes do
|
|
|
817
804
|
Person.new(attributes)
|
|
818
805
|
end
|
|
819
806
|
|
|
820
|
-
it
|
|
807
|
+
it 'sets the associations' do
|
|
821
808
|
expect(person.name).to eq(name)
|
|
822
809
|
end
|
|
823
810
|
end
|
|
824
811
|
|
|
825
|
-
context
|
|
826
|
-
|
|
812
|
+
context 'when association is a references_one' do
|
|
827
813
|
let(:game) do
|
|
828
814
|
Game.new(score: 100)
|
|
829
815
|
end
|
|
@@ -836,52 +822,49 @@ describe Mongoid::Attributes do
|
|
|
836
822
|
Person.new(attributes)
|
|
837
823
|
end
|
|
838
824
|
|
|
839
|
-
it
|
|
825
|
+
it 'sets the parent association' do
|
|
840
826
|
expect(person.game).to eq(game)
|
|
841
827
|
end
|
|
842
828
|
|
|
843
|
-
it
|
|
829
|
+
it 'sets the inverse association' do
|
|
844
830
|
expect(game.person).to eq(person)
|
|
845
831
|
end
|
|
846
832
|
end
|
|
847
833
|
|
|
848
|
-
context
|
|
849
|
-
|
|
834
|
+
context 'when association is a embedded_in' do
|
|
850
835
|
let(:person) do
|
|
851
836
|
Person.new
|
|
852
837
|
end
|
|
853
838
|
|
|
854
839
|
let(:name) do
|
|
855
|
-
Name.new(first_name:
|
|
840
|
+
Name.new(first_name: 'Tyler', person: person)
|
|
856
841
|
end
|
|
857
842
|
|
|
858
|
-
it
|
|
843
|
+
it 'sets the association' do
|
|
859
844
|
expect(name.person).to eq(person)
|
|
860
845
|
end
|
|
861
846
|
end
|
|
862
847
|
end
|
|
863
848
|
|
|
864
|
-
context
|
|
865
|
-
|
|
849
|
+
context 'when non-associations provided in the attributes' do
|
|
866
850
|
let(:employer) do
|
|
867
851
|
Employer.new
|
|
868
852
|
end
|
|
869
853
|
|
|
870
854
|
let(:attributes) do
|
|
871
|
-
{ employer_id: employer.id, title:
|
|
855
|
+
{ employer_id: employer.id, title: 'Sir' }
|
|
872
856
|
end
|
|
873
857
|
|
|
874
858
|
let(:person) do
|
|
875
859
|
Person.new(attributes)
|
|
876
860
|
end
|
|
877
861
|
|
|
878
|
-
it
|
|
879
|
-
expect(person.employer_id).to eq(
|
|
862
|
+
it 'calls the setter for the association' do
|
|
863
|
+
expect(person.employer_id).to eq('1')
|
|
880
864
|
end
|
|
881
865
|
end
|
|
882
866
|
|
|
883
|
-
context
|
|
884
|
-
|
|
867
|
+
context 'when an empty array is provided in the attributes' do
|
|
885
868
|
let(:attributes) do
|
|
886
869
|
{ aliases: [] }
|
|
887
870
|
end
|
|
@@ -890,13 +873,12 @@ describe Mongoid::Attributes do
|
|
|
890
873
|
Person.new(attributes)
|
|
891
874
|
end
|
|
892
875
|
|
|
893
|
-
it
|
|
876
|
+
it 'sets the empty array' do
|
|
894
877
|
expect(person.aliases).to be_empty
|
|
895
878
|
end
|
|
896
879
|
end
|
|
897
880
|
|
|
898
|
-
context
|
|
899
|
-
|
|
881
|
+
context 'when an empty hash is provided in the attributes' do
|
|
900
882
|
let(:attributes) do
|
|
901
883
|
{ map: {} }
|
|
902
884
|
end
|
|
@@ -905,50 +887,46 @@ describe Mongoid::Attributes do
|
|
|
905
887
|
Person.new(attributes)
|
|
906
888
|
end
|
|
907
889
|
|
|
908
|
-
it
|
|
890
|
+
it 'sets the empty hash' do
|
|
909
891
|
expect(person.map).to eq({})
|
|
910
892
|
end
|
|
911
893
|
end
|
|
912
894
|
|
|
913
|
-
context
|
|
914
|
-
|
|
895
|
+
context 'when providing tainted parameters' do
|
|
915
896
|
let(:params) do
|
|
916
|
-
ActionController::Parameters.new(title:
|
|
897
|
+
ActionController::Parameters.new(title: 'sir')
|
|
917
898
|
end
|
|
918
899
|
|
|
919
|
-
it
|
|
920
|
-
expect
|
|
900
|
+
it 'raises an error' do
|
|
901
|
+
expect do
|
|
921
902
|
Person.new(params)
|
|
922
|
-
|
|
903
|
+
end.to raise_error(ActiveModel::ForbiddenAttributesError)
|
|
923
904
|
end
|
|
924
905
|
end
|
|
925
906
|
end
|
|
926
907
|
|
|
927
|
-
context
|
|
928
|
-
|
|
908
|
+
context 'updating when attributes already exist' do
|
|
929
909
|
let(:person) do
|
|
930
|
-
Person.new(title:
|
|
910
|
+
Person.new(title: 'Sir')
|
|
931
911
|
end
|
|
932
912
|
|
|
933
913
|
let(:attributes) do
|
|
934
|
-
{ dob:
|
|
914
|
+
{ dob: '2000-01-01' }
|
|
935
915
|
end
|
|
936
916
|
|
|
937
917
|
before do
|
|
938
918
|
person.process_attributes(attributes)
|
|
939
919
|
end
|
|
940
920
|
|
|
941
|
-
it
|
|
942
|
-
expect(person.title).to eq(
|
|
921
|
+
it 'only overwrites supplied attributes' do
|
|
922
|
+
expect(person.title).to eq('Sir')
|
|
943
923
|
end
|
|
944
924
|
end
|
|
945
925
|
|
|
946
|
-
describe
|
|
947
|
-
|
|
926
|
+
describe '#read_attribute' do
|
|
948
927
|
context 'when the document has a custom attribute type' do
|
|
949
|
-
|
|
950
928
|
let(:bar) do
|
|
951
|
-
Bar.create(lat_lng: LatLng.new(52.30, 13.25))
|
|
929
|
+
Bar.create!(lat_lng: LatLng.new(52.30, 13.25))
|
|
952
930
|
end
|
|
953
931
|
|
|
954
932
|
it 'returns the demongoized version of the attribute' do
|
|
@@ -956,206 +934,185 @@ describe Mongoid::Attributes do
|
|
|
956
934
|
end
|
|
957
935
|
end
|
|
958
936
|
|
|
959
|
-
context
|
|
960
|
-
|
|
937
|
+
context 'when the document is a new record' do
|
|
961
938
|
let(:person) do
|
|
962
939
|
Person.new
|
|
963
940
|
end
|
|
964
941
|
|
|
965
|
-
context
|
|
966
|
-
|
|
967
|
-
it "returns the default value" do
|
|
942
|
+
context 'when attribute does not exist' do
|
|
943
|
+
it 'returns the default value' do
|
|
968
944
|
expect(person.age).to eq(100)
|
|
969
945
|
expect(person.pets).to be false
|
|
970
946
|
end
|
|
971
|
-
|
|
972
947
|
end
|
|
973
948
|
|
|
974
|
-
context
|
|
975
|
-
|
|
949
|
+
context 'when attribute is not accessible' do
|
|
976
950
|
before do
|
|
977
951
|
person.owner_id = 5
|
|
978
952
|
end
|
|
979
953
|
|
|
980
|
-
it
|
|
954
|
+
it 'returns the value' do
|
|
981
955
|
expect(person.read_attribute(:owner_id)).to eq(5)
|
|
982
956
|
end
|
|
983
957
|
end
|
|
984
958
|
end
|
|
985
959
|
|
|
986
|
-
context
|
|
987
|
-
|
|
960
|
+
context 'when the document is an existing record' do
|
|
988
961
|
let(:person) do
|
|
989
|
-
Person.create
|
|
962
|
+
Person.create!
|
|
990
963
|
end
|
|
991
964
|
|
|
992
|
-
context
|
|
965
|
+
context 'when the attribute does not exist' do
|
|
966
|
+
config_override :raise_not_found_error, false
|
|
993
967
|
|
|
994
968
|
before do
|
|
995
969
|
person.collection
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
Mongoid.raise_not_found_error = false
|
|
970
|
+
.find({ _id: person.id })
|
|
971
|
+
.update_one({ '$unset' => { age: 1 } })
|
|
999
972
|
person.reload
|
|
1000
|
-
Mongoid.raise_not_found_error = true
|
|
1001
973
|
end
|
|
1002
974
|
|
|
1003
|
-
it
|
|
975
|
+
it 'returns the default value' do
|
|
1004
976
|
expect(person.age).to eq(100)
|
|
1005
977
|
end
|
|
1006
978
|
end
|
|
1007
979
|
end
|
|
1008
980
|
|
|
1009
|
-
context
|
|
1010
|
-
|
|
981
|
+
context 'when attribute has an aliased name' do
|
|
1011
982
|
let(:person) do
|
|
1012
983
|
Person.new
|
|
1013
984
|
end
|
|
1014
985
|
|
|
1015
|
-
before
|
|
1016
|
-
person.write_attribute(:t,
|
|
986
|
+
before do
|
|
987
|
+
person.write_attribute(:t, 'aliased field to test')
|
|
1017
988
|
end
|
|
1018
989
|
|
|
1019
|
-
it
|
|
1020
|
-
expect(person.read_attribute(:test)).to eq(
|
|
990
|
+
it 'returns the value of the aliased field' do
|
|
991
|
+
expect(person.read_attribute(:test)).to eq('aliased field to test')
|
|
1021
992
|
end
|
|
1022
993
|
end
|
|
1023
994
|
end
|
|
1024
995
|
|
|
1025
|
-
describe
|
|
1026
|
-
|
|
996
|
+
describe '#read_attribute_before_type_cast' do
|
|
1027
997
|
let(:person) do
|
|
1028
|
-
Person.create
|
|
998
|
+
Person.create!
|
|
1029
999
|
end
|
|
1030
1000
|
|
|
1031
|
-
context
|
|
1032
|
-
|
|
1033
|
-
it "returns the default value" do
|
|
1001
|
+
context 'when the attribute has not yet been assigned' do
|
|
1002
|
+
it 'returns the default value' do
|
|
1034
1003
|
expect(person.age_before_type_cast).to eq(100)
|
|
1035
1004
|
end
|
|
1036
1005
|
end
|
|
1037
1006
|
|
|
1038
|
-
context
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
person.
|
|
1042
|
-
expect(person.age_before_type_cast).to eq("old")
|
|
1007
|
+
context 'after the attribute has been assigned' do
|
|
1008
|
+
it 'returns the default value' do
|
|
1009
|
+
person.age = '42'
|
|
1010
|
+
expect(person.age_before_type_cast).to eq('42')
|
|
1043
1011
|
end
|
|
1044
1012
|
end
|
|
1045
1013
|
|
|
1046
1014
|
context 'when reading fields on a document with projection' do
|
|
1047
|
-
|
|
1048
1015
|
let!(:person) do
|
|
1049
|
-
Person.create(title:
|
|
1016
|
+
Person.create!(title: 'sir')
|
|
1050
1017
|
end
|
|
1051
1018
|
|
|
1052
|
-
context
|
|
1053
|
-
|
|
1019
|
+
context 'when excluding with only and the field was not excluded' do
|
|
1054
1020
|
let(:from_db) do
|
|
1055
1021
|
Person.only(:_id).first
|
|
1056
1022
|
end
|
|
1057
1023
|
|
|
1058
|
-
it
|
|
1059
|
-
expect
|
|
1024
|
+
it 'raises an error' do
|
|
1025
|
+
expect do
|
|
1060
1026
|
from_db.read_attribute(:undefined_field)
|
|
1061
|
-
|
|
1027
|
+
end.to raise_error(Mongoid::Errors::AttributeNotLoaded,
|
|
1028
|
+
/Attempted to access attribute 'undefined_field' on Person which was not loaded/)
|
|
1062
1029
|
end
|
|
1063
1030
|
end
|
|
1064
1031
|
|
|
1065
|
-
context
|
|
1066
|
-
|
|
1032
|
+
context 'when excluding with without and the field was excluded' do
|
|
1067
1033
|
let(:from_db) do
|
|
1068
1034
|
Person.without(:title).first
|
|
1069
1035
|
end
|
|
1070
1036
|
|
|
1071
|
-
it
|
|
1072
|
-
expect
|
|
1037
|
+
it 'raises an error' do
|
|
1038
|
+
expect do
|
|
1073
1039
|
from_db.read_attribute(:title)
|
|
1074
|
-
|
|
1040
|
+
end.to raise_error(Mongoid::Errors::AttributeNotLoaded,
|
|
1041
|
+
/Attempted to access attribute 'title' on Person which was not loaded/)
|
|
1075
1042
|
end
|
|
1076
1043
|
end
|
|
1077
1044
|
|
|
1078
|
-
context
|
|
1079
|
-
|
|
1045
|
+
context 'when excluding with without and the field was not excluded' do
|
|
1080
1046
|
let(:from_db) do
|
|
1081
1047
|
Person.without(:title).first
|
|
1082
1048
|
end
|
|
1083
1049
|
|
|
1084
|
-
it
|
|
1085
|
-
from_db.read_attribute(:undefined_field).should
|
|
1050
|
+
it 'returns nil' do
|
|
1051
|
+
from_db.read_attribute(:undefined_field).should be_nil
|
|
1086
1052
|
end
|
|
1087
1053
|
end
|
|
1088
1054
|
end
|
|
1089
1055
|
end
|
|
1090
1056
|
|
|
1091
|
-
describe
|
|
1092
|
-
|
|
1093
|
-
context "when document is a new record" do
|
|
1094
|
-
|
|
1057
|
+
describe '#attribute_present?' do
|
|
1058
|
+
context 'when document is a new record' do
|
|
1095
1059
|
let(:person) do
|
|
1096
1060
|
Person.new
|
|
1097
1061
|
end
|
|
1098
1062
|
|
|
1099
|
-
context
|
|
1100
|
-
|
|
1101
|
-
it "returns false" do
|
|
1063
|
+
context 'when attribute does not exist' do
|
|
1064
|
+
it 'returns false' do
|
|
1102
1065
|
expect(person.attribute_present?(:owner_id)).to be false
|
|
1103
1066
|
end
|
|
1104
1067
|
end
|
|
1105
1068
|
|
|
1106
|
-
context
|
|
1069
|
+
context 'when attribute does exist' do
|
|
1107
1070
|
before do
|
|
1108
1071
|
person.owner_id = 5
|
|
1109
1072
|
end
|
|
1110
1073
|
|
|
1111
|
-
it
|
|
1074
|
+
it 'returns true' do
|
|
1112
1075
|
expect(person.attribute_present?(:owner_id)).to be true
|
|
1113
1076
|
end
|
|
1114
1077
|
end
|
|
1115
1078
|
end
|
|
1116
1079
|
|
|
1117
|
-
context
|
|
1118
|
-
|
|
1080
|
+
context 'when the document is an existing record' do
|
|
1119
1081
|
let(:person) do
|
|
1120
|
-
Person.create
|
|
1082
|
+
Person.create!
|
|
1121
1083
|
end
|
|
1122
1084
|
|
|
1123
|
-
context
|
|
1085
|
+
context 'when the attribute does not exist' do
|
|
1086
|
+
config_override :raise_not_found_error, false
|
|
1124
1087
|
|
|
1125
1088
|
before do
|
|
1126
1089
|
person.collection
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
Mongoid.raise_not_found_error = false
|
|
1090
|
+
.find({ _id: person.id })
|
|
1091
|
+
.update_one({ '$unset' => { age: 1 } })
|
|
1130
1092
|
person.reload
|
|
1131
|
-
Mongoid.raise_not_found_error = true
|
|
1132
1093
|
end
|
|
1133
1094
|
|
|
1134
|
-
it
|
|
1095
|
+
it 'returns true' do
|
|
1135
1096
|
expect(person.attribute_present?(:age)).to be true
|
|
1136
1097
|
end
|
|
1137
1098
|
end
|
|
1138
1099
|
end
|
|
1139
1100
|
|
|
1140
|
-
context
|
|
1141
|
-
|
|
1101
|
+
context 'when the value is boolean' do
|
|
1142
1102
|
let(:person) do
|
|
1143
1103
|
Person.new
|
|
1144
1104
|
end
|
|
1145
1105
|
|
|
1146
|
-
context
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
it "return true" do
|
|
1106
|
+
context 'when attribute does not exist' do
|
|
1107
|
+
context 'when the value is true' do
|
|
1108
|
+
it 'return true' do
|
|
1151
1109
|
person.terms = false
|
|
1152
1110
|
expect(person.attribute_present?(:terms)).to be true
|
|
1153
1111
|
end
|
|
1154
1112
|
end
|
|
1155
1113
|
|
|
1156
|
-
context
|
|
1157
|
-
|
|
1158
|
-
it "return true" do
|
|
1114
|
+
context 'when the value is false' do
|
|
1115
|
+
it 'return true' do
|
|
1159
1116
|
person.terms = false
|
|
1160
1117
|
expect(person.attribute_present?(:terms)).to be true
|
|
1161
1118
|
end
|
|
@@ -1163,153 +1120,137 @@ describe Mongoid::Attributes do
|
|
|
1163
1120
|
end
|
|
1164
1121
|
end
|
|
1165
1122
|
|
|
1166
|
-
context
|
|
1167
|
-
|
|
1123
|
+
context 'when the value is blank string' do
|
|
1168
1124
|
let(:person) do
|
|
1169
1125
|
Person.new(title: '')
|
|
1170
1126
|
end
|
|
1171
1127
|
|
|
1172
|
-
it
|
|
1128
|
+
it 'return false' do
|
|
1173
1129
|
expect(person.attribute_present?(:title)).to be false
|
|
1174
1130
|
end
|
|
1175
1131
|
end
|
|
1176
1132
|
|
|
1177
|
-
context
|
|
1133
|
+
context 'when the attribute is not on only list' do
|
|
1134
|
+
before { Person.create! }
|
|
1178
1135
|
|
|
1179
|
-
before { Person.create }
|
|
1180
1136
|
let(:person) do
|
|
1181
1137
|
Person.only(:id).first
|
|
1182
1138
|
end
|
|
1183
1139
|
|
|
1184
|
-
it
|
|
1140
|
+
it 'return false' do
|
|
1185
1141
|
expect(person.attribute_present?(:foobar)).to be false
|
|
1186
1142
|
end
|
|
1187
1143
|
end
|
|
1188
1144
|
end
|
|
1189
1145
|
|
|
1190
|
-
describe
|
|
1191
|
-
|
|
1146
|
+
describe '#has_attribute?' do
|
|
1192
1147
|
let(:person) do
|
|
1193
|
-
Person.new(title:
|
|
1148
|
+
Person.new(title: 'sir')
|
|
1194
1149
|
end
|
|
1195
1150
|
|
|
1196
|
-
context
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
it "returns true" do
|
|
1151
|
+
context 'when the key is in the attributes' do
|
|
1152
|
+
context 'when provided a symbol' do
|
|
1153
|
+
it 'returns true' do
|
|
1201
1154
|
expect(person.has_attribute?(:title)).to be true
|
|
1202
1155
|
end
|
|
1203
1156
|
end
|
|
1204
1157
|
|
|
1205
|
-
context
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
expect(person.has_attribute?("title")).to be true
|
|
1158
|
+
context 'when provided a string' do
|
|
1159
|
+
it 'returns true' do
|
|
1160
|
+
expect(person.has_attribute?('title')).to be true
|
|
1209
1161
|
end
|
|
1210
1162
|
end
|
|
1211
1163
|
end
|
|
1212
1164
|
|
|
1213
|
-
context
|
|
1214
|
-
|
|
1215
|
-
it "returns false" do
|
|
1165
|
+
context 'when the key is not in the attributes' do
|
|
1166
|
+
it 'returns false' do
|
|
1216
1167
|
expect(person.has_attribute?(:employer_id)).to be false
|
|
1217
1168
|
end
|
|
1218
1169
|
end
|
|
1219
1170
|
end
|
|
1220
1171
|
|
|
1221
1172
|
describe '#has_attribute_before_type_cast?' do
|
|
1222
|
-
|
|
1223
1173
|
let(:person) do
|
|
1224
1174
|
Person.new
|
|
1225
1175
|
end
|
|
1226
1176
|
|
|
1227
|
-
context
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
expect(person.has_attribute_before_type_cast?(:age)).to be false
|
|
1177
|
+
context 'before the attribute has been assigned' do
|
|
1178
|
+
it 'returns true' do
|
|
1179
|
+
expect(person.has_attribute_before_type_cast?(:age)).to be true
|
|
1231
1180
|
end
|
|
1232
1181
|
end
|
|
1233
1182
|
|
|
1234
|
-
context
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
person.age = 'old'
|
|
1183
|
+
context 'after the attribute has been assigned' do
|
|
1184
|
+
it 'returns true' do
|
|
1185
|
+
person.age = '42'
|
|
1238
1186
|
expect(person.has_attribute_before_type_cast?(:age)).to be true
|
|
1239
1187
|
end
|
|
1240
1188
|
end
|
|
1241
1189
|
end
|
|
1242
1190
|
|
|
1243
|
-
describe
|
|
1244
|
-
|
|
1245
|
-
context "when the attribute exists" do
|
|
1246
|
-
|
|
1191
|
+
describe '#remove_attribute' do
|
|
1192
|
+
context 'when the attribute exists' do
|
|
1247
1193
|
let(:person) do
|
|
1248
|
-
Person.create(title:
|
|
1194
|
+
Person.create!(title: 'Sir')
|
|
1249
1195
|
end
|
|
1250
1196
|
|
|
1251
1197
|
before do
|
|
1252
1198
|
person.remove_attribute(:title)
|
|
1253
1199
|
end
|
|
1254
1200
|
|
|
1255
|
-
it
|
|
1201
|
+
it 'removes the attribute' do
|
|
1256
1202
|
expect(person.title).to be_nil
|
|
1257
1203
|
end
|
|
1258
1204
|
|
|
1259
|
-
it
|
|
1205
|
+
it 'removes the key from the attributes hash' do
|
|
1260
1206
|
expect(person.has_attribute?(:title)).to be false
|
|
1261
1207
|
end
|
|
1262
1208
|
|
|
1263
|
-
context
|
|
1264
|
-
|
|
1209
|
+
context 'when saving after the removal' do
|
|
1265
1210
|
before do
|
|
1266
|
-
person.save
|
|
1211
|
+
person.save!
|
|
1267
1212
|
end
|
|
1268
1213
|
|
|
1269
|
-
it
|
|
1214
|
+
it 'persists the removal' do
|
|
1270
1215
|
expect(person.reload.has_attribute?(:title)).to be false
|
|
1271
1216
|
end
|
|
1272
1217
|
end
|
|
1273
1218
|
end
|
|
1274
1219
|
|
|
1275
|
-
context
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
end
|
|
1280
|
-
|
|
1281
|
-
let(:person) do
|
|
1282
|
-
Person.new(pet: pet)
|
|
1283
|
-
end
|
|
1284
|
-
|
|
1285
|
-
before do
|
|
1286
|
-
person.save
|
|
1287
|
-
person.pet.remove_attribute(:name)
|
|
1288
|
-
end
|
|
1220
|
+
context 'when the attribute exists in embedded document' do
|
|
1221
|
+
let(:pet) do
|
|
1222
|
+
Animal.new(name: 'Cat')
|
|
1223
|
+
end
|
|
1289
1224
|
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1225
|
+
let(:person) do
|
|
1226
|
+
Person.new(pet: pet)
|
|
1227
|
+
end
|
|
1293
1228
|
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1229
|
+
before do
|
|
1230
|
+
person.save!
|
|
1231
|
+
person.pet.remove_attribute(:name)
|
|
1232
|
+
end
|
|
1297
1233
|
|
|
1298
|
-
|
|
1234
|
+
it 'removes the attribute' do
|
|
1235
|
+
expect(person.pet.name).to be_nil
|
|
1236
|
+
end
|
|
1299
1237
|
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1238
|
+
it 'removes the key from the attributes hash' do
|
|
1239
|
+
expect(person.pet.has_attribute?(:name)).to be false
|
|
1240
|
+
end
|
|
1303
1241
|
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1242
|
+
context 'when saving after the removal' do
|
|
1243
|
+
before do
|
|
1244
|
+
person.save!
|
|
1245
|
+
end
|
|
1308
1246
|
|
|
1247
|
+
it 'persists the removal' do
|
|
1248
|
+
expect(person.reload.pet.has_attribute?(:name)).to be false
|
|
1249
|
+
end
|
|
1250
|
+
end
|
|
1309
1251
|
end
|
|
1310
1252
|
|
|
1311
|
-
context
|
|
1312
|
-
|
|
1253
|
+
context 'when the attribute does not exist' do
|
|
1313
1254
|
let(:person) do
|
|
1314
1255
|
Person.new
|
|
1315
1256
|
end
|
|
@@ -1318,83 +1259,77 @@ describe Mongoid::Attributes do
|
|
|
1318
1259
|
person.remove_attribute(:title)
|
|
1319
1260
|
end
|
|
1320
1261
|
|
|
1321
|
-
it
|
|
1262
|
+
it 'does not fail' do
|
|
1322
1263
|
expect(person.title).to be_nil
|
|
1323
1264
|
end
|
|
1324
1265
|
end
|
|
1325
1266
|
|
|
1326
|
-
context
|
|
1327
|
-
|
|
1267
|
+
context 'when the document is new' do
|
|
1328
1268
|
let(:person) do
|
|
1329
|
-
Person.new(title:
|
|
1269
|
+
Person.new(title: 'sir')
|
|
1330
1270
|
end
|
|
1331
1271
|
|
|
1332
1272
|
before do
|
|
1333
1273
|
person.remove_attribute(:title)
|
|
1334
1274
|
end
|
|
1335
1275
|
|
|
1336
|
-
it
|
|
1276
|
+
it 'does not add a delayed unset operation' do
|
|
1337
1277
|
expect(person.delayed_atomic_unsets).to be_empty
|
|
1338
1278
|
end
|
|
1339
1279
|
end
|
|
1340
1280
|
|
|
1341
|
-
context
|
|
1342
|
-
|
|
1281
|
+
context 'when the attribute is aliased' do
|
|
1343
1282
|
context 'when the database name is used' do
|
|
1344
|
-
|
|
1345
1283
|
let(:person) do
|
|
1346
|
-
Person.create(at: Time.now)
|
|
1284
|
+
Person.create!(at: Time.now)
|
|
1347
1285
|
end
|
|
1348
1286
|
|
|
1349
1287
|
before do
|
|
1350
1288
|
person.remove_attribute(:at)
|
|
1351
1289
|
end
|
|
1352
1290
|
|
|
1353
|
-
it
|
|
1291
|
+
it 'removes the attribute' do
|
|
1354
1292
|
expect(person.at).to be_nil
|
|
1355
1293
|
end
|
|
1356
1294
|
|
|
1357
|
-
it
|
|
1295
|
+
it 'removes the key from the attributes hash' do
|
|
1358
1296
|
expect(person.has_attribute?(:at)).to be false
|
|
1359
1297
|
end
|
|
1360
1298
|
|
|
1361
|
-
context
|
|
1362
|
-
|
|
1299
|
+
context 'when saving after the removal' do
|
|
1363
1300
|
before do
|
|
1364
|
-
person.save
|
|
1301
|
+
person.save!
|
|
1365
1302
|
end
|
|
1366
1303
|
|
|
1367
|
-
it
|
|
1304
|
+
it 'persists the removal' do
|
|
1368
1305
|
expect(person.reload.has_attribute?(:at)).to be false
|
|
1369
1306
|
end
|
|
1370
1307
|
end
|
|
1371
1308
|
end
|
|
1372
1309
|
|
|
1373
1310
|
context 'when the alias is used' do
|
|
1374
|
-
|
|
1375
1311
|
let(:person) do
|
|
1376
|
-
Person.create(aliased_timestamp: Time.now)
|
|
1312
|
+
Person.create!(aliased_timestamp: Time.now)
|
|
1377
1313
|
end
|
|
1378
1314
|
|
|
1379
1315
|
before do
|
|
1380
1316
|
person.remove_attribute(:aliased_timestamp)
|
|
1381
1317
|
end
|
|
1382
1318
|
|
|
1383
|
-
it
|
|
1319
|
+
it 'removes the attribute' do
|
|
1384
1320
|
expect(person.aliased_timestamp).to be_nil
|
|
1385
1321
|
end
|
|
1386
1322
|
|
|
1387
|
-
it
|
|
1323
|
+
it 'removes the key from the attributes hash' do
|
|
1388
1324
|
expect(person.has_attribute?(:aliased_timestamp)).to be false
|
|
1389
1325
|
end
|
|
1390
1326
|
|
|
1391
|
-
context
|
|
1392
|
-
|
|
1327
|
+
context 'when saving after the removal' do
|
|
1393
1328
|
before do
|
|
1394
|
-
person.save
|
|
1329
|
+
person.save!
|
|
1395
1330
|
end
|
|
1396
1331
|
|
|
1397
|
-
it
|
|
1332
|
+
it 'persists the removal' do
|
|
1398
1333
|
expect(person.reload.has_attribute?(:aliased_timestamp)).to be false
|
|
1399
1334
|
end
|
|
1400
1335
|
end
|
|
@@ -1402,115 +1337,100 @@ describe Mongoid::Attributes do
|
|
|
1402
1337
|
end
|
|
1403
1338
|
end
|
|
1404
1339
|
|
|
1405
|
-
describe
|
|
1406
|
-
|
|
1407
|
-
context "when allowing dynamic fields" do
|
|
1408
|
-
|
|
1340
|
+
describe '#respond_to?' do
|
|
1341
|
+
context 'when allowing dynamic fields' do
|
|
1409
1342
|
let(:person) do
|
|
1410
1343
|
Person.new
|
|
1411
1344
|
end
|
|
1412
1345
|
|
|
1413
|
-
context
|
|
1414
|
-
|
|
1415
|
-
context "when the attribute exists" do
|
|
1416
|
-
|
|
1346
|
+
context 'when asking for the getter' do
|
|
1347
|
+
context 'when the attribute exists' do
|
|
1417
1348
|
before do
|
|
1418
|
-
person[:attr] =
|
|
1349
|
+
person[:attr] = 'test'
|
|
1419
1350
|
end
|
|
1420
1351
|
|
|
1421
|
-
it
|
|
1352
|
+
it 'returns true' do
|
|
1422
1353
|
expect(person).to respond_to(:attr)
|
|
1423
1354
|
end
|
|
1424
1355
|
end
|
|
1425
1356
|
|
|
1426
|
-
context
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
expect(person).to_not respond_to(:attr)
|
|
1357
|
+
context 'when the attribute does not exist' do
|
|
1358
|
+
it 'returns false' do
|
|
1359
|
+
expect(person).not_to respond_to(:attr)
|
|
1430
1360
|
end
|
|
1431
1361
|
end
|
|
1432
1362
|
end
|
|
1433
1363
|
|
|
1434
|
-
context
|
|
1435
|
-
|
|
1436
|
-
context "when the attribute exists" do
|
|
1437
|
-
|
|
1364
|
+
context 'when asking for the setter' do
|
|
1365
|
+
context 'when the attribute exists' do
|
|
1438
1366
|
before do
|
|
1439
|
-
person[:attr] =
|
|
1367
|
+
person[:attr] = 'test'
|
|
1440
1368
|
end
|
|
1441
1369
|
|
|
1442
|
-
it
|
|
1370
|
+
it 'returns true' do
|
|
1443
1371
|
expect(person).to respond_to(:attr=)
|
|
1444
1372
|
end
|
|
1445
1373
|
end
|
|
1446
1374
|
|
|
1447
|
-
context
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
expect(person).to_not respond_to(:attr=)
|
|
1375
|
+
context 'when the attribute does not exist' do
|
|
1376
|
+
it 'returns false' do
|
|
1377
|
+
expect(person).not_to respond_to(:attr=)
|
|
1451
1378
|
end
|
|
1452
1379
|
end
|
|
1453
1380
|
end
|
|
1454
1381
|
end
|
|
1455
1382
|
|
|
1456
|
-
context
|
|
1457
|
-
|
|
1383
|
+
context 'when not allowing dynamic fields' do
|
|
1458
1384
|
let(:bar) do
|
|
1459
1385
|
Bar.new
|
|
1460
1386
|
end
|
|
1461
1387
|
|
|
1462
|
-
context
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
expect(bar).to_not respond_to(:attr)
|
|
1388
|
+
context 'when asking for the getter' do
|
|
1389
|
+
it 'returns false' do
|
|
1390
|
+
expect(bar).not_to respond_to(:attr)
|
|
1466
1391
|
end
|
|
1467
1392
|
end
|
|
1468
1393
|
|
|
1469
|
-
context
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
expect(bar).to_not respond_to(:attr=)
|
|
1394
|
+
context 'when asking for the setter' do
|
|
1395
|
+
it 'returns false' do
|
|
1396
|
+
expect(bar).not_to respond_to(:attr=)
|
|
1473
1397
|
end
|
|
1474
1398
|
end
|
|
1475
1399
|
end
|
|
1476
1400
|
end
|
|
1477
1401
|
|
|
1478
|
-
describe
|
|
1479
|
-
|
|
1480
|
-
context "when attribute does not exist" do
|
|
1481
|
-
|
|
1402
|
+
describe '#write_attribute' do
|
|
1403
|
+
context 'when attribute does not exist' do
|
|
1482
1404
|
let(:person) do
|
|
1483
1405
|
Person.new
|
|
1484
1406
|
end
|
|
1485
1407
|
|
|
1486
|
-
it
|
|
1408
|
+
it 'returns the default value' do
|
|
1487
1409
|
expect(person.age).to eq(100)
|
|
1488
1410
|
end
|
|
1489
1411
|
end
|
|
1490
1412
|
|
|
1491
|
-
context
|
|
1413
|
+
context 'when setting an attribute that needs type casting' do
|
|
1492
1414
|
let(:person) do
|
|
1493
|
-
Person.new(age:
|
|
1415
|
+
Person.new(age: '42')
|
|
1494
1416
|
end
|
|
1495
1417
|
|
|
1496
|
-
it
|
|
1497
|
-
expect(person.age_before_type_cast).to eq(
|
|
1418
|
+
it 'stores the attribute before type cast' do
|
|
1419
|
+
expect(person.age_before_type_cast).to eq('42')
|
|
1498
1420
|
end
|
|
1499
1421
|
end
|
|
1500
1422
|
|
|
1501
|
-
context
|
|
1502
|
-
|
|
1423
|
+
context 'when setting the attribute to nil' do
|
|
1503
1424
|
let(:person) do
|
|
1504
1425
|
Person.new(age: nil)
|
|
1505
1426
|
end
|
|
1506
1427
|
|
|
1507
|
-
it
|
|
1428
|
+
it 'does not use the default value' do
|
|
1508
1429
|
expect(person.age).to be_nil
|
|
1509
1430
|
end
|
|
1510
1431
|
end
|
|
1511
1432
|
|
|
1512
|
-
context
|
|
1513
|
-
|
|
1433
|
+
context 'when field has a default value' do
|
|
1514
1434
|
let(:person) do
|
|
1515
1435
|
Person.new
|
|
1516
1436
|
end
|
|
@@ -1519,163 +1439,204 @@ describe Mongoid::Attributes do
|
|
|
1519
1439
|
person.terms = true
|
|
1520
1440
|
end
|
|
1521
1441
|
|
|
1522
|
-
it
|
|
1442
|
+
it 'allows overwriting of the default value' do
|
|
1523
1443
|
expect(person.terms).to be true
|
|
1524
1444
|
end
|
|
1525
1445
|
end
|
|
1526
1446
|
|
|
1527
|
-
context
|
|
1528
|
-
|
|
1447
|
+
context 'when attribute has an aliased name' do
|
|
1529
1448
|
let(:person) do
|
|
1530
1449
|
Person.new
|
|
1531
1450
|
end
|
|
1532
1451
|
|
|
1533
|
-
before
|
|
1534
|
-
person.write_attribute(:test,
|
|
1452
|
+
before do
|
|
1453
|
+
person.write_attribute(:test, 'aliased field to test')
|
|
1535
1454
|
end
|
|
1536
1455
|
|
|
1537
|
-
it
|
|
1538
|
-
expect(person.t).to eq(
|
|
1456
|
+
it 'allows the field name to be updated' do
|
|
1457
|
+
expect(person.t).to eq('aliased field to test')
|
|
1539
1458
|
end
|
|
1540
1459
|
end
|
|
1541
1460
|
|
|
1542
|
-
context
|
|
1543
|
-
let(:person) { Person.new map: { somekey:
|
|
1461
|
+
context 'when attribute is a Hash' do
|
|
1462
|
+
let(:person) { Person.new map: { somekey: 'somevalue' } }
|
|
1544
1463
|
|
|
1545
|
-
it
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
end.to raise_error(Mongoid::Errors::InvalidValue, /Value of type Array cannot be written to a field of type Hash/)
|
|
1464
|
+
it 'writes nil when trying to set a value of invalid type - array' do
|
|
1465
|
+
person.map = []
|
|
1466
|
+
expect(person.map).to be_nil
|
|
1549
1467
|
end
|
|
1550
1468
|
|
|
1551
|
-
it
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
end.to raise_error(Mongoid::Errors::InvalidValue, /Value of type FalseClass cannot be written to a field of type Hash/)
|
|
1469
|
+
it 'writes nil when trying to set a value of invalid type - boolean' do
|
|
1470
|
+
person.map = false
|
|
1471
|
+
expect(person.map).to be_nil
|
|
1555
1472
|
end
|
|
1556
1473
|
|
|
1557
|
-
it
|
|
1558
|
-
expect(person.map).to eq(
|
|
1474
|
+
it 'can set a Hash value with stringified keys' do
|
|
1475
|
+
expect(person.map).to eq({ 'somekey' => 'somevalue' })
|
|
1559
1476
|
end
|
|
1560
1477
|
end
|
|
1561
1478
|
|
|
1562
|
-
context
|
|
1479
|
+
context 'when attribute is an Array' do
|
|
1563
1480
|
let(:person) { Person.new aliases: [ :alias_1 ] }
|
|
1564
1481
|
|
|
1565
|
-
it
|
|
1482
|
+
it 'can set an Array Value' do
|
|
1566
1483
|
expect(person.aliases).to eq([ :alias_1 ])
|
|
1567
1484
|
end
|
|
1568
1485
|
|
|
1569
|
-
it
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
end.to raise_error(Mongoid::Errors::InvalidValue, /Value of type Hash cannot be written to a field of type Array/)
|
|
1486
|
+
it 'writes nil when trying to set a value of invalid type - hash' do
|
|
1487
|
+
person.aliases = {}
|
|
1488
|
+
expect(person.aliases).to be_nil
|
|
1573
1489
|
end
|
|
1574
1490
|
|
|
1575
|
-
it
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
end.to raise_error(Mongoid::Errors::InvalidValue, /Value of type FalseClass cannot be written to a field of type Array/)
|
|
1491
|
+
it 'writes nil when trying to set a value of invalid type - boolean' do
|
|
1492
|
+
person.aliases = false
|
|
1493
|
+
expect(person.aliases).to be_nil
|
|
1579
1494
|
end
|
|
1580
1495
|
end
|
|
1581
1496
|
|
|
1582
|
-
context
|
|
1497
|
+
context 'when attribute is localized and #attributes is a BSON::Document' do
|
|
1583
1498
|
let(:dictionary) { Dictionary.new }
|
|
1584
1499
|
|
|
1585
1500
|
before do
|
|
1586
1501
|
allow(dictionary).to receive(:attributes).and_return(BSON::Document.new)
|
|
1587
1502
|
end
|
|
1588
1503
|
|
|
1589
|
-
it
|
|
1504
|
+
it 'sets the value for the current locale' do
|
|
1590
1505
|
dictionary.write_attribute(:description, 'foo')
|
|
1591
1506
|
expect(dictionary.description).to eq('foo')
|
|
1592
1507
|
end
|
|
1593
1508
|
end
|
|
1594
1509
|
|
|
1595
1510
|
context 'when writing fields on a document with projection' do
|
|
1596
|
-
|
|
1597
1511
|
let!(:person) do
|
|
1598
|
-
Person.create(title:
|
|
1512
|
+
Person.create!(title: 'sir')
|
|
1599
1513
|
end
|
|
1600
1514
|
|
|
1601
|
-
context
|
|
1602
|
-
|
|
1515
|
+
context 'when excluding with only and the field was not excluded' do
|
|
1603
1516
|
let(:from_db) do
|
|
1604
1517
|
Person.only(:_id).first
|
|
1605
1518
|
end
|
|
1606
1519
|
|
|
1607
|
-
it
|
|
1608
|
-
expect
|
|
1520
|
+
it 'raises an error' do
|
|
1521
|
+
expect do
|
|
1609
1522
|
from_db.write_attribute(:undefined_field, 'x')
|
|
1610
|
-
|
|
1523
|
+
end.to raise_error(Mongoid::Errors::AttributeNotLoaded,
|
|
1524
|
+
/Attempted to access attribute 'undefined_field' on Person which was not loaded/)
|
|
1611
1525
|
end
|
|
1612
1526
|
end
|
|
1613
1527
|
|
|
1614
|
-
context
|
|
1615
|
-
|
|
1528
|
+
context 'when excluding with without and the field was excluded' do
|
|
1616
1529
|
let(:from_db) do
|
|
1617
1530
|
Person.without(:title).first
|
|
1618
1531
|
end
|
|
1619
1532
|
|
|
1620
|
-
it
|
|
1621
|
-
expect
|
|
1533
|
+
it 'raises an error' do
|
|
1534
|
+
expect do
|
|
1622
1535
|
from_db.write_attribute(:title, 'x')
|
|
1623
|
-
|
|
1536
|
+
end.to raise_error(Mongoid::Errors::AttributeNotLoaded,
|
|
1537
|
+
/Attempted to access attribute 'title' on Person which was not loaded/)
|
|
1624
1538
|
end
|
|
1625
1539
|
end
|
|
1626
1540
|
|
|
1627
|
-
context
|
|
1628
|
-
|
|
1541
|
+
context 'when excluding with without and the field was not excluded' do
|
|
1629
1542
|
let(:from_db) do
|
|
1630
1543
|
Person.without(:title).first
|
|
1631
1544
|
end
|
|
1632
1545
|
|
|
1633
|
-
it
|
|
1546
|
+
it 'writes the value' do
|
|
1634
1547
|
from_db.write_attribute(:undefined_field, 'x')
|
|
1635
|
-
from_db.read_attribute(:undefined_field).should
|
|
1548
|
+
from_db.read_attribute(:undefined_field).should eq 'x'
|
|
1636
1549
|
end
|
|
1637
1550
|
end
|
|
1638
1551
|
end
|
|
1639
|
-
end
|
|
1640
1552
|
|
|
1641
|
-
|
|
1553
|
+
context 'when comparing the object_ids of the written value' do
|
|
1554
|
+
before do
|
|
1555
|
+
Person.create!
|
|
1556
|
+
end
|
|
1642
1557
|
|
|
1643
|
-
|
|
1644
|
-
|
|
1558
|
+
let(:person) do
|
|
1559
|
+
Person.first
|
|
1560
|
+
end
|
|
1561
|
+
|
|
1562
|
+
context 'when the field is not resizable' do
|
|
1563
|
+
let(:test) do
|
|
1564
|
+
person.write_attribute(:test, 'aliased field to test')
|
|
1565
|
+
end
|
|
1566
|
+
|
|
1567
|
+
it 'has the same object_id as the attributes hash value' do
|
|
1568
|
+
expect(test.object_id).to eq(person.test.object_id)
|
|
1569
|
+
end
|
|
1570
|
+
end
|
|
1571
|
+
|
|
1572
|
+
context 'when the field is resizable' do
|
|
1573
|
+
let(:arrays) do
|
|
1574
|
+
person.write_attribute(:arrays, [])
|
|
1575
|
+
end
|
|
1576
|
+
|
|
1577
|
+
it 'has the same object_id as the attributes hash value' do
|
|
1578
|
+
expect(arrays.object_id).to eq(person.arrays.object_id)
|
|
1579
|
+
end
|
|
1580
|
+
end
|
|
1581
|
+
|
|
1582
|
+
context 'when the field is a HABTM foreign key array' do
|
|
1583
|
+
let(:preference_ids) do
|
|
1584
|
+
person.write_attribute(:preference_ids, [])
|
|
1585
|
+
end
|
|
1586
|
+
|
|
1587
|
+
it 'has the same object_id as the attributes hash value' do
|
|
1588
|
+
expect(preference_ids.object_id).to eq(person.preference_ids.object_id)
|
|
1589
|
+
end
|
|
1590
|
+
end
|
|
1645
1591
|
end
|
|
1646
1592
|
|
|
1647
|
-
context
|
|
1593
|
+
context 'when map_big_decimal_to_decimal128 is enabled' do
|
|
1594
|
+
config_override :map_big_decimal_to_decimal128, true
|
|
1648
1595
|
|
|
1649
|
-
|
|
1650
|
-
|
|
1596
|
+
context 'when writing an identical number' do
|
|
1597
|
+
let(:band) { Band.create!(name: 'Nirvana', sales: 123_456.78).reload }
|
|
1598
|
+
|
|
1599
|
+
it 'does not mark the document as changed' do
|
|
1600
|
+
band.sales = 123_456.78
|
|
1601
|
+
expect(band.changed?).to be false
|
|
1602
|
+
end
|
|
1651
1603
|
end
|
|
1652
1604
|
end
|
|
1605
|
+
end
|
|
1606
|
+
|
|
1607
|
+
describe '#typed_value_for' do
|
|
1608
|
+
let(:person) do
|
|
1609
|
+
Person.new
|
|
1610
|
+
end
|
|
1653
1611
|
|
|
1654
|
-
context
|
|
1612
|
+
context 'when the key has been specified as a field' do
|
|
1613
|
+
it 'returns the typed value' do
|
|
1614
|
+
person.send(:typed_value_for, 'age', '51')
|
|
1615
|
+
end
|
|
1616
|
+
end
|
|
1655
1617
|
|
|
1618
|
+
context 'when the key has not been specified as a field' do
|
|
1656
1619
|
before do
|
|
1657
1620
|
allow(person).to receive(:fields).and_return({})
|
|
1658
1621
|
end
|
|
1659
1622
|
|
|
1660
|
-
it
|
|
1661
|
-
person.send(:typed_value_for,
|
|
1623
|
+
it 'returns the value' do
|
|
1624
|
+
person.send(:typed_value_for, 'age', expect('51')).to eq('51')
|
|
1662
1625
|
end
|
|
1663
1626
|
end
|
|
1664
1627
|
end
|
|
1665
1628
|
|
|
1666
|
-
describe
|
|
1667
|
-
|
|
1629
|
+
describe '#apply_default_attributes' do
|
|
1668
1630
|
let(:person) do
|
|
1669
1631
|
Person.new
|
|
1670
1632
|
end
|
|
1671
1633
|
|
|
1672
|
-
it
|
|
1634
|
+
it 'typecasts proc values' do
|
|
1673
1635
|
expect(person.age).to eq(100)
|
|
1674
1636
|
end
|
|
1675
1637
|
end
|
|
1676
1638
|
|
|
1677
|
-
describe
|
|
1678
|
-
|
|
1639
|
+
describe '#typed_attributes' do
|
|
1679
1640
|
let(:date_time) do
|
|
1680
1641
|
DateTime.current
|
|
1681
1642
|
end
|
|
@@ -1685,64 +1646,57 @@ describe Mongoid::Attributes do
|
|
|
1685
1646
|
end
|
|
1686
1647
|
|
|
1687
1648
|
it 'returns typecasted attributes' do
|
|
1688
|
-
expect(user.typed_attributes).to include(
|
|
1649
|
+
expect(user.typed_attributes).to include('last_login' => date_time)
|
|
1689
1650
|
end
|
|
1690
1651
|
end
|
|
1691
1652
|
|
|
1692
|
-
[
|
|
1693
|
-
|
|
1653
|
+
%i[attributes= write_attributes].each do |method|
|
|
1694
1654
|
describe "##{method}" do
|
|
1695
|
-
|
|
1696
|
-
context "when nested" do
|
|
1697
|
-
|
|
1655
|
+
context 'when nested' do
|
|
1698
1656
|
let(:person) do
|
|
1699
1657
|
Person.new
|
|
1700
1658
|
end
|
|
1701
1659
|
|
|
1702
1660
|
before do
|
|
1703
|
-
person.send(method, { videos: [{title:
|
|
1661
|
+
person.send(method, { videos: [ { title: 'Fight Club' } ] })
|
|
1704
1662
|
end
|
|
1705
1663
|
|
|
1706
|
-
it
|
|
1707
|
-
expect(person.videos.first.title).to eq(
|
|
1664
|
+
it 'sets nested documents' do
|
|
1665
|
+
expect(person.videos.first.title).to eq('Fight Club')
|
|
1708
1666
|
end
|
|
1709
1667
|
end
|
|
1710
1668
|
|
|
1711
|
-
context
|
|
1712
|
-
|
|
1669
|
+
context 'typecasting' do
|
|
1713
1670
|
let(:person) do
|
|
1714
1671
|
Person.new
|
|
1715
1672
|
end
|
|
1716
1673
|
|
|
1717
1674
|
let(:attributes) do
|
|
1718
|
-
{ age:
|
|
1675
|
+
{ age: '50' }
|
|
1719
1676
|
end
|
|
1720
1677
|
|
|
1721
|
-
context
|
|
1722
|
-
|
|
1678
|
+
context 'when passing a hash' do
|
|
1723
1679
|
before do
|
|
1724
1680
|
person.send(method, attributes)
|
|
1725
1681
|
end
|
|
1726
1682
|
|
|
1727
|
-
it
|
|
1683
|
+
it 'properly casts values' do
|
|
1728
1684
|
expect(person.age).to eq(50)
|
|
1729
1685
|
end
|
|
1730
1686
|
end
|
|
1731
1687
|
|
|
1732
|
-
context
|
|
1733
|
-
|
|
1688
|
+
context 'when passing nil' do
|
|
1734
1689
|
before do
|
|
1735
1690
|
person.send(method, nil)
|
|
1736
1691
|
end
|
|
1737
1692
|
|
|
1738
|
-
it
|
|
1693
|
+
it 'does not set anything' do
|
|
1739
1694
|
expect(person.age).to eq(100)
|
|
1740
1695
|
end
|
|
1741
1696
|
end
|
|
1742
1697
|
end
|
|
1743
1698
|
|
|
1744
|
-
context
|
|
1745
|
-
|
|
1699
|
+
context 'copying from instance' do
|
|
1746
1700
|
let(:person) do
|
|
1747
1701
|
Person.new
|
|
1748
1702
|
end
|
|
@@ -1759,25 +1713,23 @@ describe Mongoid::Attributes do
|
|
|
1759
1713
|
person.send(method, instance.attributes)
|
|
1760
1714
|
end
|
|
1761
1715
|
|
|
1762
|
-
it
|
|
1716
|
+
it 'properly copies values' do
|
|
1763
1717
|
expect(person.age).to eq(50)
|
|
1764
1718
|
end
|
|
1765
1719
|
|
|
1766
|
-
it
|
|
1720
|
+
it 'properly copies ranges' do
|
|
1767
1721
|
expect(person.range).to eq(1..100)
|
|
1768
1722
|
end
|
|
1769
1723
|
end
|
|
1770
1724
|
|
|
1771
|
-
context
|
|
1772
|
-
|
|
1773
|
-
context "when the parent has a has many through a has one" do
|
|
1774
|
-
|
|
1725
|
+
context 'on a parent document' do
|
|
1726
|
+
context 'when the parent has a has many through a has one' do
|
|
1775
1727
|
let(:owner) do
|
|
1776
|
-
PetOwner.new(title:
|
|
1728
|
+
PetOwner.new(title: 'Mr')
|
|
1777
1729
|
end
|
|
1778
1730
|
|
|
1779
1731
|
let(:pet) do
|
|
1780
|
-
Pet.new(name:
|
|
1732
|
+
Pet.new(name: 'Fido')
|
|
1781
1733
|
end
|
|
1782
1734
|
|
|
1783
1735
|
let(:vet_visit) do
|
|
@@ -1787,23 +1739,22 @@ describe Mongoid::Attributes do
|
|
|
1787
1739
|
before do
|
|
1788
1740
|
owner.pet = pet
|
|
1789
1741
|
pet.vet_visits = [ vet_visit ]
|
|
1790
|
-
owner.send(method, { pet: { name:
|
|
1742
|
+
owner.send(method, { pet: { name: 'Bingo' } })
|
|
1791
1743
|
end
|
|
1792
1744
|
|
|
1793
|
-
it
|
|
1794
|
-
expect(owner.pet.name).to eq(
|
|
1745
|
+
it 'does not overwrite child attributes if not in the hash' do
|
|
1746
|
+
expect(owner.pet.name).to eq('Bingo')
|
|
1795
1747
|
expect(owner.pet.vet_visits.size).to eq(1)
|
|
1796
1748
|
end
|
|
1797
1749
|
end
|
|
1798
1750
|
|
|
1799
|
-
context
|
|
1800
|
-
|
|
1751
|
+
context 'when parent destroy all child on setter' do
|
|
1801
1752
|
let(:owner) do
|
|
1802
|
-
PetOwner.create!(title:
|
|
1753
|
+
PetOwner.create!(title: 'Mr')
|
|
1803
1754
|
end
|
|
1804
1755
|
|
|
1805
1756
|
let(:pet) do
|
|
1806
|
-
Pet.create!(name:
|
|
1757
|
+
Pet.create!(name: 'Kika', pet_owner: owner)
|
|
1807
1758
|
end
|
|
1808
1759
|
|
|
1809
1760
|
let!(:vet_visit) do
|
|
@@ -1815,17 +1766,16 @@ describe Mongoid::Attributes do
|
|
|
1815
1766
|
pet.save!
|
|
1816
1767
|
end
|
|
1817
1768
|
|
|
1818
|
-
it
|
|
1819
|
-
expect(pet.vet_visits.count).to eq
|
|
1769
|
+
it 'has 3 new entries' do
|
|
1770
|
+
expect(pet.vet_visits.count).to eq 3
|
|
1820
1771
|
end
|
|
1821
1772
|
|
|
1822
|
-
it
|
|
1823
|
-
expect(pet.reload.vet_visits.count).to eq
|
|
1773
|
+
it 'persists the changes' do
|
|
1774
|
+
expect(pet.reload.vet_visits.count).to eq 3
|
|
1824
1775
|
end
|
|
1825
1776
|
end
|
|
1826
1777
|
|
|
1827
|
-
context
|
|
1828
|
-
|
|
1778
|
+
context 'when the parent has an empty embeds_many' do
|
|
1829
1779
|
let(:person) do
|
|
1830
1780
|
Person.new
|
|
1831
1781
|
end
|
|
@@ -1834,54 +1784,51 @@ describe Mongoid::Attributes do
|
|
|
1834
1784
|
{ services: [] }
|
|
1835
1785
|
end
|
|
1836
1786
|
|
|
1837
|
-
it
|
|
1787
|
+
it 'does not raise an error' do
|
|
1838
1788
|
person.send(method, attributes)
|
|
1839
1789
|
end
|
|
1840
1790
|
end
|
|
1841
1791
|
end
|
|
1842
1792
|
|
|
1843
|
-
context
|
|
1844
|
-
|
|
1845
|
-
context "when child is part of a has one" do
|
|
1846
|
-
|
|
1793
|
+
context 'on a child document' do
|
|
1794
|
+
context 'when child is part of a has one' do
|
|
1847
1795
|
let(:person) do
|
|
1848
|
-
Person.new(title:
|
|
1796
|
+
Person.new(title: 'Sir', age: 30)
|
|
1849
1797
|
end
|
|
1850
1798
|
|
|
1851
1799
|
let(:name) do
|
|
1852
|
-
Name.new(first_name:
|
|
1800
|
+
Name.new(first_name: 'Test', last_name: 'User')
|
|
1853
1801
|
end
|
|
1854
1802
|
|
|
1855
1803
|
before do
|
|
1856
1804
|
person.name = name
|
|
1857
|
-
name.send(method, first_name:
|
|
1805
|
+
name.send(method, first_name: 'Test2', last_name: 'User2')
|
|
1858
1806
|
end
|
|
1859
1807
|
|
|
1860
|
-
it
|
|
1808
|
+
it 'sets the child attributes on the parent' do
|
|
1861
1809
|
expect(name.attributes).to eq(
|
|
1862
|
-
{
|
|
1810
|
+
{ '_id' => 'Test-User', 'first_name' => 'Test2', 'last_name' => 'User2' }
|
|
1863
1811
|
)
|
|
1864
1812
|
end
|
|
1865
1813
|
end
|
|
1866
1814
|
|
|
1867
|
-
context
|
|
1868
|
-
|
|
1815
|
+
context 'when child is part of a has many' do
|
|
1869
1816
|
let(:person) do
|
|
1870
|
-
Person.new(title:
|
|
1817
|
+
Person.new(title: 'Sir')
|
|
1871
1818
|
end
|
|
1872
1819
|
|
|
1873
1820
|
let(:address) do
|
|
1874
|
-
Address.new(street:
|
|
1821
|
+
Address.new(street: 'Test')
|
|
1875
1822
|
end
|
|
1876
1823
|
|
|
1877
1824
|
before do
|
|
1878
1825
|
person.addresses << address
|
|
1879
|
-
address.send(method,
|
|
1826
|
+
address.send(method, 'street' => 'Test2')
|
|
1880
1827
|
end
|
|
1881
1828
|
|
|
1882
|
-
it
|
|
1829
|
+
it 'updates the child attributes on the parent' do
|
|
1883
1830
|
expect(address.attributes).to eq(
|
|
1884
|
-
{
|
|
1831
|
+
{ '_id' => 'test', 'street' => 'Test2' }
|
|
1885
1832
|
)
|
|
1886
1833
|
end
|
|
1887
1834
|
end
|
|
@@ -1889,173 +1836,217 @@ describe Mongoid::Attributes do
|
|
|
1889
1836
|
end
|
|
1890
1837
|
end
|
|
1891
1838
|
|
|
1892
|
-
describe
|
|
1839
|
+
describe '#unalias_attribute' do
|
|
1840
|
+
let(:shirt) { Shirt.new }
|
|
1841
|
+
let(:id) { '1234' }
|
|
1893
1842
|
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
end
|
|
1843
|
+
context 'when creating object' do
|
|
1844
|
+
let(:shirt) { Shirt.new(id: id) }
|
|
1897
1845
|
|
|
1898
|
-
|
|
1846
|
+
it 'id and _id are not the same' do
|
|
1847
|
+
expect(shirt.id).to eq(id)
|
|
1848
|
+
expect(shirt._id).not_to eq(id)
|
|
1849
|
+
end
|
|
1850
|
+
end
|
|
1899
1851
|
|
|
1852
|
+
context 'update' do
|
|
1900
1853
|
before do
|
|
1901
|
-
|
|
1854
|
+
shirt.update(id: id)
|
|
1902
1855
|
end
|
|
1903
1856
|
|
|
1904
|
-
it
|
|
1905
|
-
expect(
|
|
1857
|
+
it 'updates id but not_id' do
|
|
1858
|
+
expect(shirt.id).to eq(id)
|
|
1859
|
+
expect(shirt._id).not_to eq(id)
|
|
1906
1860
|
end
|
|
1861
|
+
end
|
|
1907
1862
|
|
|
1908
|
-
|
|
1909
|
-
|
|
1863
|
+
context 'id=' do
|
|
1864
|
+
before do
|
|
1865
|
+
shirt.id = id
|
|
1910
1866
|
end
|
|
1911
1867
|
|
|
1912
|
-
it
|
|
1913
|
-
expect(
|
|
1868
|
+
it 'sets id and not _id' do
|
|
1869
|
+
expect(shirt.id).to eq(id)
|
|
1870
|
+
expect(shirt._id).not_to eq(id)
|
|
1914
1871
|
end
|
|
1872
|
+
end
|
|
1915
1873
|
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1874
|
+
context 'id?' do
|
|
1875
|
+
context 'with null id' do
|
|
1876
|
+
it 'returns false' do
|
|
1877
|
+
expect(shirt.id?).to be false
|
|
1878
|
+
expect(shirt._id?).to be true
|
|
1879
|
+
end
|
|
1880
|
+
end
|
|
1881
|
+
|
|
1882
|
+
context 'with id' do
|
|
1883
|
+
before do
|
|
1884
|
+
shirt.id = id
|
|
1885
|
+
end
|
|
1886
|
+
|
|
1887
|
+
it 'returns true' do
|
|
1888
|
+
expect(shirt.id?).to be true
|
|
1889
|
+
end
|
|
1890
|
+
end
|
|
1891
|
+
end
|
|
1892
|
+
end
|
|
1893
|
+
|
|
1894
|
+
describe '#alias_attribute' do
|
|
1895
|
+
let(:product) do
|
|
1896
|
+
Product.new
|
|
1897
|
+
end
|
|
1919
1898
|
|
|
1920
|
-
|
|
1899
|
+
context 'when checking against the alias' do
|
|
1900
|
+
before do
|
|
1901
|
+
product.cost = 500
|
|
1902
|
+
end
|
|
1903
|
+
|
|
1904
|
+
it 'adds the alias for criteria' do
|
|
1905
|
+
expect(Product.where(cost: 500).selector).to eq('price' => 500)
|
|
1906
|
+
end
|
|
1907
|
+
|
|
1908
|
+
it 'aliases the getter' do
|
|
1909
|
+
expect(product.cost).to eq(500)
|
|
1910
|
+
end
|
|
1911
|
+
|
|
1912
|
+
it 'aliases the existence check' do
|
|
1913
|
+
expect(product.cost?).to be true
|
|
1914
|
+
end
|
|
1915
|
+
|
|
1916
|
+
it 'aliases *_changed?' do
|
|
1917
|
+
expect(product.cost_changed?).to be true
|
|
1918
|
+
end
|
|
1919
|
+
|
|
1920
|
+
it 'aliases *_change' do
|
|
1921
1921
|
expect(product.cost_change).to eq([ nil, 500 ])
|
|
1922
1922
|
end
|
|
1923
1923
|
|
|
1924
|
-
it
|
|
1924
|
+
it 'aliases *_will_change!' do
|
|
1925
1925
|
expect(product).to respond_to(:cost_will_change!)
|
|
1926
1926
|
end
|
|
1927
1927
|
|
|
1928
|
-
it
|
|
1928
|
+
it 'aliases *_was' do
|
|
1929
1929
|
expect(product.cost_was).to be_nil
|
|
1930
1930
|
end
|
|
1931
1931
|
|
|
1932
|
-
it
|
|
1932
|
+
it 'aliases reset_*!' do
|
|
1933
1933
|
product.reset_cost!
|
|
1934
1934
|
expect(product.cost).to be_nil
|
|
1935
1935
|
end
|
|
1936
1936
|
|
|
1937
|
-
it
|
|
1938
|
-
product.cost =
|
|
1939
|
-
expect(product.cost_before_type_cast).to eq(
|
|
1937
|
+
it 'aliases *_before_type_cast' do
|
|
1938
|
+
product.cost = '42'
|
|
1939
|
+
expect(product.cost_before_type_cast).to eq('42')
|
|
1940
1940
|
end
|
|
1941
1941
|
end
|
|
1942
1942
|
|
|
1943
|
-
context
|
|
1944
|
-
|
|
1943
|
+
context 'when checking against the original' do
|
|
1945
1944
|
before do
|
|
1946
1945
|
product.price = 500
|
|
1947
1946
|
end
|
|
1948
1947
|
|
|
1949
|
-
it
|
|
1948
|
+
it 'aliases the getter' do
|
|
1950
1949
|
expect(product.price).to eq(500)
|
|
1951
1950
|
end
|
|
1952
1951
|
|
|
1953
|
-
it
|
|
1952
|
+
it 'aliases the existence check' do
|
|
1954
1953
|
expect(product.price?).to be true
|
|
1955
1954
|
end
|
|
1956
1955
|
|
|
1957
|
-
it
|
|
1956
|
+
it 'aliases *_changed?' do
|
|
1958
1957
|
expect(product.price_changed?).to be true
|
|
1959
1958
|
end
|
|
1960
1959
|
|
|
1961
|
-
it
|
|
1960
|
+
it 'aliases *_change' do
|
|
1962
1961
|
expect(product.price_change).to eq([ nil, 500 ])
|
|
1963
1962
|
end
|
|
1964
1963
|
|
|
1965
|
-
it
|
|
1964
|
+
it 'aliases *_will_change!' do
|
|
1966
1965
|
expect(product).to respond_to(:price_will_change!)
|
|
1967
1966
|
end
|
|
1968
1967
|
|
|
1969
|
-
it
|
|
1968
|
+
it 'aliases *_was' do
|
|
1970
1969
|
expect(product.price_was).to be_nil
|
|
1971
1970
|
end
|
|
1972
1971
|
|
|
1973
|
-
it
|
|
1972
|
+
it 'aliases reset_*!' do
|
|
1974
1973
|
product.reset_price!
|
|
1975
1974
|
expect(product.price).to be_nil
|
|
1976
1975
|
end
|
|
1977
1976
|
end
|
|
1978
1977
|
end
|
|
1979
1978
|
|
|
1980
|
-
context
|
|
1981
|
-
|
|
1979
|
+
context 'when persisting nil attributes' do
|
|
1982
1980
|
let!(:person) do
|
|
1983
|
-
Person.create(score: nil)
|
|
1981
|
+
Person.create!(score: nil)
|
|
1984
1982
|
end
|
|
1985
1983
|
|
|
1986
|
-
it
|
|
1987
|
-
expect(person.reload.attributes).to have_key(
|
|
1984
|
+
it 'has an entry in the attributes' do
|
|
1985
|
+
expect(person.reload.attributes).to have_key('score')
|
|
1988
1986
|
end
|
|
1989
1987
|
end
|
|
1990
1988
|
|
|
1991
|
-
context
|
|
1992
|
-
|
|
1989
|
+
context 'with a default last_drink_taken_at' do
|
|
1993
1990
|
let(:person) do
|
|
1994
1991
|
Person.new
|
|
1995
1992
|
end
|
|
1996
1993
|
|
|
1997
|
-
it
|
|
1998
|
-
expect { person.save }.
|
|
1999
|
-
expect(person.last_drink_taken_at).to eq(1.day.ago.in_time_zone(
|
|
1994
|
+
it 'saves the default' do
|
|
1995
|
+
expect { person.save! }.not_to raise_error
|
|
1996
|
+
expect(person.last_drink_taken_at).to eq(1.day.ago.in_time_zone('Alaska').to_date)
|
|
2000
1997
|
end
|
|
2001
1998
|
end
|
|
2002
1999
|
|
|
2003
|
-
context
|
|
2004
|
-
|
|
2005
|
-
context "when no value exists in the database" do
|
|
2006
|
-
|
|
2000
|
+
context 'when default values are defined' do
|
|
2001
|
+
context 'when no value exists in the database' do
|
|
2007
2002
|
let(:person) do
|
|
2008
|
-
Person.create
|
|
2003
|
+
Person.create!
|
|
2009
2004
|
end
|
|
2010
2005
|
|
|
2011
|
-
it
|
|
2012
|
-
expect(person.last_drink_taken_at).to eq(1.day.ago.in_time_zone(
|
|
2006
|
+
it 'applies the default value' do
|
|
2007
|
+
expect(person.last_drink_taken_at).to eq(1.day.ago.in_time_zone('Alaska').to_date)
|
|
2013
2008
|
end
|
|
2014
2009
|
end
|
|
2015
2010
|
|
|
2016
|
-
context
|
|
2017
|
-
|
|
2018
|
-
context "when the value is not nil" do
|
|
2019
|
-
|
|
2011
|
+
context 'when a value exists in the database' do
|
|
2012
|
+
context 'when the value is not nil' do
|
|
2020
2013
|
let!(:person) do
|
|
2021
|
-
Person.create(age: 50)
|
|
2014
|
+
Person.create!(age: 50)
|
|
2022
2015
|
end
|
|
2023
2016
|
|
|
2024
2017
|
let(:from_db) do
|
|
2025
2018
|
Person.find(person.id)
|
|
2026
2019
|
end
|
|
2027
2020
|
|
|
2028
|
-
it
|
|
2021
|
+
it 'does not set the default' do
|
|
2029
2022
|
expect(from_db.age).to eq(50)
|
|
2030
2023
|
end
|
|
2031
2024
|
end
|
|
2032
2025
|
|
|
2033
|
-
context
|
|
2034
|
-
|
|
2026
|
+
context 'when the value is explicitly nil' do
|
|
2035
2027
|
let!(:person) do
|
|
2036
|
-
Person.create(age: nil)
|
|
2028
|
+
Person.create!(age: nil)
|
|
2037
2029
|
end
|
|
2038
2030
|
|
|
2039
2031
|
let(:from_db) do
|
|
2040
2032
|
Person.find(person.id)
|
|
2041
2033
|
end
|
|
2042
2034
|
|
|
2043
|
-
it
|
|
2035
|
+
it 'does not set the default' do
|
|
2044
2036
|
expect(from_db.age).to be_nil
|
|
2045
2037
|
end
|
|
2046
2038
|
end
|
|
2047
2039
|
|
|
2048
|
-
context
|
|
2049
|
-
|
|
2040
|
+
context 'when the default is a proc' do
|
|
2050
2041
|
let!(:account) do
|
|
2051
|
-
Account.create(name:
|
|
2042
|
+
Account.create!(name: 'savings', balance: 100)
|
|
2052
2043
|
end
|
|
2053
2044
|
|
|
2054
2045
|
let(:from_db) do
|
|
2055
2046
|
Account.find(account.id)
|
|
2056
2047
|
end
|
|
2057
2048
|
|
|
2058
|
-
it
|
|
2049
|
+
it 'applies the defaults after all attributes are set' do
|
|
2059
2050
|
expect(from_db).to be_balanced
|
|
2060
2051
|
end
|
|
2061
2052
|
end
|
|
@@ -2063,10 +2054,9 @@ describe Mongoid::Attributes do
|
|
|
2063
2054
|
end
|
|
2064
2055
|
|
|
2065
2056
|
context 'when calling the attribute check method' do
|
|
2066
|
-
|
|
2067
2057
|
context 'when the attribute is blank' do
|
|
2068
2058
|
let(:person) do
|
|
2069
|
-
Person.create(title: '')
|
|
2059
|
+
Person.create!(title: '')
|
|
2070
2060
|
end
|
|
2071
2061
|
|
|
2072
2062
|
it 'returns false' do
|
|
@@ -2076,18 +2066,16 @@ describe Mongoid::Attributes do
|
|
|
2076
2066
|
|
|
2077
2067
|
context 'when the attribute is localized' do
|
|
2078
2068
|
let(:person) do
|
|
2079
|
-
Person.create
|
|
2069
|
+
Person.create!
|
|
2080
2070
|
end
|
|
2081
2071
|
|
|
2082
2072
|
context 'after initialization when the field is nil' do
|
|
2083
|
-
|
|
2084
2073
|
it 'returns false' do
|
|
2085
2074
|
expect(person.desc?).to be(false)
|
|
2086
2075
|
end
|
|
2087
2076
|
end
|
|
2088
2077
|
|
|
2089
2078
|
context 'when setting the field to nil' do
|
|
2090
|
-
|
|
2091
2079
|
it 'applies the localization when checking the attribute' do
|
|
2092
2080
|
person.desc = nil
|
|
2093
2081
|
expect(person.desc?).to be(false)
|
|
@@ -2095,7 +2083,6 @@ describe Mongoid::Attributes do
|
|
|
2095
2083
|
end
|
|
2096
2084
|
|
|
2097
2085
|
context 'when the field is a boolean' do
|
|
2098
|
-
|
|
2099
2086
|
before do
|
|
2100
2087
|
person.desc = false
|
|
2101
2088
|
end
|
|
@@ -2108,7 +2095,7 @@ describe Mongoid::Attributes do
|
|
|
2108
2095
|
|
|
2109
2096
|
context 'when the attribute is not localized' do
|
|
2110
2097
|
let(:person) do
|
|
2111
|
-
Person.create(username: 'localized')
|
|
2098
|
+
Person.create!(username: 'localized')
|
|
2112
2099
|
end
|
|
2113
2100
|
|
|
2114
2101
|
before do
|
|
@@ -2120,4 +2107,519 @@ describe Mongoid::Attributes do
|
|
|
2120
2107
|
end
|
|
2121
2108
|
end
|
|
2122
2109
|
end
|
|
2110
|
+
|
|
2111
|
+
context 'when an attribute is removed then set' do
|
|
2112
|
+
let(:cat) { Cat.create!(name: 'Neil') }
|
|
2113
|
+
|
|
2114
|
+
before do
|
|
2115
|
+
cat.remove_attribute('name')
|
|
2116
|
+
cat.name = 'Nissim'
|
|
2117
|
+
cat.save!
|
|
2118
|
+
cat.reload
|
|
2119
|
+
end
|
|
2120
|
+
|
|
2121
|
+
it 'correctly sets the attribute' do
|
|
2122
|
+
expect(cat.name).to eq('Nissim')
|
|
2123
|
+
end
|
|
2124
|
+
end
|
|
2125
|
+
|
|
2126
|
+
describe 'attributes after setting an association without reloading' do
|
|
2127
|
+
context 'on embeds_many' do
|
|
2128
|
+
context 'when not setting anything' do
|
|
2129
|
+
let(:doc) { NestedBook.create! }
|
|
2130
|
+
|
|
2131
|
+
it "doesn't add attributes" do
|
|
2132
|
+
expect(doc.attributes).not_to have_key('pages')
|
|
2133
|
+
end
|
|
2134
|
+
|
|
2135
|
+
it 'has the same attributes after reloading' do
|
|
2136
|
+
expect(doc.attributes).to eq(doc.reload.attributes)
|
|
2137
|
+
end
|
|
2138
|
+
end
|
|
2139
|
+
|
|
2140
|
+
context 'when using nested attributes' do
|
|
2141
|
+
let(:doc) { NestedBook.create! }
|
|
2142
|
+
|
|
2143
|
+
before do
|
|
2144
|
+
doc.update_attributes({ pages_attributes: [ {} ] })
|
|
2145
|
+
end
|
|
2146
|
+
|
|
2147
|
+
it 'updates the attributes' do
|
|
2148
|
+
expect(doc.attributes['pages']).to eq([ { '_id' => doc.pages.first.id } ])
|
|
2149
|
+
end
|
|
2150
|
+
|
|
2151
|
+
it 'has the same attributes after reloading' do
|
|
2152
|
+
expect(doc.attributes).to eq(doc.reload.attributes)
|
|
2153
|
+
end
|
|
2154
|
+
end
|
|
2155
|
+
|
|
2156
|
+
context 'when doing assignments' do
|
|
2157
|
+
let(:doc) { NestedBook.create! }
|
|
2158
|
+
|
|
2159
|
+
before do
|
|
2160
|
+
doc.pages = [ NestedPage.new ]
|
|
2161
|
+
end
|
|
2162
|
+
|
|
2163
|
+
it 'updates the attributes' do
|
|
2164
|
+
expect(doc.attributes['pages']).to eq([ { '_id' => doc.pages.first.id } ])
|
|
2165
|
+
end
|
|
2166
|
+
|
|
2167
|
+
it 'has the same attributes after reloading' do
|
|
2168
|
+
expect(doc.attributes).to eq(doc.reload.attributes)
|
|
2169
|
+
end
|
|
2170
|
+
end
|
|
2171
|
+
|
|
2172
|
+
context 'when replacing assignments' do
|
|
2173
|
+
let(:doc) { NestedBook.create! }
|
|
2174
|
+
|
|
2175
|
+
before do
|
|
2176
|
+
doc.pages = [ NestedPage.new(number: 1) ]
|
|
2177
|
+
doc.pages = [ NestedPage.new(number: 2) ]
|
|
2178
|
+
end
|
|
2179
|
+
|
|
2180
|
+
it 'updates the attributes' do
|
|
2181
|
+
expect(doc.attributes['pages']).to eq([ { '_id' => doc.pages.first.id, 'number' => 2 } ])
|
|
2182
|
+
end
|
|
2183
|
+
|
|
2184
|
+
it 'has the same attributes after reloading' do
|
|
2185
|
+
expect(doc.attributes).to eq(doc.reload.attributes)
|
|
2186
|
+
end
|
|
2187
|
+
end
|
|
2188
|
+
|
|
2189
|
+
context 'when setting to nil' do
|
|
2190
|
+
let(:doc) { NestedBook.create! }
|
|
2191
|
+
|
|
2192
|
+
before do
|
|
2193
|
+
doc.pages = [ NestedPage.new(number: 1) ]
|
|
2194
|
+
doc.pages = nil
|
|
2195
|
+
end
|
|
2196
|
+
|
|
2197
|
+
it 'updates the attributes' do
|
|
2198
|
+
expect(doc.attributes).not_to have_key('pages')
|
|
2199
|
+
end
|
|
2200
|
+
|
|
2201
|
+
it 'has the same attributes after reloading' do
|
|
2202
|
+
expect({ 'pages' => [] }.merge(doc.attributes)).to eq(doc.reload.attributes)
|
|
2203
|
+
end
|
|
2204
|
+
end
|
|
2205
|
+
|
|
2206
|
+
context 'when setting to nil and back' do
|
|
2207
|
+
let(:doc) { NestedBook.create! }
|
|
2208
|
+
let(:page) { NestedPage.new }
|
|
2209
|
+
|
|
2210
|
+
before do
|
|
2211
|
+
doc.pages = [ page ]
|
|
2212
|
+
doc.pages = nil
|
|
2213
|
+
doc.pages = [ page ]
|
|
2214
|
+
end
|
|
2215
|
+
|
|
2216
|
+
it 'updates the attributes' do
|
|
2217
|
+
expect(doc.attributes['pages']).to eq([ { '_id' => doc.pages.first.id } ])
|
|
2218
|
+
end
|
|
2219
|
+
|
|
2220
|
+
it 'has the same attributes after reloading' do
|
|
2221
|
+
expect(doc.attributes).to eq(doc.reload.attributes)
|
|
2222
|
+
end
|
|
2223
|
+
end
|
|
2224
|
+
|
|
2225
|
+
context 'when pushing' do
|
|
2226
|
+
let(:doc) { NestedBook.create! }
|
|
2227
|
+
|
|
2228
|
+
before do
|
|
2229
|
+
doc.pages << NestedPage.new
|
|
2230
|
+
end
|
|
2231
|
+
|
|
2232
|
+
it 'updates the attributes' do
|
|
2233
|
+
expect(doc.attributes['pages']).to eq([ { '_id' => doc.pages.first.id } ])
|
|
2234
|
+
end
|
|
2235
|
+
|
|
2236
|
+
it 'has the same attributes after reloading' do
|
|
2237
|
+
expect(doc.attributes).to eq(doc.reload.attributes)
|
|
2238
|
+
end
|
|
2239
|
+
end
|
|
2240
|
+
|
|
2241
|
+
%i[shift pop].each do |meth|
|
|
2242
|
+
context "when performing #{meth}" do
|
|
2243
|
+
let(:doc) { NestedBook.create! }
|
|
2244
|
+
|
|
2245
|
+
before do
|
|
2246
|
+
doc.pages << NestedPage.new
|
|
2247
|
+
doc.pages << NestedPage.new
|
|
2248
|
+
doc.pages << NestedPage.new
|
|
2249
|
+
|
|
2250
|
+
doc.pages.send(meth, 2)
|
|
2251
|
+
end
|
|
2252
|
+
|
|
2253
|
+
it 'updates the attributes' do
|
|
2254
|
+
expect(doc.attributes['pages']).to eq([ { '_id' => doc.pages.first.id } ])
|
|
2255
|
+
end
|
|
2256
|
+
|
|
2257
|
+
it 'has the same attributes after reloading' do
|
|
2258
|
+
expect(doc.attributes).to eq(doc.reload.attributes)
|
|
2259
|
+
end
|
|
2260
|
+
end
|
|
2261
|
+
end
|
|
2262
|
+
|
|
2263
|
+
context 'when concatting' do
|
|
2264
|
+
let(:doc) { NestedBook.create! }
|
|
2265
|
+
|
|
2266
|
+
before do
|
|
2267
|
+
doc.pages << NestedPage.new
|
|
2268
|
+
doc.pages.concat([ NestedPage.new, NestedPage.new ])
|
|
2269
|
+
end
|
|
2270
|
+
|
|
2271
|
+
it 'updates the attributes' do
|
|
2272
|
+
expect(doc.attributes['pages'].count).to eq 3
|
|
2273
|
+
end
|
|
2274
|
+
|
|
2275
|
+
it 'has the same attributes after reloading' do
|
|
2276
|
+
expect(doc.attributes).to eq(doc.reload.attributes)
|
|
2277
|
+
end
|
|
2278
|
+
end
|
|
2279
|
+
|
|
2280
|
+
%i[build create].each do |meth|
|
|
2281
|
+
context "when preforming #{meth}" do
|
|
2282
|
+
let(:doc) { NestedBook.create! }
|
|
2283
|
+
|
|
2284
|
+
before do
|
|
2285
|
+
doc.pages.send(meth)
|
|
2286
|
+
end
|
|
2287
|
+
|
|
2288
|
+
it 'updates the attributes' do
|
|
2289
|
+
expect(doc.attributes['pages']).to eq([ { '_id' => doc.pages.first.id } ])
|
|
2290
|
+
end
|
|
2291
|
+
|
|
2292
|
+
it 'has the same attributes after reloading' do
|
|
2293
|
+
doc.pages.first.save
|
|
2294
|
+
expect(doc.attributes).to eq(doc.reload.attributes)
|
|
2295
|
+
end
|
|
2296
|
+
end
|
|
2297
|
+
end
|
|
2298
|
+
|
|
2299
|
+
context 'when clearing' do
|
|
2300
|
+
let(:doc) { NestedBook.create! }
|
|
2301
|
+
|
|
2302
|
+
before do
|
|
2303
|
+
doc.pages << NestedPage.new
|
|
2304
|
+
doc.pages << NestedPage.new
|
|
2305
|
+
doc.pages << NestedPage.new
|
|
2306
|
+
|
|
2307
|
+
doc.pages.clear
|
|
2308
|
+
end
|
|
2309
|
+
|
|
2310
|
+
it 'updates the attributes' do
|
|
2311
|
+
expect(doc.attributes).not_to have_key('pages')
|
|
2312
|
+
end
|
|
2313
|
+
|
|
2314
|
+
it 'has the same attributes after reloading' do
|
|
2315
|
+
expect(doc.attributes).to eq(doc.reload.attributes)
|
|
2316
|
+
end
|
|
2317
|
+
end
|
|
2318
|
+
|
|
2319
|
+
%i[delete_all destroy_all remove_all].each do |meth|
|
|
2320
|
+
context "when performing: #{meth}" do
|
|
2321
|
+
let(:doc) { NestedBook.create! }
|
|
2322
|
+
|
|
2323
|
+
before do
|
|
2324
|
+
doc.pages << NestedPage.new
|
|
2325
|
+
doc.pages << NestedPage.new
|
|
2326
|
+
doc.pages << NestedPage.new
|
|
2327
|
+
|
|
2328
|
+
doc.pages.send(meth)
|
|
2329
|
+
end
|
|
2330
|
+
|
|
2331
|
+
it 'updates the attributes' do
|
|
2332
|
+
expect(doc.attributes).not_to have_key('pages')
|
|
2333
|
+
end
|
|
2334
|
+
|
|
2335
|
+
it 'has the same attributes after reloading' do
|
|
2336
|
+
expect({ 'pages' => [] }.merge(doc.attributes)).to eq(doc.reload.attributes)
|
|
2337
|
+
end
|
|
2338
|
+
end
|
|
2339
|
+
end
|
|
2340
|
+
|
|
2341
|
+
context 'when deleting' do
|
|
2342
|
+
let(:doc) { NestedBook.create! }
|
|
2343
|
+
let(:page) { NestedPage.new }
|
|
2344
|
+
|
|
2345
|
+
before do
|
|
2346
|
+
doc.pages << page
|
|
2347
|
+
doc.pages << NestedPage.new
|
|
2348
|
+
doc.pages << NestedPage.new
|
|
2349
|
+
|
|
2350
|
+
doc.pages.delete(page)
|
|
2351
|
+
end
|
|
2352
|
+
|
|
2353
|
+
it 'updates the attributes' do
|
|
2354
|
+
expect(doc.attributes['pages'].count).to eq 2
|
|
2355
|
+
end
|
|
2356
|
+
|
|
2357
|
+
it 'has the same attributes after reloading' do
|
|
2358
|
+
expect(doc.attributes).to eq(doc.reload.attributes)
|
|
2359
|
+
end
|
|
2360
|
+
end
|
|
2361
|
+
|
|
2362
|
+
context 'when doing _remove' do
|
|
2363
|
+
let(:doc) { NestedBook.create! }
|
|
2364
|
+
let(:page) { NestedPage.new }
|
|
2365
|
+
|
|
2366
|
+
before do
|
|
2367
|
+
doc.pages << page
|
|
2368
|
+
doc.pages << NestedPage.new
|
|
2369
|
+
doc.pages << NestedPage.new
|
|
2370
|
+
|
|
2371
|
+
doc.pages._remove(page)
|
|
2372
|
+
end
|
|
2373
|
+
|
|
2374
|
+
it 'updates the attributes' do
|
|
2375
|
+
expect(doc.attributes['pages'].count).to eq 2
|
|
2376
|
+
end
|
|
2377
|
+
end
|
|
2378
|
+
|
|
2379
|
+
context 'when assigning an array of hashes' do
|
|
2380
|
+
let(:doc) { NestedBook.create! }
|
|
2381
|
+
|
|
2382
|
+
before do
|
|
2383
|
+
doc.pages = [ {} ]
|
|
2384
|
+
end
|
|
2385
|
+
|
|
2386
|
+
it 'updates the attributes' do
|
|
2387
|
+
expect(doc.attributes['pages']).to eq([ { '_id' => doc.pages.first.id } ])
|
|
2388
|
+
end
|
|
2389
|
+
|
|
2390
|
+
it 'has the same attributes after reloading' do
|
|
2391
|
+
expect(doc.attributes).to eq(doc.reload.attributes)
|
|
2392
|
+
end
|
|
2393
|
+
end
|
|
2394
|
+
|
|
2395
|
+
context 'when assigning twice' do
|
|
2396
|
+
let(:doc) { NestedBook.create! }
|
|
2397
|
+
|
|
2398
|
+
before do
|
|
2399
|
+
doc.pages = [ { number: 1 } ]
|
|
2400
|
+
doc.pages = [ {} ]
|
|
2401
|
+
end
|
|
2402
|
+
|
|
2403
|
+
it 'updates the attributes' do
|
|
2404
|
+
expect(doc.attributes['pages']).to eq([ { '_id' => doc.pages.first.id } ])
|
|
2405
|
+
end
|
|
2406
|
+
|
|
2407
|
+
it 'has the same attributes after reloading' do
|
|
2408
|
+
expect(doc.attributes).to eq(doc.reload.attributes)
|
|
2409
|
+
end
|
|
2410
|
+
end
|
|
2411
|
+
end
|
|
2412
|
+
|
|
2413
|
+
context 'on embeds_one' do
|
|
2414
|
+
let(:attrs) { { 'title' => 'Title' } }
|
|
2415
|
+
|
|
2416
|
+
context 'when using nested attributes' do
|
|
2417
|
+
let(:doc) { NestedBook.create! }
|
|
2418
|
+
|
|
2419
|
+
before do
|
|
2420
|
+
doc.update_attributes({ cover_attributes: attrs })
|
|
2421
|
+
end
|
|
2422
|
+
|
|
2423
|
+
it 'updates the attributes' do
|
|
2424
|
+
expect(doc.attributes['cover']).to eq(attrs.merge('_id' => doc.cover.id))
|
|
2425
|
+
end
|
|
2426
|
+
|
|
2427
|
+
it 'has the same attributes after reloading' do
|
|
2428
|
+
expect(doc.attributes).to eq(doc.reload.attributes)
|
|
2429
|
+
end
|
|
2430
|
+
end
|
|
2431
|
+
|
|
2432
|
+
context 'when doing assignments' do
|
|
2433
|
+
let(:doc) { NestedBook.create! }
|
|
2434
|
+
|
|
2435
|
+
before do
|
|
2436
|
+
doc.cover = NestedCover.new(attrs)
|
|
2437
|
+
end
|
|
2438
|
+
|
|
2439
|
+
it 'updates the attributes' do
|
|
2440
|
+
expect(doc.attributes['cover']).to eq(attrs.merge('_id' => doc.cover.id))
|
|
2441
|
+
end
|
|
2442
|
+
|
|
2443
|
+
it 'has the same attributes after reloading' do
|
|
2444
|
+
expect(doc.attributes).to eq(doc.reload.attributes)
|
|
2445
|
+
end
|
|
2446
|
+
end
|
|
2447
|
+
|
|
2448
|
+
context 'when replacing assignments' do
|
|
2449
|
+
let(:doc) { NestedBook.create! }
|
|
2450
|
+
|
|
2451
|
+
before do
|
|
2452
|
+
doc.cover = NestedCover.new('title' => 'Title1')
|
|
2453
|
+
doc.cover = NestedCover.new(attrs)
|
|
2454
|
+
end
|
|
2455
|
+
|
|
2456
|
+
it 'updates the attributes' do
|
|
2457
|
+
expect(doc.attributes['cover']).to eq(attrs.merge('_id' => doc.cover.id))
|
|
2458
|
+
end
|
|
2459
|
+
|
|
2460
|
+
it 'has the same attributes after reloading' do
|
|
2461
|
+
expect(doc.attributes).to eq(doc.reload.attributes)
|
|
2462
|
+
end
|
|
2463
|
+
end
|
|
2464
|
+
|
|
2465
|
+
context 'when setting to nil' do
|
|
2466
|
+
let(:doc) { NestedBook.create! }
|
|
2467
|
+
|
|
2468
|
+
before do
|
|
2469
|
+
doc.cover = NestedCover.new(attrs)
|
|
2470
|
+
doc.cover = nil
|
|
2471
|
+
end
|
|
2472
|
+
|
|
2473
|
+
it 'updates the attributes' do
|
|
2474
|
+
expect(doc.attributes.key?('cover')).to be false
|
|
2475
|
+
end
|
|
2476
|
+
|
|
2477
|
+
it 'has the same attributes after reloading' do
|
|
2478
|
+
expect(doc.attributes).to eq(doc.reload.attributes)
|
|
2479
|
+
end
|
|
2480
|
+
end
|
|
2481
|
+
|
|
2482
|
+
context 'when setting to nil and back' do
|
|
2483
|
+
let(:doc) { NestedBook.create! }
|
|
2484
|
+
|
|
2485
|
+
before do
|
|
2486
|
+
doc.cover = NestedCover.new(attrs)
|
|
2487
|
+
doc.cover = nil
|
|
2488
|
+
doc.cover = NestedCover.new(attrs)
|
|
2489
|
+
end
|
|
2490
|
+
|
|
2491
|
+
it 'updates the attributes' do
|
|
2492
|
+
expect(doc.attributes['cover']).to eq(attrs.merge('_id' => doc.cover.id))
|
|
2493
|
+
end
|
|
2494
|
+
|
|
2495
|
+
it 'has the same attributes after reloading' do
|
|
2496
|
+
expect(doc.attributes).to eq(doc.reload.attributes)
|
|
2497
|
+
end
|
|
2498
|
+
end
|
|
2499
|
+
|
|
2500
|
+
%i[build create].each do |meth|
|
|
2501
|
+
context "when preforming #{meth}" do
|
|
2502
|
+
let(:doc) { NestedBook.create! }
|
|
2503
|
+
|
|
2504
|
+
before do
|
|
2505
|
+
doc.send("#{meth}_cover", attrs)
|
|
2506
|
+
end
|
|
2507
|
+
|
|
2508
|
+
it 'updates the attributes' do
|
|
2509
|
+
expect(doc.attributes['cover']).to eq(attrs.merge('_id' => doc.cover.id))
|
|
2510
|
+
end
|
|
2511
|
+
|
|
2512
|
+
it 'has the same attributes after reloading' do
|
|
2513
|
+
doc.cover.save
|
|
2514
|
+
expect(doc.attributes).to eq(doc.reload.attributes)
|
|
2515
|
+
end
|
|
2516
|
+
end
|
|
2517
|
+
end
|
|
2518
|
+
|
|
2519
|
+
context 'when assigning a hash' do
|
|
2520
|
+
let(:doc) { NestedBook.create! }
|
|
2521
|
+
|
|
2522
|
+
before do
|
|
2523
|
+
doc.cover = attrs
|
|
2524
|
+
end
|
|
2525
|
+
|
|
2526
|
+
it 'updates the attributes' do
|
|
2527
|
+
expect(doc.attributes['cover']).to eq(attrs.merge('_id' => doc.cover.id))
|
|
2528
|
+
end
|
|
2529
|
+
|
|
2530
|
+
it 'has the same attributes after reloading' do
|
|
2531
|
+
expect(doc.attributes).to eq(doc.reload.attributes)
|
|
2532
|
+
end
|
|
2533
|
+
end
|
|
2534
|
+
|
|
2535
|
+
context 'when assigning twice' do
|
|
2536
|
+
let(:doc) { NestedBook.create! }
|
|
2537
|
+
|
|
2538
|
+
before do
|
|
2539
|
+
doc.cover = { 'title' => '1984' }
|
|
2540
|
+
doc.cover = attrs
|
|
2541
|
+
end
|
|
2542
|
+
|
|
2543
|
+
it 'updates the attributes' do
|
|
2544
|
+
expect(doc.attributes['cover']).to eq(attrs.merge('_id' => doc.cover.id))
|
|
2545
|
+
end
|
|
2546
|
+
|
|
2547
|
+
it 'has the same attributes after reloading' do
|
|
2548
|
+
expect(doc.attributes).to eq(doc.reload.attributes)
|
|
2549
|
+
end
|
|
2550
|
+
end
|
|
2551
|
+
end
|
|
2552
|
+
end
|
|
2553
|
+
|
|
2554
|
+
context 'when modifying a hash referenced with the [] notation' do
|
|
2555
|
+
let(:church) { Church.create!(location: { x: 1 }) }
|
|
2556
|
+
|
|
2557
|
+
before do
|
|
2558
|
+
church[:location].merge!(y: 2)
|
|
2559
|
+
church.save!
|
|
2560
|
+
church.reload
|
|
2561
|
+
end
|
|
2562
|
+
|
|
2563
|
+
it 'persists the updated hash' do
|
|
2564
|
+
church.location.should eq({ 'x' => 1, 'y' => 2 })
|
|
2565
|
+
end
|
|
2566
|
+
end
|
|
2567
|
+
|
|
2568
|
+
context 'when accessing an embedded document with the attribute accessor' do
|
|
2569
|
+
let(:band) { Band.create! }
|
|
2570
|
+
|
|
2571
|
+
before do
|
|
2572
|
+
Band.where(id: band.id).update_all({
|
|
2573
|
+
:$push => { records: { _id: BSON::ObjectId.new } }
|
|
2574
|
+
})
|
|
2575
|
+
end
|
|
2576
|
+
|
|
2577
|
+
it 'does not throw a conflicting update error' do
|
|
2578
|
+
b1 = Band.find(band.id)
|
|
2579
|
+
b1[:records].is_a?(Array).should be true
|
|
2580
|
+
expect { b1.save! }.not_to raise_error
|
|
2581
|
+
end
|
|
2582
|
+
end
|
|
2583
|
+
|
|
2584
|
+
context 'when modifying a set referenced with the [] notation' do
|
|
2585
|
+
let(:catalog) { Catalog.create!(set_field: [ 1 ].to_set) }
|
|
2586
|
+
|
|
2587
|
+
before do
|
|
2588
|
+
catalog[:set_field] << 2
|
|
2589
|
+
catalog.save!
|
|
2590
|
+
catalog.reload
|
|
2591
|
+
end
|
|
2592
|
+
|
|
2593
|
+
it 'persists the updated hash' do
|
|
2594
|
+
pending 'MONGOID-2951'
|
|
2595
|
+
catalog.set_field.should eq Set.new([ 1, 2 ])
|
|
2596
|
+
end
|
|
2597
|
+
end
|
|
2598
|
+
|
|
2599
|
+
context 'when an embedded field has a capitalized store_as name' do
|
|
2600
|
+
let(:person) { Person.new(Purse: { brand: 'Gucci' }) }
|
|
2601
|
+
|
|
2602
|
+
it 'sets the value' do
|
|
2603
|
+
expect(person.purse.brand).to eq('Gucci')
|
|
2604
|
+
end
|
|
2605
|
+
|
|
2606
|
+
it 'saves successfully' do
|
|
2607
|
+
expect(person.save!).to eq(true)
|
|
2608
|
+
end
|
|
2609
|
+
|
|
2610
|
+
context 'when persisted' do
|
|
2611
|
+
before do
|
|
2612
|
+
person.save!
|
|
2613
|
+
person.reload
|
|
2614
|
+
end
|
|
2615
|
+
|
|
2616
|
+
it 'persists the value' do
|
|
2617
|
+
expect(person.reload.purse.brand).to eq('Gucci')
|
|
2618
|
+
end
|
|
2619
|
+
|
|
2620
|
+
it 'uses the correct key in the database' do
|
|
2621
|
+
expect(person.collection.find(_id: person.id).first['Purse']['_id']).to eq(person.purse.id)
|
|
2622
|
+
end
|
|
2623
|
+
end
|
|
2624
|
+
end
|
|
2123
2625
|
end
|