mongoid_rails4 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +3213 -0
- data/LICENSE +20 -0
- data/README.md +62 -0
- data/Rakefile +35 -0
- data/lib/config/locales/en.yml +448 -0
- data/lib/mongoid.rb +104 -0
- data/lib/mongoid/atomic.rb +384 -0
- data/lib/mongoid/atomic/modifiers.rb +317 -0
- data/lib/mongoid/atomic/paths.rb +3 -0
- data/lib/mongoid/atomic/paths/embedded.rb +28 -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/root.rb +39 -0
- data/lib/mongoid/attributes.rb +284 -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/changeable.rb +379 -0
- data/lib/mongoid/composable.rb +104 -0
- data/lib/mongoid/config.rb +263 -0
- data/lib/mongoid/config/environment.rb +44 -0
- data/lib/mongoid/config/options.rb +74 -0
- data/lib/mongoid/config/validators.rb +3 -0
- data/lib/mongoid/config/validators/option.rb +25 -0
- data/lib/mongoid/config/validators/session.rb +140 -0
- data/lib/mongoid/contextual.rb +54 -0
- data/lib/mongoid/contextual/aggregable/memory.rb +109 -0
- data/lib/mongoid/contextual/aggregable/mongo.rb +147 -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/copyable.rb +67 -0
- data/lib/mongoid/criteria.rb +562 -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/document.rb +361 -0
- data/lib/mongoid/equality.rb +66 -0
- data/lib/mongoid/errors.rb +40 -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/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/evolvable.rb +19 -0
- data/lib/mongoid/extensions.rb +35 -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 +209 -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 +79 -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/factory.rb +46 -0
- data/lib/mongoid/fields.rb +542 -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.rb +2 -0
- data/lib/mongoid/fields/validators/macro.rb +92 -0
- data/lib/mongoid/findable.rb +133 -0
- data/lib/mongoid/identity_map.rb +163 -0
- data/lib/mongoid/indexable.rb +147 -0
- data/lib/mongoid/indexable/specification.rb +115 -0
- data/lib/mongoid/indexable/validators/options.rb +103 -0
- data/lib/mongoid/inspectable.rb +59 -0
- data/lib/mongoid/interceptable.rb +265 -0
- data/lib/mongoid/loggable.rb +69 -0
- data/lib/mongoid/matchable.rb +152 -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/persistable.rb +207 -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/positional.rb +71 -0
- data/lib/mongoid/railtie.rb +156 -0
- data/lib/mongoid/railties/database.rake +97 -0
- data/lib/mongoid/railties/document.rb +12 -0
- data/lib/mongoid/relations.rb +162 -0
- data/lib/mongoid/relations/accessors.rb +299 -0
- data/lib/mongoid/relations/auto_save.rb +106 -0
- data/lib/mongoid/relations/binding.rb +218 -0
- data/lib/mongoid/relations/bindings.rb +9 -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 +67 -0
- data/lib/mongoid/relations/bindings/referenced/one.rb +44 -0
- data/lib/mongoid/relations/builder.rb +57 -0
- data/lib/mongoid/relations/builders.rb +104 -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/cascading.rb +56 -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/constraint.rb +49 -0
- data/lib/mongoid/relations/conversions.rb +34 -0
- data/lib/mongoid/relations/counter_cache.rb +105 -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 +1174 -0
- data/lib/mongoid/relations/nested_builder.rb +74 -0
- data/lib/mongoid/relations/one.rb +48 -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 +297 -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.rb +2 -0
- data/lib/mongoid/relations/targets/enumerable.rb +473 -0
- data/lib/mongoid/relations/touchable.rb +94 -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.rb +330 -0
- data/lib/mongoid/sessions/factory.rb +129 -0
- data/lib/mongoid/sessions/mongo_uri.rb +93 -0
- data/lib/mongoid/sessions/options.rb +141 -0
- data/lib/mongoid/sessions/validators.rb +2 -0
- data/lib/mongoid/sessions/validators/storage.rb +49 -0
- data/lib/mongoid/shardable.rb +65 -0
- data/lib/mongoid/state.rb +97 -0
- data/lib/mongoid/threaded.rb +383 -0
- data/lib/mongoid/threaded/lifecycle.rb +164 -0
- data/lib/mongoid/timestamps.rb +15 -0
- data/lib/mongoid/timestamps/created.rb +30 -0
- data/lib/mongoid/timestamps/created/short.rb +19 -0
- data/lib/mongoid/timestamps/short.rb +10 -0
- data/lib/mongoid/timestamps/updated.rb +39 -0
- data/lib/mongoid/timestamps/updated/short.rb +19 -0
- data/lib/mongoid/traversable.rb +192 -0
- data/lib/mongoid/unit_of_work.rb +61 -0
- data/lib/mongoid/validatable.rb +180 -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/version.rb +4 -0
- data/lib/rack/mongoid.rb +2 -0
- data/lib/rack/mongoid/middleware/identity_map.rb +39 -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/observer/observer_generator.rb +17 -0
- data/lib/rails/generators/mongoid/observer/templates/observer.rb.tt +4 -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_recorder.rb +25 -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 +7 -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 +28 -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 +205 -0
- data/spec/app/models/pet.rb +23 -0
- data/spec/app/models/pet_owner.rb +6 -0
- data/spec/app/models/phone.rb +11 -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 +15 -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/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 +38 -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 +1412 -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 +455 -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 +1843 -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 +1189 -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 +105 -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 +522 -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 +736 -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 +241 -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 +613 -0
- data/spec/mongoid/relations/metadata_spec.rb +1917 -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 +3546 -0
- data/spec/mongoid/relations/referenced/many_to_many_spec.rb +3556 -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 +312 -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/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 +119 -0
- data/spec/mongoid/validatable/numericality_spec.rb +30 -0
- data/spec/mongoid/validatable/presence_spec.rb +511 -0
- data/spec/mongoid/validatable/uniqueness_spec.rb +2305 -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 +1159 -0
@@ -0,0 +1,305 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Mongoid::Reloadable do
|
4
|
+
|
5
|
+
describe "#reload" do
|
6
|
+
|
7
|
+
context "when using bson ids" do
|
8
|
+
|
9
|
+
let(:person) do
|
10
|
+
Person.create
|
11
|
+
end
|
12
|
+
|
13
|
+
let!(:from_db) do
|
14
|
+
Person.find(person.id).tap do |peep|
|
15
|
+
peep.age = 35
|
16
|
+
peep.save
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
it "reloads the object attributes from the db" do
|
21
|
+
person.reload
|
22
|
+
expect(person.age).to eq(35)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "reload should return self" do
|
26
|
+
expect(person.reload).to eq(from_db)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context "when using string ids" do
|
31
|
+
|
32
|
+
let(:account) do
|
33
|
+
Account.create(name: "bank", number: "1000")
|
34
|
+
end
|
35
|
+
|
36
|
+
let!(:from_db) do
|
37
|
+
Account.find(account.id).tap do |acc|
|
38
|
+
acc.number = "1001"
|
39
|
+
acc.save
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
it "reloads the object attributes from the db" do
|
44
|
+
account.reload
|
45
|
+
expect(account.number).to eq("1001")
|
46
|
+
end
|
47
|
+
|
48
|
+
it "reload should return self" do
|
49
|
+
expect(account.reload).to eq(from_db)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context "when an after initialize callback is defined" do
|
54
|
+
|
55
|
+
let!(:book) do
|
56
|
+
Book.create(title: "Snow Crash")
|
57
|
+
end
|
58
|
+
|
59
|
+
before do
|
60
|
+
book.update_attribute(:chapters, 50)
|
61
|
+
book.reload
|
62
|
+
end
|
63
|
+
|
64
|
+
it "runs the callback" do
|
65
|
+
expect(book.chapters).to eq(5)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context "when the document was dirty" do
|
70
|
+
|
71
|
+
let(:person) do
|
72
|
+
Person.create
|
73
|
+
end
|
74
|
+
|
75
|
+
before do
|
76
|
+
person.title = "Sir"
|
77
|
+
person.reload
|
78
|
+
end
|
79
|
+
|
80
|
+
it "resets the dirty modifications" do
|
81
|
+
expect(person.changes).to be_empty
|
82
|
+
end
|
83
|
+
|
84
|
+
it "resets attributes_before_type_cast" do
|
85
|
+
expect(person.attributes_before_type_cast).to be_empty
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
context "when document not saved" do
|
90
|
+
|
91
|
+
context "when raising not found error" do
|
92
|
+
|
93
|
+
it "raises an error" do
|
94
|
+
expect {
|
95
|
+
Person.new.reload
|
96
|
+
}.to raise_error(Mongoid::Errors::DocumentNotFound)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
context "when the document is embedded" do
|
102
|
+
|
103
|
+
let(:person) do
|
104
|
+
Person.create
|
105
|
+
end
|
106
|
+
|
107
|
+
context "when embedded a single level" do
|
108
|
+
|
109
|
+
context "when the relation is an embeds many" do
|
110
|
+
|
111
|
+
let!(:address) do
|
112
|
+
person.addresses.create(street: "Abbey Road", number: 4)
|
113
|
+
end
|
114
|
+
|
115
|
+
before do
|
116
|
+
Person.collection.find(
|
117
|
+
{ "_id" => person.id }
|
118
|
+
).update({ "$set" => { "addresses.0.number" => 3 }})
|
119
|
+
end
|
120
|
+
|
121
|
+
let!(:reloaded) do
|
122
|
+
address.reload
|
123
|
+
end
|
124
|
+
|
125
|
+
it "reloads the embedded document attributes" do
|
126
|
+
expect(reloaded.number).to eq(3)
|
127
|
+
end
|
128
|
+
|
129
|
+
it "reloads the reference on the parent" do
|
130
|
+
expect(person.addresses.first).to eq(reloaded)
|
131
|
+
end
|
132
|
+
|
133
|
+
it "retains the relation to the parent" do
|
134
|
+
expect(reloaded.addressable).to eq(person)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
context "when the relation is an embeds one" do
|
139
|
+
|
140
|
+
let!(:name) do
|
141
|
+
person.create_name(first_name: "Syd")
|
142
|
+
end
|
143
|
+
|
144
|
+
before do
|
145
|
+
Person.collection.find({ "_id" => person.id }).
|
146
|
+
update({ "$set" => { "name.last_name" => "Vicious" }})
|
147
|
+
end
|
148
|
+
|
149
|
+
let!(:reloaded) do
|
150
|
+
name.reload
|
151
|
+
end
|
152
|
+
|
153
|
+
it "reloads the embedded document attributes" do
|
154
|
+
expect(reloaded.last_name).to eq("Vicious")
|
155
|
+
end
|
156
|
+
|
157
|
+
it "reloads the reference on the parent" do
|
158
|
+
expect(person.name).to eq(reloaded)
|
159
|
+
end
|
160
|
+
|
161
|
+
it "retains the relation to the parent" do
|
162
|
+
expect(reloaded.namable).to eq(person)
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
context "when the relation is embedded multiple levels" do
|
168
|
+
|
169
|
+
let!(:address) do
|
170
|
+
person.addresses.create(street: "Abbey Road", number: 3)
|
171
|
+
end
|
172
|
+
|
173
|
+
let!(:location) do
|
174
|
+
address.locations.create(name: "home")
|
175
|
+
end
|
176
|
+
|
177
|
+
before do
|
178
|
+
Person.collection.find({ "_id" => person.id }).
|
179
|
+
update({ "$set" => { "addresses.0.locations.0.name" => "work" }})
|
180
|
+
end
|
181
|
+
|
182
|
+
let!(:reloaded) do
|
183
|
+
location.reload
|
184
|
+
end
|
185
|
+
|
186
|
+
it "reloads the embedded document attributes" do
|
187
|
+
expect(reloaded.name).to eq("work")
|
188
|
+
end
|
189
|
+
|
190
|
+
it "reloads the reference on the parent" do
|
191
|
+
expect(address.locations.first).to eq(reloaded)
|
192
|
+
end
|
193
|
+
|
194
|
+
it "reloads the reference on the root" do
|
195
|
+
expect(person.addresses.first.locations.first).to eq(reloaded)
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
context "when embedded documents change" do
|
201
|
+
|
202
|
+
let(:person) do
|
203
|
+
Person.create
|
204
|
+
end
|
205
|
+
|
206
|
+
let!(:address) do
|
207
|
+
person.addresses.create(number: 27, street: "Maiden Lane")
|
208
|
+
end
|
209
|
+
|
210
|
+
before do
|
211
|
+
Person.collection.find({ "_id" => person.id }).
|
212
|
+
update({ "$set" => { "addresses" => [] }})
|
213
|
+
person.reload
|
214
|
+
end
|
215
|
+
|
216
|
+
it "reloads the association" do
|
217
|
+
expect(person.addresses).to be_empty
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
context "with relational associations" do
|
222
|
+
|
223
|
+
let(:person) do
|
224
|
+
Person.create
|
225
|
+
end
|
226
|
+
|
227
|
+
context "for a has_one" do
|
228
|
+
|
229
|
+
let!(:game) do
|
230
|
+
person.create_game(score: 50)
|
231
|
+
end
|
232
|
+
|
233
|
+
before do
|
234
|
+
Game.collection.find({ "_id" => game.id }).
|
235
|
+
update({ "$set" => { "score" => 75 }})
|
236
|
+
person.reload
|
237
|
+
end
|
238
|
+
|
239
|
+
it "reloads the association" do
|
240
|
+
expect(person.game.score).to eq(75)
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
context "for a belongs_to" do
|
245
|
+
|
246
|
+
context "when the relation type does not change" do
|
247
|
+
|
248
|
+
let!(:game) do
|
249
|
+
person.create_game(score: 50)
|
250
|
+
end
|
251
|
+
|
252
|
+
before do
|
253
|
+
Person.collection.find({ "_id" => person.id }).
|
254
|
+
update({ "$set" => { "title" => "Mam" }})
|
255
|
+
game.reload
|
256
|
+
end
|
257
|
+
|
258
|
+
it "reloads the association" do
|
259
|
+
expect(game.person.title).to eq("Mam")
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
context "when the identity map is enabled" do
|
264
|
+
|
265
|
+
before do
|
266
|
+
Mongoid.identity_map_enabled = true
|
267
|
+
end
|
268
|
+
|
269
|
+
after do
|
270
|
+
Mongoid.identity_map_enabled = false
|
271
|
+
end
|
272
|
+
|
273
|
+
context "when the relation type changes" do
|
274
|
+
|
275
|
+
let!(:doctor) do
|
276
|
+
Doctor.create
|
277
|
+
end
|
278
|
+
|
279
|
+
let!(:game) do
|
280
|
+
Game.create(:person => doctor)
|
281
|
+
end
|
282
|
+
|
283
|
+
before do
|
284
|
+
doctor.becomes(Doktor)
|
285
|
+
end
|
286
|
+
|
287
|
+
context "when reloading the base" do
|
288
|
+
|
289
|
+
it "reloads the correct type of document" do
|
290
|
+
expect(game.reload.person).to be_a(Doktor)
|
291
|
+
end
|
292
|
+
end
|
293
|
+
|
294
|
+
context "when reloading the association" do
|
295
|
+
|
296
|
+
it "reloads the correct type of document" do
|
297
|
+
expect(game.person(true)).to be_a(Doktor)
|
298
|
+
end
|
299
|
+
end
|
300
|
+
end
|
301
|
+
end
|
302
|
+
end
|
303
|
+
end
|
304
|
+
end
|
305
|
+
end
|
@@ -0,0 +1,926 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Mongoid::Scopable do
|
4
|
+
|
5
|
+
describe ".default_scope" do
|
6
|
+
|
7
|
+
context "when provided a criteria" do
|
8
|
+
|
9
|
+
let(:criteria) do
|
10
|
+
Band.where(name: "Depeche Mode")
|
11
|
+
end
|
12
|
+
|
13
|
+
before do
|
14
|
+
Band.default_scope criteria
|
15
|
+
end
|
16
|
+
|
17
|
+
after do
|
18
|
+
Band.default_scoping = nil
|
19
|
+
end
|
20
|
+
|
21
|
+
it "adds the default scope to the class" do
|
22
|
+
expect(Band.default_scoping.call).to eq(criteria)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "flags as being default scoped" do
|
26
|
+
expect(Band).to be_default_scoping
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context "when provided a proc" do
|
31
|
+
|
32
|
+
let(:criteria) do
|
33
|
+
Band.where(name: "Depeche Mode")
|
34
|
+
end
|
35
|
+
|
36
|
+
before do
|
37
|
+
Band.default_scope ->{ criteria }
|
38
|
+
end
|
39
|
+
|
40
|
+
after do
|
41
|
+
Band.default_scoping = nil
|
42
|
+
end
|
43
|
+
|
44
|
+
it "adds the default scope to the class" do
|
45
|
+
expect(Band.default_scoping.call).to eq(criteria)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "flags as being default scoped" do
|
49
|
+
expect(Band).to be_default_scoping
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context "when provided a non proc or criteria" do
|
54
|
+
|
55
|
+
it "raises an error" do
|
56
|
+
expect {
|
57
|
+
Band.default_scope({})
|
58
|
+
}.to raise_error(Mongoid::Errors::InvalidScope)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context "when there is more then one default_scope" do
|
63
|
+
|
64
|
+
let(:criteria) do
|
65
|
+
Band.where(name: "Depeche Mode")
|
66
|
+
end
|
67
|
+
|
68
|
+
let(:additional_criteria) do
|
69
|
+
Band.where(origin: "England")
|
70
|
+
end
|
71
|
+
|
72
|
+
let(:proc_criteria) do
|
73
|
+
->{ Band.where(active: true) }
|
74
|
+
end
|
75
|
+
|
76
|
+
let(:rand_criteria) do
|
77
|
+
->{ Band.gt(likes: rand(100)) }
|
78
|
+
end
|
79
|
+
|
80
|
+
before do
|
81
|
+
Band.default_scope criteria
|
82
|
+
Band.default_scope additional_criteria
|
83
|
+
Band.default_scope proc_criteria
|
84
|
+
Band.default_scope rand_criteria
|
85
|
+
end
|
86
|
+
|
87
|
+
after do
|
88
|
+
Band.default_scoping = nil
|
89
|
+
end
|
90
|
+
|
91
|
+
it "adds the first default scope" do
|
92
|
+
expect(Band.default_scoping.call.selector["name"]).to eq("Depeche Mode")
|
93
|
+
end
|
94
|
+
|
95
|
+
it "adds the additional default scope" do
|
96
|
+
expect(Band.default_scoping.call.selector["origin"]).to eq("England")
|
97
|
+
end
|
98
|
+
|
99
|
+
it "adds the proc default scope" do
|
100
|
+
expect(Band.default_scoping.call.selector["active"]).to be_true
|
101
|
+
end
|
102
|
+
|
103
|
+
it "delays execution of the merge until called" do
|
104
|
+
expect(Band.all.selector["likes"]).to_not eq(Band.all.selector["likes"])
|
105
|
+
end
|
106
|
+
|
107
|
+
it "flags as being default scoped" do
|
108
|
+
expect(Band).to be_default_scoping
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
describe ".default_scopable?" do
|
114
|
+
|
115
|
+
context "when a default scope exists" do
|
116
|
+
|
117
|
+
let(:criteria) do
|
118
|
+
Band.where(name: "Depeche Mode")
|
119
|
+
end
|
120
|
+
|
121
|
+
before do
|
122
|
+
Band.default_scope ->{ criteria }
|
123
|
+
end
|
124
|
+
|
125
|
+
after do
|
126
|
+
Band.default_scoping = nil
|
127
|
+
end
|
128
|
+
|
129
|
+
context "when not in an unscoped block" do
|
130
|
+
|
131
|
+
it "returns true" do
|
132
|
+
expect(Band).to be_default_scopable
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
context "when in an unscoped block" do
|
137
|
+
|
138
|
+
it "returns false" do
|
139
|
+
Band.unscoped do
|
140
|
+
expect(Band).to_not be_default_scopable
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
context "when a default scope does not exist" do
|
147
|
+
|
148
|
+
it "returns false" do
|
149
|
+
expect(Band).to_not be_default_scopable
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
describe ".queryable" do
|
155
|
+
|
156
|
+
context "when no criteria exists on the stack" do
|
157
|
+
|
158
|
+
it "returns an empty criteria" do
|
159
|
+
expect(Band.queryable.selector).to be_empty
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
context "when a criteria exists on the stack" do
|
164
|
+
|
165
|
+
let(:criteria) do
|
166
|
+
Band.where(name: "Depeche Mode")
|
167
|
+
end
|
168
|
+
|
169
|
+
before do
|
170
|
+
Band.scope_stack.push(criteria)
|
171
|
+
end
|
172
|
+
|
173
|
+
after do
|
174
|
+
Band.scope_stack.clear
|
175
|
+
end
|
176
|
+
|
177
|
+
it "returns the criteria on the stack" do
|
178
|
+
expect(Band.queryable).to eq(criteria)
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
describe ".scope" do
|
184
|
+
|
185
|
+
context "when provided a criteria" do
|
186
|
+
|
187
|
+
context "when a block is provided" do
|
188
|
+
|
189
|
+
before do
|
190
|
+
Band.scope(:active, Band.where(active: true)) do
|
191
|
+
def add_origin
|
192
|
+
tap { |c| c.selector[:origin] = "Deutschland" }
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
after do
|
198
|
+
class << Band
|
199
|
+
undef_method :active
|
200
|
+
end
|
201
|
+
Band._declared_scopes.clear
|
202
|
+
end
|
203
|
+
|
204
|
+
let(:scope) do
|
205
|
+
Band.active.add_origin
|
206
|
+
end
|
207
|
+
|
208
|
+
it "adds the extension to the scope" do
|
209
|
+
expect(scope.selector).to eq({ "active" => true, "origin" => "Deutschland" })
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
context "when scoping an embedded document" do
|
214
|
+
|
215
|
+
before do
|
216
|
+
Record.scope(
|
217
|
+
:tool,
|
218
|
+
Record.where(:name.in => [ "undertow", "aenima", "lateralus" ])
|
219
|
+
)
|
220
|
+
end
|
221
|
+
|
222
|
+
after do
|
223
|
+
class << Record
|
224
|
+
undef_method :tool
|
225
|
+
end
|
226
|
+
Record._declared_scopes.clear
|
227
|
+
end
|
228
|
+
|
229
|
+
context "when calling the scope" do
|
230
|
+
|
231
|
+
let(:band) do
|
232
|
+
Band.new
|
233
|
+
end
|
234
|
+
|
235
|
+
let!(:undertow) do
|
236
|
+
band.records.build(name: "undertow")
|
237
|
+
end
|
238
|
+
|
239
|
+
let(:scoped) do
|
240
|
+
band.records.tool
|
241
|
+
end
|
242
|
+
|
243
|
+
it "returns the correct documents" do
|
244
|
+
expect(scoped).to eq([ undertow ])
|
245
|
+
end
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
context "when no block is provided" do
|
250
|
+
|
251
|
+
before do
|
252
|
+
Band.scope(:active, Band.where(active: true).skip(10))
|
253
|
+
end
|
254
|
+
|
255
|
+
after do
|
256
|
+
class << Band
|
257
|
+
undef_method :active
|
258
|
+
end
|
259
|
+
Band._declared_scopes.clear
|
260
|
+
end
|
261
|
+
|
262
|
+
it "adds a method for the scope" do
|
263
|
+
expect(Band).to respond_to(:active)
|
264
|
+
end
|
265
|
+
|
266
|
+
context "when calling the scope" do
|
267
|
+
|
268
|
+
context "when calling from the class" do
|
269
|
+
|
270
|
+
let(:scope) do
|
271
|
+
Band.active
|
272
|
+
end
|
273
|
+
|
274
|
+
it "returns a criteria" do
|
275
|
+
expect(scope).to be_a(Mongoid::Criteria)
|
276
|
+
end
|
277
|
+
|
278
|
+
it "contains the proper selector" do
|
279
|
+
expect(scope.selector).to eq({ "active" => true })
|
280
|
+
end
|
281
|
+
|
282
|
+
it "contains the proper options" do
|
283
|
+
expect(scope.options).to eq({ skip: 10 })
|
284
|
+
end
|
285
|
+
end
|
286
|
+
|
287
|
+
context "when chained to another scope" do
|
288
|
+
|
289
|
+
before do
|
290
|
+
Band.scope(:english, Band.where(origin: "England"))
|
291
|
+
end
|
292
|
+
|
293
|
+
after do
|
294
|
+
class << Band
|
295
|
+
undef_method :english
|
296
|
+
end
|
297
|
+
Band._declared_scopes.clear
|
298
|
+
end
|
299
|
+
|
300
|
+
let(:scope) do
|
301
|
+
Band.english.active
|
302
|
+
end
|
303
|
+
|
304
|
+
it "returns a criteria" do
|
305
|
+
expect(scope).to be_a(Mongoid::Criteria)
|
306
|
+
end
|
307
|
+
|
308
|
+
it "contains the proper selector" do
|
309
|
+
expect(scope.selector).to eq({ "active" => true, "origin" => "England" })
|
310
|
+
end
|
311
|
+
|
312
|
+
it "contains the proper options" do
|
313
|
+
expect(scope.options).to eq({ skip: 10 })
|
314
|
+
end
|
315
|
+
|
316
|
+
it "does not modify the original scope" do
|
317
|
+
expect(Band.active.selector).to eq({ "active" => true })
|
318
|
+
end
|
319
|
+
end
|
320
|
+
|
321
|
+
context "when chained to a criteria" do
|
322
|
+
|
323
|
+
let(:criteria) do
|
324
|
+
Band.where(origin: "England")
|
325
|
+
end
|
326
|
+
|
327
|
+
let(:scope) do
|
328
|
+
criteria.active
|
329
|
+
end
|
330
|
+
|
331
|
+
it "returns a criteria" do
|
332
|
+
expect(scope).to be_a(Mongoid::Criteria)
|
333
|
+
end
|
334
|
+
|
335
|
+
it "contains the proper selector" do
|
336
|
+
expect(scope.selector).to eq({ "origin" => "England", "active" => true })
|
337
|
+
end
|
338
|
+
|
339
|
+
it "contains the proper options" do
|
340
|
+
expect(scope.options).to eq({ skip: 10 })
|
341
|
+
end
|
342
|
+
|
343
|
+
it "does not modify the original scope" do
|
344
|
+
expect(Band.active.selector).to eq({ "active" => true })
|
345
|
+
end
|
346
|
+
|
347
|
+
it "does not modify the original criteria" do
|
348
|
+
expect(criteria.selector).to eq({ "origin" => "England" })
|
349
|
+
end
|
350
|
+
end
|
351
|
+
end
|
352
|
+
end
|
353
|
+
|
354
|
+
context "when the name conflict with an existing method" do
|
355
|
+
|
356
|
+
context "when raising an error" do
|
357
|
+
|
358
|
+
before do
|
359
|
+
Mongoid.scope_overwrite_exception = true
|
360
|
+
end
|
361
|
+
|
362
|
+
after do
|
363
|
+
Mongoid.scope_overwrite_exception = false
|
364
|
+
class << Band
|
365
|
+
undef_method :active
|
366
|
+
end
|
367
|
+
Band._declared_scopes.clear
|
368
|
+
end
|
369
|
+
|
370
|
+
it "raises an exception" do
|
371
|
+
expect {
|
372
|
+
Band.scope(:active, Band.where(active: true))
|
373
|
+
Band.scope(:active, Band.where(active: true))
|
374
|
+
}.to raise_error(Mongoid::Errors::ScopeOverwrite)
|
375
|
+
end
|
376
|
+
end
|
377
|
+
|
378
|
+
context "when not raising an error" do
|
379
|
+
|
380
|
+
after do
|
381
|
+
Mongoid.scope_overwrite_exception = false
|
382
|
+
class << Band
|
383
|
+
undef_method :active
|
384
|
+
end
|
385
|
+
Band._declared_scopes.clear
|
386
|
+
end
|
387
|
+
|
388
|
+
it "raises no exception" do
|
389
|
+
Band.scope(:active, Band.where(active: true))
|
390
|
+
Band.scope(:active, Band.where(active: true))
|
391
|
+
end
|
392
|
+
end
|
393
|
+
end
|
394
|
+
end
|
395
|
+
|
396
|
+
context "when provided a proc" do
|
397
|
+
|
398
|
+
context "when a block is provided" do
|
399
|
+
|
400
|
+
before do
|
401
|
+
Band.scope(:active, ->{ Band.where(active: true) }) do
|
402
|
+
def add_origin
|
403
|
+
tap { |c| c.selector[:origin] = "Deutschland" }
|
404
|
+
end
|
405
|
+
end
|
406
|
+
end
|
407
|
+
|
408
|
+
after do
|
409
|
+
class << Band
|
410
|
+
undef_method :active
|
411
|
+
end
|
412
|
+
Band._declared_scopes.clear
|
413
|
+
end
|
414
|
+
|
415
|
+
let(:scope) do
|
416
|
+
Band.active.add_origin
|
417
|
+
end
|
418
|
+
|
419
|
+
it "adds the extension to the scope" do
|
420
|
+
expect(scope.selector).to eq({ "active" => true, "origin" => "Deutschland" })
|
421
|
+
end
|
422
|
+
end
|
423
|
+
|
424
|
+
context "when no block is provided" do
|
425
|
+
|
426
|
+
before do
|
427
|
+
Band.scope(:active, ->{ Band.where(active: true).skip(10) })
|
428
|
+
Band.scope(:named_by, ->(name) { Band.where(name: name) if name })
|
429
|
+
end
|
430
|
+
|
431
|
+
after do
|
432
|
+
class << Band
|
433
|
+
undef_method :active
|
434
|
+
undef_method :named_by
|
435
|
+
end
|
436
|
+
Band._declared_scopes.clear
|
437
|
+
end
|
438
|
+
|
439
|
+
it "adds a method for the scope" do
|
440
|
+
expect(Band).to respond_to(:active)
|
441
|
+
end
|
442
|
+
|
443
|
+
context "when calling the scope" do
|
444
|
+
|
445
|
+
context "when the scope would return nil" do
|
446
|
+
|
447
|
+
it "returns a chainable empty scope" do
|
448
|
+
expect(Band.named_by(nil)).to be_a(Mongoid::Criteria)
|
449
|
+
end
|
450
|
+
end
|
451
|
+
|
452
|
+
context "when calling from the class" do
|
453
|
+
|
454
|
+
let(:scope) do
|
455
|
+
Band.active
|
456
|
+
end
|
457
|
+
|
458
|
+
it "returns a criteria" do
|
459
|
+
expect(scope).to be_a(Mongoid::Criteria)
|
460
|
+
end
|
461
|
+
|
462
|
+
it "contains the proper selector" do
|
463
|
+
expect(scope.selector).to eq({ "active" => true })
|
464
|
+
end
|
465
|
+
|
466
|
+
it "contains the proper options" do
|
467
|
+
expect(scope.options).to eq({ skip: 10 })
|
468
|
+
end
|
469
|
+
end
|
470
|
+
|
471
|
+
context "when chained to another scope" do
|
472
|
+
|
473
|
+
before do
|
474
|
+
Band.scope(:english, ->{ Band.where(origin: "England") })
|
475
|
+
end
|
476
|
+
|
477
|
+
after do
|
478
|
+
class << Band
|
479
|
+
undef_method :english
|
480
|
+
end
|
481
|
+
Band._declared_scopes.clear
|
482
|
+
end
|
483
|
+
|
484
|
+
let(:scope) do
|
485
|
+
Band.english.active
|
486
|
+
end
|
487
|
+
|
488
|
+
it "returns a criteria" do
|
489
|
+
expect(scope).to be_a(Mongoid::Criteria)
|
490
|
+
end
|
491
|
+
|
492
|
+
it "contains the proper selector" do
|
493
|
+
expect(scope.selector).to eq({ "active" => true, "origin" => "England" })
|
494
|
+
end
|
495
|
+
|
496
|
+
it "contains the proper options" do
|
497
|
+
expect(scope.options).to eq({ skip: 10 })
|
498
|
+
end
|
499
|
+
|
500
|
+
it "does not modify the original scope" do
|
501
|
+
expect(Band.active.selector).to eq({ "active" => true })
|
502
|
+
end
|
503
|
+
end
|
504
|
+
|
505
|
+
context "when chained to a criteria" do
|
506
|
+
|
507
|
+
let(:criteria) do
|
508
|
+
Band.where(origin: "England")
|
509
|
+
end
|
510
|
+
|
511
|
+
let(:scope) do
|
512
|
+
criteria.active
|
513
|
+
end
|
514
|
+
|
515
|
+
it "returns a criteria" do
|
516
|
+
expect(scope).to be_a(Mongoid::Criteria)
|
517
|
+
end
|
518
|
+
|
519
|
+
it "contains the proper selector" do
|
520
|
+
expect(scope.selector).to eq({ "origin" => "England", "active" => true })
|
521
|
+
end
|
522
|
+
|
523
|
+
it "contains the proper options" do
|
524
|
+
expect(scope.options).to eq({ skip: 10 })
|
525
|
+
end
|
526
|
+
|
527
|
+
it "does not modify the original scope" do
|
528
|
+
expect(Band.active.selector).to eq({ "active" => true })
|
529
|
+
end
|
530
|
+
|
531
|
+
it "does not modify the original criteria" do
|
532
|
+
expect(criteria.selector).to eq({ "origin" => "England" })
|
533
|
+
end
|
534
|
+
end
|
535
|
+
end
|
536
|
+
end
|
537
|
+
|
538
|
+
context "when the name conflict with an existing method" do
|
539
|
+
|
540
|
+
context "when raising an error" do
|
541
|
+
|
542
|
+
before do
|
543
|
+
Mongoid.scope_overwrite_exception = true
|
544
|
+
end
|
545
|
+
|
546
|
+
after do
|
547
|
+
Mongoid.scope_overwrite_exception = false
|
548
|
+
class << Band
|
549
|
+
undef_method :active
|
550
|
+
end
|
551
|
+
Band._declared_scopes.clear
|
552
|
+
end
|
553
|
+
|
554
|
+
it "raises an exception" do
|
555
|
+
expect {
|
556
|
+
Band.scope(:active, ->{ Band.where(active: true) })
|
557
|
+
Band.scope(:active, ->{ Band.where(active: true) })
|
558
|
+
}.to raise_error(Mongoid::Errors::ScopeOverwrite)
|
559
|
+
end
|
560
|
+
end
|
561
|
+
|
562
|
+
context "when not raising an error" do
|
563
|
+
|
564
|
+
after do
|
565
|
+
Mongoid.scope_overwrite_exception = false
|
566
|
+
class << Band
|
567
|
+
undef_method :active
|
568
|
+
end
|
569
|
+
Band._declared_scopes.clear
|
570
|
+
end
|
571
|
+
|
572
|
+
it "raises no exception" do
|
573
|
+
Band.scope(:active, ->{ Band.where(active: true) })
|
574
|
+
Band.scope(:active, ->{ Band.where(active: true) })
|
575
|
+
end
|
576
|
+
end
|
577
|
+
end
|
578
|
+
end
|
579
|
+
|
580
|
+
context "when provided a non proc or criteria" do
|
581
|
+
|
582
|
+
it "raises an error" do
|
583
|
+
expect {
|
584
|
+
Band.scope(:active, {})
|
585
|
+
}.to raise_error(Mongoid::Errors::InvalidScope)
|
586
|
+
end
|
587
|
+
end
|
588
|
+
|
589
|
+
context "when chaining a non proc with a proc" do
|
590
|
+
|
591
|
+
context "when both scopes are or queries" do
|
592
|
+
|
593
|
+
let(:time) do
|
594
|
+
Time.now
|
595
|
+
end
|
596
|
+
|
597
|
+
before do
|
598
|
+
Band.scope(:xxx, Band.any_of({ :aaa.gt => 0 }, { :bbb.gt => 0 }))
|
599
|
+
Band.scope(:yyy, ->{ Band.any_of({ :ccc => nil }, { :ccc.gt => time }) })
|
600
|
+
end
|
601
|
+
|
602
|
+
after do
|
603
|
+
class << Band
|
604
|
+
undef_method :xxx
|
605
|
+
undef_method :yyy
|
606
|
+
end
|
607
|
+
Band._declared_scopes.clear
|
608
|
+
end
|
609
|
+
|
610
|
+
let(:criteria) do
|
611
|
+
Band.yyy.xxx
|
612
|
+
end
|
613
|
+
|
614
|
+
it "properly chains the $or queries together" do
|
615
|
+
expect(criteria.selector).to eq({
|
616
|
+
"$or" => [
|
617
|
+
{ "ccc" => nil },
|
618
|
+
{ "ccc" => { "$gt" => time }},
|
619
|
+
{ "aaa" => { "$gt" => 0.0 }},
|
620
|
+
{ "bbb" => { "$gt" => 0.0 }}
|
621
|
+
]
|
622
|
+
})
|
623
|
+
end
|
624
|
+
end
|
625
|
+
end
|
626
|
+
|
627
|
+
context "when working with a subclass" do
|
628
|
+
|
629
|
+
before do
|
630
|
+
Shape.scope(:located_at, ->(x,y) {Shape.where(x: x, y: y)})
|
631
|
+
Circle.scope(:with_radius, ->(r) {Circle.where(radius: r)})
|
632
|
+
end
|
633
|
+
|
634
|
+
after do
|
635
|
+
class << Shape
|
636
|
+
undef_method :located_at
|
637
|
+
end
|
638
|
+
Shape._declared_scopes.clear
|
639
|
+
|
640
|
+
class << Circle
|
641
|
+
undef_method :with_radius
|
642
|
+
end
|
643
|
+
Circle._declared_scopes.clear
|
644
|
+
end
|
645
|
+
|
646
|
+
let(:shape_scope_keys) do
|
647
|
+
Shape.scopes.keys
|
648
|
+
end
|
649
|
+
|
650
|
+
let(:circle_located_at) do
|
651
|
+
Circle.located_at(0,0)
|
652
|
+
end
|
653
|
+
|
654
|
+
let(:circle_scope_keys) do
|
655
|
+
Circle.scopes.keys
|
656
|
+
end
|
657
|
+
|
658
|
+
it "doesn't include subclass scopes in superclass scope list" do
|
659
|
+
expect(shape_scope_keys).to match_array([:located_at])
|
660
|
+
end
|
661
|
+
|
662
|
+
it "includes superclass scope methods on subclass" do
|
663
|
+
expect(circle_located_at).to be_a(Mongoid::Criteria)
|
664
|
+
end
|
665
|
+
|
666
|
+
it "includes superclass scopes in subclass scope list" do
|
667
|
+
expect(circle_scope_keys).to match_array([:located_at, :with_radius])
|
668
|
+
end
|
669
|
+
end
|
670
|
+
end
|
671
|
+
|
672
|
+
describe ".scope_stack" do
|
673
|
+
|
674
|
+
context "when the scope stack has not been accessed" do
|
675
|
+
|
676
|
+
it "returns an empty array" do
|
677
|
+
expect(Band.scope_stack).to eq([])
|
678
|
+
end
|
679
|
+
end
|
680
|
+
|
681
|
+
context "when a criteria exists on the current thread" do
|
682
|
+
|
683
|
+
let(:criteria) do
|
684
|
+
Band.where(active: true)
|
685
|
+
end
|
686
|
+
|
687
|
+
before do
|
688
|
+
Mongoid::Threaded.scope_stack[Band.object_id] = [ criteria ]
|
689
|
+
end
|
690
|
+
|
691
|
+
after do
|
692
|
+
Mongoid::Threaded.scope_stack[Band.object_id].clear
|
693
|
+
end
|
694
|
+
|
695
|
+
it "returns the criteria in the array" do
|
696
|
+
expect(Band.scope_stack).to eq([ criteria ])
|
697
|
+
end
|
698
|
+
end
|
699
|
+
end
|
700
|
+
|
701
|
+
describe ".scoped" do
|
702
|
+
|
703
|
+
context "when no options are provided" do
|
704
|
+
|
705
|
+
let(:scoped) do
|
706
|
+
Band.scoped
|
707
|
+
end
|
708
|
+
|
709
|
+
it "returns a criteria" do
|
710
|
+
expect(scoped).to be_a(Mongoid::Criteria)
|
711
|
+
end
|
712
|
+
|
713
|
+
it "contains an empty selector" do
|
714
|
+
expect(scoped.selector).to be_empty
|
715
|
+
end
|
716
|
+
|
717
|
+
it "contains empty options" do
|
718
|
+
expect(scoped.options).to be_empty
|
719
|
+
end
|
720
|
+
end
|
721
|
+
|
722
|
+
context "when options are provided" do
|
723
|
+
|
724
|
+
let(:scoped) do
|
725
|
+
Band.scoped(skip: 10, limit: 10)
|
726
|
+
end
|
727
|
+
|
728
|
+
it "returns a criteria" do
|
729
|
+
expect(scoped).to be_a(Mongoid::Criteria)
|
730
|
+
end
|
731
|
+
|
732
|
+
it "contains an empty selector" do
|
733
|
+
expect(scoped.selector).to be_empty
|
734
|
+
end
|
735
|
+
|
736
|
+
it "contains the options" do
|
737
|
+
expect(scoped.options).to eq({ skip: 10, limit: 10 })
|
738
|
+
end
|
739
|
+
end
|
740
|
+
|
741
|
+
context "when a default scope exists" do
|
742
|
+
|
743
|
+
let(:criteria) do
|
744
|
+
Band.where(name: "Depeche Mode")
|
745
|
+
end
|
746
|
+
|
747
|
+
before do
|
748
|
+
Band.default_scope criteria
|
749
|
+
end
|
750
|
+
|
751
|
+
after do
|
752
|
+
Band.default_scoping = nil
|
753
|
+
end
|
754
|
+
|
755
|
+
let(:scoped) do
|
756
|
+
Band.scoped
|
757
|
+
end
|
758
|
+
|
759
|
+
it "allows the default scope to be added" do
|
760
|
+
expect(scoped.selector).to eq({ "name" => "Depeche Mode" })
|
761
|
+
end
|
762
|
+
|
763
|
+
context "when chained after an unscoped criteria" do
|
764
|
+
|
765
|
+
let(:scoped) do
|
766
|
+
Band.unscoped.scoped
|
767
|
+
end
|
768
|
+
|
769
|
+
it "reapplies the default scope" do
|
770
|
+
expect(scoped.selector).to eq({ "name" => "Depeche Mode" })
|
771
|
+
end
|
772
|
+
end
|
773
|
+
end
|
774
|
+
end
|
775
|
+
|
776
|
+
describe ".unscoped" do
|
777
|
+
|
778
|
+
let(:criteria) do
|
779
|
+
Band.where(name: "Depeche Mode")
|
780
|
+
end
|
781
|
+
|
782
|
+
before do
|
783
|
+
Band.default_scope criteria
|
784
|
+
end
|
785
|
+
|
786
|
+
after do
|
787
|
+
Band.default_scoping = nil
|
788
|
+
end
|
789
|
+
|
790
|
+
context "when called directly" do
|
791
|
+
|
792
|
+
let(:unscoped) do
|
793
|
+
Band.unscoped
|
794
|
+
end
|
795
|
+
|
796
|
+
it "removes the default scope from the criteria" do
|
797
|
+
expect(unscoped.selector).to be_empty
|
798
|
+
end
|
799
|
+
|
800
|
+
context "when chained after a scoped criteria" do
|
801
|
+
|
802
|
+
let(:unscoped) do
|
803
|
+
Band.scoped.unscoped
|
804
|
+
end
|
805
|
+
|
806
|
+
it "removes all scoping" do
|
807
|
+
expect(unscoped.selector).to be_empty
|
808
|
+
end
|
809
|
+
end
|
810
|
+
end
|
811
|
+
|
812
|
+
context "when used with a block" do
|
813
|
+
|
814
|
+
context "when a criteria is called in the block" do
|
815
|
+
|
816
|
+
it "does not allow default scoping to be added in the block" do
|
817
|
+
Band.unscoped do
|
818
|
+
expect(Band.skip(10).selector).to be_empty
|
819
|
+
end
|
820
|
+
end
|
821
|
+
end
|
822
|
+
|
823
|
+
context "when a call is made to scoped in the block" do
|
824
|
+
|
825
|
+
it "does not allow default scoping to be added in the block" do
|
826
|
+
Band.unscoped do
|
827
|
+
expect(Band.scoped.selector).to be_empty
|
828
|
+
end
|
829
|
+
end
|
830
|
+
end
|
831
|
+
|
832
|
+
context "when a named scope is called in the block" do
|
833
|
+
|
834
|
+
before do
|
835
|
+
Band.scope(:skipped, Band.skip(10))
|
836
|
+
end
|
837
|
+
|
838
|
+
after do
|
839
|
+
class << Band
|
840
|
+
undef_method :skipped
|
841
|
+
end
|
842
|
+
Band._declared_scopes.clear
|
843
|
+
end
|
844
|
+
|
845
|
+
it "does not allow the default scope to be applied" do
|
846
|
+
Band.unscoped do
|
847
|
+
expect(Band.skipped.selector).to be_empty
|
848
|
+
end
|
849
|
+
end
|
850
|
+
end
|
851
|
+
end
|
852
|
+
end
|
853
|
+
|
854
|
+
describe ".with_default_scope" do
|
855
|
+
|
856
|
+
let(:criteria) do
|
857
|
+
Band.where(name: "Depeche Mode")
|
858
|
+
end
|
859
|
+
|
860
|
+
before do
|
861
|
+
Band.default_scope criteria
|
862
|
+
end
|
863
|
+
|
864
|
+
after do
|
865
|
+
Band.default_scoping = nil
|
866
|
+
end
|
867
|
+
|
868
|
+
context "when inside an unscoped block" do
|
869
|
+
|
870
|
+
it "returns an empty criteria" do
|
871
|
+
Band.unscoped do
|
872
|
+
expect(Band.with_default_scope.selector).to be_empty
|
873
|
+
end
|
874
|
+
end
|
875
|
+
end
|
876
|
+
|
877
|
+
context "when the criteria is unscoped" do
|
878
|
+
|
879
|
+
let(:scoped) do
|
880
|
+
Band.unscoped.with_default_scope
|
881
|
+
end
|
882
|
+
|
883
|
+
it "returns an empty criteria" do
|
884
|
+
expect(scoped.selector).to be_empty
|
885
|
+
end
|
886
|
+
end
|
887
|
+
|
888
|
+
context "when no unscoping exists" do
|
889
|
+
|
890
|
+
let(:scoped) do
|
891
|
+
Band.with_default_scope
|
892
|
+
end
|
893
|
+
|
894
|
+
it "returns a scoped criteria" do
|
895
|
+
expect(scoped.selector).to eq({ "name" => "Depeche Mode" })
|
896
|
+
end
|
897
|
+
end
|
898
|
+
end
|
899
|
+
|
900
|
+
describe ".with_scope" do
|
901
|
+
|
902
|
+
let(:criteria) do
|
903
|
+
Band.where(active: true)
|
904
|
+
end
|
905
|
+
|
906
|
+
it "yields to the criteria" do
|
907
|
+
Band.with_scope(criteria) do |crit|
|
908
|
+
expect(crit.selector).to eq({ "active" => true })
|
909
|
+
end
|
910
|
+
end
|
911
|
+
|
912
|
+
it "pops the criteria off the stack" do
|
913
|
+
Band.with_scope(criteria) {}
|
914
|
+
expect(Band.scope_stack).to be_empty
|
915
|
+
end
|
916
|
+
end
|
917
|
+
|
918
|
+
describe ".without_default_scope" do
|
919
|
+
|
920
|
+
it "sets the threading options" do
|
921
|
+
Band.without_default_scope do
|
922
|
+
expect(Mongoid::Threaded).to be_executing(:without_default_scope)
|
923
|
+
end
|
924
|
+
end
|
925
|
+
end
|
926
|
+
end
|