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