sepastian-mongoid-rails4 4.0.0.alpha
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +3290 -0
- data/LICENSE +20 -0
- data/README.md +62 -0
- data/Rakefile +35 -0
- data/lib/config/locales/en.yml +452 -0
- data/lib/mongoid/atomic/modifiers.rb +317 -0
- data/lib/mongoid/atomic/paths/embedded/many.rb +44 -0
- data/lib/mongoid/atomic/paths/embedded/one.rb +43 -0
- data/lib/mongoid/atomic/paths/embedded.rb +28 -0
- data/lib/mongoid/atomic/paths/root.rb +39 -0
- data/lib/mongoid/atomic/paths.rb +3 -0
- data/lib/mongoid/atomic.rb +384 -0
- data/lib/mongoid/attributes/dynamic.rb +154 -0
- data/lib/mongoid/attributes/nested.rb +82 -0
- data/lib/mongoid/attributes/processing.rb +147 -0
- data/lib/mongoid/attributes/readonly.rb +56 -0
- data/lib/mongoid/attributes.rb +305 -0
- data/lib/mongoid/changeable.rb +379 -0
- data/lib/mongoid/composable.rb +104 -0
- data/lib/mongoid/config/environment.rb +44 -0
- data/lib/mongoid/config/options.rb +74 -0
- data/lib/mongoid/config/validators/option.rb +25 -0
- data/lib/mongoid/config/validators/session.rb +140 -0
- data/lib/mongoid/config/validators.rb +3 -0
- data/lib/mongoid/config.rb +263 -0
- data/lib/mongoid/contextual/aggregable/memory.rb +109 -0
- data/lib/mongoid/contextual/aggregable/mongo.rb +145 -0
- data/lib/mongoid/contextual/atomic.rb +180 -0
- data/lib/mongoid/contextual/command.rb +61 -0
- data/lib/mongoid/contextual/eager.rb +158 -0
- data/lib/mongoid/contextual/find_and_modify.rb +69 -0
- data/lib/mongoid/contextual/geo_near.rb +238 -0
- data/lib/mongoid/contextual/map_reduce.rb +324 -0
- data/lib/mongoid/contextual/memory.rb +440 -0
- data/lib/mongoid/contextual/mongo.rb +676 -0
- data/lib/mongoid/contextual/queryable.rb +25 -0
- data/lib/mongoid/contextual/text_search.rb +180 -0
- data/lib/mongoid/contextual.rb +54 -0
- data/lib/mongoid/copyable.rb +67 -0
- data/lib/mongoid/criteria/findable.rb +179 -0
- data/lib/mongoid/criteria/inspectable.rb +25 -0
- data/lib/mongoid/criteria/marshalable.rb +50 -0
- data/lib/mongoid/criteria/modifiable.rb +189 -0
- data/lib/mongoid/criteria/scopable.rb +158 -0
- data/lib/mongoid/criteria.rb +562 -0
- data/lib/mongoid/document.rb +361 -0
- data/lib/mongoid/equality.rb +66 -0
- data/lib/mongoid/errors/ambiguous_relationship.rb +51 -0
- data/lib/mongoid/errors/callback.rb +25 -0
- data/lib/mongoid/errors/delete_restriction.rb +29 -0
- data/lib/mongoid/errors/document_not_found.rb +111 -0
- data/lib/mongoid/errors/eager_load.rb +22 -0
- data/lib/mongoid/errors/invalid_collection.rb +18 -0
- data/lib/mongoid/errors/invalid_config_option.rb +27 -0
- data/lib/mongoid/errors/invalid_field.rb +64 -0
- data/lib/mongoid/errors/invalid_field_option.rb +35 -0
- data/lib/mongoid/errors/invalid_find.rb +19 -0
- data/lib/mongoid/errors/invalid_includes.rb +32 -0
- data/lib/mongoid/errors/invalid_index.rb +28 -0
- data/lib/mongoid/errors/invalid_options.rb +28 -0
- data/lib/mongoid/errors/invalid_path.rb +21 -0
- data/lib/mongoid/errors/invalid_scope.rb +24 -0
- data/lib/mongoid/errors/invalid_set_polymorphic_relation.rb +38 -0
- data/lib/mongoid/errors/invalid_storage_options.rb +27 -0
- data/lib/mongoid/errors/invalid_time.rb +22 -0
- data/lib/mongoid/errors/invalid_value.rb +16 -0
- data/lib/mongoid/errors/inverse_not_found.rb +29 -0
- data/lib/mongoid/errors/mixed_relations.rb +32 -0
- data/lib/mongoid/errors/mixed_session_configuration.rb +28 -0
- data/lib/mongoid/errors/mongoid_error.rb +92 -0
- data/lib/mongoid/errors/nested_attributes_metadata_not_found.rb +28 -0
- data/lib/mongoid/errors/no_default_session.rb +23 -0
- data/lib/mongoid/errors/no_environment.rb +19 -0
- data/lib/mongoid/errors/no_map_reduce_output.rb +24 -0
- data/lib/mongoid/errors/no_metadata.rb +21 -0
- data/lib/mongoid/errors/no_parent.rb +24 -0
- data/lib/mongoid/errors/no_session_config.rb +22 -0
- data/lib/mongoid/errors/no_session_database.rb +27 -0
- data/lib/mongoid/errors/no_session_hosts.rb +27 -0
- data/lib/mongoid/errors/no_sessions_config.rb +20 -0
- data/lib/mongoid/errors/readonly_attribute.rb +25 -0
- data/lib/mongoid/errors/scope_overwrite.rb +21 -0
- data/lib/mongoid/errors/too_many_nested_attribute_records.rb +20 -0
- data/lib/mongoid/errors/unknown_attribute.rb +25 -0
- data/lib/mongoid/errors/unsaved_document.rb +19 -0
- data/lib/mongoid/errors/unsupported_javascript.rb +27 -0
- data/lib/mongoid/errors/validations.rb +29 -0
- data/lib/mongoid/errors.rb +41 -0
- data/lib/mongoid/evolvable.rb +19 -0
- data/lib/mongoid/extensions/array.rb +180 -0
- data/lib/mongoid/extensions/big_decimal.rb +69 -0
- data/lib/mongoid/extensions/boolean.rb +21 -0
- data/lib/mongoid/extensions/date.rb +77 -0
- data/lib/mongoid/extensions/date_time.rb +73 -0
- data/lib/mongoid/extensions/false_class.rb +38 -0
- data/lib/mongoid/extensions/float.rb +56 -0
- data/lib/mongoid/extensions/hash.rb +217 -0
- data/lib/mongoid/extensions/integer.rb +67 -0
- data/lib/mongoid/extensions/module.rb +28 -0
- data/lib/mongoid/extensions/nil_class.rb +33 -0
- data/lib/mongoid/extensions/object.rb +274 -0
- data/lib/mongoid/extensions/object_id.rb +54 -0
- data/lib/mongoid/extensions/range.rb +85 -0
- data/lib/mongoid/extensions/regexp.rb +27 -0
- data/lib/mongoid/extensions/set.rb +55 -0
- data/lib/mongoid/extensions/string.rb +199 -0
- data/lib/mongoid/extensions/symbol.rb +54 -0
- data/lib/mongoid/extensions/time.rb +88 -0
- data/lib/mongoid/extensions/time_with_zone.rb +56 -0
- data/lib/mongoid/extensions/true_class.rb +38 -0
- data/lib/mongoid/extensions.rb +35 -0
- data/lib/mongoid/factory.rb +46 -0
- data/lib/mongoid/fields/foreign_key.rb +174 -0
- data/lib/mongoid/fields/localized.rb +73 -0
- data/lib/mongoid/fields/standard.rb +273 -0
- data/lib/mongoid/fields/validators/macro.rb +92 -0
- data/lib/mongoid/fields/validators.rb +2 -0
- data/lib/mongoid/fields.rb +542 -0
- data/lib/mongoid/findable.rb +133 -0
- data/lib/mongoid/identity_map.rb +163 -0
- data/lib/mongoid/indexable/specification.rb +115 -0
- data/lib/mongoid/indexable/validators/options.rb +103 -0
- data/lib/mongoid/indexable.rb +147 -0
- data/lib/mongoid/inspectable.rb +59 -0
- data/lib/mongoid/interceptable.rb +265 -0
- data/lib/mongoid/log_subscriber.rb +16 -0
- data/lib/mongoid/loggable.rb +69 -0
- data/lib/mongoid/matchable/all.rb +27 -0
- data/lib/mongoid/matchable/and.rb +30 -0
- data/lib/mongoid/matchable/default.rb +72 -0
- data/lib/mongoid/matchable/exists.rb +23 -0
- data/lib/mongoid/matchable/gt.rb +21 -0
- data/lib/mongoid/matchable/gte.rb +21 -0
- data/lib/mongoid/matchable/in.rb +24 -0
- data/lib/mongoid/matchable/lt.rb +21 -0
- data/lib/mongoid/matchable/lte.rb +21 -0
- data/lib/mongoid/matchable/ne.rb +21 -0
- data/lib/mongoid/matchable/nin.rb +21 -0
- data/lib/mongoid/matchable/or.rb +33 -0
- data/lib/mongoid/matchable/size.rb +21 -0
- data/lib/mongoid/matchable.rb +152 -0
- data/lib/mongoid/persistable/creatable.rb +189 -0
- data/lib/mongoid/persistable/deletable.rb +149 -0
- data/lib/mongoid/persistable/destroyable.rb +55 -0
- data/lib/mongoid/persistable/incrementable.rb +36 -0
- data/lib/mongoid/persistable/logical.rb +38 -0
- data/lib/mongoid/persistable/poppable.rb +39 -0
- data/lib/mongoid/persistable/pullable.rb +55 -0
- data/lib/mongoid/persistable/pushable.rb +62 -0
- data/lib/mongoid/persistable/renamable.rb +35 -0
- data/lib/mongoid/persistable/savable.rb +52 -0
- data/lib/mongoid/persistable/settable.rb +33 -0
- data/lib/mongoid/persistable/unsettable.rb +36 -0
- data/lib/mongoid/persistable/updatable.rb +151 -0
- data/lib/mongoid/persistable/upsertable.rb +55 -0
- data/lib/mongoid/persistable.rb +212 -0
- data/lib/mongoid/positional.rb +71 -0
- data/lib/mongoid/railtie.rb +156 -0
- data/lib/mongoid/railties/database.rake +95 -0
- data/lib/mongoid/railties/document.rb +12 -0
- data/lib/mongoid/relations/accessors.rb +280 -0
- data/lib/mongoid/relations/auto_save.rb +106 -0
- data/lib/mongoid/relations/binding.rb +218 -0
- data/lib/mongoid/relations/bindings/embedded/in.rb +63 -0
- data/lib/mongoid/relations/bindings/embedded/many.rb +50 -0
- data/lib/mongoid/relations/bindings/embedded/one.rb +55 -0
- data/lib/mongoid/relations/bindings/referenced/in.rb +65 -0
- data/lib/mongoid/relations/bindings/referenced/many.rb +42 -0
- data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +69 -0
- data/lib/mongoid/relations/bindings/referenced/one.rb +44 -0
- data/lib/mongoid/relations/bindings.rb +9 -0
- data/lib/mongoid/relations/builder.rb +57 -0
- data/lib/mongoid/relations/builders/embedded/in.rb +29 -0
- data/lib/mongoid/relations/builders/embedded/many.rb +36 -0
- data/lib/mongoid/relations/builders/embedded/one.rb +30 -0
- data/lib/mongoid/relations/builders/nested_attributes/many.rb +174 -0
- data/lib/mongoid/relations/builders/nested_attributes/one.rb +126 -0
- data/lib/mongoid/relations/builders/referenced/in.rb +26 -0
- data/lib/mongoid/relations/builders/referenced/many.rb +27 -0
- data/lib/mongoid/relations/builders/referenced/many_to_many.rb +40 -0
- data/lib/mongoid/relations/builders/referenced/one.rb +26 -0
- data/lib/mongoid/relations/builders.rb +104 -0
- data/lib/mongoid/relations/cascading/delete.rb +44 -0
- data/lib/mongoid/relations/cascading/destroy.rb +43 -0
- data/lib/mongoid/relations/cascading/nullify.rb +35 -0
- data/lib/mongoid/relations/cascading/restrict.rb +39 -0
- data/lib/mongoid/relations/cascading.rb +56 -0
- data/lib/mongoid/relations/constraint.rb +49 -0
- data/lib/mongoid/relations/conversions.rb +34 -0
- data/lib/mongoid/relations/counter_cache.rb +118 -0
- data/lib/mongoid/relations/cyclic.rb +107 -0
- data/lib/mongoid/relations/embedded/batchable.rb +355 -0
- data/lib/mongoid/relations/embedded/in.rb +231 -0
- data/lib/mongoid/relations/embedded/many.rb +639 -0
- data/lib/mongoid/relations/embedded/one.rb +223 -0
- data/lib/mongoid/relations/macros.rb +356 -0
- data/lib/mongoid/relations/many.rb +208 -0
- data/lib/mongoid/relations/marshalable.rb +32 -0
- data/lib/mongoid/relations/metadata.rb +1172 -0
- data/lib/mongoid/relations/nested_builder.rb +74 -0
- data/lib/mongoid/relations/one.rb +60 -0
- data/lib/mongoid/relations/options.rb +48 -0
- data/lib/mongoid/relations/polymorphic.rb +39 -0
- data/lib/mongoid/relations/proxy.rb +270 -0
- data/lib/mongoid/relations/referenced/in.rb +314 -0
- data/lib/mongoid/relations/referenced/many.rb +787 -0
- data/lib/mongoid/relations/referenced/many_to_many.rb +486 -0
- data/lib/mongoid/relations/referenced/one.rb +290 -0
- data/lib/mongoid/relations/reflections.rb +62 -0
- data/lib/mongoid/relations/synchronization.rb +169 -0
- data/lib/mongoid/relations/targets/enumerable.rb +473 -0
- data/lib/mongoid/relations/targets.rb +2 -0
- data/lib/mongoid/relations/touchable.rb +94 -0
- data/lib/mongoid/relations.rb +162 -0
- data/lib/mongoid/reloadable.rb +95 -0
- data/lib/mongoid/scopable.rb +379 -0
- data/lib/mongoid/selectable.rb +59 -0
- data/lib/mongoid/serializable.rb +170 -0
- data/lib/mongoid/sessions/factory.rb +131 -0
- data/lib/mongoid/sessions/mongo_uri.rb +93 -0
- data/lib/mongoid/sessions/options.rb +141 -0
- data/lib/mongoid/sessions/validators/storage.rb +49 -0
- data/lib/mongoid/sessions/validators.rb +2 -0
- data/lib/mongoid/sessions.rb +330 -0
- data/lib/mongoid/shardable.rb +65 -0
- data/lib/mongoid/state.rb +97 -0
- data/lib/mongoid/threaded/lifecycle.rb +164 -0
- data/lib/mongoid/threaded.rb +383 -0
- data/lib/mongoid/timestamps/created/short.rb +19 -0
- data/lib/mongoid/timestamps/created.rb +34 -0
- data/lib/mongoid/timestamps/short.rb +10 -0
- data/lib/mongoid/timestamps/timeless.rb +81 -0
- data/lib/mongoid/timestamps/updated/short.rb +19 -0
- data/lib/mongoid/timestamps/updated.rb +45 -0
- data/lib/mongoid/timestamps.rb +16 -0
- data/lib/mongoid/traversable.rb +192 -0
- data/lib/mongoid/unit_of_work.rb +61 -0
- data/lib/mongoid/validatable/associated.rb +48 -0
- data/lib/mongoid/validatable/format.rb +20 -0
- data/lib/mongoid/validatable/length.rb +20 -0
- data/lib/mongoid/validatable/localizable.rb +30 -0
- data/lib/mongoid/validatable/macros.rb +94 -0
- data/lib/mongoid/validatable/presence.rb +86 -0
- data/lib/mongoid/validatable/queryable.rb +30 -0
- data/lib/mongoid/validatable/uniqueness.rb +330 -0
- data/lib/mongoid/validatable.rb +180 -0
- data/lib/mongoid/version.rb +4 -0
- data/lib/mongoid.rb +105 -0
- data/lib/rack/mongoid/middleware/identity_map.rb +39 -0
- data/lib/rack/mongoid.rb +2 -0
- data/lib/rails/generators/mongoid/config/config_generator.rb +25 -0
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +76 -0
- data/lib/rails/generators/mongoid/model/model_generator.rb +25 -0
- data/lib/rails/generators/mongoid/model/templates/model.rb.tt +19 -0
- data/lib/rails/generators/mongoid_generator.rb +65 -0
- data/lib/rails/mongoid.rb +180 -0
- data/lib/support/ruby_version.rb +26 -0
- data/spec/app/models/account.rb +28 -0
- data/spec/app/models/acolyte.rb +17 -0
- data/spec/app/models/actor.rb +18 -0
- data/spec/app/models/actress.rb +2 -0
- data/spec/app/models/address.rb +77 -0
- data/spec/app/models/address_component.rb +5 -0
- data/spec/app/models/address_number.rb +6 -0
- data/spec/app/models/agency.rb +5 -0
- data/spec/app/models/agent.rb +12 -0
- data/spec/app/models/album.rb +14 -0
- data/spec/app/models/alert.rb +5 -0
- data/spec/app/models/animal.rb +25 -0
- data/spec/app/models/answer.rb +4 -0
- data/spec/app/models/appointment.rb +7 -0
- data/spec/app/models/article.rb +10 -0
- data/spec/app/models/artist.rb +66 -0
- data/spec/app/models/artwork.rb +4 -0
- data/spec/app/models/audio.rb +5 -0
- data/spec/app/models/augmentation.rb +11 -0
- data/spec/app/models/author.rb +4 -0
- data/spec/app/models/band.rb +26 -0
- data/spec/app/models/bar.rb +10 -0
- data/spec/app/models/basic.rb +6 -0
- data/spec/app/models/bed.rb +1 -0
- data/spec/app/models/big_palette.rb +2 -0
- data/spec/app/models/birthday.rb +13 -0
- data/spec/app/models/book.rb +13 -0
- data/spec/app/models/breed.rb +4 -0
- data/spec/app/models/browser.rb +6 -0
- data/spec/app/models/building.rb +5 -0
- data/spec/app/models/building_address.rb +5 -0
- data/spec/app/models/bus.rb +7 -0
- data/spec/app/models/business.rb +5 -0
- data/spec/app/models/callback_test.rb +9 -0
- data/spec/app/models/canvas.rb +25 -0
- data/spec/app/models/car.rb +1 -0
- data/spec/app/models/cat.rb +8 -0
- data/spec/app/models/category.rb +8 -0
- data/spec/app/models/child.rb +4 -0
- data/spec/app/models/child_doc.rb +22 -0
- data/spec/app/models/church.rb +4 -0
- data/spec/app/models/circle.rb +3 -0
- data/spec/app/models/circuit.rb +4 -0
- data/spec/app/models/circus.rb +7 -0
- data/spec/app/models/code.rb +5 -0
- data/spec/app/models/comment.rb +16 -0
- data/spec/app/models/contractor.rb +5 -0
- data/spec/app/models/cookie.rb +6 -0
- data/spec/app/models/country_code.rb +8 -0
- data/spec/app/models/definition.rb +8 -0
- data/spec/app/models/description.rb +11 -0
- data/spec/app/models/dictionary.rb +10 -0
- data/spec/app/models/division.rb +10 -0
- data/spec/app/models/doctor.rb +12 -0
- data/spec/app/models/dog.rb +7 -0
- data/spec/app/models/dokument.rb +5 -0
- data/spec/app/models/dragon.rb +4 -0
- data/spec/app/models/driver.rb +7 -0
- data/spec/app/models/drug.rb +6 -0
- data/spec/app/models/dungeon.rb +4 -0
- data/spec/app/models/email.rb +6 -0
- data/spec/app/models/employer.rb +5 -0
- data/spec/app/models/entry.rb +6 -0
- data/spec/app/models/eraser.rb +1 -0
- data/spec/app/models/event.rb +22 -0
- data/spec/app/models/exhibition.rb +4 -0
- data/spec/app/models/exhibitor.rb +5 -0
- data/spec/app/models/eye.rb +9 -0
- data/spec/app/models/eye_bowl.rb +9 -0
- data/spec/app/models/face.rb +8 -0
- data/spec/app/models/favorite.rb +6 -0
- data/spec/app/models/filesystem.rb +5 -0
- data/spec/app/models/fire_hydrant.rb +6 -0
- data/spec/app/models/firefox.rb +4 -0
- data/spec/app/models/fish.rb +7 -0
- data/spec/app/models/folder.rb +7 -0
- data/spec/app/models/folder_item.rb +9 -0
- data/spec/app/models/fruits.rb +34 -0
- data/spec/app/models/game.rb +19 -0
- data/spec/app/models/ghost.rb +7 -0
- data/spec/app/models/home.rb +4 -0
- data/spec/app/models/house.rb +6 -0
- data/spec/app/models/html_writer.rb +3 -0
- data/spec/app/models/image.rb +22 -0
- data/spec/app/models/implant.rb +16 -0
- data/spec/app/models/item.rb +8 -0
- data/spec/app/models/jar.rb +7 -0
- data/spec/app/models/label.rb +40 -0
- data/spec/app/models/language.rb +5 -0
- data/spec/app/models/lat_lng.rb +15 -0
- data/spec/app/models/league.rb +11 -0
- data/spec/app/models/learner.rb +2 -0
- data/spec/app/models/line_item.rb +6 -0
- data/spec/app/models/location.rb +8 -0
- data/spec/app/models/login.rb +8 -0
- data/spec/app/models/manufacturer.rb +7 -0
- data/spec/app/models/meat.rb +4 -0
- data/spec/app/models/membership.rb +4 -0
- data/spec/app/models/mixed_drink.rb +4 -0
- data/spec/app/models/movie.rb +13 -0
- data/spec/app/models/my_hash.rb +2 -0
- data/spec/app/models/name.rb +23 -0
- data/spec/app/models/node.rb +5 -0
- data/spec/app/models/note.rb +12 -0
- data/spec/app/models/ordered_post.rb +6 -0
- data/spec/app/models/ordered_preference.rb +6 -0
- data/spec/app/models/oscar.rb +15 -0
- data/spec/app/models/override.rb +16 -0
- data/spec/app/models/ownable.rb +6 -0
- data/spec/app/models/owner.rb +6 -0
- data/spec/app/models/pack.rb +3 -0
- data/spec/app/models/page.rb +5 -0
- data/spec/app/models/page_question.rb +4 -0
- data/spec/app/models/palette.rb +7 -0
- data/spec/app/models/parent.rb +5 -0
- data/spec/app/models/parent_doc.rb +6 -0
- data/spec/app/models/passport.rb +5 -0
- data/spec/app/models/patient.rb +9 -0
- data/spec/app/models/pdf_writer.rb +3 -0
- data/spec/app/models/pencil.rb +1 -0
- data/spec/app/models/person.rb +206 -0
- data/spec/app/models/pet.rb +23 -0
- data/spec/app/models/pet_owner.rb +6 -0
- data/spec/app/models/phone.rb +9 -0
- data/spec/app/models/pizza.rb +7 -0
- data/spec/app/models/player.rb +35 -0
- data/spec/app/models/post.rb +44 -0
- data/spec/app/models/powerup.rb +11 -0
- data/spec/app/models/preference.rb +9 -0
- data/spec/app/models/princess.rb +8 -0
- data/spec/app/models/product.rb +17 -0
- data/spec/app/models/profile.rb +5 -0
- data/spec/app/models/pronunciation.rb +5 -0
- data/spec/app/models/purchase.rb +4 -0
- data/spec/app/models/question.rb +8 -0
- data/spec/app/models/quiz.rb +7 -0
- data/spec/app/models/rating.rb +8 -0
- data/spec/app/models/record.rb +46 -0
- data/spec/app/models/registry.rb +4 -0
- data/spec/app/models/role.rb +7 -0
- data/spec/app/models/root_category.rb +4 -0
- data/spec/app/models/sandwich.rb +4 -0
- data/spec/app/models/scheduler.rb +7 -0
- data/spec/app/models/seo.rb +7 -0
- data/spec/app/models/series.rb +4 -0
- data/spec/app/models/server.rb +13 -0
- data/spec/app/models/service.rb +22 -0
- data/spec/app/models/shape.rb +12 -0
- data/spec/app/models/shelf.rb +5 -0
- data/spec/app/models/shipping_container.rb +5 -0
- data/spec/app/models/shipping_pack.rb +3 -0
- data/spec/app/models/shop.rb +6 -0
- data/spec/app/models/short_agent.rb +4 -0
- data/spec/app/models/short_quiz.rb +5 -0
- data/spec/app/models/slave.rb +6 -0
- data/spec/app/models/song.rb +8 -0
- data/spec/app/models/sound.rb +5 -0
- data/spec/app/models/square.rb +4 -0
- data/spec/app/models/strategy.rb +3 -0
- data/spec/app/models/sub_item.rb +3 -0
- data/spec/app/models/subscription.rb +4 -0
- data/spec/app/models/survey.rb +5 -0
- data/spec/app/models/symptom.rb +6 -0
- data/spec/app/models/tag.rb +8 -0
- data/spec/app/models/target.rb +5 -0
- data/spec/app/models/template.rb +5 -0
- data/spec/app/models/thing.rb +9 -0
- data/spec/app/models/title.rb +3 -0
- data/spec/app/models/tool.rb +8 -0
- data/spec/app/models/topping.rb +5 -0
- data/spec/app/models/track.rb +38 -0
- data/spec/app/models/translation.rb +5 -0
- data/spec/app/models/tree.rb +9 -0
- data/spec/app/models/truck.rb +3 -0
- data/spec/app/models/user.rb +21 -0
- data/spec/app/models/user_account.rb +10 -0
- data/spec/app/models/validation_callback.rb +10 -0
- data/spec/app/models/vehicle.rb +11 -0
- data/spec/app/models/version.rb +5 -0
- data/spec/app/models/vet_visit.rb +5 -0
- data/spec/app/models/video.rb +13 -0
- data/spec/app/models/video_game.rb +1 -0
- data/spec/app/models/weapon.rb +11 -0
- data/spec/app/models/wiki_page.rb +14 -0
- data/spec/app/models/word.rb +15 -0
- data/spec/app/models/word_origin.rb +11 -0
- data/spec/app/models/writer.rb +11 -0
- data/spec/config/mongoid.yml +40 -0
- data/spec/mongoid/atomic/modifiers_spec.rb +456 -0
- data/spec/mongoid/atomic/paths/embedded/many_spec.rb +118 -0
- data/spec/mongoid/atomic/paths/embedded/one_spec.rb +110 -0
- data/spec/mongoid/atomic/paths/root_spec.rb +48 -0
- data/spec/mongoid/atomic/paths_spec.rb +270 -0
- data/spec/mongoid/atomic_spec.rb +365 -0
- data/spec/mongoid/attributes/nested_spec.rb +4832 -0
- data/spec/mongoid/attributes/readonly_spec.rb +169 -0
- data/spec/mongoid/attributes_spec.rb +1467 -0
- data/spec/mongoid/changeable_spec.rb +1507 -0
- data/spec/mongoid/composable_spec.rb +24 -0
- data/spec/mongoid/config/environment_spec.rb +83 -0
- data/spec/mongoid/config/options_spec.rb +56 -0
- data/spec/mongoid/config_spec.rb +318 -0
- data/spec/mongoid/contextual/aggregable/memory_spec.rb +293 -0
- data/spec/mongoid/contextual/aggregable/mongo_spec.rb +495 -0
- data/spec/mongoid/contextual/atomic_spec.rb +529 -0
- data/spec/mongoid/contextual/find_and_modify_spec.rb +220 -0
- data/spec/mongoid/contextual/geo_near_spec.rb +405 -0
- data/spec/mongoid/contextual/map_reduce_spec.rb +464 -0
- data/spec/mongoid/contextual/memory_spec.rb +1236 -0
- data/spec/mongoid/contextual/mongo_spec.rb +1863 -0
- data/spec/mongoid/contextual/text_search_spec.rb +207 -0
- data/spec/mongoid/copyable_spec.rb +393 -0
- data/spec/mongoid/criteria/findable_spec.rb +1222 -0
- data/spec/mongoid/criteria/inspectable_spec.rb +27 -0
- data/spec/mongoid/criteria/marshalable_spec.rb +28 -0
- data/spec/mongoid/criteria/modifiable_spec.rb +1063 -0
- data/spec/mongoid/criteria/scopable_spec.rb +391 -0
- data/spec/mongoid/criteria_spec.rb +3821 -0
- data/spec/mongoid/document_spec.rb +1205 -0
- data/spec/mongoid/equality_spec.rb +241 -0
- data/spec/mongoid/errors/ambiguous_relationship_spec.rb +29 -0
- data/spec/mongoid/errors/callback_spec.rb +29 -0
- data/spec/mongoid/errors/delete_restriction_spec.rb +29 -0
- data/spec/mongoid/errors/document_not_found_spec.rb +104 -0
- data/spec/mongoid/errors/eager_load_spec.rb +29 -0
- data/spec/mongoid/errors/invalid_collection_spec.rb +36 -0
- data/spec/mongoid/errors/invalid_config_option_spec.rb +29 -0
- data/spec/mongoid/errors/invalid_field_option_spec.rb +29 -0
- data/spec/mongoid/errors/invalid_field_spec.rb +37 -0
- data/spec/mongoid/errors/invalid_find_spec.rb +29 -0
- data/spec/mongoid/errors/invalid_includes_spec.rb +40 -0
- data/spec/mongoid/errors/invalid_index_spec.rb +29 -0
- data/spec/mongoid/errors/invalid_options_spec.rb +29 -0
- data/spec/mongoid/errors/invalid_path_spec.rb +23 -0
- data/spec/mongoid/errors/invalid_scope_spec.rb +29 -0
- data/spec/mongoid/errors/invalid_set_polymorphic_relation_spec.rb +17 -0
- data/spec/mongoid/errors/invalid_storage_options_spec.rb +29 -0
- data/spec/mongoid/errors/invalid_time_spec.rb +29 -0
- data/spec/mongoid/errors/inverse_not_found_spec.rb +29 -0
- data/spec/mongoid/errors/mixed_relations_spec.rb +29 -0
- data/spec/mongoid/errors/mixed_session_configuration_spec.rb +29 -0
- data/spec/mongoid/errors/mongoid_error_spec.rb +48 -0
- data/spec/mongoid/errors/nested_attributes_metadata_not_found_spec.rb +29 -0
- data/spec/mongoid/errors/no_environment_spec.rb +29 -0
- data/spec/mongoid/errors/no_map_reduce_output_spec.rb +29 -0
- data/spec/mongoid/errors/no_metadata_spec.rb +23 -0
- data/spec/mongoid/errors/no_parent_spec.rb +29 -0
- data/spec/mongoid/errors/no_session_config_spec.rb +29 -0
- data/spec/mongoid/errors/no_session_database_spec.rb +29 -0
- data/spec/mongoid/errors/no_session_hosts_spec.rb +29 -0
- data/spec/mongoid/errors/no_sessions_config_spec.rb +29 -0
- data/spec/mongoid/errors/readonly_attribute_spec.rb +29 -0
- data/spec/mongoid/errors/scope_overwrite_spec.rb +29 -0
- data/spec/mongoid/errors/too_many_nested_attribute_records_spec.rb +29 -0
- data/spec/mongoid/errors/unknown_attribute_spec.rb +29 -0
- data/spec/mongoid/errors/unsaved_document_spec.rb +37 -0
- data/spec/mongoid/errors/unsupported_javascript_spec.rb +29 -0
- data/spec/mongoid/errors/validations_spec.rb +45 -0
- data/spec/mongoid/extensions/array_spec.rb +638 -0
- data/spec/mongoid/extensions/big_decimal_spec.rb +104 -0
- data/spec/mongoid/extensions/binary_spec.rb +60 -0
- data/spec/mongoid/extensions/boolean_spec.rb +135 -0
- data/spec/mongoid/extensions/date_spec.rb +235 -0
- data/spec/mongoid/extensions/date_time_spec.rb +155 -0
- data/spec/mongoid/extensions/false_class_spec.rb +42 -0
- data/spec/mongoid/extensions/float_spec.rb +133 -0
- data/spec/mongoid/extensions/hash_spec.rb +333 -0
- data/spec/mongoid/extensions/integer_spec.rb +136 -0
- data/spec/mongoid/extensions/module_spec.rb +42 -0
- data/spec/mongoid/extensions/nil_class_spec.rb +11 -0
- data/spec/mongoid/extensions/object_id_spec.rb +946 -0
- data/spec/mongoid/extensions/object_spec.rb +292 -0
- data/spec/mongoid/extensions/range_spec.rb +154 -0
- data/spec/mongoid/extensions/regexp_spec.rb +47 -0
- data/spec/mongoid/extensions/set_spec.rb +33 -0
- data/spec/mongoid/extensions/string_spec.rb +357 -0
- data/spec/mongoid/extensions/symbol_spec.rb +76 -0
- data/spec/mongoid/extensions/time_spec.rb +467 -0
- data/spec/mongoid/extensions/time_with_zone_spec.rb +405 -0
- data/spec/mongoid/extensions/true_class_spec.rb +42 -0
- data/spec/mongoid/extensions_spec.rb +15 -0
- data/spec/mongoid/factory_spec.rb +185 -0
- data/spec/mongoid/fields/foreign_key_spec.rb +694 -0
- data/spec/mongoid/fields/internal/foreign_keys/array_spec.rb +184 -0
- data/spec/mongoid/fields/internal/foreign_keys/object_spec.rb +201 -0
- data/spec/mongoid/fields/localized_spec.rb +386 -0
- data/spec/mongoid/fields/standard_spec.rb +166 -0
- data/spec/mongoid/fields_spec.rb +1229 -0
- data/spec/mongoid/findable_spec.rb +342 -0
- data/spec/mongoid/identity_map_spec.rb +564 -0
- data/spec/mongoid/indexable/specification_spec.rb +87 -0
- data/spec/mongoid/indexable_spec.rb +504 -0
- data/spec/mongoid/inspectable_spec.rb +49 -0
- data/spec/mongoid/interceptable_spec.rb +1564 -0
- data/spec/mongoid/loggable_spec.rb +21 -0
- data/spec/mongoid/matchable/all_spec.rb +31 -0
- data/spec/mongoid/matchable/and_spec.rb +162 -0
- data/spec/mongoid/matchable/default_spec.rb +130 -0
- data/spec/mongoid/matchable/exists_spec.rb +57 -0
- data/spec/mongoid/matchable/gt_spec.rb +75 -0
- data/spec/mongoid/matchable/gte_spec.rb +74 -0
- data/spec/mongoid/matchable/in_spec.rb +25 -0
- data/spec/mongoid/matchable/lt_spec.rb +74 -0
- data/spec/mongoid/matchable/lte_spec.rb +74 -0
- data/spec/mongoid/matchable/ne_spec.rb +25 -0
- data/spec/mongoid/matchable/nin_spec.rb +25 -0
- data/spec/mongoid/matchable/or_spec.rb +106 -0
- data/spec/mongoid/matchable/size_spec.rb +25 -0
- data/spec/mongoid/matchable_spec.rb +532 -0
- data/spec/mongoid/persistable/creatable_spec.rb +512 -0
- data/spec/mongoid/persistable/deletable_spec.rb +205 -0
- data/spec/mongoid/persistable/destroyable_spec.rb +148 -0
- data/spec/mongoid/persistable/incrementable_spec.rb +173 -0
- data/spec/mongoid/persistable/logical_spec.rb +143 -0
- data/spec/mongoid/persistable/poppable_spec.rb +115 -0
- data/spec/mongoid/persistable/pullable_spec.rb +228 -0
- data/spec/mongoid/persistable/pushable_spec.rb +258 -0
- data/spec/mongoid/persistable/renamable_spec.rb +135 -0
- data/spec/mongoid/persistable/savable_spec.rb +432 -0
- data/spec/mongoid/persistable/settable_spec.rb +139 -0
- data/spec/mongoid/persistable/unsettable_spec.rb +155 -0
- data/spec/mongoid/persistable/updatable_spec.rb +558 -0
- data/spec/mongoid/persistable/upsertable_spec.rb +106 -0
- data/spec/mongoid/persistable_spec.rb +206 -0
- data/spec/mongoid/positional_spec.rb +227 -0
- data/spec/mongoid/railties/document_spec.rb +24 -0
- data/spec/mongoid/relations/accessors_spec.rb +752 -0
- data/spec/mongoid/relations/auto_save_spec.rb +261 -0
- data/spec/mongoid/relations/bindings/embedded/in_spec.rb +171 -0
- data/spec/mongoid/relations/bindings/embedded/many_spec.rb +54 -0
- data/spec/mongoid/relations/bindings/embedded/one_spec.rb +77 -0
- data/spec/mongoid/relations/bindings/referenced/in_spec.rb +241 -0
- data/spec/mongoid/relations/bindings/referenced/many_spec.rb +153 -0
- data/spec/mongoid/relations/bindings/referenced/many_to_many_spec.rb +178 -0
- data/spec/mongoid/relations/bindings/referenced/one_spec.rb +131 -0
- data/spec/mongoid/relations/builders/embedded/in_spec.rb +34 -0
- data/spec/mongoid/relations/builders/embedded/many_spec.rb +132 -0
- data/spec/mongoid/relations/builders/embedded/one_spec.rb +99 -0
- data/spec/mongoid/relations/builders/nested_attributes/many_spec.rb +234 -0
- data/spec/mongoid/relations/builders/nested_attributes/one_spec.rb +250 -0
- data/spec/mongoid/relations/builders/referenced/in_spec.rb +242 -0
- data/spec/mongoid/relations/builders/referenced/many_spec.rb +137 -0
- data/spec/mongoid/relations/builders/referenced/many_to_many_spec.rb +178 -0
- data/spec/mongoid/relations/builders/referenced/one_spec.rb +124 -0
- data/spec/mongoid/relations/builders_spec.rb +226 -0
- data/spec/mongoid/relations/cascading/delete_spec.rb +101 -0
- data/spec/mongoid/relations/cascading/destroy_spec.rb +47 -0
- data/spec/mongoid/relations/cascading/nullify_spec.rb +32 -0
- data/spec/mongoid/relations/cascading/restrict_spec.rb +68 -0
- data/spec/mongoid/relations/cascading_spec.rb +355 -0
- data/spec/mongoid/relations/constraint_spec.rb +74 -0
- data/spec/mongoid/relations/conversions_spec.rb +126 -0
- data/spec/mongoid/relations/counter_cache_spec.rb +205 -0
- data/spec/mongoid/relations/cyclic_spec.rb +156 -0
- data/spec/mongoid/relations/embedded/dirty_spec.rb +65 -0
- data/spec/mongoid/relations/embedded/in_spec.rb +579 -0
- data/spec/mongoid/relations/embedded/many_spec.rb +3781 -0
- data/spec/mongoid/relations/embedded/one_spec.rb +1014 -0
- data/spec/mongoid/relations/macros_spec.rb +631 -0
- data/spec/mongoid/relations/metadata_spec.rb +1936 -0
- data/spec/mongoid/relations/options_spec.rb +35 -0
- data/spec/mongoid/relations/polymorphic_spec.rb +128 -0
- data/spec/mongoid/relations/proxy_spec.rb +48 -0
- data/spec/mongoid/relations/referenced/in_spec.rb +1435 -0
- data/spec/mongoid/relations/referenced/many_spec.rb +3600 -0
- data/spec/mongoid/relations/referenced/many_to_many_spec.rb +3566 -0
- data/spec/mongoid/relations/referenced/one_spec.rb +1289 -0
- data/spec/mongoid/relations/reflections_spec.rb +101 -0
- data/spec/mongoid/relations/synchronization_spec.rb +449 -0
- data/spec/mongoid/relations/targets/enumerable_spec.rb +1710 -0
- data/spec/mongoid/relations/touchable_spec.rb +296 -0
- data/spec/mongoid/relations_spec.rb +188 -0
- data/spec/mongoid/reloadable_spec.rb +305 -0
- data/spec/mongoid/scopable_spec.rb +926 -0
- data/spec/mongoid/selectable_spec.rb +134 -0
- data/spec/mongoid/serializable_spec.rb +862 -0
- data/spec/mongoid/sessions/factory_spec.rb +333 -0
- data/spec/mongoid/sessions/mongo_uri_spec.rb +103 -0
- data/spec/mongoid/sessions/options_spec.rb +71 -0
- data/spec/mongoid/sessions_spec.rb +1078 -0
- data/spec/mongoid/shardable_spec.rb +61 -0
- data/spec/mongoid/state_spec.rb +102 -0
- data/spec/mongoid/threaded_spec.rb +258 -0
- data/spec/mongoid/timestamps/created/short_spec.rb +51 -0
- data/spec/mongoid/timestamps/created_spec.rb +44 -0
- data/spec/mongoid/timestamps/timeless_spec.rb +130 -0
- data/spec/mongoid/timestamps/updated/short_spec.rb +90 -0
- data/spec/mongoid/timestamps/updated_spec.rb +86 -0
- data/spec/mongoid/timestamps_spec.rb +112 -0
- data/spec/mongoid/traversable_spec.rb +244 -0
- data/spec/mongoid/unit_of_work_spec.rb +196 -0
- data/spec/mongoid/validatable/associated_spec.rb +183 -0
- data/spec/mongoid/validatable/format_spec.rb +83 -0
- data/spec/mongoid/validatable/length_spec.rb +223 -0
- data/spec/mongoid/validatable/numericality_spec.rb +30 -0
- data/spec/mongoid/validatable/presence_spec.rb +592 -0
- data/spec/mongoid/validatable/uniqueness_spec.rb +2342 -0
- data/spec/mongoid/validatable_spec.rb +309 -0
- data/spec/mongoid_spec.rb +74 -0
- data/spec/rack/mongoid/middleware/identity_map_spec.rb +72 -0
- data/spec/rails/mongoid_spec.rb +462 -0
- data/spec/spec_helper.rb +103 -0
- metadata +1174 -0
@@ -0,0 +1,4832 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Mongoid::Attributes::Nested do
|
4
|
+
|
5
|
+
describe ".accepts_nested_attributes_for" do
|
6
|
+
|
7
|
+
context "when the autosave option is not defined" do
|
8
|
+
|
9
|
+
let(:person) do
|
10
|
+
Person.new
|
11
|
+
end
|
12
|
+
|
13
|
+
before do
|
14
|
+
Person.accepts_nested_attributes_for :favorites
|
15
|
+
end
|
16
|
+
|
17
|
+
after do
|
18
|
+
Person.send(:undef_method, :favorites_attributes=)
|
19
|
+
Person.nested_attributes.clear
|
20
|
+
end
|
21
|
+
|
22
|
+
it "adds a method for handling the attributes" do
|
23
|
+
expect(person).to respond_to(:favorites_attributes=)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "adds the method name to the nested attributes list" do
|
27
|
+
expect(Person.nested_attributes).to eq({
|
28
|
+
"favorites_attributes" => "favorites_attributes="
|
29
|
+
})
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context "when autosave is explicitly false" do
|
34
|
+
|
35
|
+
before do
|
36
|
+
Account.accepts_nested_attributes_for :alerts
|
37
|
+
end
|
38
|
+
|
39
|
+
after do
|
40
|
+
Account.send(:undef_method, :alerts_attributes=)
|
41
|
+
Account.nested_attributes.clear
|
42
|
+
end
|
43
|
+
|
44
|
+
let(:metadata) do
|
45
|
+
Account.reflect_on_association(:alerts)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "keeps autosave set to false" do
|
49
|
+
expect(metadata).to_not be_autosave
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe "#initialize" do
|
55
|
+
|
56
|
+
context "when the relation is an embeds one" do
|
57
|
+
|
58
|
+
before(:all) do
|
59
|
+
Person.send(:undef_method, :name_attributes=)
|
60
|
+
Person.accepts_nested_attributes_for :name
|
61
|
+
end
|
62
|
+
|
63
|
+
let(:person) do
|
64
|
+
Person.new(name_attributes: { first_name: "Johnny" })
|
65
|
+
end
|
66
|
+
|
67
|
+
it "sets the nested attributes" do
|
68
|
+
expect(person.name.first_name).to eq("Johnny")
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context "when the relation is an embeds many" do
|
73
|
+
|
74
|
+
before(:all) do
|
75
|
+
Person.send(:undef_method, :addresses_attributes=)
|
76
|
+
Person.accepts_nested_attributes_for :addresses
|
77
|
+
end
|
78
|
+
|
79
|
+
let(:person) do
|
80
|
+
Person.new(addresses_attributes: { "1" => { street: "Alexanderstr" }})
|
81
|
+
end
|
82
|
+
|
83
|
+
it "sets the nested attributes" do
|
84
|
+
expect(person.addresses.first.street).to eq("Alexanderstr")
|
85
|
+
end
|
86
|
+
|
87
|
+
context "when there are 10 or more child records" do
|
88
|
+
|
89
|
+
let(:person) do
|
90
|
+
Person.new(addresses: addresses)
|
91
|
+
end
|
92
|
+
|
93
|
+
let(:addresses) do
|
94
|
+
('0'..'10').inject({}) do |addresses,i|
|
95
|
+
addresses.merge(i => {number: i})
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
it "preserves the order of the children" do
|
100
|
+
expect(person.addresses.map(&:number)).to eq((0..10).to_a)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
context "when the relation is an embedded in" do
|
106
|
+
|
107
|
+
before(:all) do
|
108
|
+
Video.accepts_nested_attributes_for :person
|
109
|
+
end
|
110
|
+
|
111
|
+
let(:video) do
|
112
|
+
Video.new(person_attributes: { title: "Sir" })
|
113
|
+
end
|
114
|
+
|
115
|
+
it "sets the nested attributes" do
|
116
|
+
expect(video.person.title).to eq("Sir")
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
context "when the relation is a references one" do
|
121
|
+
|
122
|
+
before(:all) do
|
123
|
+
Person.send(:undef_method, :game_attributes=)
|
124
|
+
Person.accepts_nested_attributes_for :game
|
125
|
+
end
|
126
|
+
|
127
|
+
let(:person) do
|
128
|
+
Person.new(game_attributes: { name: "Tron" })
|
129
|
+
end
|
130
|
+
|
131
|
+
it "sets the nested attributes" do
|
132
|
+
expect(person.game.name).to eq("Tron")
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
context "when the relation is a references many" do
|
137
|
+
|
138
|
+
before(:all) do
|
139
|
+
Person.send(:undef_method, :posts_attributes=)
|
140
|
+
Person.accepts_nested_attributes_for :posts
|
141
|
+
end
|
142
|
+
|
143
|
+
let(:person) do
|
144
|
+
Person.new(posts_attributes: { "1" => { title: "First" }})
|
145
|
+
end
|
146
|
+
|
147
|
+
it "sets the nested attributes" do
|
148
|
+
expect(person.posts.first.title).to eq("First")
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
context "when the relation is a references and referenced in many" do
|
153
|
+
|
154
|
+
before(:all) do
|
155
|
+
Person.send(:undef_method, :preferences_attributes=)
|
156
|
+
Person.accepts_nested_attributes_for :preferences
|
157
|
+
end
|
158
|
+
|
159
|
+
let(:person) do
|
160
|
+
Person.new(preferences_attributes: { "1" => { name: "First" }})
|
161
|
+
end
|
162
|
+
|
163
|
+
it "sets the nested attributes" do
|
164
|
+
expect(person.preferences.first.name).to eq("First")
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
context "when the relation is a referenced in" do
|
169
|
+
|
170
|
+
before(:all) do
|
171
|
+
Post.accepts_nested_attributes_for :person
|
172
|
+
end
|
173
|
+
|
174
|
+
let(:post) do
|
175
|
+
Post.new(person_attributes: { title: "Sir" })
|
176
|
+
end
|
177
|
+
|
178
|
+
it "sets the nested attributes" do
|
179
|
+
expect(post.person.title).to eq("Sir")
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
describe "##{name}_attributes=" do
|
185
|
+
|
186
|
+
context "when the parent document is new" do
|
187
|
+
|
188
|
+
context "when the relation is an embeds one" do
|
189
|
+
|
190
|
+
context "when the parent document is persisted" do
|
191
|
+
|
192
|
+
let(:person) do
|
193
|
+
Person.create
|
194
|
+
end
|
195
|
+
|
196
|
+
before do
|
197
|
+
Person.send(:undef_method, :name_attributes=)
|
198
|
+
Person.accepts_nested_attributes_for :name, allow_destroy: true
|
199
|
+
end
|
200
|
+
|
201
|
+
after do
|
202
|
+
Person.send(:undef_method, :name_attributes=)
|
203
|
+
Person.accepts_nested_attributes_for :name
|
204
|
+
end
|
205
|
+
|
206
|
+
context "when setting the child attributes" do
|
207
|
+
|
208
|
+
before do
|
209
|
+
person.name_attributes = { last_name: "Fischer" }
|
210
|
+
end
|
211
|
+
|
212
|
+
it "sets the child document" do
|
213
|
+
expect(person.name.last_name).to eq("Fischer")
|
214
|
+
end
|
215
|
+
|
216
|
+
it "does not persist the child document" do
|
217
|
+
expect(person.name).to_not be_persisted
|
218
|
+
end
|
219
|
+
|
220
|
+
context "when saving the parent" do
|
221
|
+
|
222
|
+
before do
|
223
|
+
person.save
|
224
|
+
person.reload
|
225
|
+
end
|
226
|
+
|
227
|
+
it "persists the child document" do
|
228
|
+
expect(person.name).to be_persisted
|
229
|
+
end
|
230
|
+
end
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
let(:person) do
|
235
|
+
Person.new
|
236
|
+
end
|
237
|
+
|
238
|
+
context "when a reject proc is specified" do
|
239
|
+
|
240
|
+
before(:all) do
|
241
|
+
Person.send(:undef_method, :name_attributes=)
|
242
|
+
Person.accepts_nested_attributes_for \
|
243
|
+
:name, reject_if: ->(attrs){ attrs[:first_name].blank? }
|
244
|
+
end
|
245
|
+
|
246
|
+
after(:all) do
|
247
|
+
Person.send(:undef_method, :name_attributes=)
|
248
|
+
Person.accepts_nested_attributes_for :name
|
249
|
+
end
|
250
|
+
|
251
|
+
context "when the attributes match" do
|
252
|
+
|
253
|
+
before do
|
254
|
+
person.name_attributes = { last_name: "Lang" }
|
255
|
+
end
|
256
|
+
|
257
|
+
it "does not add the document" do
|
258
|
+
expect(person.name).to be_nil
|
259
|
+
end
|
260
|
+
end
|
261
|
+
|
262
|
+
context "when the attributes do not match" do
|
263
|
+
|
264
|
+
before do
|
265
|
+
person.name_attributes = { first_name: "Lang" }
|
266
|
+
end
|
267
|
+
|
268
|
+
it "adds the document" do
|
269
|
+
expect(person.name.first_name).to eq("Lang")
|
270
|
+
end
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
274
|
+
context "when :reject_if => :all_blank is specified" do
|
275
|
+
|
276
|
+
context "when the relation is not autobuilding" do
|
277
|
+
|
278
|
+
before(:all) do
|
279
|
+
Person.send(:undef_method, :name_attributes=)
|
280
|
+
Person.accepts_nested_attributes_for \
|
281
|
+
:name, reject_if: :all_blank
|
282
|
+
end
|
283
|
+
|
284
|
+
after(:all) do
|
285
|
+
Person.send(:undef_method, :name_attributes=)
|
286
|
+
Person.accepts_nested_attributes_for :name
|
287
|
+
end
|
288
|
+
|
289
|
+
context "when all attributes are empty" do
|
290
|
+
|
291
|
+
before do
|
292
|
+
person.name_attributes = { last_name: "" }
|
293
|
+
end
|
294
|
+
|
295
|
+
it "does not add the document" do
|
296
|
+
expect(person.name).to be_nil
|
297
|
+
end
|
298
|
+
end
|
299
|
+
|
300
|
+
context "when an attribute is non-empty" do
|
301
|
+
|
302
|
+
before do
|
303
|
+
person.name_attributes = { first_name: "Lang" }
|
304
|
+
end
|
305
|
+
|
306
|
+
it "adds the document" do
|
307
|
+
expect(person.name.first_name).to eq("Lang")
|
308
|
+
end
|
309
|
+
end
|
310
|
+
end
|
311
|
+
|
312
|
+
context "when the relation is autobuilding" do
|
313
|
+
|
314
|
+
before(:all) do
|
315
|
+
Product.accepts_nested_attributes_for :seo, reject_if: :all_blank
|
316
|
+
end
|
317
|
+
|
318
|
+
after(:all) do
|
319
|
+
Product.send(:undef_method, :seo_attributes=)
|
320
|
+
end
|
321
|
+
|
322
|
+
context "when all attributes are empty" do
|
323
|
+
|
324
|
+
let(:product) do
|
325
|
+
Product.create(name: "testing")
|
326
|
+
end
|
327
|
+
|
328
|
+
it "does not add the document" do
|
329
|
+
expect(product.seo).to_not be_persisted
|
330
|
+
end
|
331
|
+
end
|
332
|
+
end
|
333
|
+
end
|
334
|
+
|
335
|
+
context "when no id has been passed" do
|
336
|
+
|
337
|
+
context "with no destroy attribute" do
|
338
|
+
|
339
|
+
before do
|
340
|
+
person.name_attributes = { first_name: "Leo" }
|
341
|
+
end
|
342
|
+
|
343
|
+
it "builds a new document" do
|
344
|
+
expect(person.name.first_name).to eq("Leo")
|
345
|
+
end
|
346
|
+
end
|
347
|
+
|
348
|
+
context "with a destroy attribute" do
|
349
|
+
|
350
|
+
context "when allow_destroy is true" do
|
351
|
+
|
352
|
+
before(:all) do
|
353
|
+
Person.send(:undef_method, :name_attributes=)
|
354
|
+
Person.accepts_nested_attributes_for :name, allow_destroy: true
|
355
|
+
end
|
356
|
+
|
357
|
+
after(:all) do
|
358
|
+
Person.send(:undef_method, :name_attributes=)
|
359
|
+
Person.accepts_nested_attributes_for :name
|
360
|
+
end
|
361
|
+
|
362
|
+
context "when destroy is a symbol" do
|
363
|
+
|
364
|
+
before do
|
365
|
+
person.name_attributes = { first_name: "Leo", _destroy: "1" }
|
366
|
+
end
|
367
|
+
|
368
|
+
it "does not build the document" do
|
369
|
+
expect(person.name).to be_nil
|
370
|
+
end
|
371
|
+
end
|
372
|
+
|
373
|
+
context "when destroy is a string" do
|
374
|
+
|
375
|
+
before do
|
376
|
+
person.name_attributes = { first_name: "Leo", "_destroy" => "1" }
|
377
|
+
end
|
378
|
+
|
379
|
+
it "does not build the document" do
|
380
|
+
expect(person.name).to be_nil
|
381
|
+
end
|
382
|
+
end
|
383
|
+
end
|
384
|
+
|
385
|
+
context "when allow_destroy is false" do
|
386
|
+
|
387
|
+
before(:all) do
|
388
|
+
Person.send(:undef_method, :name_attributes=)
|
389
|
+
Person.accepts_nested_attributes_for :name, allow_destroy: false
|
390
|
+
end
|
391
|
+
|
392
|
+
after(:all) do
|
393
|
+
Person.send(:undef_method, :name_attributes=)
|
394
|
+
Person.accepts_nested_attributes_for :name
|
395
|
+
end
|
396
|
+
|
397
|
+
before do
|
398
|
+
person.name_attributes = { first_name: "Leo", _destroy: "1" }
|
399
|
+
end
|
400
|
+
|
401
|
+
it "builds the document" do
|
402
|
+
expect(person.name.first_name).to eq("Leo")
|
403
|
+
end
|
404
|
+
end
|
405
|
+
end
|
406
|
+
|
407
|
+
context "with empty attributes" do
|
408
|
+
|
409
|
+
before do
|
410
|
+
person.name_attributes = {}
|
411
|
+
end
|
412
|
+
|
413
|
+
it "does not build the document" do
|
414
|
+
expect(person.name).to be_nil
|
415
|
+
end
|
416
|
+
end
|
417
|
+
|
418
|
+
context "when there is an existing document" do
|
419
|
+
|
420
|
+
context "with no destroy attribute" do
|
421
|
+
|
422
|
+
before do
|
423
|
+
person.name = Name.new(first_name: "Michael")
|
424
|
+
person.name_attributes = { first_name: "Jack" }
|
425
|
+
end
|
426
|
+
|
427
|
+
it "replaces the document" do
|
428
|
+
expect(person.name.first_name).to eq("Jack")
|
429
|
+
end
|
430
|
+
end
|
431
|
+
|
432
|
+
context "with a destroy attribute" do
|
433
|
+
|
434
|
+
context "when allow_destroy is true" do
|
435
|
+
|
436
|
+
before(:all) do
|
437
|
+
Person.send(:undef_method, :name_attributes=)
|
438
|
+
Person.accepts_nested_attributes_for :name, allow_destroy: true
|
439
|
+
end
|
440
|
+
|
441
|
+
after(:all) do
|
442
|
+
Person.send(:undef_method, :name_attributes=)
|
443
|
+
Person.accepts_nested_attributes_for :name
|
444
|
+
end
|
445
|
+
|
446
|
+
before do
|
447
|
+
person.name = Name.new(first_name: "Michael")
|
448
|
+
person.name_attributes = { first_name: "Jack", _destroy: "1" }
|
449
|
+
end
|
450
|
+
|
451
|
+
it "does not replace the document" do
|
452
|
+
expect(person.name.first_name).to eq("Michael")
|
453
|
+
end
|
454
|
+
end
|
455
|
+
|
456
|
+
context "when allow_destroy is false" do
|
457
|
+
|
458
|
+
before(:all) do
|
459
|
+
Person.send(:undef_method, :name_attributes=)
|
460
|
+
Person.accepts_nested_attributes_for :name, allow_destroy: false
|
461
|
+
end
|
462
|
+
|
463
|
+
after(:all) do
|
464
|
+
Person.send(:undef_method, :name_attributes=)
|
465
|
+
Person.accepts_nested_attributes_for :name
|
466
|
+
end
|
467
|
+
|
468
|
+
before do
|
469
|
+
person.name = Name.new(first_name: "Michael")
|
470
|
+
person.name_attributes = { first_name: "Jack", _destroy: "1" }
|
471
|
+
end
|
472
|
+
|
473
|
+
it "replaces the document" do
|
474
|
+
expect(person.name.first_name).to eq("Jack")
|
475
|
+
end
|
476
|
+
end
|
477
|
+
end
|
478
|
+
end
|
479
|
+
end
|
480
|
+
|
481
|
+
context "when an id is passed" do
|
482
|
+
|
483
|
+
context "when there is an existing record" do
|
484
|
+
|
485
|
+
let(:name) do
|
486
|
+
Name.new(first_name: "Joe")
|
487
|
+
end
|
488
|
+
|
489
|
+
before do
|
490
|
+
person.name = name
|
491
|
+
end
|
492
|
+
|
493
|
+
context "when the id matches" do
|
494
|
+
|
495
|
+
context "when passed keys as symbols" do
|
496
|
+
|
497
|
+
before do
|
498
|
+
person.name_attributes =
|
499
|
+
{ id: name.id.to_s, first_name: "Bob" }
|
500
|
+
end
|
501
|
+
|
502
|
+
it "updates the existing document" do
|
503
|
+
expect(person.name.first_name).to eq("Bob")
|
504
|
+
end
|
505
|
+
end
|
506
|
+
|
507
|
+
context "when passed keys as strings" do
|
508
|
+
|
509
|
+
before do
|
510
|
+
person.name_attributes =
|
511
|
+
{ "id" => name.id.to_s, "first_name" => "Bob" }
|
512
|
+
end
|
513
|
+
|
514
|
+
it "updates the existing document" do
|
515
|
+
expect(person.name.first_name).to eq("Bob")
|
516
|
+
end
|
517
|
+
end
|
518
|
+
|
519
|
+
context "when allow_destroy is true" do
|
520
|
+
|
521
|
+
before(:all) do
|
522
|
+
Person.send(:undef_method, :name_attributes=)
|
523
|
+
Person.accepts_nested_attributes_for :name, allow_destroy: true
|
524
|
+
end
|
525
|
+
|
526
|
+
after(:all) do
|
527
|
+
Person.send(:undef_method, :name_attributes=)
|
528
|
+
Person.accepts_nested_attributes_for :name
|
529
|
+
end
|
530
|
+
|
531
|
+
[ 1, "1", true, "true" ].each do |truth|
|
532
|
+
|
533
|
+
context "when passed #{truth} with destroy" do
|
534
|
+
|
535
|
+
context "when the document has no callbacks" do
|
536
|
+
|
537
|
+
before do
|
538
|
+
person.name_attributes =
|
539
|
+
{ id: name.id, _destroy: truth }
|
540
|
+
end
|
541
|
+
|
542
|
+
it "destroys the existing document" do
|
543
|
+
expect(person.name).to be_nil
|
544
|
+
end
|
545
|
+
end
|
546
|
+
|
547
|
+
context "when the document has destroy callbacks" do
|
548
|
+
|
549
|
+
before(:all) do
|
550
|
+
PetOwner.accepts_nested_attributes_for :pet, allow_destroy: true
|
551
|
+
end
|
552
|
+
|
553
|
+
after(:all) do
|
554
|
+
PetOwner.send(:undef_method, :pet_attributes=)
|
555
|
+
end
|
556
|
+
|
557
|
+
let(:owner) do
|
558
|
+
PetOwner.create
|
559
|
+
end
|
560
|
+
|
561
|
+
let!(:pet) do
|
562
|
+
owner.create_pet
|
563
|
+
end
|
564
|
+
|
565
|
+
before do
|
566
|
+
owner.pet_attributes = { id: pet.id, _destroy: truth }
|
567
|
+
owner.save
|
568
|
+
end
|
569
|
+
|
570
|
+
it "destroys the existing document" do
|
571
|
+
expect(owner.pet).to be_nil
|
572
|
+
end
|
573
|
+
|
574
|
+
it "runs the destroy callbacks" do
|
575
|
+
expect(pet.destroy_flag).to be_true
|
576
|
+
end
|
577
|
+
end
|
578
|
+
end
|
579
|
+
end
|
580
|
+
|
581
|
+
[ nil, 0, "0", false, "false" ].each do |falsehood|
|
582
|
+
|
583
|
+
context "when passed #{falsehood} with destroy" do
|
584
|
+
|
585
|
+
before do
|
586
|
+
person.name_attributes =
|
587
|
+
{ id: name.id, _destroy: falsehood }
|
588
|
+
end
|
589
|
+
|
590
|
+
it "does not destroy the existing document" do
|
591
|
+
expect(person.name).to eq(name)
|
592
|
+
end
|
593
|
+
end
|
594
|
+
end
|
595
|
+
end
|
596
|
+
|
597
|
+
context "when allow destroy is false" do
|
598
|
+
|
599
|
+
before(:all) do
|
600
|
+
Person.send(:undef_method, :name_attributes=)
|
601
|
+
Person.accepts_nested_attributes_for :name, allow_destroy: false
|
602
|
+
end
|
603
|
+
|
604
|
+
after(:all) do
|
605
|
+
Person.send(:undef_method, :name_attributes=)
|
606
|
+
Person.accepts_nested_attributes_for :name
|
607
|
+
end
|
608
|
+
|
609
|
+
context "when a destroy attribute is passed" do
|
610
|
+
|
611
|
+
before do
|
612
|
+
person.name_attributes =
|
613
|
+
{ id: name.id, _destroy: true }
|
614
|
+
end
|
615
|
+
|
616
|
+
it "does not destroy the document" do
|
617
|
+
expect(person.name).to eq(name)
|
618
|
+
end
|
619
|
+
end
|
620
|
+
end
|
621
|
+
|
622
|
+
context "when update only is true" do
|
623
|
+
|
624
|
+
before(:all) do
|
625
|
+
Person.send(:undef_method, :name_attributes=)
|
626
|
+
Person.accepts_nested_attributes_for \
|
627
|
+
:name,
|
628
|
+
update_only: true,
|
629
|
+
allow_destroy: true
|
630
|
+
end
|
631
|
+
|
632
|
+
after(:all) do
|
633
|
+
Person.send(:undef_method, :name_attributes=)
|
634
|
+
Person.accepts_nested_attributes_for :name
|
635
|
+
end
|
636
|
+
|
637
|
+
context "when the id matches" do
|
638
|
+
|
639
|
+
before do
|
640
|
+
person.name_attributes =
|
641
|
+
{ id: name.id, first_name: "Ro" }
|
642
|
+
end
|
643
|
+
|
644
|
+
it "updates the existing document" do
|
645
|
+
expect(person.name.first_name).to eq("Ro")
|
646
|
+
end
|
647
|
+
end
|
648
|
+
|
649
|
+
context "when the id does not match" do
|
650
|
+
|
651
|
+
before do
|
652
|
+
person.name_attributes =
|
653
|
+
{ id: BSON::ObjectId.new.to_s, first_name: "Durran" }
|
654
|
+
end
|
655
|
+
|
656
|
+
it "updates the existing document" do
|
657
|
+
expect(person.name.first_name).to eq("Durran")
|
658
|
+
end
|
659
|
+
end
|
660
|
+
|
661
|
+
context "when passed a destroy truth" do
|
662
|
+
|
663
|
+
before do
|
664
|
+
person.name_attributes =
|
665
|
+
{ id: name.id, _destroy: true }
|
666
|
+
end
|
667
|
+
|
668
|
+
it "destroys the existing document" do
|
669
|
+
expect(person.name).to be_nil
|
670
|
+
end
|
671
|
+
end
|
672
|
+
end
|
673
|
+
|
674
|
+
context "when ids are ObjectId strings" do
|
675
|
+
|
676
|
+
let(:quiz) do
|
677
|
+
person.quiz = Quiz.new(topic: "Math")
|
678
|
+
end
|
679
|
+
|
680
|
+
before do
|
681
|
+
person.quiz_attributes = {
|
682
|
+
"id" => quiz.id.to_s, topic: "English"
|
683
|
+
}
|
684
|
+
end
|
685
|
+
|
686
|
+
it "updates the existing document" do
|
687
|
+
expect(person.quiz.topic).to eq("English")
|
688
|
+
end
|
689
|
+
end
|
690
|
+
end
|
691
|
+
end
|
692
|
+
end
|
693
|
+
|
694
|
+
context "when the nested document is invalid" do
|
695
|
+
|
696
|
+
before(:all) do
|
697
|
+
Person.validates_associated(:pet)
|
698
|
+
end
|
699
|
+
|
700
|
+
after(:all) do
|
701
|
+
Person.reset_callbacks(:validate)
|
702
|
+
end
|
703
|
+
|
704
|
+
before do
|
705
|
+
person.pet_attributes = { name: "$$$" }
|
706
|
+
end
|
707
|
+
|
708
|
+
it "propagates invalidity to parent" do
|
709
|
+
expect(person.pet).to_not be_valid
|
710
|
+
expect(person).to_not be_valid
|
711
|
+
end
|
712
|
+
end
|
713
|
+
|
714
|
+
context "when a type is passed" do
|
715
|
+
|
716
|
+
let(:canvas) do
|
717
|
+
Canvas.new
|
718
|
+
end
|
719
|
+
|
720
|
+
before do
|
721
|
+
Canvas.send(:undef_method, :writer_attributes=)
|
722
|
+
Canvas.accepts_nested_attributes_for :writer
|
723
|
+
canvas.writer_attributes = { _type: "HtmlWriter" }
|
724
|
+
end
|
725
|
+
|
726
|
+
it "instantiates an object of the given type" do
|
727
|
+
expect(canvas.writer.class).to eq(HtmlWriter)
|
728
|
+
end
|
729
|
+
end
|
730
|
+
end
|
731
|
+
|
732
|
+
context "when the relation is embedded in" do
|
733
|
+
|
734
|
+
context "when the child is new" do
|
735
|
+
|
736
|
+
let(:animal) do
|
737
|
+
Animal.new
|
738
|
+
end
|
739
|
+
|
740
|
+
context "when no id has been passed" do
|
741
|
+
|
742
|
+
context "when no destroy attribute passed" do
|
743
|
+
|
744
|
+
before do
|
745
|
+
animal.person_attributes = { title: "Sir" }
|
746
|
+
end
|
747
|
+
|
748
|
+
it "builds a new document" do
|
749
|
+
expect(animal.person.title).to eq("Sir")
|
750
|
+
end
|
751
|
+
|
752
|
+
end
|
753
|
+
|
754
|
+
context "when a destroy attribute is passed" do
|
755
|
+
|
756
|
+
context "when allow_destroy is true" do
|
757
|
+
|
758
|
+
before(:all) do
|
759
|
+
Animal.send(:undef_method, :person_attributes=)
|
760
|
+
Animal.accepts_nested_attributes_for :person, allow_destroy: true
|
761
|
+
end
|
762
|
+
|
763
|
+
after(:all) do
|
764
|
+
Animal.send(:undef_method, :person_attributes=)
|
765
|
+
Animal.accepts_nested_attributes_for :person
|
766
|
+
end
|
767
|
+
|
768
|
+
before do
|
769
|
+
animal.person_attributes = { title: "Sir", _destroy: 1 }
|
770
|
+
end
|
771
|
+
|
772
|
+
it "does not build a new document" do
|
773
|
+
expect(animal.person).to be_nil
|
774
|
+
end
|
775
|
+
end
|
776
|
+
|
777
|
+
context "when allow_destroy is false" do
|
778
|
+
|
779
|
+
before(:all) do
|
780
|
+
Animal.send(:undef_method, :person_attributes=)
|
781
|
+
Animal.accepts_nested_attributes_for :person, allow_destroy: false
|
782
|
+
end
|
783
|
+
|
784
|
+
after(:all) do
|
785
|
+
Animal.send(:undef_method, :person_attributes=)
|
786
|
+
Animal.accepts_nested_attributes_for :person
|
787
|
+
end
|
788
|
+
|
789
|
+
before do
|
790
|
+
animal.person_attributes = { title: "Sir", _destroy: 1 }
|
791
|
+
end
|
792
|
+
|
793
|
+
it "builds a new document" do
|
794
|
+
expect(animal.person.title).to eq("Sir")
|
795
|
+
end
|
796
|
+
end
|
797
|
+
end
|
798
|
+
end
|
799
|
+
|
800
|
+
context "when an id has been passed" do
|
801
|
+
|
802
|
+
let(:person) do
|
803
|
+
Person.new
|
804
|
+
end
|
805
|
+
|
806
|
+
context "when no destroy attribute passed" do
|
807
|
+
|
808
|
+
context "when the id matches" do
|
809
|
+
|
810
|
+
before do
|
811
|
+
animal.person_attributes = { id: person.id, title: "Sir" }
|
812
|
+
end
|
813
|
+
|
814
|
+
it "updates the existing document" do
|
815
|
+
expect(animal.person.title).to eq("Sir")
|
816
|
+
end
|
817
|
+
end
|
818
|
+
end
|
819
|
+
|
820
|
+
context "when there is an existing document" do
|
821
|
+
|
822
|
+
before do
|
823
|
+
animal.person = person
|
824
|
+
end
|
825
|
+
|
826
|
+
context "when allow destroy is true" do
|
827
|
+
|
828
|
+
before(:all) do
|
829
|
+
Animal.send(:undef_method, :person_attributes=)
|
830
|
+
Animal.accepts_nested_attributes_for :person, allow_destroy: true
|
831
|
+
end
|
832
|
+
|
833
|
+
after(:all) do
|
834
|
+
Animal.send(:undef_method, :person_attributes=)
|
835
|
+
Animal.accepts_nested_attributes_for :person
|
836
|
+
end
|
837
|
+
|
838
|
+
[ 1, "1", true, "true" ].each do |truth|
|
839
|
+
|
840
|
+
context "when passed #{truth} with destroy" do
|
841
|
+
|
842
|
+
before do
|
843
|
+
animal.person_attributes =
|
844
|
+
{ id: person.id, _destroy: truth }
|
845
|
+
end
|
846
|
+
|
847
|
+
it "destroys the existing document" do
|
848
|
+
expect(animal.person).to be_nil
|
849
|
+
end
|
850
|
+
end
|
851
|
+
end
|
852
|
+
|
853
|
+
[ nil, 0, "0", false, "false" ].each do |falsehood|
|
854
|
+
|
855
|
+
context "when passed #{falsehood} with destroy" do
|
856
|
+
|
857
|
+
before do
|
858
|
+
animal.person_attributes =
|
859
|
+
{ id: person.id, _destroy: falsehood }
|
860
|
+
end
|
861
|
+
|
862
|
+
it "does not destroy the existing document" do
|
863
|
+
expect(animal.person).to eq(person)
|
864
|
+
end
|
865
|
+
end
|
866
|
+
end
|
867
|
+
end
|
868
|
+
|
869
|
+
context "when allow destroy is false" do
|
870
|
+
|
871
|
+
before(:all) do
|
872
|
+
Animal.send(:undef_method, :person_attributes=)
|
873
|
+
Animal.accepts_nested_attributes_for :person, allow_destroy: false
|
874
|
+
end
|
875
|
+
|
876
|
+
after(:all) do
|
877
|
+
Animal.send(:undef_method, :person_attributes=)
|
878
|
+
Animal.accepts_nested_attributes_for :person
|
879
|
+
end
|
880
|
+
|
881
|
+
context "when a destroy attribute is passed" do
|
882
|
+
|
883
|
+
before do
|
884
|
+
animal.person_attributes =
|
885
|
+
{ id: person.id, _destroy: true }
|
886
|
+
end
|
887
|
+
|
888
|
+
it "does not delete the document" do
|
889
|
+
expect(animal.person).to eq(person)
|
890
|
+
end
|
891
|
+
end
|
892
|
+
end
|
893
|
+
|
894
|
+
context "when update only is true" do
|
895
|
+
|
896
|
+
before do
|
897
|
+
Animal.send(:undef_method, :person_attributes=)
|
898
|
+
Animal.accepts_nested_attributes_for \
|
899
|
+
:person,
|
900
|
+
update_only: true,
|
901
|
+
allow_destroy: true
|
902
|
+
end
|
903
|
+
|
904
|
+
context "when the id matches" do
|
905
|
+
|
906
|
+
before do
|
907
|
+
animal.person_attributes =
|
908
|
+
{ id: person.id, title: "Madam" }
|
909
|
+
end
|
910
|
+
|
911
|
+
it "updates the existing document" do
|
912
|
+
expect(animal.person.title).to eq("Madam")
|
913
|
+
end
|
914
|
+
end
|
915
|
+
|
916
|
+
context "when the id does not match" do
|
917
|
+
|
918
|
+
before do
|
919
|
+
animal.person_attributes =
|
920
|
+
{ id: BSON::ObjectId.new.to_s, title: "Madam" }
|
921
|
+
end
|
922
|
+
|
923
|
+
it "updates the existing document" do
|
924
|
+
expect(animal.person.title).to eq("Madam")
|
925
|
+
end
|
926
|
+
end
|
927
|
+
|
928
|
+
context "when passed a destroy truth" do
|
929
|
+
|
930
|
+
before do
|
931
|
+
animal.person_attributes =
|
932
|
+
{ id: person.id, title: "Madam", _destroy: "true" }
|
933
|
+
end
|
934
|
+
|
935
|
+
it "deletes the existing document" do
|
936
|
+
expect(animal.person).to be_nil
|
937
|
+
end
|
938
|
+
end
|
939
|
+
end
|
940
|
+
end
|
941
|
+
end
|
942
|
+
|
943
|
+
context "when the nested document is invalid" do
|
944
|
+
|
945
|
+
before(:all) do
|
946
|
+
Person.validates_format_of :ssn, without: /\$\$\$/
|
947
|
+
end
|
948
|
+
|
949
|
+
after(:all) do
|
950
|
+
Person.reset_callbacks(:validate)
|
951
|
+
end
|
952
|
+
|
953
|
+
before do
|
954
|
+
animal.person_attributes = { ssn: '$$$' }
|
955
|
+
end
|
956
|
+
|
957
|
+
it "does not propagate invalidity to parent" do
|
958
|
+
expect(animal.person).to_not be_valid
|
959
|
+
expect(animal).to be_valid
|
960
|
+
end
|
961
|
+
end
|
962
|
+
end
|
963
|
+
|
964
|
+
context "when a type is passed" do
|
965
|
+
|
966
|
+
let(:tool) do
|
967
|
+
Tool.new
|
968
|
+
end
|
969
|
+
|
970
|
+
before do
|
971
|
+
tool.palette_attributes ={ _type: "BigPalette" }
|
972
|
+
end
|
973
|
+
|
974
|
+
it "instantiates an object of the given type" do
|
975
|
+
expect(tool.palette.class).to eq(BigPalette)
|
976
|
+
end
|
977
|
+
end
|
978
|
+
end
|
979
|
+
|
980
|
+
context "when the relation is an embeds many" do
|
981
|
+
|
982
|
+
context "when the parent document is persisted" do
|
983
|
+
|
984
|
+
let(:person) do
|
985
|
+
Person.create
|
986
|
+
end
|
987
|
+
|
988
|
+
before do
|
989
|
+
Person.send(:undef_method, :addresses_attributes=)
|
990
|
+
Person.accepts_nested_attributes_for :addresses
|
991
|
+
end
|
992
|
+
|
993
|
+
after do
|
994
|
+
Person.send(:undef_method, :addresses_attributes=)
|
995
|
+
Person.accepts_nested_attributes_for :addresses
|
996
|
+
end
|
997
|
+
|
998
|
+
context "when setting the child attributes" do
|
999
|
+
|
1000
|
+
let(:attributes) do
|
1001
|
+
{ "foo" => { "street" => "Maybachufer" } }
|
1002
|
+
end
|
1003
|
+
|
1004
|
+
before do
|
1005
|
+
person.addresses_attributes = attributes
|
1006
|
+
end
|
1007
|
+
|
1008
|
+
it "sets the child documents" do
|
1009
|
+
expect(person.addresses.first.street).to eq("Maybachufer")
|
1010
|
+
end
|
1011
|
+
|
1012
|
+
it "does not persist the child documents" do
|
1013
|
+
expect(person.addresses.first).to_not be_persisted
|
1014
|
+
end
|
1015
|
+
|
1016
|
+
context "when saving the parent" do
|
1017
|
+
|
1018
|
+
before do
|
1019
|
+
person.save
|
1020
|
+
person.reload
|
1021
|
+
end
|
1022
|
+
|
1023
|
+
it "saves the child documents" do
|
1024
|
+
expect(person.addresses.first).to be_persisted
|
1025
|
+
end
|
1026
|
+
end
|
1027
|
+
end
|
1028
|
+
end
|
1029
|
+
|
1030
|
+
let(:person) do
|
1031
|
+
Person.new
|
1032
|
+
end
|
1033
|
+
|
1034
|
+
let(:address_one) do
|
1035
|
+
Address.new(street: "Unter den Linden")
|
1036
|
+
end
|
1037
|
+
|
1038
|
+
let(:address_two) do
|
1039
|
+
Address.new(street: "Kurfeurstendamm")
|
1040
|
+
end
|
1041
|
+
|
1042
|
+
context "when a limit is specified" do
|
1043
|
+
|
1044
|
+
before(:all) do
|
1045
|
+
Person.send(:undef_method, :addresses_attributes=)
|
1046
|
+
Person.accepts_nested_attributes_for :addresses, limit: 2
|
1047
|
+
end
|
1048
|
+
|
1049
|
+
after(:all) do
|
1050
|
+
Person.send(:undef_method, :addresses_attributes=)
|
1051
|
+
Person.accepts_nested_attributes_for :addresses
|
1052
|
+
end
|
1053
|
+
|
1054
|
+
context "when more are provided than the limit" do
|
1055
|
+
|
1056
|
+
let(:attributes) do
|
1057
|
+
{
|
1058
|
+
"foo" => { "street" => "Maybachufer" },
|
1059
|
+
"bar" => { "street" => "Alexander Platz" },
|
1060
|
+
"baz" => { "street" => "Unter den Linden" }
|
1061
|
+
}
|
1062
|
+
end
|
1063
|
+
|
1064
|
+
it "raises an error" do
|
1065
|
+
expect {
|
1066
|
+
person.addresses_attributes = attributes
|
1067
|
+
}.to raise_error(Mongoid::Errors::TooManyNestedAttributeRecords)
|
1068
|
+
end
|
1069
|
+
end
|
1070
|
+
|
1071
|
+
context "when less are provided than the limit" do
|
1072
|
+
|
1073
|
+
let(:attributes) do
|
1074
|
+
{
|
1075
|
+
"foo" => { "street" => "Maybachufer" },
|
1076
|
+
"bar" => { "street" => "Alexander Platz" }
|
1077
|
+
}
|
1078
|
+
end
|
1079
|
+
|
1080
|
+
before do
|
1081
|
+
person.addresses_attributes = attributes
|
1082
|
+
end
|
1083
|
+
|
1084
|
+
it "sets the documents on the relation" do
|
1085
|
+
expect(person.addresses.size).to eq(2)
|
1086
|
+
end
|
1087
|
+
end
|
1088
|
+
|
1089
|
+
context "when an array of attributes are passed" do
|
1090
|
+
|
1091
|
+
let(:attributes) do
|
1092
|
+
[
|
1093
|
+
{ "street" => "Maybachufer" },
|
1094
|
+
{ "street" => "Alexander Platz" }
|
1095
|
+
]
|
1096
|
+
end
|
1097
|
+
|
1098
|
+
before do
|
1099
|
+
person.addresses_attributes = attributes
|
1100
|
+
end
|
1101
|
+
|
1102
|
+
it "sets the documents on the relation" do
|
1103
|
+
expect(person.addresses.size).to eq(2)
|
1104
|
+
end
|
1105
|
+
end
|
1106
|
+
|
1107
|
+
context "when cascading callbacks" do
|
1108
|
+
|
1109
|
+
before(:all) do
|
1110
|
+
Band.accepts_nested_attributes_for :records
|
1111
|
+
end
|
1112
|
+
|
1113
|
+
after(:all) do
|
1114
|
+
Band.send(:undef_method, :records_attributes=)
|
1115
|
+
end
|
1116
|
+
|
1117
|
+
let(:band) do
|
1118
|
+
Band.new
|
1119
|
+
end
|
1120
|
+
|
1121
|
+
let(:attributes) do
|
1122
|
+
[
|
1123
|
+
{ "name" => "101" },
|
1124
|
+
{ "name" => "Ultra" }
|
1125
|
+
]
|
1126
|
+
end
|
1127
|
+
|
1128
|
+
before do
|
1129
|
+
band.records_attributes = attributes
|
1130
|
+
end
|
1131
|
+
|
1132
|
+
context "when the parent is saved" do
|
1133
|
+
|
1134
|
+
before do
|
1135
|
+
band.save
|
1136
|
+
end
|
1137
|
+
|
1138
|
+
it "runs the first child create callbacks" do
|
1139
|
+
expect(band.records.first.before_create_called).to be_true
|
1140
|
+
end
|
1141
|
+
|
1142
|
+
it "runs the last child create callbacks" do
|
1143
|
+
expect(band.records.last.before_create_called).to be_true
|
1144
|
+
end
|
1145
|
+
end
|
1146
|
+
end
|
1147
|
+
end
|
1148
|
+
|
1149
|
+
context "when ids are passed" do
|
1150
|
+
|
1151
|
+
before do
|
1152
|
+
person.addresses << [ address_one, address_two ]
|
1153
|
+
end
|
1154
|
+
|
1155
|
+
context "when no destroy attributes are passed" do
|
1156
|
+
|
1157
|
+
context "when the ids match" do
|
1158
|
+
|
1159
|
+
before do
|
1160
|
+
person.addresses_attributes =
|
1161
|
+
{
|
1162
|
+
"foo" => { "id" => address_one.id, "street" => "Maybachufer" },
|
1163
|
+
"bar" => { "id" => address_two.id, "street" => "Alexander Platz" }
|
1164
|
+
}
|
1165
|
+
end
|
1166
|
+
|
1167
|
+
it "updates the first existing document" do
|
1168
|
+
expect(person.addresses.first.street).to eq("Maybachufer")
|
1169
|
+
end
|
1170
|
+
|
1171
|
+
it "updates the second existing document" do
|
1172
|
+
expect(person.addresses.second.street).to eq("Alexander Platz")
|
1173
|
+
end
|
1174
|
+
|
1175
|
+
it "does not add new documents" do
|
1176
|
+
expect(person.addresses.size).to eq(2)
|
1177
|
+
end
|
1178
|
+
end
|
1179
|
+
|
1180
|
+
context "when the ids match in an array of attributes" do
|
1181
|
+
|
1182
|
+
context "when passing in id" do
|
1183
|
+
|
1184
|
+
before do
|
1185
|
+
person.addresses_attributes =
|
1186
|
+
[
|
1187
|
+
{ "id" => address_one.id, "street" => "Maybachufer" },
|
1188
|
+
{ "id" => address_two.id, "street" => "Alexander Platz" }
|
1189
|
+
]
|
1190
|
+
end
|
1191
|
+
|
1192
|
+
it "updates the first existing document" do
|
1193
|
+
expect(person.addresses.collect { |a| a['street'] }).to include('Maybachufer')
|
1194
|
+
end
|
1195
|
+
|
1196
|
+
it "updates the second existing document" do
|
1197
|
+
expect(person.addresses.collect { |a| a['street'] }).to include('Alexander Platz')
|
1198
|
+
end
|
1199
|
+
|
1200
|
+
it "does not add new documents" do
|
1201
|
+
expect(person.addresses.size).to eq(2)
|
1202
|
+
end
|
1203
|
+
end
|
1204
|
+
|
1205
|
+
context "when passing in _id" do
|
1206
|
+
|
1207
|
+
before do
|
1208
|
+
person.addresses_attributes =
|
1209
|
+
[
|
1210
|
+
{ "_id" => address_one.id, "street" => "Maybachufer" },
|
1211
|
+
{ "_id" => address_two.id, "street" => "Alexander Platz" }
|
1212
|
+
]
|
1213
|
+
end
|
1214
|
+
|
1215
|
+
it "updates the first existing document" do
|
1216
|
+
expect(person.addresses.collect { |a| a['street'] }).to include('Maybachufer')
|
1217
|
+
end
|
1218
|
+
|
1219
|
+
it "updates the second existing document" do
|
1220
|
+
expect(person.addresses.collect { |a| a['street'] }).to include('Alexander Platz')
|
1221
|
+
end
|
1222
|
+
|
1223
|
+
it "does not add new documents" do
|
1224
|
+
expect(person.addresses.size).to eq(2)
|
1225
|
+
end
|
1226
|
+
end
|
1227
|
+
end
|
1228
|
+
|
1229
|
+
context "when the ids match in an array of attributes and start with '_'" do
|
1230
|
+
|
1231
|
+
before do
|
1232
|
+
person.addresses_attributes =
|
1233
|
+
[
|
1234
|
+
{ "_id" => address_one.id, "street" => "Maybachufer" },
|
1235
|
+
{ "_id" => address_two.id, "street" => "Alexander Platz" }
|
1236
|
+
]
|
1237
|
+
end
|
1238
|
+
|
1239
|
+
it "updates the first existing document" do
|
1240
|
+
expect(person.addresses.collect { |a| a['street'] }).to include('Maybachufer')
|
1241
|
+
end
|
1242
|
+
|
1243
|
+
it "updates the second existing document" do
|
1244
|
+
expect(person.addresses.collect { |a| a['street'] }).to include('Alexander Platz')
|
1245
|
+
end
|
1246
|
+
|
1247
|
+
it "does not add new documents" do
|
1248
|
+
expect(person.addresses.size).to eq(2)
|
1249
|
+
end
|
1250
|
+
end
|
1251
|
+
|
1252
|
+
context "when the ids are _id symbols" do
|
1253
|
+
|
1254
|
+
before do
|
1255
|
+
person.addresses_attributes =
|
1256
|
+
[
|
1257
|
+
{ _id: address_one.id, "street" => "Maybachufer" },
|
1258
|
+
{ _id: address_two.id, "street" => "Alexander Platz" }
|
1259
|
+
]
|
1260
|
+
end
|
1261
|
+
|
1262
|
+
it "updates the first existing document" do
|
1263
|
+
expect(person.addresses.collect { |a| a['street'] }).to include('Maybachufer')
|
1264
|
+
end
|
1265
|
+
|
1266
|
+
it "updates the second existing document" do
|
1267
|
+
expect(person.addresses.collect { |a| a['street'] }).to include('Alexander Platz')
|
1268
|
+
end
|
1269
|
+
|
1270
|
+
it "does not add new documents" do
|
1271
|
+
expect(person.addresses.size).to eq(2)
|
1272
|
+
end
|
1273
|
+
end
|
1274
|
+
|
1275
|
+
context "when the ids are id symbols" do
|
1276
|
+
|
1277
|
+
before do
|
1278
|
+
person.addresses_attributes =
|
1279
|
+
[
|
1280
|
+
{ id: address_one.id, "street" => "Maybachufer" },
|
1281
|
+
{ id: address_two.id, "street" => "Alexander Platz" }
|
1282
|
+
]
|
1283
|
+
end
|
1284
|
+
|
1285
|
+
it "updates the first existing document" do
|
1286
|
+
expect(person.addresses.collect { |a| a['street'] }).to include('Maybachufer')
|
1287
|
+
end
|
1288
|
+
|
1289
|
+
it "updates the second existing document" do
|
1290
|
+
expect(person.addresses.collect { |a| a['street'] }).to include('Alexander Platz')
|
1291
|
+
end
|
1292
|
+
|
1293
|
+
it "does not add new documents" do
|
1294
|
+
expect(person.addresses.size).to eq(2)
|
1295
|
+
end
|
1296
|
+
end
|
1297
|
+
|
1298
|
+
context "when the ids do not match" do
|
1299
|
+
|
1300
|
+
it "raises an error" do
|
1301
|
+
expect {
|
1302
|
+
person.addresses_attributes =
|
1303
|
+
{ "foo" => { "id" => "test", "street" => "Test" } }
|
1304
|
+
}.to raise_error
|
1305
|
+
end
|
1306
|
+
end
|
1307
|
+
end
|
1308
|
+
|
1309
|
+
context "when destroy attributes are passed" do
|
1310
|
+
|
1311
|
+
context "when the ids match" do
|
1312
|
+
|
1313
|
+
context "when allow_destroy is true" do
|
1314
|
+
|
1315
|
+
context "when the parent validation failed" do
|
1316
|
+
|
1317
|
+
before(:all) do
|
1318
|
+
Band.validates_presence_of :name
|
1319
|
+
Band.accepts_nested_attributes_for :records, :allow_destroy => true
|
1320
|
+
end
|
1321
|
+
|
1322
|
+
after(:all) do
|
1323
|
+
Band.send(:undef_method, :records_attributes=)
|
1324
|
+
Band.reset_callbacks(:validate)
|
1325
|
+
end
|
1326
|
+
|
1327
|
+
let!(:band) do
|
1328
|
+
Band.create(name: "Depeche Mode")
|
1329
|
+
end
|
1330
|
+
|
1331
|
+
let!(:record) do
|
1332
|
+
band.records.create
|
1333
|
+
end
|
1334
|
+
|
1335
|
+
let(:attributes) do
|
1336
|
+
{
|
1337
|
+
name: nil,
|
1338
|
+
records_attributes: { "foo" => { "id" => record.id, "_destroy" => true }}
|
1339
|
+
}
|
1340
|
+
end
|
1341
|
+
|
1342
|
+
before do
|
1343
|
+
band.update_attributes(attributes)
|
1344
|
+
end
|
1345
|
+
|
1346
|
+
it "does not remove the child document" do
|
1347
|
+
expect(band.records).to_not be_empty
|
1348
|
+
end
|
1349
|
+
|
1350
|
+
it "keeps the child flagged for destruction" do
|
1351
|
+
expect(record).to be_flagged_for_destroy
|
1352
|
+
end
|
1353
|
+
|
1354
|
+
it "does not persist any change" do
|
1355
|
+
expect(band.reload.records).to eq([ record ])
|
1356
|
+
end
|
1357
|
+
end
|
1358
|
+
|
1359
|
+
context "when the child accesses the parent after destroy" do
|
1360
|
+
|
1361
|
+
before(:all) do
|
1362
|
+
Band.accepts_nested_attributes_for :records, :allow_destroy => true
|
1363
|
+
end
|
1364
|
+
|
1365
|
+
after(:all) do
|
1366
|
+
Band.send(:undef_method, :records_attributes=)
|
1367
|
+
end
|
1368
|
+
|
1369
|
+
let!(:band) do
|
1370
|
+
Band.create
|
1371
|
+
end
|
1372
|
+
|
1373
|
+
let!(:record) do
|
1374
|
+
band.records.create
|
1375
|
+
end
|
1376
|
+
|
1377
|
+
before do
|
1378
|
+
band.records_attributes =
|
1379
|
+
{ "foo" => { "id" => record.id, "_destroy" => true }}
|
1380
|
+
band.save
|
1381
|
+
end
|
1382
|
+
|
1383
|
+
it "deletes the child document" do
|
1384
|
+
expect(band.records).to be_empty
|
1385
|
+
end
|
1386
|
+
|
1387
|
+
it "persists the changes" do
|
1388
|
+
expect(band.reload.records).to be_empty
|
1389
|
+
end
|
1390
|
+
end
|
1391
|
+
|
1392
|
+
context "when the child has defaults" do
|
1393
|
+
|
1394
|
+
before(:all) do
|
1395
|
+
Person.accepts_nested_attributes_for :appointments, allow_destroy: true
|
1396
|
+
end
|
1397
|
+
|
1398
|
+
after(:all) do
|
1399
|
+
Person.send(:undef_method, :appointments_attributes=)
|
1400
|
+
end
|
1401
|
+
|
1402
|
+
context "when the parent is persisted" do
|
1403
|
+
|
1404
|
+
let!(:persisted) do
|
1405
|
+
Person.create(age: 42)
|
1406
|
+
end
|
1407
|
+
|
1408
|
+
context "when the child returns false in a before callback" do
|
1409
|
+
|
1410
|
+
context "when the child is not paranoid" do
|
1411
|
+
|
1412
|
+
let(:actor) do
|
1413
|
+
Actor.create
|
1414
|
+
end
|
1415
|
+
|
1416
|
+
let!(:thing) do
|
1417
|
+
actor.things.create
|
1418
|
+
end
|
1419
|
+
|
1420
|
+
before do
|
1421
|
+
actor.things_attributes =
|
1422
|
+
{ "foo" => { "id" => thing.id, "_destroy" => true }}
|
1423
|
+
actor.save
|
1424
|
+
end
|
1425
|
+
|
1426
|
+
it "does not destroy the child" do
|
1427
|
+
expect(actor.reload.things).to_not be_empty
|
1428
|
+
end
|
1429
|
+
end
|
1430
|
+
end
|
1431
|
+
|
1432
|
+
context "when only 1 child has the default persisted" do
|
1433
|
+
|
1434
|
+
let!(:app_one) do
|
1435
|
+
persisted.appointments.create
|
1436
|
+
end
|
1437
|
+
|
1438
|
+
let!(:app_two) do
|
1439
|
+
persisted.appointments.create.tap do |app|
|
1440
|
+
app.unset(:timed)
|
1441
|
+
end
|
1442
|
+
end
|
1443
|
+
|
1444
|
+
context "when destroying both children" do
|
1445
|
+
|
1446
|
+
let(:from_db) do
|
1447
|
+
Person.find(persisted.id)
|
1448
|
+
end
|
1449
|
+
|
1450
|
+
before do
|
1451
|
+
from_db.appointments_attributes =
|
1452
|
+
{
|
1453
|
+
"bar" => { "id" => app_one.id, "_destroy" => true },
|
1454
|
+
"foo" => { "id" => app_two.id, "_destroy" => true }
|
1455
|
+
}
|
1456
|
+
from_db.save
|
1457
|
+
end
|
1458
|
+
|
1459
|
+
it "destroys both children" do
|
1460
|
+
expect(from_db.appointments).to be_empty
|
1461
|
+
end
|
1462
|
+
|
1463
|
+
it "persists the deletes" do
|
1464
|
+
expect(from_db.reload.appointments).to be_empty
|
1465
|
+
end
|
1466
|
+
end
|
1467
|
+
end
|
1468
|
+
end
|
1469
|
+
end
|
1470
|
+
|
1471
|
+
context "when the child is not paranoid" do
|
1472
|
+
|
1473
|
+
before(:all) do
|
1474
|
+
Person.send(:undef_method, :addresses_attributes=)
|
1475
|
+
Person.accepts_nested_attributes_for :addresses, allow_destroy: true
|
1476
|
+
end
|
1477
|
+
|
1478
|
+
after(:all) do
|
1479
|
+
Person.send(:undef_method, :addresses_attributes=)
|
1480
|
+
Person.accepts_nested_attributes_for :addresses
|
1481
|
+
end
|
1482
|
+
|
1483
|
+
[ 1, "1", true, "true" ].each do |truth|
|
1484
|
+
|
1485
|
+
context "when passed a #{truth} with destroy" do
|
1486
|
+
|
1487
|
+
context "when the parent is new" do
|
1488
|
+
|
1489
|
+
context "when provided a hash of attributes" do
|
1490
|
+
|
1491
|
+
before do
|
1492
|
+
person.addresses_attributes =
|
1493
|
+
{
|
1494
|
+
"bar" => { "id" => address_one.id.to_s, "_destroy" => truth },
|
1495
|
+
"foo" => { "id" => address_two.id, "street" => "Alexander Platz" }
|
1496
|
+
}
|
1497
|
+
end
|
1498
|
+
|
1499
|
+
it "deletes the marked document" do
|
1500
|
+
expect(person.addresses.size).to eq(1)
|
1501
|
+
end
|
1502
|
+
|
1503
|
+
it "does not delete the unmarked document" do
|
1504
|
+
expect(person.addresses.first.street).to eq("Alexander Platz")
|
1505
|
+
end
|
1506
|
+
end
|
1507
|
+
|
1508
|
+
context "when provided an array of attributes" do
|
1509
|
+
|
1510
|
+
before do
|
1511
|
+
person.addresses_attributes =
|
1512
|
+
[
|
1513
|
+
{ "id" => address_one.id.to_s, "_destroy" => truth },
|
1514
|
+
{ "id" => address_two.id, "street" => "Alexander Platz" }
|
1515
|
+
]
|
1516
|
+
end
|
1517
|
+
|
1518
|
+
it "deletes the marked document" do
|
1519
|
+
expect(person.addresses.size).to eq(1)
|
1520
|
+
end
|
1521
|
+
|
1522
|
+
it "does not delete the unmarked document" do
|
1523
|
+
expect(person.addresses.first.street).to eq("Alexander Platz")
|
1524
|
+
end
|
1525
|
+
end
|
1526
|
+
end
|
1527
|
+
|
1528
|
+
context "when the parent is persisted" do
|
1529
|
+
|
1530
|
+
let!(:persisted) do
|
1531
|
+
Person.create do |p|
|
1532
|
+
p.addresses << [ address_one, address_two ]
|
1533
|
+
end
|
1534
|
+
end
|
1535
|
+
|
1536
|
+
context "when setting, pulling, and pushing in one op" do
|
1537
|
+
|
1538
|
+
before do
|
1539
|
+
persisted.addresses_attributes =
|
1540
|
+
{
|
1541
|
+
"bar" => { "id" => address_one.id, "_destroy" => truth },
|
1542
|
+
"foo" => { "id" => address_two.id, "street" => "Alexander Platz" },
|
1543
|
+
"baz" => { "street" => "Potsdammer Platz" }
|
1544
|
+
}
|
1545
|
+
end
|
1546
|
+
|
1547
|
+
it "does not remove the first document from the relation" do
|
1548
|
+
expect(persisted.addresses.size).to eq(3)
|
1549
|
+
end
|
1550
|
+
|
1551
|
+
it "flags the destroyed document for removal" do
|
1552
|
+
expect(address_one).to be_marked_for_destruction
|
1553
|
+
end
|
1554
|
+
|
1555
|
+
it "does not delete the unmarked document" do
|
1556
|
+
expect(persisted.addresses.second.street).to eq(
|
1557
|
+
"Alexander Platz"
|
1558
|
+
)
|
1559
|
+
end
|
1560
|
+
|
1561
|
+
it "adds the new document to the relation" do
|
1562
|
+
expect(persisted.addresses.last.street).to eq(
|
1563
|
+
"Potsdammer Platz"
|
1564
|
+
)
|
1565
|
+
end
|
1566
|
+
|
1567
|
+
it "has the proper persisted count" do
|
1568
|
+
expect(persisted.addresses.count).to eq(2)
|
1569
|
+
end
|
1570
|
+
|
1571
|
+
it "does not delete the removed document" do
|
1572
|
+
expect(address_one).to_not be_destroyed
|
1573
|
+
end
|
1574
|
+
|
1575
|
+
context "when saving the parent" do
|
1576
|
+
|
1577
|
+
before do
|
1578
|
+
persisted.save
|
1579
|
+
end
|
1580
|
+
|
1581
|
+
it "deletes the marked document from the relation" do
|
1582
|
+
expect(persisted.reload.addresses.count).to eq(2)
|
1583
|
+
end
|
1584
|
+
|
1585
|
+
it "does not delete the unmarked document" do
|
1586
|
+
expect(persisted.reload.addresses.first.street).to eq(
|
1587
|
+
"Alexander Platz"
|
1588
|
+
)
|
1589
|
+
end
|
1590
|
+
|
1591
|
+
it "persists the new document to the relation" do
|
1592
|
+
expect(persisted.reload.addresses.last.street).to eq(
|
1593
|
+
"Potsdammer Platz"
|
1594
|
+
)
|
1595
|
+
end
|
1596
|
+
end
|
1597
|
+
end
|
1598
|
+
|
1599
|
+
context "when pulling and pushing in one op" do
|
1600
|
+
|
1601
|
+
before do
|
1602
|
+
persisted.addresses_attributes =
|
1603
|
+
{
|
1604
|
+
"bar" => { "id" => address_one.id, "_destroy" => truth },
|
1605
|
+
"baz" => { "street" => "Potsdammer Platz" }
|
1606
|
+
}
|
1607
|
+
end
|
1608
|
+
|
1609
|
+
it "does not remove the first document from the relation" do
|
1610
|
+
expect(persisted.addresses.size).to eq(3)
|
1611
|
+
end
|
1612
|
+
|
1613
|
+
it "marks the first document for destruction" do
|
1614
|
+
expect(address_one).to be_marked_for_destruction
|
1615
|
+
end
|
1616
|
+
|
1617
|
+
it "adds the new document to the relation" do
|
1618
|
+
expect(persisted.addresses.last.street).to eq(
|
1619
|
+
"Potsdammer Platz"
|
1620
|
+
)
|
1621
|
+
end
|
1622
|
+
|
1623
|
+
it "has the proper persisted count" do
|
1624
|
+
expect(persisted.addresses.count).to eq(2)
|
1625
|
+
end
|
1626
|
+
|
1627
|
+
it "does not delete the removed document" do
|
1628
|
+
expect(address_one).to_not be_destroyed
|
1629
|
+
end
|
1630
|
+
|
1631
|
+
context "when saving the parent" do
|
1632
|
+
|
1633
|
+
before do
|
1634
|
+
persisted.save
|
1635
|
+
end
|
1636
|
+
|
1637
|
+
it "deletes the marked document from the relation" do
|
1638
|
+
expect(persisted.reload.addresses.count).to eq(2)
|
1639
|
+
end
|
1640
|
+
|
1641
|
+
it "persists the new document to the relation" do
|
1642
|
+
expect(persisted.reload.addresses.last.street).to eq(
|
1643
|
+
"Potsdammer Platz"
|
1644
|
+
)
|
1645
|
+
end
|
1646
|
+
end
|
1647
|
+
end
|
1648
|
+
end
|
1649
|
+
end
|
1650
|
+
end
|
1651
|
+
|
1652
|
+
[ 0, "0", false, "false" ].each do |falsehood|
|
1653
|
+
|
1654
|
+
context "when passed a #{falsehood} with destroy" do
|
1655
|
+
|
1656
|
+
before do
|
1657
|
+
person.addresses_attributes =
|
1658
|
+
{
|
1659
|
+
"bar" => { "id" => address_one.id, "_destroy" => falsehood },
|
1660
|
+
"foo" => { "id" => address_two.id, "street" => "Alexander Platz" }
|
1661
|
+
}
|
1662
|
+
end
|
1663
|
+
|
1664
|
+
it "does not delete the marked document" do
|
1665
|
+
expect(person.addresses.size).to eq(2)
|
1666
|
+
end
|
1667
|
+
|
1668
|
+
it "does not delete the unmarked document" do
|
1669
|
+
expect(person.addresses.last.street).to eq("Alexander Platz")
|
1670
|
+
end
|
1671
|
+
end
|
1672
|
+
end
|
1673
|
+
end
|
1674
|
+
end
|
1675
|
+
|
1676
|
+
context "when allow_destroy is false" do
|
1677
|
+
|
1678
|
+
before(:all) do
|
1679
|
+
Person.send(:undef_method, :addresses_attributes=)
|
1680
|
+
Person.accepts_nested_attributes_for :addresses, allow_destroy: false
|
1681
|
+
end
|
1682
|
+
|
1683
|
+
after(:all) do
|
1684
|
+
Person.send(:undef_method, :addresses_attributes=)
|
1685
|
+
Person.accepts_nested_attributes_for :addresses
|
1686
|
+
end
|
1687
|
+
|
1688
|
+
[ 1, "1", true, "true" ].each do |truth|
|
1689
|
+
|
1690
|
+
context "when passed a #{truth} with destroy" do
|
1691
|
+
|
1692
|
+
before do
|
1693
|
+
person.addresses_attributes =
|
1694
|
+
{
|
1695
|
+
"bar" => {
|
1696
|
+
"id" => address_one.id, "street" => "Maybachufer", "_destroy" => truth },
|
1697
|
+
"foo" => { "id" => address_two.id, "street" => "Alexander Platz" }
|
1698
|
+
}
|
1699
|
+
end
|
1700
|
+
|
1701
|
+
it "does not ignore the marked document" do
|
1702
|
+
expect(person.addresses.first.street).to eq("Maybachufer")
|
1703
|
+
end
|
1704
|
+
|
1705
|
+
it "does not delete the unmarked document" do
|
1706
|
+
expect(person.addresses.last.street).to eq("Alexander Platz")
|
1707
|
+
end
|
1708
|
+
|
1709
|
+
it "does not add additional documents" do
|
1710
|
+
expect(person.addresses.size).to eq(2)
|
1711
|
+
end
|
1712
|
+
end
|
1713
|
+
end
|
1714
|
+
|
1715
|
+
[ 0, "0", false, "false" ].each do |falsehood|
|
1716
|
+
|
1717
|
+
context "when passed a #{falsehood} with destroy" do
|
1718
|
+
|
1719
|
+
before do
|
1720
|
+
person.addresses_attributes =
|
1721
|
+
{
|
1722
|
+
"bar" => { "id" => address_one.id, "_destroy" => falsehood },
|
1723
|
+
"foo" => { "id" => address_two.id, "street" => "Alexander Platz" }
|
1724
|
+
}
|
1725
|
+
end
|
1726
|
+
|
1727
|
+
it "does not delete the marked document" do
|
1728
|
+
expect(person.addresses.size).to eq(2)
|
1729
|
+
end
|
1730
|
+
|
1731
|
+
it "does not delete the unmarked document" do
|
1732
|
+
expect(person.addresses.last.street).to eq("Alexander Platz")
|
1733
|
+
end
|
1734
|
+
end
|
1735
|
+
end
|
1736
|
+
end
|
1737
|
+
|
1738
|
+
context "when allow_destroy is undefined" do
|
1739
|
+
|
1740
|
+
before(:all) do
|
1741
|
+
Person.send(:undef_method, :addresses_attributes=)
|
1742
|
+
Person.accepts_nested_attributes_for :addresses
|
1743
|
+
end
|
1744
|
+
|
1745
|
+
[ 1, "1", true, "true" ].each do |truth|
|
1746
|
+
|
1747
|
+
context "when passed a #{truth} with destroy" do
|
1748
|
+
|
1749
|
+
before do
|
1750
|
+
person.addresses_attributes =
|
1751
|
+
{
|
1752
|
+
"bar" => {
|
1753
|
+
"id" => address_one.id, "street" => "Maybachufer", "_destroy" => truth },
|
1754
|
+
"foo" => { "id" => address_two.id, "street" => "Alexander Platz" }
|
1755
|
+
}
|
1756
|
+
end
|
1757
|
+
|
1758
|
+
it "does not ignore the marked document" do
|
1759
|
+
expect(person.addresses.first.street).to eq("Maybachufer")
|
1760
|
+
end
|
1761
|
+
|
1762
|
+
it "does not delete the unmarked document" do
|
1763
|
+
expect(person.addresses.last.street).to eq("Alexander Platz")
|
1764
|
+
end
|
1765
|
+
|
1766
|
+
it "does not add additional documents" do
|
1767
|
+
expect(person.addresses.size).to eq(2)
|
1768
|
+
end
|
1769
|
+
end
|
1770
|
+
end
|
1771
|
+
|
1772
|
+
[ 0, "0", false, "false" ].each do |falsehood|
|
1773
|
+
|
1774
|
+
context "when passed a #{falsehood} with destroy" do
|
1775
|
+
|
1776
|
+
before do
|
1777
|
+
person.addresses_attributes =
|
1778
|
+
{
|
1779
|
+
"bar" => { "id" => address_one.id, "_destroy" => falsehood },
|
1780
|
+
"foo" => { "id" => address_two.id, "street" => "Alexander Platz" }
|
1781
|
+
}
|
1782
|
+
end
|
1783
|
+
|
1784
|
+
it "does not delete the marked document" do
|
1785
|
+
expect(person.addresses.size).to eq(2)
|
1786
|
+
end
|
1787
|
+
|
1788
|
+
it "does not delete the unmarked document" do
|
1789
|
+
expect(person.addresses.last.street).to eq("Alexander Platz")
|
1790
|
+
end
|
1791
|
+
end
|
1792
|
+
end
|
1793
|
+
end
|
1794
|
+
end
|
1795
|
+
end
|
1796
|
+
end
|
1797
|
+
|
1798
|
+
context "when no ids are passed" do
|
1799
|
+
|
1800
|
+
context "when no destroy attributes are passed" do
|
1801
|
+
|
1802
|
+
before do
|
1803
|
+
person.addresses_attributes =
|
1804
|
+
{
|
1805
|
+
"4" => { "street" => "Maybachufer" },
|
1806
|
+
"1" => { "street" => "Frederichstrasse" },
|
1807
|
+
"2" => { "street" => "Alexander Platz" }
|
1808
|
+
}
|
1809
|
+
end
|
1810
|
+
|
1811
|
+
it "builds a new first document" do
|
1812
|
+
expect(person.addresses.first.street).to eq("Frederichstrasse")
|
1813
|
+
end
|
1814
|
+
|
1815
|
+
it "builds a new second document" do
|
1816
|
+
expect(person.addresses.second.street).to eq("Alexander Platz")
|
1817
|
+
end
|
1818
|
+
|
1819
|
+
it "builds a new third document" do
|
1820
|
+
expect(person.addresses.third.street).to eq("Maybachufer")
|
1821
|
+
end
|
1822
|
+
|
1823
|
+
it "does not add extra documents" do
|
1824
|
+
expect(person.addresses.size).to eq(3)
|
1825
|
+
end
|
1826
|
+
|
1827
|
+
it "adds the documents in the sorted hash key order" do
|
1828
|
+
expect(person.addresses.map(&:street)).to eq(
|
1829
|
+
[ "Frederichstrasse", "Alexander Platz", "Maybachufer" ]
|
1830
|
+
)
|
1831
|
+
end
|
1832
|
+
end
|
1833
|
+
|
1834
|
+
context "when a reject block is supplied" do
|
1835
|
+
|
1836
|
+
before(:all) do
|
1837
|
+
Person.send(:undef_method, :addresses_attributes=)
|
1838
|
+
Person.accepts_nested_attributes_for \
|
1839
|
+
:addresses, reject_if: ->(attrs){ attrs["street"].blank? }
|
1840
|
+
end
|
1841
|
+
|
1842
|
+
after(:all) do
|
1843
|
+
Person.send(:undef_method, :addresses_attributes=)
|
1844
|
+
Person.accepts_nested_attributes_for :addresses
|
1845
|
+
end
|
1846
|
+
|
1847
|
+
context "when the attributes match" do
|
1848
|
+
|
1849
|
+
before do
|
1850
|
+
person.addresses_attributes =
|
1851
|
+
{ "3" => { "city" => "Berlin" } }
|
1852
|
+
end
|
1853
|
+
|
1854
|
+
it "does not add the new document" do
|
1855
|
+
expect(person.addresses).to be_empty
|
1856
|
+
end
|
1857
|
+
end
|
1858
|
+
|
1859
|
+
context "when the attributes do not match" do
|
1860
|
+
|
1861
|
+
before do
|
1862
|
+
person.addresses_attributes =
|
1863
|
+
{ "3" => { "street" => "Maybachufer" } }
|
1864
|
+
end
|
1865
|
+
|
1866
|
+
it "adds the new document" do
|
1867
|
+
expect(person.addresses.size).to eq(1)
|
1868
|
+
end
|
1869
|
+
|
1870
|
+
it "sets the correct attributes" do
|
1871
|
+
expect(person.addresses.first.street).to eq("Maybachufer")
|
1872
|
+
end
|
1873
|
+
end
|
1874
|
+
end
|
1875
|
+
|
1876
|
+
context "when :reject_if => :all_blank is supplied" do
|
1877
|
+
|
1878
|
+
before(:all) do
|
1879
|
+
Person.send(:undef_method, :addresses_attributes=)
|
1880
|
+
Person.accepts_nested_attributes_for \
|
1881
|
+
:addresses, reject_if: :all_blank
|
1882
|
+
end
|
1883
|
+
|
1884
|
+
after(:all) do
|
1885
|
+
Person.send(:undef_method, :addresses_attributes=)
|
1886
|
+
Person.accepts_nested_attributes_for :addresses
|
1887
|
+
end
|
1888
|
+
|
1889
|
+
context "when all attributes are empty" do
|
1890
|
+
|
1891
|
+
before do
|
1892
|
+
person.addresses_attributes =
|
1893
|
+
{ "3" => { "city" => "" } }
|
1894
|
+
end
|
1895
|
+
|
1896
|
+
it "does not add the new document" do
|
1897
|
+
expect(person.addresses).to be_empty
|
1898
|
+
end
|
1899
|
+
end
|
1900
|
+
|
1901
|
+
context "when an attribute is not-empty" do
|
1902
|
+
|
1903
|
+
before do
|
1904
|
+
person.addresses_attributes =
|
1905
|
+
{ "3" => { "street" => "Maybachufer" } }
|
1906
|
+
end
|
1907
|
+
|
1908
|
+
it "adds the new document" do
|
1909
|
+
expect(person.addresses.size).to eq(1)
|
1910
|
+
end
|
1911
|
+
|
1912
|
+
it "sets the correct attributes" do
|
1913
|
+
expect(person.addresses.first.street).to eq("Maybachufer")
|
1914
|
+
end
|
1915
|
+
end
|
1916
|
+
end
|
1917
|
+
|
1918
|
+
context "when destroy attributes are passed" do
|
1919
|
+
|
1920
|
+
context "when allow_destroy is true" do
|
1921
|
+
|
1922
|
+
before(:all) do
|
1923
|
+
Person.send(:undef_method, :addresses_attributes=)
|
1924
|
+
Person.accepts_nested_attributes_for :addresses, allow_destroy: true
|
1925
|
+
end
|
1926
|
+
|
1927
|
+
after(:all) do
|
1928
|
+
Person.send(:undef_method, :addresses_attributes=)
|
1929
|
+
Person.accepts_nested_attributes_for :addresses
|
1930
|
+
end
|
1931
|
+
|
1932
|
+
[ 1, "1", true, "true" ].each do |truth|
|
1933
|
+
|
1934
|
+
context "when passed a #{truth} with destroy" do
|
1935
|
+
|
1936
|
+
before do
|
1937
|
+
person.addresses_attributes =
|
1938
|
+
{
|
1939
|
+
"bar" => { "street" => "Maybachufer", "_destroy" => truth },
|
1940
|
+
"foo" => { "street" => "Alexander Platz" }
|
1941
|
+
}
|
1942
|
+
end
|
1943
|
+
|
1944
|
+
it "ignores the the marked document" do
|
1945
|
+
expect(person.addresses.size).to eq(1)
|
1946
|
+
end
|
1947
|
+
|
1948
|
+
it "adds the new unmarked document" do
|
1949
|
+
expect(person.addresses.first.street).to eq("Alexander Platz")
|
1950
|
+
end
|
1951
|
+
end
|
1952
|
+
end
|
1953
|
+
|
1954
|
+
[ 0, "0", false, "false" ].each do |falsehood|
|
1955
|
+
|
1956
|
+
context "when passed a #{falsehood} with destroy" do
|
1957
|
+
|
1958
|
+
before do
|
1959
|
+
person.addresses_attributes =
|
1960
|
+
{
|
1961
|
+
"0" => { "street" => "Maybachufer", "_destroy" => falsehood },
|
1962
|
+
"1" => { "street" => "Alexander Platz" }
|
1963
|
+
}
|
1964
|
+
end
|
1965
|
+
|
1966
|
+
it "adds the new marked document" do
|
1967
|
+
expect(person.addresses.first.street).to eq("Maybachufer")
|
1968
|
+
end
|
1969
|
+
|
1970
|
+
it "adds the new unmarked document" do
|
1971
|
+
expect(person.addresses.last.street).to eq("Alexander Platz")
|
1972
|
+
end
|
1973
|
+
|
1974
|
+
it "does not add extra documents" do
|
1975
|
+
expect(person.addresses.size).to eq(2)
|
1976
|
+
end
|
1977
|
+
end
|
1978
|
+
end
|
1979
|
+
end
|
1980
|
+
|
1981
|
+
context "when allow destroy is false" do
|
1982
|
+
|
1983
|
+
before(:all) do
|
1984
|
+
Person.send(:undef_method, :addresses_attributes=)
|
1985
|
+
Person.accepts_nested_attributes_for :addresses, allow_destroy: false
|
1986
|
+
end
|
1987
|
+
|
1988
|
+
after(:all) do
|
1989
|
+
Person.send(:undef_method, :addresses_attributes=)
|
1990
|
+
Person.accepts_nested_attributes_for :addresses
|
1991
|
+
end
|
1992
|
+
|
1993
|
+
[ 1, "1", true, "true" ].each do |truth|
|
1994
|
+
|
1995
|
+
context "when passed a #{truth} with destroy" do
|
1996
|
+
|
1997
|
+
before do
|
1998
|
+
person.addresses_attributes =
|
1999
|
+
{
|
2000
|
+
"0" => { "street" => "Maybachufer", "_destroy" => truth },
|
2001
|
+
"1" => { "street" => "Alexander Platz" }
|
2002
|
+
}
|
2003
|
+
end
|
2004
|
+
|
2005
|
+
it "adds the the marked document" do
|
2006
|
+
expect(person.addresses.first.street).to eq("Maybachufer")
|
2007
|
+
end
|
2008
|
+
|
2009
|
+
it "adds the new unmarked document" do
|
2010
|
+
expect(person.addresses.last.street).to eq("Alexander Platz")
|
2011
|
+
end
|
2012
|
+
|
2013
|
+
it "adds the correct number of documents" do
|
2014
|
+
expect(person.addresses.size).to eq(2)
|
2015
|
+
end
|
2016
|
+
end
|
2017
|
+
end
|
2018
|
+
|
2019
|
+
[ 0, "0", false, "false" ].each do |falsehood|
|
2020
|
+
|
2021
|
+
context "when passed a #{falsehood} with destroy" do
|
2022
|
+
|
2023
|
+
before do
|
2024
|
+
person.addresses_attributes =
|
2025
|
+
{
|
2026
|
+
"0" => { "street" => "Maybachufer", "_destroy" => falsehood },
|
2027
|
+
"1" => { "street" => "Alexander Platz" }
|
2028
|
+
}
|
2029
|
+
end
|
2030
|
+
|
2031
|
+
it "adds the new marked document" do
|
2032
|
+
expect(person.addresses.first.street).to eq("Maybachufer")
|
2033
|
+
end
|
2034
|
+
|
2035
|
+
it "adds the new unmarked document" do
|
2036
|
+
expect(person.addresses.last.street).to eq("Alexander Platz")
|
2037
|
+
end
|
2038
|
+
|
2039
|
+
it "does not add extra documents" do
|
2040
|
+
expect(person.addresses.size).to eq(2)
|
2041
|
+
end
|
2042
|
+
end
|
2043
|
+
end
|
2044
|
+
end
|
2045
|
+
|
2046
|
+
context "when allow destroy is not defined" do
|
2047
|
+
|
2048
|
+
before(:all) do
|
2049
|
+
Person.send(:undef_method, :addresses_attributes=)
|
2050
|
+
Person.accepts_nested_attributes_for :addresses
|
2051
|
+
end
|
2052
|
+
|
2053
|
+
[ 1, "1", true, "true" ].each do |truth|
|
2054
|
+
|
2055
|
+
context "when passed a #{truth} with destroy" do
|
2056
|
+
|
2057
|
+
before do
|
2058
|
+
person.addresses_attributes =
|
2059
|
+
{
|
2060
|
+
"0" => { "street" => "Maybachufer", "_destroy" => truth },
|
2061
|
+
"1" => { "street" => "Alexander Platz" }
|
2062
|
+
}
|
2063
|
+
end
|
2064
|
+
|
2065
|
+
it "adds the the marked document" do
|
2066
|
+
expect(person.addresses.first.street).to eq("Maybachufer")
|
2067
|
+
end
|
2068
|
+
|
2069
|
+
it "adds the new unmarked document" do
|
2070
|
+
expect(person.addresses.last.street).to eq("Alexander Platz")
|
2071
|
+
end
|
2072
|
+
|
2073
|
+
it "adds the correct number of documents" do
|
2074
|
+
expect(person.addresses.size).to eq(2)
|
2075
|
+
end
|
2076
|
+
end
|
2077
|
+
end
|
2078
|
+
|
2079
|
+
[ 0, "0", false, "false" ].each do |falsehood|
|
2080
|
+
|
2081
|
+
context "when passed a #{falsehood} with destroy" do
|
2082
|
+
|
2083
|
+
before do
|
2084
|
+
person.addresses_attributes =
|
2085
|
+
{
|
2086
|
+
"0" => { "street" => "Maybachufer", "_destroy" => falsehood },
|
2087
|
+
"1" => { "street" => "Alexander Platz" }
|
2088
|
+
}
|
2089
|
+
end
|
2090
|
+
|
2091
|
+
it "adds the new marked document" do
|
2092
|
+
expect(person.addresses.first.street).to eq("Maybachufer")
|
2093
|
+
end
|
2094
|
+
|
2095
|
+
it "adds the new unmarked document" do
|
2096
|
+
expect(person.addresses.last.street).to eq("Alexander Platz")
|
2097
|
+
end
|
2098
|
+
|
2099
|
+
it "does not add extra documents" do
|
2100
|
+
expect(person.addresses.size).to eq(2)
|
2101
|
+
end
|
2102
|
+
end
|
2103
|
+
end
|
2104
|
+
end
|
2105
|
+
end
|
2106
|
+
|
2107
|
+
context "when 'reject_if: :all_blank' and 'allow_destroy: true' are specified" do
|
2108
|
+
|
2109
|
+
before(:all) do
|
2110
|
+
Person.send(:undef_method, :addresses_attributes=)
|
2111
|
+
Person.accepts_nested_attributes_for \
|
2112
|
+
:addresses, reject_if: :all_blank, allow_destroy: true
|
2113
|
+
end
|
2114
|
+
|
2115
|
+
after(:all) do
|
2116
|
+
Person.send(:undef_method, :addresses_attributes=)
|
2117
|
+
Person.accepts_nested_attributes_for :addresses
|
2118
|
+
end
|
2119
|
+
|
2120
|
+
context "when all attributes are blank and _destroy has a truthy, non-blank value" do
|
2121
|
+
|
2122
|
+
before do
|
2123
|
+
person.addresses_attributes =
|
2124
|
+
{ "3" => { last_name: "", _destroy: "0" } }
|
2125
|
+
end
|
2126
|
+
|
2127
|
+
it "does not add the document" do
|
2128
|
+
expect(person.addresses).to be_empty
|
2129
|
+
end
|
2130
|
+
end
|
2131
|
+
end
|
2132
|
+
end
|
2133
|
+
|
2134
|
+
context "when the nested document is invalid" do
|
2135
|
+
|
2136
|
+
before(:all) do
|
2137
|
+
Person.validates_associated(:addresses)
|
2138
|
+
end
|
2139
|
+
|
2140
|
+
after(:all) do
|
2141
|
+
Person.reset_callbacks(:validate)
|
2142
|
+
end
|
2143
|
+
|
2144
|
+
before do
|
2145
|
+
person.addresses_attributes = {
|
2146
|
+
"0" => { street: '123' }
|
2147
|
+
}
|
2148
|
+
end
|
2149
|
+
|
2150
|
+
it "propagates invalidity to parent" do
|
2151
|
+
expect(person.addresses.first).to_not be_valid
|
2152
|
+
expect(person).to_not be_valid
|
2153
|
+
end
|
2154
|
+
end
|
2155
|
+
|
2156
|
+
context "when a type is passed" do
|
2157
|
+
|
2158
|
+
let(:canvas) do
|
2159
|
+
Canvas.new
|
2160
|
+
end
|
2161
|
+
|
2162
|
+
before do
|
2163
|
+
Canvas.send(:undef_method, :shapes_attributes=)
|
2164
|
+
Canvas.accepts_nested_attributes_for :shapes
|
2165
|
+
canvas.shapes_attributes =
|
2166
|
+
{
|
2167
|
+
"foo" => { "_type" => "Square" },
|
2168
|
+
"bar" => { "_type" => "Circle" }
|
2169
|
+
}
|
2170
|
+
end
|
2171
|
+
|
2172
|
+
it "instantiates an object of the given type" do
|
2173
|
+
expect(canvas.shapes.map(&:class)).to eq([Square, Circle])
|
2174
|
+
end
|
2175
|
+
end
|
2176
|
+
end
|
2177
|
+
|
2178
|
+
context "when the relation is a references one" do
|
2179
|
+
|
2180
|
+
let(:person) do
|
2181
|
+
Person.new
|
2182
|
+
end
|
2183
|
+
|
2184
|
+
context "when a reject proc is specified" do
|
2185
|
+
|
2186
|
+
before(:all) do
|
2187
|
+
Person.send(:undef_method, :game_attributes=)
|
2188
|
+
Person.accepts_nested_attributes_for \
|
2189
|
+
:game, reject_if: ->(attrs){ attrs[:name].blank? }
|
2190
|
+
end
|
2191
|
+
|
2192
|
+
after(:all) do
|
2193
|
+
Person.send(:undef_method, :game_attributes=)
|
2194
|
+
Person.accepts_nested_attributes_for :game
|
2195
|
+
end
|
2196
|
+
|
2197
|
+
context "when the attributes match" do
|
2198
|
+
|
2199
|
+
before do
|
2200
|
+
person.game_attributes = { score: 10 }
|
2201
|
+
end
|
2202
|
+
|
2203
|
+
it "does not add the document" do
|
2204
|
+
expect(person.game).to be_nil
|
2205
|
+
end
|
2206
|
+
end
|
2207
|
+
|
2208
|
+
context "when the attributes do not match" do
|
2209
|
+
|
2210
|
+
before do
|
2211
|
+
person.game_attributes = { name: "Tron" }
|
2212
|
+
end
|
2213
|
+
|
2214
|
+
it "adds the document" do
|
2215
|
+
expect(person.game.name).to eq("Tron")
|
2216
|
+
end
|
2217
|
+
end
|
2218
|
+
end
|
2219
|
+
|
2220
|
+
context "when reject_if => :all_blank is specified" do
|
2221
|
+
|
2222
|
+
before(:all) do
|
2223
|
+
Person.send(:undef_method, :game_attributes=)
|
2224
|
+
Person.accepts_nested_attributes_for \
|
2225
|
+
:game, reject_if: :all_blank
|
2226
|
+
end
|
2227
|
+
|
2228
|
+
after(:all) do
|
2229
|
+
Person.send(:undef_method, :game_attributes=)
|
2230
|
+
Person.accepts_nested_attributes_for :game
|
2231
|
+
end
|
2232
|
+
|
2233
|
+
context "when all attributes are empty" do
|
2234
|
+
|
2235
|
+
before do
|
2236
|
+
person.game_attributes = { score: nil }
|
2237
|
+
end
|
2238
|
+
|
2239
|
+
it "does not add the document" do
|
2240
|
+
expect(person.game).to be_nil
|
2241
|
+
end
|
2242
|
+
end
|
2243
|
+
|
2244
|
+
context "when an attribute is non-empty" do
|
2245
|
+
|
2246
|
+
before do
|
2247
|
+
person.game_attributes = { name: "Tron" }
|
2248
|
+
end
|
2249
|
+
|
2250
|
+
it "adds the document" do
|
2251
|
+
expect(person.game.name).to eq("Tron")
|
2252
|
+
end
|
2253
|
+
end
|
2254
|
+
end
|
2255
|
+
|
2256
|
+
context "when no id has been passed" do
|
2257
|
+
|
2258
|
+
context "with no destroy attribute" do
|
2259
|
+
|
2260
|
+
before do
|
2261
|
+
person.game_attributes = { name: "Tron" }
|
2262
|
+
end
|
2263
|
+
|
2264
|
+
it "builds a new document" do
|
2265
|
+
expect(person.game.name).to eq("Tron")
|
2266
|
+
end
|
2267
|
+
end
|
2268
|
+
|
2269
|
+
context "with a destroy attribute" do
|
2270
|
+
|
2271
|
+
context "when allow_destroy is true" do
|
2272
|
+
|
2273
|
+
before(:all) do
|
2274
|
+
Person.send(:undef_method, :game_attributes=)
|
2275
|
+
Person.accepts_nested_attributes_for :game, allow_destroy: true
|
2276
|
+
end
|
2277
|
+
|
2278
|
+
after(:all) do
|
2279
|
+
Person.send(:undef_method, :game_attributes=)
|
2280
|
+
Person.accepts_nested_attributes_for :game
|
2281
|
+
end
|
2282
|
+
|
2283
|
+
before do
|
2284
|
+
person.game_attributes = { name: "Tron", _destroy: "1" }
|
2285
|
+
end
|
2286
|
+
|
2287
|
+
it "does not build the document" do
|
2288
|
+
expect(person.game).to be_nil
|
2289
|
+
end
|
2290
|
+
end
|
2291
|
+
|
2292
|
+
context "when allow_destroy is false" do
|
2293
|
+
|
2294
|
+
before(:all) do
|
2295
|
+
Person.send(:undef_method, :game_attributes=)
|
2296
|
+
Person.accepts_nested_attributes_for :game, allow_destroy: false
|
2297
|
+
end
|
2298
|
+
|
2299
|
+
after(:all) do
|
2300
|
+
Person.send(:undef_method, :game_attributes=)
|
2301
|
+
Person.accepts_nested_attributes_for :game
|
2302
|
+
end
|
2303
|
+
|
2304
|
+
before do
|
2305
|
+
person.game_attributes = { name: "Tron", _destroy: "1" }
|
2306
|
+
end
|
2307
|
+
|
2308
|
+
it "builds the document" do
|
2309
|
+
expect(person.game.name).to eq("Tron")
|
2310
|
+
end
|
2311
|
+
end
|
2312
|
+
end
|
2313
|
+
|
2314
|
+
context "with empty attributes" do
|
2315
|
+
|
2316
|
+
before do
|
2317
|
+
person.game_attributes = {}
|
2318
|
+
end
|
2319
|
+
|
2320
|
+
it "does not build the document" do
|
2321
|
+
expect(person.game).to be_nil
|
2322
|
+
end
|
2323
|
+
end
|
2324
|
+
|
2325
|
+
context "when there is an existing document" do
|
2326
|
+
|
2327
|
+
context "with no destroy attribute" do
|
2328
|
+
|
2329
|
+
before do
|
2330
|
+
person.game = Game.new(name: "Tron")
|
2331
|
+
person.game_attributes = { name: "Pong" }
|
2332
|
+
end
|
2333
|
+
|
2334
|
+
it "replaces the document" do
|
2335
|
+
expect(person.game.name).to eq("Pong")
|
2336
|
+
end
|
2337
|
+
end
|
2338
|
+
|
2339
|
+
context "when updating attributes" do
|
2340
|
+
|
2341
|
+
let!(:pizza) do
|
2342
|
+
Pizza.create(name: "large")
|
2343
|
+
end
|
2344
|
+
|
2345
|
+
before do
|
2346
|
+
pizza.topping = Topping.create(name: "cheese")
|
2347
|
+
pizza.update_attributes(topping_attributes: { name: "onions" })
|
2348
|
+
end
|
2349
|
+
|
2350
|
+
it "persists the attribute changes" do
|
2351
|
+
expect(pizza.reload.topping.name).to eq("onions")
|
2352
|
+
end
|
2353
|
+
end
|
2354
|
+
|
2355
|
+
context "with a destroy attribute" do
|
2356
|
+
|
2357
|
+
context "when allow_destroy is true" do
|
2358
|
+
|
2359
|
+
before(:all) do
|
2360
|
+
Person.send(:undef_method, :game_attributes=)
|
2361
|
+
Person.accepts_nested_attributes_for :game, allow_destroy: true
|
2362
|
+
end
|
2363
|
+
|
2364
|
+
after(:all) do
|
2365
|
+
Person.send(:undef_method, :game_attributes=)
|
2366
|
+
Person.accepts_nested_attributes_for :game
|
2367
|
+
end
|
2368
|
+
|
2369
|
+
before do
|
2370
|
+
person.game = Game.new(name: "Tron")
|
2371
|
+
person.game_attributes = { name: "Pong", _destroy: "1" }
|
2372
|
+
end
|
2373
|
+
|
2374
|
+
it "does not replace the document" do
|
2375
|
+
expect(person.game.name).to eq("Tron")
|
2376
|
+
end
|
2377
|
+
end
|
2378
|
+
|
2379
|
+
context "when allow_destroy is false" do
|
2380
|
+
|
2381
|
+
before(:all) do
|
2382
|
+
Person.send(:undef_method, :game_attributes=)
|
2383
|
+
Person.accepts_nested_attributes_for :game, allow_destroy: false
|
2384
|
+
end
|
2385
|
+
|
2386
|
+
after(:all) do
|
2387
|
+
Person.send(:undef_method, :game_attributes=)
|
2388
|
+
Person.accepts_nested_attributes_for :game
|
2389
|
+
end
|
2390
|
+
|
2391
|
+
before do
|
2392
|
+
person.game = Game.new(name: "Tron")
|
2393
|
+
person.game_attributes = { name: "Pong", _destroy: "1" }
|
2394
|
+
end
|
2395
|
+
|
2396
|
+
it "replaces the document" do
|
2397
|
+
expect(person.game.name).to eq("Pong")
|
2398
|
+
end
|
2399
|
+
end
|
2400
|
+
end
|
2401
|
+
end
|
2402
|
+
end
|
2403
|
+
|
2404
|
+
context "when an id is passed" do
|
2405
|
+
|
2406
|
+
context "when there is an existing record" do
|
2407
|
+
|
2408
|
+
let(:game) do
|
2409
|
+
Game.new(name: "Tron")
|
2410
|
+
end
|
2411
|
+
|
2412
|
+
before do
|
2413
|
+
person.game = game
|
2414
|
+
end
|
2415
|
+
|
2416
|
+
context "when the id matches" do
|
2417
|
+
|
2418
|
+
context "when passed keys as symbols" do
|
2419
|
+
|
2420
|
+
before do
|
2421
|
+
person.game_attributes =
|
2422
|
+
{ id: game.id, name: "Pong" }
|
2423
|
+
end
|
2424
|
+
|
2425
|
+
it "updates the existing document" do
|
2426
|
+
expect(person.game.name).to eq("Pong")
|
2427
|
+
end
|
2428
|
+
end
|
2429
|
+
|
2430
|
+
context "when passed keys as strings" do
|
2431
|
+
|
2432
|
+
before do
|
2433
|
+
person.game_attributes =
|
2434
|
+
{ "id" => game.id, "name" => "Pong" }
|
2435
|
+
end
|
2436
|
+
|
2437
|
+
it "updates the existing document" do
|
2438
|
+
expect(person.game.name).to eq("Pong")
|
2439
|
+
end
|
2440
|
+
end
|
2441
|
+
|
2442
|
+
context "when allow_destroy is true" do
|
2443
|
+
|
2444
|
+
before(:all) do
|
2445
|
+
Person.send(:undef_method, :game_attributes=)
|
2446
|
+
Person.accepts_nested_attributes_for :game, allow_destroy: true
|
2447
|
+
end
|
2448
|
+
|
2449
|
+
after(:all) do
|
2450
|
+
Person.send(:undef_method, :game_attributes=)
|
2451
|
+
Person.accepts_nested_attributes_for :game
|
2452
|
+
end
|
2453
|
+
|
2454
|
+
[ 1, "1", true, "true" ].each do |truth|
|
2455
|
+
|
2456
|
+
context "when passed #{truth} with destroy" do
|
2457
|
+
|
2458
|
+
before do
|
2459
|
+
person.game_attributes =
|
2460
|
+
{ id: game.id, _destroy: truth }
|
2461
|
+
end
|
2462
|
+
|
2463
|
+
it "destroys the existing document" do
|
2464
|
+
expect(person.game).to be_nil
|
2465
|
+
end
|
2466
|
+
end
|
2467
|
+
end
|
2468
|
+
|
2469
|
+
[ nil, 0, "0", false, "false" ].each do |falsehood|
|
2470
|
+
|
2471
|
+
context "when passed #{falsehood} with destroy" do
|
2472
|
+
|
2473
|
+
before do
|
2474
|
+
person.game_attributes =
|
2475
|
+
{ id: game.id, _destroy: falsehood }
|
2476
|
+
end
|
2477
|
+
|
2478
|
+
it "does not destroy the existing document" do
|
2479
|
+
expect(person.game).to eq(game)
|
2480
|
+
end
|
2481
|
+
end
|
2482
|
+
end
|
2483
|
+
end
|
2484
|
+
|
2485
|
+
context "when allow destroy is false" do
|
2486
|
+
|
2487
|
+
before(:all) do
|
2488
|
+
Person.send(:undef_method, :game_attributes=)
|
2489
|
+
Person.accepts_nested_attributes_for :game, allow_destroy: false
|
2490
|
+
end
|
2491
|
+
|
2492
|
+
after(:all) do
|
2493
|
+
Person.send(:undef_method, :game_attributes=)
|
2494
|
+
Person.accepts_nested_attributes_for :game
|
2495
|
+
end
|
2496
|
+
|
2497
|
+
context "when a destroy attribute is passed" do
|
2498
|
+
|
2499
|
+
before do
|
2500
|
+
person.game_attributes =
|
2501
|
+
{ id: game.id, _destroy: true }
|
2502
|
+
end
|
2503
|
+
|
2504
|
+
it "does not destroy the document" do
|
2505
|
+
expect(person.game).to eq(game)
|
2506
|
+
end
|
2507
|
+
end
|
2508
|
+
end
|
2509
|
+
|
2510
|
+
context "when update only is true" do
|
2511
|
+
|
2512
|
+
before(:all) do
|
2513
|
+
Person.send(:undef_method, :game_attributes=)
|
2514
|
+
Person.accepts_nested_attributes_for \
|
2515
|
+
:game,
|
2516
|
+
update_only: true,
|
2517
|
+
allow_destroy: true
|
2518
|
+
end
|
2519
|
+
|
2520
|
+
after(:all) do
|
2521
|
+
Person.send(:undef_method, :game_attributes=)
|
2522
|
+
Person.accepts_nested_attributes_for :game
|
2523
|
+
end
|
2524
|
+
|
2525
|
+
context "when the id matches" do
|
2526
|
+
|
2527
|
+
before do
|
2528
|
+
person.game_attributes =
|
2529
|
+
{ id: game.id, name: "Donkey Kong" }
|
2530
|
+
end
|
2531
|
+
|
2532
|
+
it "updates the existing document" do
|
2533
|
+
expect(person.game.name).to eq("Donkey Kong")
|
2534
|
+
end
|
2535
|
+
end
|
2536
|
+
|
2537
|
+
context "when the id does not match" do
|
2538
|
+
|
2539
|
+
before do
|
2540
|
+
person.game_attributes =
|
2541
|
+
{ id: BSON::ObjectId.new.to_s, name: "Pong" }
|
2542
|
+
end
|
2543
|
+
|
2544
|
+
it "updates the existing document" do
|
2545
|
+
expect(person.game.name).to eq("Pong")
|
2546
|
+
end
|
2547
|
+
end
|
2548
|
+
|
2549
|
+
context "when passed a destroy truth" do
|
2550
|
+
|
2551
|
+
before do
|
2552
|
+
person.game_attributes =
|
2553
|
+
{ id: game.id, _destroy: true }
|
2554
|
+
end
|
2555
|
+
|
2556
|
+
it "destroys the existing document" do
|
2557
|
+
expect(person.game).to be_nil
|
2558
|
+
end
|
2559
|
+
end
|
2560
|
+
end
|
2561
|
+
end
|
2562
|
+
end
|
2563
|
+
end
|
2564
|
+
|
2565
|
+
context "when the nested document is invalid" do
|
2566
|
+
|
2567
|
+
before(:all) do
|
2568
|
+
Person.validates_associated(:game)
|
2569
|
+
end
|
2570
|
+
|
2571
|
+
after(:all) do
|
2572
|
+
Person.reset_callbacks(:validate)
|
2573
|
+
end
|
2574
|
+
|
2575
|
+
before do
|
2576
|
+
person.game_attributes = { name: '$$$' }
|
2577
|
+
end
|
2578
|
+
|
2579
|
+
it "propagates invalidity to parent" do
|
2580
|
+
expect(person.game).to_not be_valid
|
2581
|
+
expect(person).to_not be_valid
|
2582
|
+
end
|
2583
|
+
end
|
2584
|
+
|
2585
|
+
context "when a type is passed" do
|
2586
|
+
|
2587
|
+
let(:driver) do
|
2588
|
+
Driver.new
|
2589
|
+
end
|
2590
|
+
|
2591
|
+
before do
|
2592
|
+
Driver.send(:undef_method, :vehicle_attributes=)
|
2593
|
+
Driver.accepts_nested_attributes_for :vehicle
|
2594
|
+
driver.vehicle_attributes = { "_type" => "Truck" }
|
2595
|
+
end
|
2596
|
+
|
2597
|
+
it "instantiates an object of the given type" do
|
2598
|
+
expect(driver.vehicle.class).to eq(Truck)
|
2599
|
+
end
|
2600
|
+
end
|
2601
|
+
end
|
2602
|
+
|
2603
|
+
context "when the relation is referenced in" do
|
2604
|
+
|
2605
|
+
context "when the child is new" do
|
2606
|
+
|
2607
|
+
let(:game) do
|
2608
|
+
Game.new
|
2609
|
+
end
|
2610
|
+
|
2611
|
+
context "when no id has been passed" do
|
2612
|
+
|
2613
|
+
context "when no destroy attribute passed" do
|
2614
|
+
|
2615
|
+
before do
|
2616
|
+
game.person_attributes = { title: "Sir" }
|
2617
|
+
end
|
2618
|
+
|
2619
|
+
it "builds a new document" do
|
2620
|
+
expect(game.person.title).to eq("Sir")
|
2621
|
+
end
|
2622
|
+
|
2623
|
+
end
|
2624
|
+
|
2625
|
+
context "when a destroy attribute is passed" do
|
2626
|
+
|
2627
|
+
context "when allow_destroy is true" do
|
2628
|
+
|
2629
|
+
before(:all) do
|
2630
|
+
Game.send(:undef_method, :person_attributes=)
|
2631
|
+
Game.accepts_nested_attributes_for :person, allow_destroy: true
|
2632
|
+
end
|
2633
|
+
|
2634
|
+
after(:all) do
|
2635
|
+
Game.send(:undef_method, :person_attributes=)
|
2636
|
+
Game.accepts_nested_attributes_for :person
|
2637
|
+
end
|
2638
|
+
|
2639
|
+
before do
|
2640
|
+
game.person_attributes = { title: "Sir", _destroy: 1 }
|
2641
|
+
end
|
2642
|
+
|
2643
|
+
it "does not build a new document" do
|
2644
|
+
expect(game.person).to be_nil
|
2645
|
+
end
|
2646
|
+
end
|
2647
|
+
|
2648
|
+
context "when allow_destroy is false" do
|
2649
|
+
|
2650
|
+
before(:all) do
|
2651
|
+
Game.send(:undef_method, :person_attributes=)
|
2652
|
+
Game.accepts_nested_attributes_for :person, allow_destroy: false
|
2653
|
+
end
|
2654
|
+
|
2655
|
+
after(:all) do
|
2656
|
+
Game.send(:undef_method, :person_attributes=)
|
2657
|
+
Game.accepts_nested_attributes_for :person
|
2658
|
+
end
|
2659
|
+
|
2660
|
+
before do
|
2661
|
+
game.person_attributes = { title: "Sir", _destroy: 1 }
|
2662
|
+
end
|
2663
|
+
|
2664
|
+
it "builds a new document" do
|
2665
|
+
expect(game.person.title).to eq("Sir")
|
2666
|
+
end
|
2667
|
+
end
|
2668
|
+
end
|
2669
|
+
end
|
2670
|
+
|
2671
|
+
context "when an id has been passed" do
|
2672
|
+
|
2673
|
+
let(:person) do
|
2674
|
+
Person.new
|
2675
|
+
end
|
2676
|
+
|
2677
|
+
context "when no destroy attribute passed" do
|
2678
|
+
|
2679
|
+
context "when the id matches" do
|
2680
|
+
|
2681
|
+
before do
|
2682
|
+
game.person_attributes = { id: person.id, title: "Sir" }
|
2683
|
+
end
|
2684
|
+
|
2685
|
+
it "updates the existing document" do
|
2686
|
+
expect(game.person.title).to eq("Sir")
|
2687
|
+
end
|
2688
|
+
end
|
2689
|
+
end
|
2690
|
+
|
2691
|
+
context "when there is an existing document" do
|
2692
|
+
|
2693
|
+
before do
|
2694
|
+
game.person = person
|
2695
|
+
end
|
2696
|
+
|
2697
|
+
context "when allow destroy is true" do
|
2698
|
+
|
2699
|
+
before(:all) do
|
2700
|
+
Game.send(:undef_method, :person_attributes=)
|
2701
|
+
Game.accepts_nested_attributes_for :person, allow_destroy: true
|
2702
|
+
end
|
2703
|
+
|
2704
|
+
after(:all) do
|
2705
|
+
Game.send(:undef_method, :person_attributes=)
|
2706
|
+
Game.accepts_nested_attributes_for :person
|
2707
|
+
end
|
2708
|
+
|
2709
|
+
[ 1, "1", true, "true" ].each do |truth|
|
2710
|
+
|
2711
|
+
context "when passed #{truth} with destroy" do
|
2712
|
+
|
2713
|
+
before do
|
2714
|
+
game.person_attributes =
|
2715
|
+
{ id: person.id, _destroy: truth }
|
2716
|
+
end
|
2717
|
+
|
2718
|
+
it "destroys the existing document" do
|
2719
|
+
expect(game.person).to be_nil
|
2720
|
+
end
|
2721
|
+
end
|
2722
|
+
end
|
2723
|
+
|
2724
|
+
[ nil, 0, "0", false, "false" ].each do |falsehood|
|
2725
|
+
|
2726
|
+
context "when passed #{falsehood} with destroy" do
|
2727
|
+
|
2728
|
+
before do
|
2729
|
+
game.person_attributes =
|
2730
|
+
{ id: person.id, _destroy: falsehood }
|
2731
|
+
end
|
2732
|
+
|
2733
|
+
it "does not destroy the existing document" do
|
2734
|
+
expect(game.person).to eq(person)
|
2735
|
+
end
|
2736
|
+
end
|
2737
|
+
end
|
2738
|
+
end
|
2739
|
+
|
2740
|
+
context "when allow destroy is false" do
|
2741
|
+
|
2742
|
+
before(:all) do
|
2743
|
+
Game.send(:undef_method, :person_attributes=)
|
2744
|
+
Game.accepts_nested_attributes_for :person, allow_destroy: false
|
2745
|
+
end
|
2746
|
+
|
2747
|
+
after(:all) do
|
2748
|
+
Game.send(:undef_method, :person_attributes=)
|
2749
|
+
Game.accepts_nested_attributes_for :person
|
2750
|
+
end
|
2751
|
+
|
2752
|
+
context "when a destroy attribute is passed" do
|
2753
|
+
|
2754
|
+
before do
|
2755
|
+
game.person_attributes =
|
2756
|
+
{ id: person.id, _destroy: true }
|
2757
|
+
end
|
2758
|
+
|
2759
|
+
it "does not delete the document" do
|
2760
|
+
expect(game.person).to eq(person)
|
2761
|
+
end
|
2762
|
+
end
|
2763
|
+
end
|
2764
|
+
|
2765
|
+
context "when update only is true" do
|
2766
|
+
|
2767
|
+
before do
|
2768
|
+
Game.send(:undef_method, :person_attributes=)
|
2769
|
+
Game.accepts_nested_attributes_for \
|
2770
|
+
:person,
|
2771
|
+
update_only: true,
|
2772
|
+
allow_destroy: true
|
2773
|
+
end
|
2774
|
+
|
2775
|
+
context "when the id matches" do
|
2776
|
+
|
2777
|
+
before do
|
2778
|
+
game.person_attributes =
|
2779
|
+
{ id: person.id, title: "Madam" }
|
2780
|
+
end
|
2781
|
+
|
2782
|
+
it "updates the existing document" do
|
2783
|
+
expect(game.person.title).to eq("Madam")
|
2784
|
+
end
|
2785
|
+
end
|
2786
|
+
|
2787
|
+
context "when the id does not match" do
|
2788
|
+
|
2789
|
+
before do
|
2790
|
+
game.person_attributes =
|
2791
|
+
{ id: BSON::ObjectId.new.to_s, title: "Madam" }
|
2792
|
+
end
|
2793
|
+
|
2794
|
+
it "updates the existing document" do
|
2795
|
+
expect(game.person.title).to eq("Madam")
|
2796
|
+
end
|
2797
|
+
end
|
2798
|
+
|
2799
|
+
context "when passed a destroy truth" do
|
2800
|
+
|
2801
|
+
before do
|
2802
|
+
game.person_attributes =
|
2803
|
+
{ id: person.id, title: "Madam", _destroy: "true" }
|
2804
|
+
end
|
2805
|
+
|
2806
|
+
it "deletes the existing document" do
|
2807
|
+
expect(game.person).to be_nil
|
2808
|
+
end
|
2809
|
+
end
|
2810
|
+
end
|
2811
|
+
end
|
2812
|
+
end
|
2813
|
+
|
2814
|
+
context "when the nested document is invalid" do
|
2815
|
+
|
2816
|
+
before(:all) do
|
2817
|
+
Person.validates_format_of :ssn, without: /\$\$\$/
|
2818
|
+
end
|
2819
|
+
|
2820
|
+
after(:all) do
|
2821
|
+
Person.reset_callbacks(:validate)
|
2822
|
+
end
|
2823
|
+
|
2824
|
+
before do
|
2825
|
+
game.person_attributes = { ssn: '$$$' }
|
2826
|
+
end
|
2827
|
+
|
2828
|
+
it "propagates invalidity to parent" do
|
2829
|
+
expect(game.person).to_not be_valid
|
2830
|
+
expect(game).to_not be_valid
|
2831
|
+
end
|
2832
|
+
end
|
2833
|
+
end
|
2834
|
+
|
2835
|
+
context "when a type is passed" do
|
2836
|
+
|
2837
|
+
let(:vehicle) do
|
2838
|
+
Vehicle.new
|
2839
|
+
end
|
2840
|
+
|
2841
|
+
before do
|
2842
|
+
Vehicle.send(:undef_method, :driver_attributes=)
|
2843
|
+
Vehicle.accepts_nested_attributes_for :driver
|
2844
|
+
vehicle.driver_attributes = { "_type" => "Learner" }
|
2845
|
+
end
|
2846
|
+
|
2847
|
+
it "instantiates an object of the given type" do
|
2848
|
+
expect(vehicle.driver.class).to eq(Learner)
|
2849
|
+
end
|
2850
|
+
end
|
2851
|
+
end
|
2852
|
+
|
2853
|
+
context "when the relation is a references many" do
|
2854
|
+
|
2855
|
+
let(:person) do
|
2856
|
+
Person.new
|
2857
|
+
end
|
2858
|
+
|
2859
|
+
let(:post_one) do
|
2860
|
+
Post.new(title: "First post")
|
2861
|
+
end
|
2862
|
+
|
2863
|
+
let(:post_two) do
|
2864
|
+
Post.new(title: "First response")
|
2865
|
+
end
|
2866
|
+
|
2867
|
+
context "when a limit is specified" do
|
2868
|
+
|
2869
|
+
before(:all) do
|
2870
|
+
Person.send(:undef_method, :posts_attributes=)
|
2871
|
+
Person.accepts_nested_attributes_for :posts, limit: 2
|
2872
|
+
end
|
2873
|
+
|
2874
|
+
after(:all) do
|
2875
|
+
Person.send(:undef_method, :posts_attributes=)
|
2876
|
+
Person.accepts_nested_attributes_for :posts
|
2877
|
+
end
|
2878
|
+
|
2879
|
+
context "when more are provided than the limit" do
|
2880
|
+
|
2881
|
+
let(:attributes) do
|
2882
|
+
{
|
2883
|
+
"foo" => { "title" => "First" },
|
2884
|
+
"bar" => { "title" => "Second" },
|
2885
|
+
"baz" => { "title" => "Third" }
|
2886
|
+
}
|
2887
|
+
end
|
2888
|
+
|
2889
|
+
it "raises an error" do
|
2890
|
+
expect {
|
2891
|
+
person.posts_attributes = attributes
|
2892
|
+
}.to raise_error(Mongoid::Errors::TooManyNestedAttributeRecords)
|
2893
|
+
end
|
2894
|
+
end
|
2895
|
+
|
2896
|
+
context "when less are provided than the limit" do
|
2897
|
+
|
2898
|
+
let(:attributes) do
|
2899
|
+
{
|
2900
|
+
"foo" => { "title" => "First" },
|
2901
|
+
"bar" => { "title" => "Second" }
|
2902
|
+
}
|
2903
|
+
end
|
2904
|
+
|
2905
|
+
before do
|
2906
|
+
person.posts_attributes = attributes
|
2907
|
+
end
|
2908
|
+
|
2909
|
+
it "sets the documents on the relation" do
|
2910
|
+
expect(person.posts.size).to eq(2)
|
2911
|
+
end
|
2912
|
+
|
2913
|
+
it "does not persist the new documents" do
|
2914
|
+
expect(person.posts.count).to eq(0)
|
2915
|
+
end
|
2916
|
+
end
|
2917
|
+
end
|
2918
|
+
|
2919
|
+
context "when ids are passed" do
|
2920
|
+
|
2921
|
+
let(:person) do
|
2922
|
+
Person.create
|
2923
|
+
end
|
2924
|
+
|
2925
|
+
before do
|
2926
|
+
person.posts << [ post_one, post_two ]
|
2927
|
+
end
|
2928
|
+
|
2929
|
+
context "when no destroy attributes are passed" do
|
2930
|
+
|
2931
|
+
context "when the ids match" do
|
2932
|
+
|
2933
|
+
before do
|
2934
|
+
person.posts_attributes =
|
2935
|
+
{
|
2936
|
+
"0" => { "id" => post_one.id, "title" => "First" },
|
2937
|
+
"1" => { "id" => post_two.id, "title" => "Second" }
|
2938
|
+
}
|
2939
|
+
end
|
2940
|
+
|
2941
|
+
context "when reloading the document" do
|
2942
|
+
|
2943
|
+
it "updates the first existing document" do
|
2944
|
+
expect(person.posts(true).first.title).to eq("First")
|
2945
|
+
end
|
2946
|
+
|
2947
|
+
it "updates the second existing document" do
|
2948
|
+
expect(person.posts(true).last.title).to eq("Second")
|
2949
|
+
end
|
2950
|
+
|
2951
|
+
it "does not add new documents" do
|
2952
|
+
expect(person.posts(true).size).to eq(2)
|
2953
|
+
end
|
2954
|
+
end
|
2955
|
+
|
2956
|
+
context "when there are no documents" do
|
2957
|
+
|
2958
|
+
before do
|
2959
|
+
person.posts.clear
|
2960
|
+
end
|
2961
|
+
|
2962
|
+
it "raises a document not found error" do
|
2963
|
+
expect {
|
2964
|
+
person.posts_attributes =
|
2965
|
+
{ "0" =>
|
2966
|
+
{ "id" => BSON::ObjectId.new.to_s, "title" => "Rogue" }
|
2967
|
+
}
|
2968
|
+
}.to raise_error(Mongoid::Errors::DocumentNotFound)
|
2969
|
+
end
|
2970
|
+
end
|
2971
|
+
end
|
2972
|
+
|
2973
|
+
context "when the parent is freshly loaded from the db" do
|
2974
|
+
|
2975
|
+
before do
|
2976
|
+
person.reload
|
2977
|
+
end
|
2978
|
+
|
2979
|
+
context "when updating valid documents with invalid values" do
|
2980
|
+
|
2981
|
+
before do
|
2982
|
+
person.posts_attributes =
|
2983
|
+
{
|
2984
|
+
"0" => { "id" => post_one.id, "title" => "testing again" },
|
2985
|
+
"1" => { "id" => post_two.id, "title" => "$$$" }
|
2986
|
+
}
|
2987
|
+
person.save
|
2988
|
+
end
|
2989
|
+
|
2990
|
+
it "does not perist the invalid value" do
|
2991
|
+
expect(post_two.reload.title).to eq("First response")
|
2992
|
+
end
|
2993
|
+
end
|
2994
|
+
end
|
2995
|
+
|
2996
|
+
context "when the ids do not match" do
|
2997
|
+
|
2998
|
+
it "raises an error" do
|
2999
|
+
expect {
|
3000
|
+
person.posts_attributes =
|
3001
|
+
{ "foo" => { "id" => "test", "title" => "Test" } }
|
3002
|
+
}.to raise_error
|
3003
|
+
end
|
3004
|
+
end
|
3005
|
+
end
|
3006
|
+
|
3007
|
+
context "when destroy attributes are passed" do
|
3008
|
+
|
3009
|
+
context "when the ids match" do
|
3010
|
+
|
3011
|
+
context "when allow_destroy is true" do
|
3012
|
+
|
3013
|
+
before do
|
3014
|
+
Person.send(:undef_method, :posts_attributes=)
|
3015
|
+
Person.accepts_nested_attributes_for :posts, allow_destroy: true
|
3016
|
+
end
|
3017
|
+
|
3018
|
+
after do
|
3019
|
+
Person.send(:undef_method, :posts_attributes=)
|
3020
|
+
Person.accepts_nested_attributes_for :posts
|
3021
|
+
end
|
3022
|
+
|
3023
|
+
[ 1, "1", true, "true" ].each do |truth|
|
3024
|
+
|
3025
|
+
context "when passed a #{truth} with destroy" do
|
3026
|
+
|
3027
|
+
before do
|
3028
|
+
person.posts_attributes =
|
3029
|
+
{
|
3030
|
+
"0" => { "id" => post_one.id, "_destroy" => truth },
|
3031
|
+
"1" => { "id" => post_two.id, "title" => "My Blog" }
|
3032
|
+
}
|
3033
|
+
end
|
3034
|
+
|
3035
|
+
context "when reloading the documents" do
|
3036
|
+
|
3037
|
+
it "deletes the marked document" do
|
3038
|
+
expect(person.posts(true).size).to eq(1)
|
3039
|
+
end
|
3040
|
+
|
3041
|
+
it "does not delete the unmarked document" do
|
3042
|
+
expect(person.posts(true).first.title).to eq("My Blog")
|
3043
|
+
end
|
3044
|
+
end
|
3045
|
+
end
|
3046
|
+
end
|
3047
|
+
|
3048
|
+
[ 0, "0", false, "false" ].each do |falsehood|
|
3049
|
+
|
3050
|
+
context "when passed a #{falsehood} with destroy" do
|
3051
|
+
|
3052
|
+
before do
|
3053
|
+
person.posts_attributes =
|
3054
|
+
{
|
3055
|
+
"0" => { "id" => post_one.id, "_destroy" => falsehood },
|
3056
|
+
"1" => { "id" => post_two.id, "title" => "My Blog" }
|
3057
|
+
}
|
3058
|
+
end
|
3059
|
+
|
3060
|
+
context "when reloading the document" do
|
3061
|
+
|
3062
|
+
it "does not delete the marked document" do
|
3063
|
+
expect(person.posts(true).size).to eq(2)
|
3064
|
+
end
|
3065
|
+
|
3066
|
+
it "does not delete the unmarked document" do
|
3067
|
+
expect(person.posts(true).map(&:title)).to include("My Blog")
|
3068
|
+
end
|
3069
|
+
end
|
3070
|
+
end
|
3071
|
+
end
|
3072
|
+
end
|
3073
|
+
|
3074
|
+
context "when allow_destroy is false" do
|
3075
|
+
|
3076
|
+
before do
|
3077
|
+
Person.send(:undef_method, :posts_attributes=)
|
3078
|
+
Person.accepts_nested_attributes_for :posts, allow_destroy: false
|
3079
|
+
end
|
3080
|
+
|
3081
|
+
after do
|
3082
|
+
Person.send(:undef_method, :posts_attributes=)
|
3083
|
+
Person.accepts_nested_attributes_for :posts
|
3084
|
+
end
|
3085
|
+
|
3086
|
+
[ 1, "1", true, "true" ].each do |truth|
|
3087
|
+
|
3088
|
+
context "when passed a #{truth} with destroy" do
|
3089
|
+
|
3090
|
+
before do
|
3091
|
+
person.posts_attributes =
|
3092
|
+
{
|
3093
|
+
"0" => {
|
3094
|
+
"id" => post_one.id, "title" => "Another Title", "_destroy" => truth },
|
3095
|
+
"1" => { "id" => post_two.id, "title" => "New Title" }
|
3096
|
+
}
|
3097
|
+
end
|
3098
|
+
|
3099
|
+
context "when reloading the document" do
|
3100
|
+
|
3101
|
+
it "does not ignore the marked document" do
|
3102
|
+
expect(person.posts(true).first.title).to eq("Another Title")
|
3103
|
+
end
|
3104
|
+
|
3105
|
+
it "does not delete the unmarked document" do
|
3106
|
+
expect(person.posts(true).last.title).to eq("New Title")
|
3107
|
+
end
|
3108
|
+
|
3109
|
+
it "does not add additional documents" do
|
3110
|
+
expect(person.posts(true).size).to eq(2)
|
3111
|
+
end
|
3112
|
+
end
|
3113
|
+
end
|
3114
|
+
end
|
3115
|
+
|
3116
|
+
[ 0, "0", false, "false" ].each do |falsehood|
|
3117
|
+
|
3118
|
+
context "when passed a #{falsehood} with destroy" do
|
3119
|
+
|
3120
|
+
before do
|
3121
|
+
person.posts_attributes =
|
3122
|
+
{
|
3123
|
+
"0" => { "id" => post_one.id, "_destroy" => falsehood },
|
3124
|
+
"1" => { "id" => post_two.id, "title" => "New Title" }
|
3125
|
+
}
|
3126
|
+
end
|
3127
|
+
|
3128
|
+
context "when reloading the documents" do
|
3129
|
+
|
3130
|
+
it "does not delete the marked document" do
|
3131
|
+
expect(person.posts(true).size).to eq(2)
|
3132
|
+
end
|
3133
|
+
|
3134
|
+
it "does not delete the unmarked document" do
|
3135
|
+
expect(person.posts(true).last.title).to eq("New Title")
|
3136
|
+
end
|
3137
|
+
end
|
3138
|
+
end
|
3139
|
+
end
|
3140
|
+
end
|
3141
|
+
|
3142
|
+
context "when allow_destroy is undefined" do
|
3143
|
+
|
3144
|
+
before(:all) do
|
3145
|
+
Person.send(:undef_method, :posts_attributes=)
|
3146
|
+
Person.accepts_nested_attributes_for :posts
|
3147
|
+
end
|
3148
|
+
|
3149
|
+
after(:all) do
|
3150
|
+
Person.send(:undef_method, :posts_attributes=)
|
3151
|
+
Person.accepts_nested_attributes_for :posts
|
3152
|
+
end
|
3153
|
+
|
3154
|
+
[ 1, "1", true, "true" ].each do |truth|
|
3155
|
+
|
3156
|
+
context "when passed a #{truth} with destroy" do
|
3157
|
+
|
3158
|
+
before do
|
3159
|
+
person.posts_attributes =
|
3160
|
+
{
|
3161
|
+
"0" => {
|
3162
|
+
"id" => post_one.id,
|
3163
|
+
"title" => "Another Title",
|
3164
|
+
"_destroy" => truth
|
3165
|
+
},
|
3166
|
+
"1" => { "id" => post_two.id, "title" => "New Title" }
|
3167
|
+
}
|
3168
|
+
end
|
3169
|
+
|
3170
|
+
context "when reloading" do
|
3171
|
+
|
3172
|
+
it "does not ignore the marked document" do
|
3173
|
+
expect(person.posts(true).find(post_one.id).title).to eq("Another Title")
|
3174
|
+
end
|
3175
|
+
|
3176
|
+
it "does not delete the unmarked document" do
|
3177
|
+
expect(person.posts(true).find(post_two.id).title).to eq("New Title")
|
3178
|
+
end
|
3179
|
+
|
3180
|
+
it "does not add additional documents" do
|
3181
|
+
expect(person.posts(true).size).to eq(2)
|
3182
|
+
end
|
3183
|
+
end
|
3184
|
+
end
|
3185
|
+
end
|
3186
|
+
|
3187
|
+
[ 0, "0", false, "false" ].each do |falsehood|
|
3188
|
+
|
3189
|
+
context "when passed a #{falsehood} with destroy" do
|
3190
|
+
|
3191
|
+
before do
|
3192
|
+
person.posts_attributes =
|
3193
|
+
{
|
3194
|
+
"0" => { "id" => post_one.id, "_destroy" => falsehood },
|
3195
|
+
"1" => { "id" => post_two.id, "title" => "New Title" }
|
3196
|
+
}
|
3197
|
+
end
|
3198
|
+
|
3199
|
+
context "when reloading" do
|
3200
|
+
|
3201
|
+
it "does not delete the marked document" do
|
3202
|
+
expect(person.posts(true).size).to eq(2)
|
3203
|
+
end
|
3204
|
+
|
3205
|
+
it "does not delete the unmarked document" do
|
3206
|
+
expect(person.posts(true).last.title).to eq("New Title")
|
3207
|
+
end
|
3208
|
+
end
|
3209
|
+
end
|
3210
|
+
end
|
3211
|
+
end
|
3212
|
+
end
|
3213
|
+
end
|
3214
|
+
end
|
3215
|
+
|
3216
|
+
context "when no ids are passed" do
|
3217
|
+
|
3218
|
+
context "when no destroy attributes are passed" do
|
3219
|
+
|
3220
|
+
context "when passing a hash of attributes" do
|
3221
|
+
|
3222
|
+
before do
|
3223
|
+
person.posts_attributes =
|
3224
|
+
{
|
3225
|
+
"4" => { "title" => "Third" },
|
3226
|
+
"1" => { "title" => "First" },
|
3227
|
+
"2" => { "title" => "Second" }
|
3228
|
+
}
|
3229
|
+
end
|
3230
|
+
|
3231
|
+
it "builds a new first document" do
|
3232
|
+
expect(person.posts.first.title).to eq("First")
|
3233
|
+
end
|
3234
|
+
|
3235
|
+
it "builds a new second document" do
|
3236
|
+
expect(person.posts.second.title).to eq("Second")
|
3237
|
+
end
|
3238
|
+
|
3239
|
+
it "builds a new third document" do
|
3240
|
+
expect(person.posts.third.title).to eq("Third")
|
3241
|
+
end
|
3242
|
+
|
3243
|
+
it "does not add extra documents" do
|
3244
|
+
expect(person.posts.size).to eq(3)
|
3245
|
+
end
|
3246
|
+
|
3247
|
+
it "does not persist the documents" do
|
3248
|
+
expect(person.posts.count).to eq(0)
|
3249
|
+
end
|
3250
|
+
|
3251
|
+
it "adds the documents in the sorted hash key order" do
|
3252
|
+
expect(person.posts.map(&:title)).to eq(
|
3253
|
+
[ "First", "Second", "Third" ]
|
3254
|
+
)
|
3255
|
+
end
|
3256
|
+
end
|
3257
|
+
|
3258
|
+
context "when passing an array of attributes" do
|
3259
|
+
|
3260
|
+
context "when the parent is saved" do
|
3261
|
+
|
3262
|
+
before do
|
3263
|
+
person.save
|
3264
|
+
person.posts_attributes =
|
3265
|
+
[
|
3266
|
+
{ "title" => "Third" },
|
3267
|
+
{ "title" => "First" },
|
3268
|
+
{ "title" => "Second" }
|
3269
|
+
]
|
3270
|
+
end
|
3271
|
+
|
3272
|
+
it "builds a new first document" do
|
3273
|
+
expect(person.posts.first.title).to eq("Third")
|
3274
|
+
end
|
3275
|
+
|
3276
|
+
it "builds a new second document" do
|
3277
|
+
expect(person.posts.second.title).to eq("First")
|
3278
|
+
end
|
3279
|
+
|
3280
|
+
it "builds a new third document" do
|
3281
|
+
expect(person.posts.third.title).to eq("Second")
|
3282
|
+
end
|
3283
|
+
|
3284
|
+
it "does not add extra documents" do
|
3285
|
+
expect(person.posts.size).to eq(3)
|
3286
|
+
end
|
3287
|
+
|
3288
|
+
it "does not persist the documents" do
|
3289
|
+
expect(person.posts.count).to eq(0)
|
3290
|
+
end
|
3291
|
+
end
|
3292
|
+
end
|
3293
|
+
end
|
3294
|
+
|
3295
|
+
context "when a reject block is supplied" do
|
3296
|
+
|
3297
|
+
before(:all) do
|
3298
|
+
Person.send(:undef_method, :posts_attributes=)
|
3299
|
+
Person.accepts_nested_attributes_for \
|
3300
|
+
:posts, reject_if: ->(attrs){ attrs["title"].blank? }
|
3301
|
+
end
|
3302
|
+
|
3303
|
+
after(:all) do
|
3304
|
+
Person.send(:undef_method, :posts_attributes=)
|
3305
|
+
Person.accepts_nested_attributes_for :posts
|
3306
|
+
end
|
3307
|
+
|
3308
|
+
context "when the attributes match" do
|
3309
|
+
|
3310
|
+
before do
|
3311
|
+
person.posts_attributes =
|
3312
|
+
{ "3" => { "content" => "My first blog" } }
|
3313
|
+
end
|
3314
|
+
|
3315
|
+
it "does not add the new document" do
|
3316
|
+
expect(person.posts).to be_empty
|
3317
|
+
end
|
3318
|
+
end
|
3319
|
+
|
3320
|
+
context "when the attributes do not match" do
|
3321
|
+
|
3322
|
+
before do
|
3323
|
+
person.posts_attributes =
|
3324
|
+
{ "3" => { "title" => "Blogging" } }
|
3325
|
+
end
|
3326
|
+
|
3327
|
+
it "adds the new document" do
|
3328
|
+
expect(person.posts.size).to eq(1)
|
3329
|
+
end
|
3330
|
+
|
3331
|
+
it "sets the correct attributes" do
|
3332
|
+
expect(person.posts.first.title).to eq("Blogging")
|
3333
|
+
end
|
3334
|
+
end
|
3335
|
+
end
|
3336
|
+
|
3337
|
+
context "when :reject_if => :all_blank is supplied" do
|
3338
|
+
|
3339
|
+
before(:all) do
|
3340
|
+
Person.send(:undef_method, :posts_attributes=)
|
3341
|
+
Person.accepts_nested_attributes_for \
|
3342
|
+
:posts, reject_if: :all_blank
|
3343
|
+
end
|
3344
|
+
|
3345
|
+
after(:all) do
|
3346
|
+
Person.send(:undef_method, :posts_attributes=)
|
3347
|
+
Person.accepts_nested_attributes_for :posts
|
3348
|
+
end
|
3349
|
+
|
3350
|
+
context "when all attributes are blank" do
|
3351
|
+
|
3352
|
+
before do
|
3353
|
+
person.posts_attributes =
|
3354
|
+
{ "3" => { "content" => "" } }
|
3355
|
+
end
|
3356
|
+
|
3357
|
+
it "does not add the new document" do
|
3358
|
+
expect(person.posts).to be_empty
|
3359
|
+
end
|
3360
|
+
end
|
3361
|
+
|
3362
|
+
context "when an attribute is non-empty" do
|
3363
|
+
|
3364
|
+
before do
|
3365
|
+
person.posts_attributes =
|
3366
|
+
{ "3" => { "title" => "Blogging" } }
|
3367
|
+
end
|
3368
|
+
|
3369
|
+
it "adds the new document" do
|
3370
|
+
expect(person.posts.size).to eq(1)
|
3371
|
+
end
|
3372
|
+
|
3373
|
+
it "sets the correct attributes" do
|
3374
|
+
expect(person.posts.first.title).to eq("Blogging")
|
3375
|
+
end
|
3376
|
+
end
|
3377
|
+
end
|
3378
|
+
|
3379
|
+
context "when destroy attributes are passed" do
|
3380
|
+
|
3381
|
+
context "when allow_destroy is true" do
|
3382
|
+
|
3383
|
+
before(:all) do
|
3384
|
+
Person.send(:undef_method, :posts_attributes=)
|
3385
|
+
Person.accepts_nested_attributes_for :posts, allow_destroy: true
|
3386
|
+
end
|
3387
|
+
|
3388
|
+
after(:all) do
|
3389
|
+
Person.send(:undef_method, :posts_attributes=)
|
3390
|
+
Person.accepts_nested_attributes_for :posts
|
3391
|
+
end
|
3392
|
+
|
3393
|
+
[ 1, "1", true, "true" ].each do |truth|
|
3394
|
+
|
3395
|
+
context "when passed a #{truth} with destroy" do
|
3396
|
+
|
3397
|
+
before do
|
3398
|
+
person.posts_attributes =
|
3399
|
+
{
|
3400
|
+
"0" => { "title" => "New Blog", "_destroy" => truth },
|
3401
|
+
"1" => { "title" => "Blog Two" }
|
3402
|
+
}
|
3403
|
+
end
|
3404
|
+
|
3405
|
+
it "ignores the the marked document" do
|
3406
|
+
expect(person.posts.size).to eq(1)
|
3407
|
+
end
|
3408
|
+
|
3409
|
+
it "adds the new unmarked document" do
|
3410
|
+
expect(person.posts.first.title).to eq("Blog Two")
|
3411
|
+
end
|
3412
|
+
end
|
3413
|
+
end
|
3414
|
+
|
3415
|
+
[ 0, "0", false, "false" ].each do |falsehood|
|
3416
|
+
|
3417
|
+
context "when passed a #{falsehood} with destroy" do
|
3418
|
+
|
3419
|
+
before do
|
3420
|
+
person.posts_attributes =
|
3421
|
+
{
|
3422
|
+
"0" => { "title" => "New Blog", "_destroy" => falsehood },
|
3423
|
+
"1" => { "title" => "Blog Two" }
|
3424
|
+
}
|
3425
|
+
end
|
3426
|
+
|
3427
|
+
it "adds the new marked document" do
|
3428
|
+
expect(person.posts.first.title).to eq("New Blog")
|
3429
|
+
end
|
3430
|
+
|
3431
|
+
it "adds the new unmarked document" do
|
3432
|
+
expect(person.posts.last.title).to eq("Blog Two")
|
3433
|
+
end
|
3434
|
+
|
3435
|
+
it "does not add extra documents" do
|
3436
|
+
expect(person.posts.size).to eq(2)
|
3437
|
+
end
|
3438
|
+
end
|
3439
|
+
end
|
3440
|
+
end
|
3441
|
+
|
3442
|
+
context "when allow destroy is false" do
|
3443
|
+
|
3444
|
+
before(:all) do
|
3445
|
+
Person.send(:undef_method, :posts_attributes=)
|
3446
|
+
Person.accepts_nested_attributes_for :posts, allow_destroy: false
|
3447
|
+
end
|
3448
|
+
|
3449
|
+
after(:all) do
|
3450
|
+
Person.send(:undef_method, :posts_attributes=)
|
3451
|
+
Person.accepts_nested_attributes_for :posts
|
3452
|
+
end
|
3453
|
+
|
3454
|
+
[ 1, "1", true, "true" ].each do |truth|
|
3455
|
+
|
3456
|
+
context "when passed a #{truth} with destroy" do
|
3457
|
+
|
3458
|
+
before do
|
3459
|
+
person.posts_attributes =
|
3460
|
+
{
|
3461
|
+
"0" => { "title" => "New Blog", "_destroy" => truth },
|
3462
|
+
"1" => { "title" => "Blog Two" }
|
3463
|
+
}
|
3464
|
+
end
|
3465
|
+
|
3466
|
+
it "adds the the marked document" do
|
3467
|
+
expect(person.posts.first.title).to eq("New Blog")
|
3468
|
+
end
|
3469
|
+
|
3470
|
+
it "adds the new unmarked document" do
|
3471
|
+
expect(person.posts.last.title).to eq("Blog Two")
|
3472
|
+
end
|
3473
|
+
|
3474
|
+
it "adds the correct number of documents" do
|
3475
|
+
expect(person.posts.size).to eq(2)
|
3476
|
+
end
|
3477
|
+
end
|
3478
|
+
end
|
3479
|
+
|
3480
|
+
[ 0, "0", false, "false" ].each do |falsehood|
|
3481
|
+
|
3482
|
+
context "when passed a #{falsehood} with destroy" do
|
3483
|
+
|
3484
|
+
before do
|
3485
|
+
person.posts_attributes =
|
3486
|
+
{
|
3487
|
+
"0" => { "title" => "New Blog", "_destroy" => falsehood },
|
3488
|
+
"1" => { "title" => "Blog Two" }
|
3489
|
+
}
|
3490
|
+
end
|
3491
|
+
|
3492
|
+
it "adds the new marked document" do
|
3493
|
+
expect(person.posts.first.title).to eq("New Blog")
|
3494
|
+
end
|
3495
|
+
|
3496
|
+
it "adds the new unmarked document" do
|
3497
|
+
expect(person.posts.last.title).to eq("Blog Two")
|
3498
|
+
end
|
3499
|
+
|
3500
|
+
it "does not add extra documents" do
|
3501
|
+
expect(person.posts.size).to eq(2)
|
3502
|
+
end
|
3503
|
+
end
|
3504
|
+
end
|
3505
|
+
end
|
3506
|
+
|
3507
|
+
context "when allow destroy is not defined" do
|
3508
|
+
|
3509
|
+
before(:all) do
|
3510
|
+
Person.send(:undef_method, :posts_attributes=)
|
3511
|
+
Person.accepts_nested_attributes_for :posts
|
3512
|
+
end
|
3513
|
+
|
3514
|
+
[ 1, "1", true, "true" ].each do |truth|
|
3515
|
+
|
3516
|
+
context "when passed a #{truth} with destroy" do
|
3517
|
+
|
3518
|
+
before do
|
3519
|
+
person.posts_attributes =
|
3520
|
+
{
|
3521
|
+
"0" => { "title" => "New Blog", "_destroy" => truth },
|
3522
|
+
"1" => { "title" => "Blog Two" }
|
3523
|
+
}
|
3524
|
+
end
|
3525
|
+
|
3526
|
+
it "adds the the marked document" do
|
3527
|
+
expect(person.posts.first.title).to eq("New Blog")
|
3528
|
+
end
|
3529
|
+
|
3530
|
+
it "adds the new unmarked document" do
|
3531
|
+
expect(person.posts.last.title).to eq("Blog Two")
|
3532
|
+
end
|
3533
|
+
|
3534
|
+
it "adds the correct number of documents" do
|
3535
|
+
expect(person.posts.size).to eq(2)
|
3536
|
+
end
|
3537
|
+
end
|
3538
|
+
end
|
3539
|
+
|
3540
|
+
[ 0, "0", false, "false" ].each do |falsehood|
|
3541
|
+
|
3542
|
+
context "when passed a #{falsehood} with destroy" do
|
3543
|
+
|
3544
|
+
before do
|
3545
|
+
person.posts_attributes =
|
3546
|
+
{
|
3547
|
+
"0" => { "title" => "New Blog", "_destroy" => falsehood },
|
3548
|
+
"1" => { "title" => "Blog Two" }
|
3549
|
+
}
|
3550
|
+
end
|
3551
|
+
|
3552
|
+
it "adds the new marked document" do
|
3553
|
+
expect(person.posts.first.title).to eq("New Blog")
|
3554
|
+
end
|
3555
|
+
|
3556
|
+
it "adds the new unmarked document" do
|
3557
|
+
expect(person.posts.last.title).to eq("Blog Two")
|
3558
|
+
end
|
3559
|
+
|
3560
|
+
it "does not add extra documents" do
|
3561
|
+
expect(person.posts.size).to eq(2)
|
3562
|
+
end
|
3563
|
+
end
|
3564
|
+
end
|
3565
|
+
end
|
3566
|
+
end
|
3567
|
+
end
|
3568
|
+
|
3569
|
+
context "when the nested document is invalid" do
|
3570
|
+
|
3571
|
+
before(:all) do
|
3572
|
+
Person.validates_associated(:posts)
|
3573
|
+
end
|
3574
|
+
|
3575
|
+
after(:all) do
|
3576
|
+
Person.reset_callbacks(:validate)
|
3577
|
+
end
|
3578
|
+
|
3579
|
+
before do
|
3580
|
+
person.posts_attributes = {
|
3581
|
+
"0" => { title: "$$$" }
|
3582
|
+
}
|
3583
|
+
end
|
3584
|
+
|
3585
|
+
it "propagates invalidity to parent" do
|
3586
|
+
expect(person).to_not be_valid
|
3587
|
+
expect(person.posts.first).to_not be_valid
|
3588
|
+
end
|
3589
|
+
end
|
3590
|
+
|
3591
|
+
context "when a type is passed" do
|
3592
|
+
|
3593
|
+
let(:shipping_container) do
|
3594
|
+
ShippingContainer.new
|
3595
|
+
end
|
3596
|
+
|
3597
|
+
before do
|
3598
|
+
ShippingContainer.send(:undef_method, :vehicles_attributes=)
|
3599
|
+
ShippingContainer.accepts_nested_attributes_for :vehicles
|
3600
|
+
shipping_container.vehicles_attributes =
|
3601
|
+
{
|
3602
|
+
"foo" => { "_type" => "Car" },
|
3603
|
+
"bar" => { "_type" => "Truck" }
|
3604
|
+
}
|
3605
|
+
end
|
3606
|
+
|
3607
|
+
it "instantiates an object of the given type" do
|
3608
|
+
expect(shipping_container.vehicles.map(&:class)).to eq([Car, Truck])
|
3609
|
+
end
|
3610
|
+
end
|
3611
|
+
end
|
3612
|
+
|
3613
|
+
context "when the relation is a references many to many" do
|
3614
|
+
|
3615
|
+
let(:person) do
|
3616
|
+
Person.new
|
3617
|
+
end
|
3618
|
+
|
3619
|
+
let(:preference_one) do
|
3620
|
+
Preference.new(name: "First preference")
|
3621
|
+
end
|
3622
|
+
|
3623
|
+
let(:preference_two) do
|
3624
|
+
Preference.new(name: "First response")
|
3625
|
+
end
|
3626
|
+
|
3627
|
+
context "when a limit is specified" do
|
3628
|
+
|
3629
|
+
before(:all) do
|
3630
|
+
Person.send(:undef_method, :preferences_attributes=)
|
3631
|
+
Person.accepts_nested_attributes_for :preferences, limit: 2
|
3632
|
+
end
|
3633
|
+
|
3634
|
+
after(:all) do
|
3635
|
+
Person.send(:undef_method, :preferences_attributes=)
|
3636
|
+
Person.accepts_nested_attributes_for :preferences
|
3637
|
+
end
|
3638
|
+
|
3639
|
+
context "when more are provided than the limit" do
|
3640
|
+
|
3641
|
+
let(:attributes) do
|
3642
|
+
{
|
3643
|
+
"foo" => { "name" => "First" },
|
3644
|
+
"bar" => { "name" => "Second" },
|
3645
|
+
"baz" => { "name" => "Third" }
|
3646
|
+
}
|
3647
|
+
end
|
3648
|
+
|
3649
|
+
it "raises an error" do
|
3650
|
+
expect {
|
3651
|
+
person.preferences_attributes = attributes
|
3652
|
+
}.to raise_error(Mongoid::Errors::TooManyNestedAttributeRecords)
|
3653
|
+
end
|
3654
|
+
end
|
3655
|
+
|
3656
|
+
context "when less are provided than the limit" do
|
3657
|
+
|
3658
|
+
let(:attributes) do
|
3659
|
+
{
|
3660
|
+
"foo" => { "name" => "First" },
|
3661
|
+
"bar" => { "name" => "Second" }
|
3662
|
+
}
|
3663
|
+
end
|
3664
|
+
|
3665
|
+
before do
|
3666
|
+
person.preferences_attributes = attributes
|
3667
|
+
end
|
3668
|
+
|
3669
|
+
it "sets the documents on the relation" do
|
3670
|
+
expect(person.preferences.size).to eq(2)
|
3671
|
+
end
|
3672
|
+
end
|
3673
|
+
end
|
3674
|
+
|
3675
|
+
context "when ids are passed" do
|
3676
|
+
|
3677
|
+
let(:person) do
|
3678
|
+
Person.create
|
3679
|
+
end
|
3680
|
+
|
3681
|
+
before do
|
3682
|
+
person.preferences << [ preference_one, preference_two ]
|
3683
|
+
end
|
3684
|
+
|
3685
|
+
context "when no destroy attributes are passed" do
|
3686
|
+
|
3687
|
+
context "when the ids match" do
|
3688
|
+
|
3689
|
+
before do
|
3690
|
+
person.preferences_attributes =
|
3691
|
+
{
|
3692
|
+
"0" => { "id" => preference_one.id, "name" => "First" },
|
3693
|
+
"1" => { "id" => preference_two.id, "name" => "Second" }
|
3694
|
+
}
|
3695
|
+
end
|
3696
|
+
|
3697
|
+
context "when reloading the document" do
|
3698
|
+
|
3699
|
+
it "updates the first existing document" do
|
3700
|
+
expect(person.preferences(true).first.name).to eq("First")
|
3701
|
+
end
|
3702
|
+
|
3703
|
+
it "updates the second existing document" do
|
3704
|
+
expect(person.preferences(true).second.name).to eq("Second")
|
3705
|
+
end
|
3706
|
+
|
3707
|
+
it "does not add new documents" do
|
3708
|
+
expect(person.preferences(true).size).to eq(2)
|
3709
|
+
end
|
3710
|
+
end
|
3711
|
+
end
|
3712
|
+
|
3713
|
+
context "when the ids do not match" do
|
3714
|
+
|
3715
|
+
it "raises an error" do
|
3716
|
+
expect {
|
3717
|
+
person.preferences_attributes =
|
3718
|
+
{ "foo" => { "id" => "test", "name" => "Test" } }
|
3719
|
+
}.to raise_error
|
3720
|
+
end
|
3721
|
+
end
|
3722
|
+
end
|
3723
|
+
|
3724
|
+
context "when destroy attributes are passed" do
|
3725
|
+
|
3726
|
+
context "when the ids match" do
|
3727
|
+
|
3728
|
+
context "when allow_destroy is true" do
|
3729
|
+
|
3730
|
+
before(:all) do
|
3731
|
+
Person.send(:undef_method, :preferences_attributes=)
|
3732
|
+
Person.accepts_nested_attributes_for :preferences, allow_destroy: true
|
3733
|
+
end
|
3734
|
+
|
3735
|
+
after(:all) do
|
3736
|
+
Person.send(:undef_method, :preferences_attributes=)
|
3737
|
+
Person.accepts_nested_attributes_for :preferences
|
3738
|
+
end
|
3739
|
+
|
3740
|
+
[ 1, "1", true, "true" ].each do |truth|
|
3741
|
+
|
3742
|
+
context "when passed a #{truth} with destroy" do
|
3743
|
+
|
3744
|
+
before do
|
3745
|
+
person.preferences_attributes =
|
3746
|
+
{
|
3747
|
+
"0" => { "id" => preference_one.id, "_destroy" => truth },
|
3748
|
+
"1" => { "id" => preference_two.id, "name" => "My Blog" }
|
3749
|
+
}
|
3750
|
+
end
|
3751
|
+
|
3752
|
+
context "when reloading the documents" do
|
3753
|
+
|
3754
|
+
it "deletes the marked document" do
|
3755
|
+
expect(person.preferences(true).size).to eq(1)
|
3756
|
+
end
|
3757
|
+
|
3758
|
+
it "does not delete the unmarked document" do
|
3759
|
+
expect(person.preferences(true).first.name).to eq("My Blog")
|
3760
|
+
end
|
3761
|
+
end
|
3762
|
+
end
|
3763
|
+
end
|
3764
|
+
|
3765
|
+
[ 0, "0", false, "false" ].each do |falsehood|
|
3766
|
+
|
3767
|
+
context "when passed a #{falsehood} with destroy" do
|
3768
|
+
|
3769
|
+
before do
|
3770
|
+
person.preferences_attributes =
|
3771
|
+
{
|
3772
|
+
"0" => { "id" => preference_one.id, "_destroy" => falsehood },
|
3773
|
+
"1" => { "id" => preference_two.id, "name" => "My Blog" }
|
3774
|
+
}
|
3775
|
+
end
|
3776
|
+
|
3777
|
+
context "when reloading the document" do
|
3778
|
+
|
3779
|
+
it "does not delete the marked document" do
|
3780
|
+
expect(person.preferences(true).size).to eq(2)
|
3781
|
+
end
|
3782
|
+
|
3783
|
+
it "does not delete the unmarked document" do
|
3784
|
+
expect(person.preferences(true).last.name).to eq("My Blog")
|
3785
|
+
end
|
3786
|
+
end
|
3787
|
+
end
|
3788
|
+
end
|
3789
|
+
end
|
3790
|
+
|
3791
|
+
context "when allow_destroy is false" do
|
3792
|
+
|
3793
|
+
before(:all) do
|
3794
|
+
Person.send(:undef_method, :preferences_attributes=)
|
3795
|
+
Person.accepts_nested_attributes_for :preferences, allow_destroy: false
|
3796
|
+
end
|
3797
|
+
|
3798
|
+
after(:all) do
|
3799
|
+
Person.send(:undef_method, :preferences_attributes=)
|
3800
|
+
Person.accepts_nested_attributes_for :preferences
|
3801
|
+
end
|
3802
|
+
|
3803
|
+
[ 1, "1", true, "true" ].each do |truth|
|
3804
|
+
|
3805
|
+
context "when passed a #{truth} with destroy" do
|
3806
|
+
|
3807
|
+
before do
|
3808
|
+
person.preferences_attributes =
|
3809
|
+
{
|
3810
|
+
"0" => {
|
3811
|
+
"id" => preference_one.id, "name" => "Another Title", "_destroy" => truth },
|
3812
|
+
"1" => { "id" => preference_two.id, "name" => "New Title" }
|
3813
|
+
}
|
3814
|
+
end
|
3815
|
+
|
3816
|
+
context "when reloading the document" do
|
3817
|
+
|
3818
|
+
it "does not ignore the marked document" do
|
3819
|
+
expect(person.preferences(true).first.name).to eq("Another Title")
|
3820
|
+
end
|
3821
|
+
|
3822
|
+
it "does not delete the unmarked document" do
|
3823
|
+
expect(person.preferences(true).last.name).to eq("New Title")
|
3824
|
+
end
|
3825
|
+
|
3826
|
+
it "does not add additional documents" do
|
3827
|
+
expect(person.preferences(true).size).to eq(2)
|
3828
|
+
end
|
3829
|
+
end
|
3830
|
+
end
|
3831
|
+
end
|
3832
|
+
|
3833
|
+
[ 0, "0", false, "false" ].each do |falsehood|
|
3834
|
+
|
3835
|
+
context "when passed a #{falsehood} with destroy" do
|
3836
|
+
|
3837
|
+
before do
|
3838
|
+
person.preferences_attributes =
|
3839
|
+
{
|
3840
|
+
"0" => { "id" => preference_one.id, "_destroy" => falsehood },
|
3841
|
+
"1" => { "id" => preference_two.id, "name" => "New Title" }
|
3842
|
+
}
|
3843
|
+
end
|
3844
|
+
|
3845
|
+
context "when reloading the documents" do
|
3846
|
+
|
3847
|
+
it "does not delete the marked document" do
|
3848
|
+
expect(person.preferences(true).size).to eq(2)
|
3849
|
+
end
|
3850
|
+
|
3851
|
+
it "does not delete the unmarked document" do
|
3852
|
+
expect(person.preferences(true).last.name).to eq("New Title")
|
3853
|
+
end
|
3854
|
+
end
|
3855
|
+
end
|
3856
|
+
end
|
3857
|
+
end
|
3858
|
+
|
3859
|
+
context "when allow_destroy is undefined" do
|
3860
|
+
|
3861
|
+
before(:all) do
|
3862
|
+
Person.send(:undef_method, :preferences_attributes=)
|
3863
|
+
Person.accepts_nested_attributes_for :preferences
|
3864
|
+
end
|
3865
|
+
|
3866
|
+
[ 1, "1", true, "true" ].each do |truth|
|
3867
|
+
|
3868
|
+
context "when passed a #{truth} with destroy" do
|
3869
|
+
|
3870
|
+
before do
|
3871
|
+
person.preferences_attributes =
|
3872
|
+
{
|
3873
|
+
"0" => {
|
3874
|
+
"id" => preference_one.id, "name" => "Another Title", "_destroy" => truth },
|
3875
|
+
"1" => { "id" => preference_two.id, "name" => "New Title" }
|
3876
|
+
}
|
3877
|
+
end
|
3878
|
+
|
3879
|
+
context "when reloading" do
|
3880
|
+
|
3881
|
+
it "does not ignore the marked document" do
|
3882
|
+
expect(person.preferences(true).first.name).to eq("Another Title")
|
3883
|
+
end
|
3884
|
+
|
3885
|
+
it "does not delete the unmarked document" do
|
3886
|
+
expect(person.preferences(true).last.name).to eq("New Title")
|
3887
|
+
end
|
3888
|
+
|
3889
|
+
it "does not add additional documents" do
|
3890
|
+
expect(person.preferences(true).size).to eq(2)
|
3891
|
+
end
|
3892
|
+
end
|
3893
|
+
end
|
3894
|
+
end
|
3895
|
+
|
3896
|
+
[ 0, "0", false, "false" ].each do |falsehood|
|
3897
|
+
|
3898
|
+
context "when passed a #{falsehood} with destroy" do
|
3899
|
+
|
3900
|
+
before do
|
3901
|
+
person.preferences_attributes =
|
3902
|
+
{
|
3903
|
+
"0" => { "id" => preference_one.id, "_destroy" => falsehood },
|
3904
|
+
"1" => { "id" => preference_two.id, "name" => "New Title" }
|
3905
|
+
}
|
3906
|
+
end
|
3907
|
+
|
3908
|
+
context "when reloading" do
|
3909
|
+
|
3910
|
+
it "does not delete the marked document" do
|
3911
|
+
expect(person.preferences(true).size).to eq(2)
|
3912
|
+
end
|
3913
|
+
|
3914
|
+
it "does not delete the unmarked document" do
|
3915
|
+
expect(person.preferences(true).last.name).to eq("New Title")
|
3916
|
+
end
|
3917
|
+
end
|
3918
|
+
end
|
3919
|
+
end
|
3920
|
+
end
|
3921
|
+
end
|
3922
|
+
end
|
3923
|
+
end
|
3924
|
+
|
3925
|
+
context "when no ids are passed" do
|
3926
|
+
|
3927
|
+
context "when no destroy attributes are passed" do
|
3928
|
+
|
3929
|
+
before do
|
3930
|
+
person.preferences_attributes =
|
3931
|
+
{
|
3932
|
+
"4" => { "name" => "Third" },
|
3933
|
+
"1" => { "name" => "First" },
|
3934
|
+
"2" => { "name" => "Second" }
|
3935
|
+
}
|
3936
|
+
end
|
3937
|
+
|
3938
|
+
it "builds a new first document" do
|
3939
|
+
expect(person.preferences.first.name).to eq("First")
|
3940
|
+
end
|
3941
|
+
|
3942
|
+
it "builds a new second document" do
|
3943
|
+
expect(person.preferences.second.name).to eq("Second")
|
3944
|
+
end
|
3945
|
+
|
3946
|
+
it "builds a new third document" do
|
3947
|
+
expect(person.preferences.third.name).to eq("Third")
|
3948
|
+
end
|
3949
|
+
|
3950
|
+
it "does not add extra documents" do
|
3951
|
+
expect(person.preferences.size).to eq(3)
|
3952
|
+
end
|
3953
|
+
|
3954
|
+
it "adds the documents in the sorted hash key order" do
|
3955
|
+
expect(person.preferences.map(&:name)).to eq(
|
3956
|
+
[ "First", "Second", "Third" ]
|
3957
|
+
)
|
3958
|
+
end
|
3959
|
+
end
|
3960
|
+
|
3961
|
+
context "when a reject block is supplied" do
|
3962
|
+
|
3963
|
+
before(:all) do
|
3964
|
+
Person.send(:undef_method, :preferences_attributes=)
|
3965
|
+
Person.accepts_nested_attributes_for \
|
3966
|
+
:preferences, reject_if: ->(attrs){ attrs["name"].blank? }
|
3967
|
+
end
|
3968
|
+
|
3969
|
+
after(:all) do
|
3970
|
+
Person.send(:undef_method, :preferences_attributes=)
|
3971
|
+
Person.accepts_nested_attributes_for :preferences
|
3972
|
+
end
|
3973
|
+
|
3974
|
+
context "when the attributes match" do
|
3975
|
+
|
3976
|
+
before do
|
3977
|
+
person.preferences_attributes =
|
3978
|
+
{ "3" => { "content" => "My first blog" } }
|
3979
|
+
end
|
3980
|
+
|
3981
|
+
it "does not add the new document" do
|
3982
|
+
expect(person.preferences).to be_empty
|
3983
|
+
end
|
3984
|
+
end
|
3985
|
+
|
3986
|
+
context "when the attributes do not match" do
|
3987
|
+
|
3988
|
+
before do
|
3989
|
+
person.preferences_attributes =
|
3990
|
+
{ "3" => { "name" => "Blogging" } }
|
3991
|
+
end
|
3992
|
+
|
3993
|
+
it "adds the new document" do
|
3994
|
+
expect(person.preferences.size).to eq(1)
|
3995
|
+
end
|
3996
|
+
|
3997
|
+
it "sets the correct attributes" do
|
3998
|
+
expect(person.preferences.first.name).to eq("Blogging")
|
3999
|
+
end
|
4000
|
+
end
|
4001
|
+
end
|
4002
|
+
|
4003
|
+
context "when :reject_if => :all_blank is supplied" do
|
4004
|
+
|
4005
|
+
before(:all) do
|
4006
|
+
Person.send(:undef_method, :preferences_attributes=)
|
4007
|
+
Person.accepts_nested_attributes_for \
|
4008
|
+
:preferences, reject_if: :all_blank
|
4009
|
+
end
|
4010
|
+
|
4011
|
+
after(:all) do
|
4012
|
+
Person.send(:undef_method, :preferences_attributes=)
|
4013
|
+
Person.accepts_nested_attributes_for :preferences
|
4014
|
+
end
|
4015
|
+
|
4016
|
+
context "when all attributes are empty" do
|
4017
|
+
|
4018
|
+
before do
|
4019
|
+
person.preferences_attributes =
|
4020
|
+
{ "3" => { "content" => "" } }
|
4021
|
+
end
|
4022
|
+
|
4023
|
+
it "does not add the new document" do
|
4024
|
+
expect(person.preferences).to be_empty
|
4025
|
+
end
|
4026
|
+
end
|
4027
|
+
|
4028
|
+
context "when an attribute is non-empty" do
|
4029
|
+
|
4030
|
+
before do
|
4031
|
+
person.preferences_attributes =
|
4032
|
+
{ "3" => { "name" => "Blogging" } }
|
4033
|
+
end
|
4034
|
+
|
4035
|
+
it "adds the new document" do
|
4036
|
+
expect(person.preferences.size).to eq(1)
|
4037
|
+
end
|
4038
|
+
|
4039
|
+
it "sets the correct attributes" do
|
4040
|
+
expect(person.preferences.first.name).to eq("Blogging")
|
4041
|
+
end
|
4042
|
+
end
|
4043
|
+
end
|
4044
|
+
|
4045
|
+
context "when destroy attributes are passed" do
|
4046
|
+
|
4047
|
+
context "when allow_destroy is true" do
|
4048
|
+
|
4049
|
+
before(:all) do
|
4050
|
+
Person.send(:undef_method, :preferences_attributes=)
|
4051
|
+
Person.accepts_nested_attributes_for :preferences, allow_destroy: true
|
4052
|
+
end
|
4053
|
+
|
4054
|
+
after(:all) do
|
4055
|
+
Person.send(:undef_method, :preferences_attributes=)
|
4056
|
+
Person.accepts_nested_attributes_for :preferences
|
4057
|
+
end
|
4058
|
+
|
4059
|
+
[ 1, "1", true, "true" ].each do |truth|
|
4060
|
+
|
4061
|
+
context "when passed a #{truth} with destroy" do
|
4062
|
+
|
4063
|
+
before do
|
4064
|
+
person.preferences_attributes =
|
4065
|
+
{
|
4066
|
+
"0" => { "name" => "New Blog", "_destroy" => truth },
|
4067
|
+
"1" => { "name" => "Blog Two" }
|
4068
|
+
}
|
4069
|
+
end
|
4070
|
+
|
4071
|
+
it "ignores the the marked document" do
|
4072
|
+
expect(person.preferences.size).to eq(1)
|
4073
|
+
end
|
4074
|
+
|
4075
|
+
it "adds the new unmarked document" do
|
4076
|
+
expect(person.preferences.first.name).to eq("Blog Two")
|
4077
|
+
end
|
4078
|
+
end
|
4079
|
+
end
|
4080
|
+
|
4081
|
+
[ 0, "0", false, "false" ].each do |falsehood|
|
4082
|
+
|
4083
|
+
context "when passed a #{falsehood} with destroy" do
|
4084
|
+
|
4085
|
+
before do
|
4086
|
+
person.preferences_attributes =
|
4087
|
+
{
|
4088
|
+
"0" => { "name" => "New Blog", "_destroy" => falsehood },
|
4089
|
+
"1" => { "name" => "Blog Two" }
|
4090
|
+
}
|
4091
|
+
end
|
4092
|
+
|
4093
|
+
it "adds the new marked document" do
|
4094
|
+
expect(person.preferences.first.name).to eq("New Blog")
|
4095
|
+
end
|
4096
|
+
|
4097
|
+
it "adds the new unmarked document" do
|
4098
|
+
expect(person.preferences.last.name).to eq("Blog Two")
|
4099
|
+
end
|
4100
|
+
|
4101
|
+
it "does not add extra documents" do
|
4102
|
+
expect(person.preferences.size).to eq(2)
|
4103
|
+
end
|
4104
|
+
end
|
4105
|
+
end
|
4106
|
+
end
|
4107
|
+
|
4108
|
+
context "when allow destroy is false" do
|
4109
|
+
|
4110
|
+
before(:all) do
|
4111
|
+
Person.send(:undef_method, :preferences_attributes=)
|
4112
|
+
Person.accepts_nested_attributes_for :preferences, allow_destroy: false
|
4113
|
+
end
|
4114
|
+
|
4115
|
+
after(:all) do
|
4116
|
+
Person.send(:undef_method, :preferences_attributes=)
|
4117
|
+
Person.accepts_nested_attributes_for :preferences
|
4118
|
+
end
|
4119
|
+
|
4120
|
+
[ 1, "1", true, "true" ].each do |truth|
|
4121
|
+
|
4122
|
+
context "when passed a #{truth} with destroy" do
|
4123
|
+
|
4124
|
+
before do
|
4125
|
+
person.preferences_attributes =
|
4126
|
+
{
|
4127
|
+
"0" => { "name" => "New Blog", "_destroy" => truth },
|
4128
|
+
"1" => { "name" => "Blog Two" }
|
4129
|
+
}
|
4130
|
+
end
|
4131
|
+
|
4132
|
+
it "adds the the marked document" do
|
4133
|
+
expect(person.preferences.first.name).to eq("New Blog")
|
4134
|
+
end
|
4135
|
+
|
4136
|
+
it "adds the new unmarked document" do
|
4137
|
+
expect(person.preferences.last.name).to eq("Blog Two")
|
4138
|
+
end
|
4139
|
+
|
4140
|
+
it "adds the correct number of documents" do
|
4141
|
+
expect(person.preferences.size).to eq(2)
|
4142
|
+
end
|
4143
|
+
end
|
4144
|
+
end
|
4145
|
+
|
4146
|
+
[ 0, "0", false, "false" ].each do |falsehood|
|
4147
|
+
|
4148
|
+
context "when passed a #{falsehood} with destroy" do
|
4149
|
+
|
4150
|
+
before do
|
4151
|
+
person.preferences_attributes =
|
4152
|
+
{
|
4153
|
+
"0" => { "name" => "New Blog", "_destroy" => falsehood },
|
4154
|
+
"1" => { "name" => "Blog Two" }
|
4155
|
+
}
|
4156
|
+
end
|
4157
|
+
|
4158
|
+
it "adds the new marked document" do
|
4159
|
+
expect(person.preferences.first.name).to eq("New Blog")
|
4160
|
+
end
|
4161
|
+
|
4162
|
+
it "adds the new unmarked document" do
|
4163
|
+
expect(person.preferences.last.name).to eq("Blog Two")
|
4164
|
+
end
|
4165
|
+
|
4166
|
+
it "does not add extra documents" do
|
4167
|
+
expect(person.preferences.size).to eq(2)
|
4168
|
+
end
|
4169
|
+
end
|
4170
|
+
end
|
4171
|
+
end
|
4172
|
+
|
4173
|
+
context "when allow destroy is not defined" do
|
4174
|
+
|
4175
|
+
before(:all) do
|
4176
|
+
Person.send(:undef_method, :preferences_attributes=)
|
4177
|
+
Person.accepts_nested_attributes_for :preferences
|
4178
|
+
end
|
4179
|
+
|
4180
|
+
[ 1, "1", true, "true" ].each do |truth|
|
4181
|
+
|
4182
|
+
context "when passed a #{truth} with destroy" do
|
4183
|
+
|
4184
|
+
before do
|
4185
|
+
person.preferences_attributes =
|
4186
|
+
{
|
4187
|
+
"0" => { "name" => "New Blog", "_destroy" => truth },
|
4188
|
+
"1" => { "name" => "Blog Two" }
|
4189
|
+
}
|
4190
|
+
end
|
4191
|
+
|
4192
|
+
it "adds the the marked document" do
|
4193
|
+
expect(person.preferences.first.name).to eq("New Blog")
|
4194
|
+
end
|
4195
|
+
|
4196
|
+
it "adds the new unmarked document" do
|
4197
|
+
expect(person.preferences.last.name).to eq("Blog Two")
|
4198
|
+
end
|
4199
|
+
|
4200
|
+
it "adds the correct number of documents" do
|
4201
|
+
expect(person.preferences.size).to eq(2)
|
4202
|
+
end
|
4203
|
+
end
|
4204
|
+
end
|
4205
|
+
|
4206
|
+
[ 0, "0", false, "false" ].each do |falsehood|
|
4207
|
+
|
4208
|
+
context "when passed a #{falsehood} with destroy" do
|
4209
|
+
|
4210
|
+
before do
|
4211
|
+
person.preferences_attributes =
|
4212
|
+
{
|
4213
|
+
"0" => { "name" => "New Blog", "_destroy" => falsehood },
|
4214
|
+
"1" => { "name" => "Blog Two" }
|
4215
|
+
}
|
4216
|
+
end
|
4217
|
+
|
4218
|
+
it "adds the new marked document" do
|
4219
|
+
expect(person.preferences.first.name).to eq("New Blog")
|
4220
|
+
end
|
4221
|
+
|
4222
|
+
it "adds the new unmarked document" do
|
4223
|
+
expect(person.preferences.last.name).to eq("Blog Two")
|
4224
|
+
end
|
4225
|
+
|
4226
|
+
it "does not add extra documents" do
|
4227
|
+
expect(person.preferences.size).to eq(2)
|
4228
|
+
end
|
4229
|
+
end
|
4230
|
+
end
|
4231
|
+
end
|
4232
|
+
end
|
4233
|
+
end
|
4234
|
+
|
4235
|
+
context "when the nested document is invalid" do
|
4236
|
+
|
4237
|
+
before(:all) do
|
4238
|
+
Person.validates_associated(:preferences)
|
4239
|
+
end
|
4240
|
+
|
4241
|
+
after(:all) do
|
4242
|
+
Person.reset_callbacks(:validate)
|
4243
|
+
end
|
4244
|
+
|
4245
|
+
before do
|
4246
|
+
person.preferences_attributes = {
|
4247
|
+
"0" => { name: 'x' }
|
4248
|
+
}
|
4249
|
+
end
|
4250
|
+
|
4251
|
+
it "propagates invalidity to parent" do
|
4252
|
+
expect(person.preferences.first).to_not be_valid
|
4253
|
+
expect(person).to_not be_valid
|
4254
|
+
end
|
4255
|
+
end
|
4256
|
+
end
|
4257
|
+
end
|
4258
|
+
end
|
4259
|
+
|
4260
|
+
describe "#update_attributes" do
|
4261
|
+
|
4262
|
+
before(:all) do
|
4263
|
+
Person.send(:undef_method, :addresses_attributes=)
|
4264
|
+
Person.accepts_nested_attributes_for :addresses
|
4265
|
+
end
|
4266
|
+
|
4267
|
+
context "when embedding one level behind a has many" do
|
4268
|
+
|
4269
|
+
let(:node) do
|
4270
|
+
Node.create
|
4271
|
+
end
|
4272
|
+
|
4273
|
+
let!(:server) do
|
4274
|
+
node.servers.create(name: "prod")
|
4275
|
+
end
|
4276
|
+
|
4277
|
+
context "when adding a new embedded document" do
|
4278
|
+
|
4279
|
+
let(:attributes) do
|
4280
|
+
{ servers_attributes:
|
4281
|
+
{ "0" =>
|
4282
|
+
{
|
4283
|
+
_id: server.id,
|
4284
|
+
filesystems_attributes: {
|
4285
|
+
"0" => { name: "NFS" }
|
4286
|
+
}
|
4287
|
+
}
|
4288
|
+
}
|
4289
|
+
}
|
4290
|
+
end
|
4291
|
+
|
4292
|
+
before do
|
4293
|
+
node.update_attributes(attributes)
|
4294
|
+
end
|
4295
|
+
|
4296
|
+
it "adds the new embedded document" do
|
4297
|
+
expect(server.reload.filesystems.first.name).to eq("NFS")
|
4298
|
+
end
|
4299
|
+
|
4300
|
+
it "does not add more than one document" do
|
4301
|
+
expect(server.reload.filesystems.count).to eq(1)
|
4302
|
+
end
|
4303
|
+
end
|
4304
|
+
end
|
4305
|
+
|
4306
|
+
context "when deleting the child document" do
|
4307
|
+
|
4308
|
+
let(:person) do
|
4309
|
+
Person.create
|
4310
|
+
end
|
4311
|
+
|
4312
|
+
let!(:service) do
|
4313
|
+
person.services.create(sid: "123")
|
4314
|
+
end
|
4315
|
+
|
4316
|
+
let(:attributes) do
|
4317
|
+
{ services_attributes:
|
4318
|
+
{ "0" =>
|
4319
|
+
{ _id: service.id, sid: service.sid, _destroy: 1 }
|
4320
|
+
}
|
4321
|
+
}
|
4322
|
+
end
|
4323
|
+
|
4324
|
+
before do
|
4325
|
+
person.update_attributes(attributes)
|
4326
|
+
end
|
4327
|
+
|
4328
|
+
it "removes the document from the parent" do
|
4329
|
+
expect(person.services).to be_empty
|
4330
|
+
end
|
4331
|
+
|
4332
|
+
it "deletes the document" do
|
4333
|
+
expect(service).to be_destroyed
|
4334
|
+
end
|
4335
|
+
|
4336
|
+
it "runs the before destroy callbacks" do
|
4337
|
+
expect(service.before_destroy_called).to be_true
|
4338
|
+
end
|
4339
|
+
|
4340
|
+
it "runs the after destroy callbacks" do
|
4341
|
+
expect(service.after_destroy_called).to be_true
|
4342
|
+
end
|
4343
|
+
|
4344
|
+
it "clears the delayed atomic pulls from the parent" do
|
4345
|
+
expect(person.delayed_atomic_pulls).to be_empty
|
4346
|
+
end
|
4347
|
+
end
|
4348
|
+
|
4349
|
+
context "when nesting multiple levels and parent is timestamped" do
|
4350
|
+
|
4351
|
+
before(:all) do
|
4352
|
+
class Address
|
4353
|
+
after_save do
|
4354
|
+
addressable.touch
|
4355
|
+
end
|
4356
|
+
end
|
4357
|
+
end
|
4358
|
+
|
4359
|
+
after(:all) do
|
4360
|
+
Address.reset_callbacks(:save)
|
4361
|
+
end
|
4362
|
+
|
4363
|
+
let(:dokument) do
|
4364
|
+
Dokument.create
|
4365
|
+
end
|
4366
|
+
|
4367
|
+
let!(:address) do
|
4368
|
+
dokument.addresses.create(street: "hobrecht")
|
4369
|
+
end
|
4370
|
+
|
4371
|
+
let!(:location) do
|
4372
|
+
address.locations.create(name: "work")
|
4373
|
+
end
|
4374
|
+
|
4375
|
+
let(:attributes) do
|
4376
|
+
{
|
4377
|
+
locations_attributes: {
|
4378
|
+
a: { name: "home" }
|
4379
|
+
}
|
4380
|
+
}
|
4381
|
+
end
|
4382
|
+
|
4383
|
+
before do
|
4384
|
+
address.update_attributes(attributes)
|
4385
|
+
address.reload
|
4386
|
+
end
|
4387
|
+
|
4388
|
+
it "does not add any extra locations" do
|
4389
|
+
expect(address.locations.size).to eq(2)
|
4390
|
+
end
|
4391
|
+
end
|
4392
|
+
|
4393
|
+
context "when nesting multiple levels" do
|
4394
|
+
|
4395
|
+
let!(:person) do
|
4396
|
+
Person.create
|
4397
|
+
end
|
4398
|
+
|
4399
|
+
context "when second level is a one to many" do
|
4400
|
+
|
4401
|
+
let(:person_one) do
|
4402
|
+
Person.create
|
4403
|
+
end
|
4404
|
+
|
4405
|
+
let!(:address_one) do
|
4406
|
+
person_one.addresses.create(street: "hobrecht")
|
4407
|
+
end
|
4408
|
+
|
4409
|
+
let!(:location_one) do
|
4410
|
+
address_one.locations.create(name: "home")
|
4411
|
+
end
|
4412
|
+
|
4413
|
+
context "when destroying a second level document" do
|
4414
|
+
|
4415
|
+
let(:attributes) do
|
4416
|
+
{ addresses_attributes:
|
4417
|
+
{ "0" =>
|
4418
|
+
{
|
4419
|
+
_id: address_one.id,
|
4420
|
+
locations_attributes: { "0" => { _id: location_one.id, _destroy: true }}
|
4421
|
+
}
|
4422
|
+
}
|
4423
|
+
}
|
4424
|
+
end
|
4425
|
+
|
4426
|
+
before do
|
4427
|
+
person_one.update_attributes(attributes)
|
4428
|
+
end
|
4429
|
+
|
4430
|
+
it "deletes the document from the relation" do
|
4431
|
+
expect(address_one.locations).to be_empty
|
4432
|
+
end
|
4433
|
+
|
4434
|
+
it "persists the change" do
|
4435
|
+
expect(address_one.reload.locations).to be_empty
|
4436
|
+
end
|
4437
|
+
end
|
4438
|
+
|
4439
|
+
context "when destroying a second level document with callbacks" do
|
4440
|
+
|
4441
|
+
let(:band) do
|
4442
|
+
Band.create(name: "Tool")
|
4443
|
+
end
|
4444
|
+
|
4445
|
+
let(:record) do
|
4446
|
+
band.records.create(name: "Undertow")
|
4447
|
+
end
|
4448
|
+
|
4449
|
+
let!(:track) do
|
4450
|
+
record.tracks.create(name: "Sober")
|
4451
|
+
end
|
4452
|
+
|
4453
|
+
context "when cascading callbacks" do
|
4454
|
+
|
4455
|
+
before(:all) do
|
4456
|
+
Band.accepts_nested_attributes_for :records
|
4457
|
+
Record.accepts_nested_attributes_for :tracks, allow_destroy: true
|
4458
|
+
end
|
4459
|
+
|
4460
|
+
after(:all) do
|
4461
|
+
Band.send(:undef_method, :records_attributes=)
|
4462
|
+
Record.send(:undef_method, :tracks_attributes=)
|
4463
|
+
end
|
4464
|
+
|
4465
|
+
let(:attributes) do
|
4466
|
+
{ records_attributes:
|
4467
|
+
{ "0" =>
|
4468
|
+
{
|
4469
|
+
_id: record.id,
|
4470
|
+
tracks_attributes: { "0" => { _id: track.id, _destroy: true }}
|
4471
|
+
}
|
4472
|
+
}
|
4473
|
+
}
|
4474
|
+
end
|
4475
|
+
|
4476
|
+
before do
|
4477
|
+
band.update_attributes(attributes)
|
4478
|
+
end
|
4479
|
+
|
4480
|
+
it "removes the child from the relation" do
|
4481
|
+
expect(record.tracks).to be_empty
|
4482
|
+
end
|
4483
|
+
|
4484
|
+
it "deletes the child document" do
|
4485
|
+
expect(track).to be_destroyed
|
4486
|
+
end
|
4487
|
+
|
4488
|
+
it "runs the child's callbacks" do
|
4489
|
+
expect(track.before_destroy_called).to be_true
|
4490
|
+
end
|
4491
|
+
end
|
4492
|
+
end
|
4493
|
+
|
4494
|
+
context "when adding new documents in both levels" do
|
4495
|
+
|
4496
|
+
context "when no documents has previously existed" do
|
4497
|
+
|
4498
|
+
let(:attributes) do
|
4499
|
+
{ addresses_attributes:
|
4500
|
+
{ "0" =>
|
4501
|
+
{
|
4502
|
+
street: "Alexanderstr",
|
4503
|
+
locations_attributes: { "0" => { name: "Home" } }
|
4504
|
+
}
|
4505
|
+
}
|
4506
|
+
}
|
4507
|
+
end
|
4508
|
+
|
4509
|
+
before do
|
4510
|
+
person.update_attributes(attributes)
|
4511
|
+
end
|
4512
|
+
|
4513
|
+
let(:address) do
|
4514
|
+
person.addresses.first
|
4515
|
+
end
|
4516
|
+
|
4517
|
+
let(:location) do
|
4518
|
+
address.locations.first
|
4519
|
+
end
|
4520
|
+
|
4521
|
+
it "adds the new first level embedded document" do
|
4522
|
+
expect(address.street).to eq("Alexanderstr")
|
4523
|
+
end
|
4524
|
+
|
4525
|
+
it "adds the nested embedded document" do
|
4526
|
+
expect(location.name).to eq("Home")
|
4527
|
+
end
|
4528
|
+
end
|
4529
|
+
|
4530
|
+
context "when adding to an existing document in the first level" do
|
4531
|
+
|
4532
|
+
let!(:address) do
|
4533
|
+
person.addresses.create(street: "hobrecht")
|
4534
|
+
end
|
4535
|
+
|
4536
|
+
let!(:location) do
|
4537
|
+
address.locations.create(name: "work")
|
4538
|
+
end
|
4539
|
+
|
4540
|
+
let(:attributes) do
|
4541
|
+
{
|
4542
|
+
addresses_attributes: {
|
4543
|
+
a: { id: address.id, locations_attributes: { b: { name: "home" }}},
|
4544
|
+
c: { street: "pfluger" }
|
4545
|
+
}
|
4546
|
+
}
|
4547
|
+
end
|
4548
|
+
|
4549
|
+
before do
|
4550
|
+
person.update_attributes(attributes)
|
4551
|
+
person.reload
|
4552
|
+
end
|
4553
|
+
|
4554
|
+
it "adds the new location to the existing address" do
|
4555
|
+
expect(person.addresses.first.locations.count).to eq(2)
|
4556
|
+
end
|
4557
|
+
|
4558
|
+
it "adds the new address" do
|
4559
|
+
expect(person.addresses.count).to eq(2)
|
4560
|
+
end
|
4561
|
+
end
|
4562
|
+
end
|
4563
|
+
end
|
4564
|
+
|
4565
|
+
context "when the second level is a one to one" do
|
4566
|
+
|
4567
|
+
context "when the nested document is new" do
|
4568
|
+
|
4569
|
+
let(:attributes) do
|
4570
|
+
{ addresses_attributes:
|
4571
|
+
{ "0" =>
|
4572
|
+
{
|
4573
|
+
street: "Alexanderstr",
|
4574
|
+
code_attributes: { name: "Home" }
|
4575
|
+
}
|
4576
|
+
}
|
4577
|
+
}
|
4578
|
+
end
|
4579
|
+
|
4580
|
+
before do
|
4581
|
+
person.update_attributes(attributes)
|
4582
|
+
end
|
4583
|
+
|
4584
|
+
let(:address) do
|
4585
|
+
person.addresses.first
|
4586
|
+
end
|
4587
|
+
|
4588
|
+
let(:code) do
|
4589
|
+
address.code
|
4590
|
+
end
|
4591
|
+
|
4592
|
+
it "adds the new first level embedded document" do
|
4593
|
+
expect(address.street).to eq("Alexanderstr")
|
4594
|
+
end
|
4595
|
+
|
4596
|
+
it "adds the nested embedded document" do
|
4597
|
+
expect(code.name).to eq("Home")
|
4598
|
+
end
|
4599
|
+
end
|
4600
|
+
end
|
4601
|
+
|
4602
|
+
context "when the nested document is getting updated" do
|
4603
|
+
|
4604
|
+
context "when the nested document is not polymorphic" do
|
4605
|
+
|
4606
|
+
let!(:address) do
|
4607
|
+
person.addresses.create(street: "Alexanderstr", number: 1)
|
4608
|
+
end
|
4609
|
+
|
4610
|
+
let!(:code) do
|
4611
|
+
address.create_code(name: "Home")
|
4612
|
+
end
|
4613
|
+
|
4614
|
+
let(:attributes) do
|
4615
|
+
{ addresses_attributes:
|
4616
|
+
{ "0" =>
|
4617
|
+
{
|
4618
|
+
_id: address.id,
|
4619
|
+
number: 45,
|
4620
|
+
code_attributes: {
|
4621
|
+
_id: code.id,
|
4622
|
+
name: "Work"
|
4623
|
+
}
|
4624
|
+
}
|
4625
|
+
}
|
4626
|
+
}
|
4627
|
+
end
|
4628
|
+
|
4629
|
+
before do
|
4630
|
+
person.update_attributes(attributes)
|
4631
|
+
end
|
4632
|
+
|
4633
|
+
it "updates the first level embedded document" do
|
4634
|
+
expect(address.number).to eq(45)
|
4635
|
+
end
|
4636
|
+
|
4637
|
+
it "updates the nested embedded document" do
|
4638
|
+
expect(code.name).to eq("Work")
|
4639
|
+
end
|
4640
|
+
end
|
4641
|
+
|
4642
|
+
context "when the nested document is polymorphic" do
|
4643
|
+
|
4644
|
+
context "when the first level is an embeds many" do
|
4645
|
+
|
4646
|
+
let!(:address) do
|
4647
|
+
person.addresses.create(street: "Alexanderstr", number: 1)
|
4648
|
+
end
|
4649
|
+
|
4650
|
+
let!(:target) do
|
4651
|
+
address.create_target(name: "test")
|
4652
|
+
end
|
4653
|
+
|
4654
|
+
let(:attributes) do
|
4655
|
+
{ addresses_attributes:
|
4656
|
+
{ "0" =>
|
4657
|
+
{
|
4658
|
+
_id: address.id,
|
4659
|
+
number: 45,
|
4660
|
+
target_attributes: {
|
4661
|
+
_id: target.id,
|
4662
|
+
name: "updated"
|
4663
|
+
}
|
4664
|
+
}
|
4665
|
+
}
|
4666
|
+
}
|
4667
|
+
end
|
4668
|
+
|
4669
|
+
before do
|
4670
|
+
person.update_attributes(attributes)
|
4671
|
+
end
|
4672
|
+
|
4673
|
+
it "updates the first level embedded document" do
|
4674
|
+
expect(address.number).to eq(45)
|
4675
|
+
end
|
4676
|
+
|
4677
|
+
it "updates the nested embedded document" do
|
4678
|
+
expect(target.name).to eq("updated")
|
4679
|
+
end
|
4680
|
+
end
|
4681
|
+
|
4682
|
+
context "when the first level is an embeds one" do
|
4683
|
+
|
4684
|
+
context "when the id is passed as a string" do
|
4685
|
+
|
4686
|
+
let!(:name) do
|
4687
|
+
person.create_name(first_name: "john", last_name: "doe")
|
4688
|
+
end
|
4689
|
+
|
4690
|
+
let!(:language) do
|
4691
|
+
name.create_language(name: "english")
|
4692
|
+
end
|
4693
|
+
|
4694
|
+
let(:attributes) do
|
4695
|
+
{ name_attributes:
|
4696
|
+
{
|
4697
|
+
language_attributes: {
|
4698
|
+
_id: language.id.to_s,
|
4699
|
+
name: "deutsch"
|
4700
|
+
}
|
4701
|
+
}
|
4702
|
+
}
|
4703
|
+
end
|
4704
|
+
|
4705
|
+
before do
|
4706
|
+
person.update_attributes(attributes)
|
4707
|
+
end
|
4708
|
+
|
4709
|
+
it "updates the nested embedded document" do
|
4710
|
+
expect(language.name).to eq("deutsch")
|
4711
|
+
end
|
4712
|
+
end
|
4713
|
+
end
|
4714
|
+
end
|
4715
|
+
end
|
4716
|
+
end
|
4717
|
+
|
4718
|
+
context "when the relation is a has many" do
|
4719
|
+
|
4720
|
+
context "when updating with valid attributes" do
|
4721
|
+
|
4722
|
+
let(:user) do
|
4723
|
+
User.create
|
4724
|
+
end
|
4725
|
+
|
4726
|
+
let(:params) do
|
4727
|
+
{ posts_attributes:
|
4728
|
+
{ "0" => { title: "Testing" }}
|
4729
|
+
}
|
4730
|
+
end
|
4731
|
+
|
4732
|
+
before do
|
4733
|
+
user.update_attributes(params)
|
4734
|
+
end
|
4735
|
+
|
4736
|
+
let(:post) do
|
4737
|
+
user.posts.first
|
4738
|
+
end
|
4739
|
+
|
4740
|
+
it "adds the new document to the relation" do
|
4741
|
+
expect(post.title).to eq("Testing")
|
4742
|
+
end
|
4743
|
+
|
4744
|
+
it "autosaves the relation" do
|
4745
|
+
expect(user.posts(true).first.title).to eq("Testing")
|
4746
|
+
end
|
4747
|
+
end
|
4748
|
+
|
4749
|
+
context "when the document is freshly loaded from the db" do
|
4750
|
+
|
4751
|
+
let!(:node) do
|
4752
|
+
Node.create
|
4753
|
+
end
|
4754
|
+
|
4755
|
+
let!(:server) do
|
4756
|
+
node.servers.create(name: "test")
|
4757
|
+
end
|
4758
|
+
|
4759
|
+
before do
|
4760
|
+
node.reload
|
4761
|
+
end
|
4762
|
+
|
4763
|
+
context "when updating invalid attributes" do
|
4764
|
+
|
4765
|
+
let!(:update) do
|
4766
|
+
node.update_attributes({
|
4767
|
+
servers_attributes: { "0" => { "_id" => server.id, "name" => "" }}
|
4768
|
+
})
|
4769
|
+
end
|
4770
|
+
|
4771
|
+
it "returns false" do
|
4772
|
+
expect(update).to be_false
|
4773
|
+
end
|
4774
|
+
|
4775
|
+
it "does not update the child document" do
|
4776
|
+
expect(server.reload.name).to eq("test")
|
4777
|
+
end
|
4778
|
+
|
4779
|
+
it "adds the errors to the document" do
|
4780
|
+
expect(node.errors[:servers]).to_not be_nil
|
4781
|
+
end
|
4782
|
+
end
|
4783
|
+
end
|
4784
|
+
end
|
4785
|
+
|
4786
|
+
context "when the relation is an embeds many" do
|
4787
|
+
|
4788
|
+
let(:league) do
|
4789
|
+
League.create
|
4790
|
+
end
|
4791
|
+
|
4792
|
+
let!(:division) do
|
4793
|
+
league.divisions.create(name: "Old Name")
|
4794
|
+
end
|
4795
|
+
|
4796
|
+
let(:params) do
|
4797
|
+
{ divisions_attributes:
|
4798
|
+
{ "0" => { id: division.id.to_s, name: "New Name" }}
|
4799
|
+
}
|
4800
|
+
end
|
4801
|
+
|
4802
|
+
before do
|
4803
|
+
league.update_attributes(params)
|
4804
|
+
end
|
4805
|
+
|
4806
|
+
it "sets the nested attributes" do
|
4807
|
+
expect(league.reload.divisions.first.name).to eq("New Name")
|
4808
|
+
end
|
4809
|
+
|
4810
|
+
context "with corrupted data" do
|
4811
|
+
|
4812
|
+
before do
|
4813
|
+
league[:league] = params
|
4814
|
+
end
|
4815
|
+
|
4816
|
+
let(:new_params) do
|
4817
|
+
{ divisions_attributes:
|
4818
|
+
{ "0" => { id: division.id.to_s, name: "Name" }}
|
4819
|
+
}
|
4820
|
+
end
|
4821
|
+
|
4822
|
+
before do
|
4823
|
+
league.update_attributes(new_params)
|
4824
|
+
end
|
4825
|
+
|
4826
|
+
it "sets the nested attributes" do
|
4827
|
+
expect(league.reload.divisions.first.name).to eq("Name")
|
4828
|
+
end
|
4829
|
+
end
|
4830
|
+
end
|
4831
|
+
end
|
4832
|
+
end
|