mongoid 6.4.8 → 7.0.0.beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/Rakefile +0 -26
- data/lib/config/locales/en.yml +17 -21
- data/lib/mongoid.rb +2 -2
- data/lib/mongoid/association.rb +150 -0
- data/lib/mongoid/association/accessors.rb +339 -0
- data/lib/mongoid/{relations/binding.rb → association/bindable.rb} +32 -52
- data/lib/mongoid/association/builders.rb +92 -0
- data/lib/mongoid/{relations/constraint.rb → association/constrainable.rb} +11 -22
- data/lib/mongoid/association/depending.rb +116 -0
- data/lib/mongoid/{relations/eager.rb → association/eager_loadable.rb} +11 -11
- data/lib/mongoid/association/embedded.rb +4 -0
- data/lib/mongoid/{relations → association}/embedded/batchable.rb +27 -53
- data/lib/mongoid/association/embedded/cyclic.rb +109 -0
- data/lib/mongoid/association/embedded/embedded_in.rb +154 -0
- data/lib/mongoid/association/embedded/embedded_in/binding.rb +56 -0
- data/lib/mongoid/{relations/builders/embedded/in.rb → association/embedded/embedded_in/buildable.rb} +12 -6
- data/lib/mongoid/association/embedded/embedded_in/proxy.rb +121 -0
- data/lib/mongoid/association/embedded/embeds_many.rb +210 -0
- data/lib/mongoid/{relations/bindings/embedded/many.rb → association/embedded/embeds_many/binding.rb} +11 -9
- data/lib/mongoid/{relations/builders/embedded/many.rb → association/embedded/embeds_many/buildable.rb} +13 -7
- data/lib/mongoid/association/embedded/embeds_many/proxy.rb +529 -0
- data/lib/mongoid/association/embedded/embeds_one.rb +173 -0
- data/lib/mongoid/{relations/bindings/embedded/one.rb → association/embedded/embeds_one/binding.rb} +12 -10
- data/lib/mongoid/{relations/builders/embedded/one.rb → association/embedded/embeds_one/buildable.rb} +13 -7
- data/lib/mongoid/association/embedded/embeds_one/proxy.rb +130 -0
- data/lib/mongoid/association/macros.rb +204 -0
- data/lib/mongoid/{relations → association}/many.rb +18 -52
- data/lib/mongoid/{relations → association}/marshalable.rb +6 -4
- data/lib/mongoid/association/nested.rb +15 -0
- data/lib/mongoid/association/nested/many.rb +200 -0
- data/lib/mongoid/association/nested/nested_buildable.rb +72 -0
- data/lib/mongoid/association/nested/one.rb +127 -0
- data/lib/mongoid/{relations → association}/one.rb +6 -6
- data/lib/mongoid/association/options.rb +152 -0
- data/lib/mongoid/{relations → association}/proxy.rb +31 -58
- data/lib/mongoid/association/referenced.rb +7 -0
- data/lib/mongoid/association/referenced/auto_save.rb +79 -0
- data/lib/mongoid/association/referenced/belongs_to.rb +248 -0
- data/lib/mongoid/association/referenced/belongs_to/binding.rb +87 -0
- data/lib/mongoid/association/referenced/belongs_to/buildable.rb +46 -0
- data/lib/mongoid/association/referenced/belongs_to/eager.rb +36 -0
- data/lib/mongoid/association/referenced/belongs_to/proxy.rb +136 -0
- data/lib/mongoid/association/referenced/counter_cache.rb +163 -0
- data/lib/mongoid/association/referenced/eager.rb +159 -0
- data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +290 -0
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/binding.rb +71 -0
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb +40 -0
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb +52 -0
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb +310 -0
- data/lib/mongoid/association/referenced/has_many.rb +273 -0
- data/lib/mongoid/{relations/bindings/referenced/many.rb → association/referenced/has_many/binding.rb} +6 -5
- data/lib/mongoid/association/referenced/has_many/buildable.rb +38 -0
- data/lib/mongoid/association/referenced/has_many/eager.rb +43 -0
- data/lib/mongoid/association/referenced/has_many/enumerable.rb +479 -0
- data/lib/mongoid/association/referenced/has_many/proxy.rb +577 -0
- data/lib/mongoid/association/referenced/has_one.rb +204 -0
- data/lib/mongoid/{relations/bindings/referenced/one.rb → association/referenced/has_one/binding.rb} +11 -8
- data/lib/mongoid/association/referenced/has_one/buildable.rb +60 -0
- data/lib/mongoid/association/referenced/has_one/eager.rb +35 -0
- data/lib/mongoid/{relations/builders/nested_attributes/one.rb → association/referenced/has_one/nested_builder.rb} +9 -9
- data/lib/mongoid/association/referenced/has_one/proxy.rb +113 -0
- data/lib/mongoid/association/referenced/syncable.rb +170 -0
- data/lib/mongoid/{relations → association}/reflections.rb +21 -17
- data/lib/mongoid/association/relatable.rb +415 -0
- data/lib/mongoid/association/touchable.rb +97 -0
- data/lib/mongoid/atomic.rb +6 -6
- data/lib/mongoid/atomic/modifiers.rb +8 -12
- data/lib/mongoid/atomic/paths/embedded/many.rb +1 -1
- data/lib/mongoid/atomic/paths/embedded/one.rb +1 -1
- data/lib/mongoid/attributes.rb +2 -1
- data/lib/mongoid/attributes/nested.rb +10 -10
- data/lib/mongoid/attributes/processing.rb +2 -2
- data/lib/mongoid/attributes/readonly.rb +2 -4
- data/lib/mongoid/clients.rb +0 -2
- data/lib/mongoid/clients/options.rb +1 -1
- data/lib/mongoid/clients/storage_options.rb +0 -1
- data/lib/mongoid/composable.rb +3 -4
- data/lib/mongoid/config.rb +1 -0
- data/lib/mongoid/contextual/aggregable/mongo.rb +1 -1
- data/lib/mongoid/contextual/atomic.rb +3 -6
- data/lib/mongoid/contextual/map_reduce.rb +3 -7
- data/lib/mongoid/contextual/memory.rb +5 -10
- data/lib/mongoid/contextual/mongo.rb +10 -27
- data/lib/mongoid/copyable.rb +6 -6
- data/lib/mongoid/criteria.rb +1 -2
- data/lib/mongoid/criteria/includable.rb +14 -14
- data/lib/mongoid/criteria/modifiable.rb +8 -14
- data/lib/mongoid/criteria/queryable/mergeable.rb +1 -3
- data/lib/mongoid/criteria/queryable/pipeline.rb +10 -5
- data/lib/mongoid/criteria/queryable/selectable.rb +10 -34
- data/lib/mongoid/document.rb +6 -6
- data/lib/mongoid/errors.rb +3 -1
- data/lib/mongoid/errors/invalid_dependent_strategy.rb +32 -0
- data/lib/mongoid/errors/invalid_relation_option.rb +29 -0
- data/lib/mongoid/errors/unknown_model.rb +25 -0
- data/lib/mongoid/extensions/array.rb +5 -5
- data/lib/mongoid/extensions/big_decimal.rb +1 -1
- data/lib/mongoid/extensions/object.rb +4 -4
- data/lib/mongoid/extensions/range.rb +1 -0
- data/lib/mongoid/extensions/regexp.rb +0 -1
- data/lib/mongoid/extensions/string.rb +1 -3
- data/lib/mongoid/factory.rb +4 -3
- data/lib/mongoid/fields.rb +1 -1
- data/lib/mongoid/fields/foreign_key.rb +5 -5
- data/lib/mongoid/fields/standard.rb +2 -14
- data/lib/mongoid/fields/validators/macro.rb +1 -1
- data/lib/mongoid/indexable.rb +8 -5
- data/lib/mongoid/interceptable.rb +5 -5
- data/lib/mongoid/matchable.rb +0 -3
- data/lib/mongoid/persistable.rb +4 -5
- data/lib/mongoid/persistable/creatable.rb +2 -4
- data/lib/mongoid/persistable/deletable.rb +9 -10
- data/lib/mongoid/persistable/destroyable.rb +5 -1
- data/lib/mongoid/persistable/incrementable.rb +1 -1
- data/lib/mongoid/persistable/logical.rb +1 -1
- data/lib/mongoid/persistable/settable.rb +5 -5
- data/lib/mongoid/persistable/updatable.rb +2 -2
- data/lib/mongoid/persistable/upsertable.rb +1 -2
- data/lib/mongoid/persistence_context.rb +4 -9
- data/lib/mongoid/query_cache.rb +18 -65
- data/lib/mongoid/railtie.rb +0 -17
- data/lib/mongoid/reloadable.rb +1 -1
- data/lib/mongoid/scopable.rb +3 -3
- data/lib/mongoid/serializable.rb +3 -3
- data/lib/mongoid/tasks/database.rb +2 -3
- data/lib/mongoid/threaded.rb +0 -74
- data/lib/mongoid/traversable.rb +2 -2
- data/lib/mongoid/validatable.rb +8 -8
- data/lib/mongoid/validatable/presence.rb +2 -2
- data/lib/mongoid/validatable/uniqueness.rb +4 -4
- data/lib/mongoid/version.rb +1 -1
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +3 -4
- data/spec/app/models/animal.rb +2 -1
- data/spec/app/models/band.rb +0 -1
- data/spec/app/models/bomb.rb +1 -1
- data/spec/app/models/message.rb +1 -1
- data/spec/app/models/person.rb +5 -2
- data/spec/app/models/vertex.rb +6 -0
- data/spec/app/models/wiki_page.rb +1 -1
- data/spec/config/mongoid.yml +1 -0
- data/spec/mongoid/{relations → association}/accessors_spec.rb +1 -1
- data/spec/mongoid/{relations → association}/auto_save_spec.rb +60 -12
- data/spec/mongoid/{relations → association}/builders_spec.rb +1 -1
- data/spec/mongoid/association/constrainable_spec.rb +115 -0
- data/spec/mongoid/{relations → association}/counter_cache_spec.rb +1 -1
- data/spec/mongoid/association/depending_spec.rb +613 -0
- data/spec/mongoid/{relations → association}/eager_spec.rb +12 -12
- data/spec/mongoid/{relations → association/embedded}/cyclic_spec.rb +1 -1
- data/spec/mongoid/{relations → association}/embedded/dirty_spec.rb +0 -0
- data/spec/mongoid/{relations/bindings/embedded/in_spec.rb → association/embedded/embedded_in/binding_spec.rb} +13 -13
- data/spec/mongoid/{relations/builders/embedded/in_spec.rb → association/embedded/embedded_in/buildable_spec.rb} +9 -9
- data/spec/mongoid/{relations/embedded/in_spec.rb → association/embedded/embedded_in/proxy_spec.rb} +5 -77
- data/spec/mongoid/association/embedded/embedded_in_spec.rb +843 -0
- data/spec/mongoid/{relations/bindings/embedded/many_spec.rb → association/embedded/embeds_many/binding_spec.rb} +3 -3
- data/spec/mongoid/{relations/builders/embedded/many_spec.rb → association/embedded/embeds_many/buildable_spec.rb} +17 -45
- data/spec/mongoid/{relations/embedded/many_spec.rb → association/embedded/embeds_many/proxy_spec.rb} +140 -428
- data/spec/mongoid/association/embedded/embeds_many_spec.rb +852 -0
- data/spec/mongoid/{relations/bindings/embedded/one_spec.rb → association/embedded/embeds_one/binding_spec.rb} +4 -4
- data/spec/mongoid/{relations/builders/embedded/one_spec.rb → association/embedded/embeds_one/buildable_spec.rb} +14 -34
- data/spec/mongoid/{relations/embedded/one_spec.rb → association/embedded/embeds_one/proxy_spec.rb} +39 -84
- data/spec/mongoid/association/embedded/embeds_one_spec.rb +908 -0
- data/spec/mongoid/{relations → association}/macros_spec.rb +148 -93
- data/spec/mongoid/{relations/builders/nested_attributes → association/nested}/many_spec.rb +16 -19
- data/spec/mongoid/{relations/builders/nested_attributes → association/nested}/one_spec.rb +17 -20
- data/spec/mongoid/association/options_spec.rb +1321 -0
- data/spec/mongoid/{relations → association}/polymorphic_spec.rb +7 -34
- data/spec/mongoid/{relations/bindings/referenced/in_spec.rb → association/referenced/belongs_to/binding_spec.rb} +7 -7
- data/spec/mongoid/{relations/builders/referenced/in_spec.rb → association/referenced/belongs_to/buildable_spec.rb} +46 -79
- data/spec/mongoid/{relations/eager/belongs_to_spec.rb → association/referenced/belongs_to/eager_spec.rb} +9 -9
- data/spec/mongoid/{relations/referenced/in_spec.rb → association/referenced/belongs_to/proxy_spec.rb} +57 -91
- data/spec/mongoid/association/referenced/belongs_to_spec.rb +1963 -0
- data/spec/mongoid/{relations/bindings/referenced/many_to_many_spec.rb → association/referenced/has_and_belongs_to_many/binding_spec.rb} +5 -5
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/buildable_spec.rb +121 -0
- data/spec/mongoid/{relations/eager/has_and_belongs_to_many_spec.rb → association/referenced/has_and_belongs_to_many/eager_spec.rb} +5 -5
- data/spec/mongoid/{relations/referenced/many_to_many_spec.rb → association/referenced/has_and_belongs_to_many/proxy_spec.rb} +107 -98
- data/spec/mongoid/association/referenced/has_and_belongs_to_many_spec.rb +1027 -0
- data/spec/mongoid/{relations/bindings/referenced/many_spec.rb → association/referenced/has_many/binding_spec.rb} +5 -5
- data/spec/mongoid/association/referenced/has_many/buildable_spec.rb +119 -0
- data/spec/mongoid/{relations/eager/has_many_spec.rb → association/referenced/has_many/eager_spec.rb} +11 -11
- data/spec/mongoid/{relations/targets → association/referenced/has_many}/enumerable_spec.rb +1 -109
- data/spec/mongoid/{relations/referenced/many_spec.rb → association/referenced/has_many/proxy_spec.rb} +28 -93
- data/spec/mongoid/association/referenced/has_many_spec.rb +1225 -0
- data/spec/mongoid/{relations/bindings/referenced/one_spec.rb → association/referenced/has_one/binding_spec.rb} +4 -4
- data/spec/mongoid/association/referenced/has_one/buildable_spec.rb +113 -0
- data/spec/mongoid/{relations/eager/has_one_spec.rb → association/referenced/has_one/eager_spec.rb} +10 -10
- data/spec/mongoid/{relations/referenced/one_spec.rb → association/referenced/has_one/proxy_spec.rb} +9 -109
- data/spec/mongoid/association/referenced/has_one_spec.rb +1244 -0
- data/spec/mongoid/{relations → association}/reflections_spec.rb +1 -12
- data/spec/mongoid/{relations/synchronization_spec.rb → association/syncable_spec.rb} +4 -2
- data/spec/mongoid/{relations → association}/touchable_spec.rb +19 -1
- data/spec/mongoid/{relations_spec.rb → association_spec.rb} +1 -1
- data/spec/mongoid/atomic/modifiers_spec.rb +17 -17
- data/spec/mongoid/atomic_spec.rb +17 -17
- data/spec/mongoid/attributes/nested_spec.rb +14 -12
- data/spec/mongoid/attributes/readonly_spec.rb +80 -125
- data/spec/mongoid/clients/factory_spec.rb +28 -52
- data/spec/mongoid/clients/options_spec.rb +65 -69
- data/spec/mongoid/config_spec.rb +24 -0
- data/spec/mongoid/contextual/geo_near_spec.rb +0 -1
- data/spec/mongoid/contextual/mongo_spec.rb +4 -112
- data/spec/mongoid/criteria/modifiable_spec.rb +183 -60
- data/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb +3 -3
- data/spec/mongoid/criteria/queryable/pipeline_spec.rb +12 -0
- data/spec/mongoid/criteria/queryable/selectable_spec.rb +6 -74
- data/spec/mongoid/criteria/queryable/selector_spec.rb +2 -2
- data/spec/mongoid/criteria/scopable_spec.rb +0 -81
- data/spec/mongoid/criteria_spec.rb +16 -19
- data/spec/mongoid/document_spec.rb +2 -56
- data/spec/mongoid/extensions/array_spec.rb +11 -15
- data/spec/mongoid/extensions/big_decimal_spec.rb +9 -9
- data/spec/mongoid/extensions/object_spec.rb +7 -11
- data/spec/mongoid/extensions/range_spec.rb +7 -0
- data/spec/mongoid/extensions/regexp_spec.rb +0 -23
- data/spec/mongoid/extensions/string_spec.rb +7 -35
- data/spec/mongoid/factory_spec.rb +18 -11
- data/spec/mongoid/fields/foreign_key_spec.rb +24 -32
- data/spec/mongoid/fields_spec.rb +2 -2
- data/spec/mongoid/findable_spec.rb +1 -1
- data/spec/mongoid/indexable_spec.rb +18 -8
- data/spec/mongoid/interceptable_spec.rb +21 -2
- data/spec/mongoid/matchable_spec.rb +1 -26
- data/spec/mongoid/persistable/deletable_spec.rb +0 -19
- data/spec/mongoid/persistable/destroyable_spec.rb +0 -19
- data/spec/mongoid/persistable/incrementable_spec.rb +6 -6
- data/spec/mongoid/persistable/savable_spec.rb +3 -3
- data/spec/mongoid/persistable/settable_spec.rb +1 -35
- data/spec/mongoid/persistable/updatable_spec.rb +2 -2
- data/spec/mongoid/persistable_spec.rb +16 -16
- data/spec/mongoid/persistence_context_spec.rb +0 -14
- data/spec/mongoid/positional_spec.rb +10 -10
- data/spec/mongoid/query_cache_spec.rb +18 -87
- data/spec/mongoid/relations/proxy_spec.rb +124 -124
- data/spec/mongoid/scopable_spec.rb +0 -13
- data/spec/mongoid/threaded_spec.rb +0 -68
- data/spec/mongoid/validatable/associated_spec.rb +1 -1
- data/spec/mongoid/validatable/presence_spec.rb +7 -6
- data/spec/mongoid/validatable_spec.rb +1 -1
- data/spec/spec_helper.rb +7 -83
- metadata +586 -611
- metadata.gz.sig +2 -5
- data/lib/mongoid/clients/sessions.rb +0 -113
- data/lib/mongoid/errors/invalid_session_use.rb +0 -24
- data/lib/mongoid/matchable/nor.rb +0 -37
- data/lib/mongoid/railties/controller_runtime.rb +0 -86
- data/lib/mongoid/relations.rb +0 -148
- data/lib/mongoid/relations/accessors.rb +0 -267
- data/lib/mongoid/relations/auto_save.rb +0 -94
- data/lib/mongoid/relations/bindings.rb +0 -9
- data/lib/mongoid/relations/bindings/embedded/in.rb +0 -59
- data/lib/mongoid/relations/bindings/referenced/in.rb +0 -65
- data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +0 -70
- data/lib/mongoid/relations/builder.rb +0 -57
- data/lib/mongoid/relations/builders.rb +0 -106
- data/lib/mongoid/relations/builders/nested_attributes/many.rb +0 -199
- data/lib/mongoid/relations/builders/referenced/in.rb +0 -26
- data/lib/mongoid/relations/builders/referenced/many.rb +0 -26
- data/lib/mongoid/relations/builders/referenced/many_to_many.rb +0 -39
- data/lib/mongoid/relations/builders/referenced/one.rb +0 -26
- data/lib/mongoid/relations/cascading.rb +0 -56
- data/lib/mongoid/relations/cascading/delete.rb +0 -44
- data/lib/mongoid/relations/cascading/destroy.rb +0 -43
- data/lib/mongoid/relations/cascading/nullify.rb +0 -35
- data/lib/mongoid/relations/cascading/restrict.rb +0 -39
- data/lib/mongoid/relations/conversions.rb +0 -34
- data/lib/mongoid/relations/counter_cache.rb +0 -160
- data/lib/mongoid/relations/cyclic.rb +0 -107
- data/lib/mongoid/relations/eager/base.rb +0 -153
- data/lib/mongoid/relations/eager/belongs_to.rb +0 -31
- data/lib/mongoid/relations/eager/has_and_belongs_to_many.rb +0 -47
- data/lib/mongoid/relations/eager/has_many.rb +0 -38
- data/lib/mongoid/relations/eager/has_one.rb +0 -30
- data/lib/mongoid/relations/embedded/in.rb +0 -241
- data/lib/mongoid/relations/embedded/many.rb +0 -683
- data/lib/mongoid/relations/embedded/one.rb +0 -235
- data/lib/mongoid/relations/macros.rb +0 -367
- data/lib/mongoid/relations/metadata.rb +0 -1179
- data/lib/mongoid/relations/nested_builder.rb +0 -74
- data/lib/mongoid/relations/options.rb +0 -49
- data/lib/mongoid/relations/polymorphic.rb +0 -39
- data/lib/mongoid/relations/referenced/in.rb +0 -304
- data/lib/mongoid/relations/referenced/many.rb +0 -812
- data/lib/mongoid/relations/referenced/many_to_many.rb +0 -479
- data/lib/mongoid/relations/referenced/one.rb +0 -290
- data/lib/mongoid/relations/synchronization.rb +0 -169
- data/lib/mongoid/relations/targets.rb +0 -2
- data/lib/mongoid/relations/targets/enumerable.rb +0 -493
- data/lib/mongoid/relations/touchable.rb +0 -97
- data/spec/app/models/array_field.rb +0 -7
- data/spec/app/models/delegating_patient.rb +0 -16
- data/spec/integration/document_spec.rb +0 -22
- data/spec/mongoid/clients/sessions_spec.rb +0 -334
- data/spec/mongoid/fields/internal/foreign_keys/array_spec.rb +0 -184
- data/spec/mongoid/fields/internal/foreign_keys/object_spec.rb +0 -201
- data/spec/mongoid/matchable/nor_spec.rb +0 -209
- data/spec/mongoid/relations/builders/referenced/many_spec.rb +0 -137
- data/spec/mongoid/relations/builders/referenced/many_to_many_spec.rb +0 -178
- data/spec/mongoid/relations/builders/referenced/one_spec.rb +0 -111
- data/spec/mongoid/relations/cascading/delete_spec.rb +0 -101
- data/spec/mongoid/relations/cascading/destroy_spec.rb +0 -47
- data/spec/mongoid/relations/cascading/nullify_spec.rb +0 -32
- data/spec/mongoid/relations/cascading/restrict_spec.rb +0 -68
- data/spec/mongoid/relations/cascading_spec.rb +0 -355
- data/spec/mongoid/relations/constraint_spec.rb +0 -75
- data/spec/mongoid/relations/conversions_spec.rb +0 -128
- data/spec/mongoid/relations/metadata_spec.rb +0 -1985
- data/spec/mongoid/relations/options_spec.rb +0 -35
- data/spec/rails/controller_extension/controller_runtime_spec.rb +0 -110
- data/spec/support/cluster_config.rb +0 -158
- data/spec/support/constraints.rb +0 -101
- data/spec/support/macros.rb +0 -20
- data/spec/support/session_registry.rb +0 -50
- data/spec/support/spec_config.rb +0 -42
|
@@ -62,8 +62,8 @@ module Mongoid
|
|
|
62
62
|
# @since 3.0.0
|
|
63
63
|
def relation_or_fk_missing?(doc, attr, value)
|
|
64
64
|
return true if value.blank? && doc.send(attr).blank?
|
|
65
|
-
|
|
66
|
-
|
|
65
|
+
association = doc.relations[attr.to_s]
|
|
66
|
+
association.stores_foreign_key? && doc.send(association.foreign_key).blank?
|
|
67
67
|
end
|
|
68
68
|
|
|
69
69
|
# For guarding against false values.
|
|
@@ -224,9 +224,9 @@ module Mongoid
|
|
|
224
224
|
#
|
|
225
225
|
# @since 2.4.4
|
|
226
226
|
def to_validate(document, attribute, value)
|
|
227
|
-
|
|
228
|
-
if
|
|
229
|
-
[
|
|
227
|
+
association = document.relations[attribute.to_s]
|
|
228
|
+
if association && association.stores_foreign_key?
|
|
229
|
+
[ association.foreign_key, value && value._id ]
|
|
230
230
|
else
|
|
231
231
|
[ attribute, value ]
|
|
232
232
|
end
|
|
@@ -246,7 +246,7 @@ module Mongoid
|
|
|
246
246
|
# @since 2.4.10
|
|
247
247
|
def validate_embedded(document, attribute, value)
|
|
248
248
|
return if skip_validation?(document)
|
|
249
|
-
relation = document._parent.send(document.
|
|
249
|
+
relation = document._parent.send(document.association_name)
|
|
250
250
|
criteria = create_criteria(relation, document, attribute, value)
|
|
251
251
|
criteria = criteria.merge(options[:conditions].call) if options[:conditions]
|
|
252
252
|
add_error(document, attribute, value) if criteria.count > 1
|
data/lib/mongoid/version.rb
CHANGED
|
@@ -11,10 +11,6 @@ development:
|
|
|
11
11
|
hosts:
|
|
12
12
|
- localhost:27017
|
|
13
13
|
options:
|
|
14
|
-
# Note that all options listed below are Ruby driver client options (the mongo gem).
|
|
15
|
-
# Please refer to the driver documentation of the version of the mongo gem you are using
|
|
16
|
-
# for the most up-to-date list of options.
|
|
17
|
-
#
|
|
18
14
|
# Change the default write concern. (default = { w: 1 })
|
|
19
15
|
# write:
|
|
20
16
|
# w: 1
|
|
@@ -149,6 +145,9 @@ development:
|
|
|
149
145
|
# Application name that is printed to the mongodb logs upon establishing a
|
|
150
146
|
# connection in server versions >= 3.4. Note that the name cannot exceed 128 bytes.
|
|
151
147
|
# app_name: MyApplicationName
|
|
148
|
+
|
|
149
|
+
# Use background indexes by default if `background` option not specified. (default: false)
|
|
150
|
+
# background_indexing: false
|
|
152
151
|
test:
|
|
153
152
|
clients:
|
|
154
153
|
default:
|
data/spec/app/models/animal.rb
CHANGED
|
@@ -9,7 +9,8 @@ class Animal
|
|
|
9
9
|
field :tags, type: Array
|
|
10
10
|
|
|
11
11
|
embedded_in :person
|
|
12
|
-
embedded_in :circus
|
|
12
|
+
embedded_in :circus, class_name: 'Circus' # class_name is necessary because ActiveRecord think the singular of Circus
|
|
13
|
+
# is Circu
|
|
13
14
|
|
|
14
15
|
validates_format_of :name, without: /\$\$\$/
|
|
15
16
|
|
data/spec/app/models/band.rb
CHANGED
|
@@ -20,7 +20,6 @@ class Band
|
|
|
20
20
|
|
|
21
21
|
embeds_many :records, cascade_callbacks: true
|
|
22
22
|
embeds_many :notes, as: :noteable, cascade_callbacks: true, validate: false
|
|
23
|
-
embeds_many :labels
|
|
24
23
|
embeds_one :label, cascade_callbacks: true
|
|
25
24
|
|
|
26
25
|
has_many :same_name, class_name: 'Agent', inverse_of: :same_name
|
data/spec/app/models/bomb.rb
CHANGED
data/spec/app/models/message.rb
CHANGED
data/spec/app/models/person.rb
CHANGED
|
@@ -85,7 +85,7 @@ class Person
|
|
|
85
85
|
|
|
86
86
|
has_many \
|
|
87
87
|
:posts,
|
|
88
|
-
dependent: :
|
|
88
|
+
dependent: :delete_all,
|
|
89
89
|
validate: false do
|
|
90
90
|
def extension
|
|
91
91
|
"Testing"
|
|
@@ -105,7 +105,7 @@ class Person
|
|
|
105
105
|
has_one :account, validate: false
|
|
106
106
|
has_one :cat, dependent: :nullify, validate: false, primary_key: :username
|
|
107
107
|
has_one :book, autobuild: true, validate: false
|
|
108
|
-
has_one :home, dependent: :
|
|
108
|
+
has_one :home, dependent: :delete_all, validate: false
|
|
109
109
|
|
|
110
110
|
has_and_belongs_to_many \
|
|
111
111
|
:administrated_events,
|
|
@@ -114,6 +114,9 @@ class Person
|
|
|
114
114
|
dependent: :nullify,
|
|
115
115
|
validate: false
|
|
116
116
|
|
|
117
|
+
belongs_to :mother, class_name: 'Person'
|
|
118
|
+
has_many :children, class_name: 'Person'
|
|
119
|
+
|
|
117
120
|
accepts_nested_attributes_for :addresses
|
|
118
121
|
accepts_nested_attributes_for :name, update_only: true
|
|
119
122
|
accepts_nested_attributes_for :pet, allow_destroy: true
|
|
@@ -9,6 +9,6 @@ class WikiPage
|
|
|
9
9
|
|
|
10
10
|
embeds_many :edits, validate: false
|
|
11
11
|
has_many :comments, dependent: :destroy, validate: false
|
|
12
|
-
has_many :child_pages, class_name: "WikiPage", dependent: :
|
|
12
|
+
has_many :child_pages, class_name: "WikiPage", dependent: :delete_all, inverse_of: :parent_pages
|
|
13
13
|
belongs_to :parent_pages, class_name: "WikiPage", inverse_of: :child_pages
|
|
14
14
|
end
|
data/spec/config/mongoid.yml
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
require "spec_helper"
|
|
2
2
|
|
|
3
|
-
describe Mongoid::
|
|
3
|
+
describe Mongoid::Association::Referenced::AutoSave do
|
|
4
4
|
|
|
5
5
|
describe ".auto_save" do
|
|
6
6
|
|
|
7
7
|
before(:all) do
|
|
8
|
-
Person.autosave
|
|
8
|
+
Person.has_many :drugs, validate: false, autosave: true
|
|
9
|
+
Person.has_one :account, validate: false, autosave: true
|
|
9
10
|
end
|
|
10
11
|
|
|
11
12
|
after(:all) do
|
|
@@ -42,13 +43,8 @@ describe Mongoid::Relations::AutoSave do
|
|
|
42
43
|
|
|
43
44
|
context "when the relation has already had the autosave callback added" do
|
|
44
45
|
|
|
45
|
-
let(:metadata) do
|
|
46
|
-
Person.relations["drugs"].merge!(autosave: true)
|
|
47
|
-
end
|
|
48
|
-
|
|
49
46
|
before do
|
|
50
|
-
Person.autosave
|
|
51
|
-
Person.autosave(metadata)
|
|
47
|
+
Person.has_many :drugs, validate: false, autosave: true
|
|
52
48
|
end
|
|
53
49
|
|
|
54
50
|
let(:drug) do
|
|
@@ -71,6 +67,11 @@ describe Mongoid::Relations::AutoSave do
|
|
|
71
67
|
context "when saving a new parent document" do
|
|
72
68
|
|
|
73
69
|
context 'when persistence options are not set on the parent' do
|
|
70
|
+
|
|
71
|
+
before do
|
|
72
|
+
Person.has_many :drugs, validate: false, autosave: true
|
|
73
|
+
end
|
|
74
|
+
|
|
74
75
|
before do
|
|
75
76
|
person.drugs << drug
|
|
76
77
|
person.save
|
|
@@ -254,10 +255,6 @@ describe Mongoid::Relations::AutoSave do
|
|
|
254
255
|
|
|
255
256
|
context "when it has two relations with autosaves" do
|
|
256
257
|
|
|
257
|
-
before do
|
|
258
|
-
Person.autosave(Person.relations["drugs"].merge!(autosave: true))
|
|
259
|
-
end
|
|
260
|
-
|
|
261
258
|
let!(:person) do
|
|
262
259
|
Person.create(drugs: [percocet], account: account)
|
|
263
260
|
end
|
|
@@ -309,6 +306,57 @@ describe Mongoid::Relations::AutoSave do
|
|
|
309
306
|
end
|
|
310
307
|
end
|
|
311
308
|
end
|
|
309
|
+
|
|
310
|
+
context 'when the autosave should be cascaded' do
|
|
311
|
+
|
|
312
|
+
before do
|
|
313
|
+
class King
|
|
314
|
+
include Mongoid::Document
|
|
315
|
+
has_one :peasant, autosave: true
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
class Peasant
|
|
319
|
+
include Mongoid::Document
|
|
320
|
+
belongs_to :king
|
|
321
|
+
has_one :harvest, autosave: true
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
class Harvest
|
|
325
|
+
include Mongoid::Document
|
|
326
|
+
field :season, type: String
|
|
327
|
+
belongs_to :peasant
|
|
328
|
+
end
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
after do
|
|
332
|
+
Object.send(:remove_const, :King)
|
|
333
|
+
Object.send(:remove_const, :Peasant)
|
|
334
|
+
Object.send(:remove_const, :Harvest)
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
let(:king) do
|
|
338
|
+
King.create
|
|
339
|
+
end
|
|
340
|
+
|
|
341
|
+
let(:peasant) do
|
|
342
|
+
Peasant.create
|
|
343
|
+
end
|
|
344
|
+
|
|
345
|
+
let(:harvest) do
|
|
346
|
+
Harvest.create(season: 'Summer')
|
|
347
|
+
end
|
|
348
|
+
|
|
349
|
+
before do
|
|
350
|
+
peasant.harvest = harvest
|
|
351
|
+
king.peasant = peasant
|
|
352
|
+
harvest.season = 'Fall'
|
|
353
|
+
king.save
|
|
354
|
+
end
|
|
355
|
+
|
|
356
|
+
it 'cascades the save' do
|
|
357
|
+
expect(harvest.reload.season).to eq('Fall')
|
|
358
|
+
end
|
|
359
|
+
end
|
|
312
360
|
end
|
|
313
361
|
end
|
|
314
362
|
end
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
require "spec_helper"
|
|
2
|
+
|
|
3
|
+
describe Mongoid::Association::Constrainable do
|
|
4
|
+
|
|
5
|
+
describe "#convert_to_foreign_key" do
|
|
6
|
+
|
|
7
|
+
context "when the id's class stores object ids" do
|
|
8
|
+
|
|
9
|
+
before(:all) do
|
|
10
|
+
Person.field(
|
|
11
|
+
:_id,
|
|
12
|
+
type: BSON::ObjectId,
|
|
13
|
+
pre_processed: true,
|
|
14
|
+
default: ->{ BSON::ObjectId.new },
|
|
15
|
+
overwrite: true
|
|
16
|
+
)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
let(:constrainable) do
|
|
20
|
+
Post.belongs_to :person
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
context "when provided an object id" do
|
|
24
|
+
|
|
25
|
+
let(:object) do
|
|
26
|
+
BSON::ObjectId.new
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it "returns the object id" do
|
|
30
|
+
expect(constrainable.convert_to_foreign_key(object)).to eq(object)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
context "when provided a string" do
|
|
35
|
+
|
|
36
|
+
let(:object) do
|
|
37
|
+
BSON::ObjectId.new
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
it "returns an object id from the string" do
|
|
41
|
+
expect(constrainable.convert_to_foreign_key(object.to_s)).to eq(object)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
context "when the id's class does not store object ids" do
|
|
47
|
+
|
|
48
|
+
let(:constrainable) do
|
|
49
|
+
Alert.belongs_to :account
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
it "returns the object" do
|
|
53
|
+
expect(constrainable.convert_to_foreign_key("testing")).to eq("testing")
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
context 'when the association is polymorphic' do
|
|
58
|
+
|
|
59
|
+
let(:constrainable) do
|
|
60
|
+
Post.relations['posteable']
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
let(:result) do
|
|
64
|
+
constrainable.convert_to_foreign_key(object)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
context 'when a BSON::ObjectId is passed' do
|
|
68
|
+
|
|
69
|
+
let(:object) do
|
|
70
|
+
BSON::ObjectId.new
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
it 'returns the object id' do
|
|
74
|
+
expect(result).to eq(object)
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
context 'when a string is passed' do
|
|
79
|
+
|
|
80
|
+
context 'when the string represents an ObjectId' do
|
|
81
|
+
|
|
82
|
+
let(:object) do
|
|
83
|
+
BSON::ObjectId.new.to_s
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
it 'returns the object id' do
|
|
87
|
+
expect(result).to eq(BSON::ObjectId.from_string(object))
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
context 'when the string does not represent an ObjectId' do
|
|
92
|
+
|
|
93
|
+
let(:object) do
|
|
94
|
+
'some-other-string'
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
it 'returns the object' do
|
|
98
|
+
expect(result).to eq(object)
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
context 'when a model object is passed' do
|
|
104
|
+
|
|
105
|
+
let(:object) do
|
|
106
|
+
Post.new
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
it 'returns the id' do
|
|
110
|
+
expect(result).to eq(object.id)
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
@@ -0,0 +1,613 @@
|
|
|
1
|
+
require "spec_helper"
|
|
2
|
+
|
|
3
|
+
describe Mongoid::Association::Depending do
|
|
4
|
+
|
|
5
|
+
around(:each) do |example|
|
|
6
|
+
relations_before = Person.relations
|
|
7
|
+
example.run
|
|
8
|
+
Person.relations = relations_before
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
describe "#apply_delete_dependencies!" do
|
|
12
|
+
|
|
13
|
+
let(:band) do
|
|
14
|
+
Band.new
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
context "when the association exists in the list of dependencies" do
|
|
18
|
+
|
|
19
|
+
context "when the association has no dependent strategy" do
|
|
20
|
+
|
|
21
|
+
before do
|
|
22
|
+
band.dependents.push(Band.relations["records"])
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
after do
|
|
26
|
+
band.dependents.delete(Band.relations["records"])
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it "ignores the dependency" do
|
|
30
|
+
expect(band.apply_delete_dependencies!).to eq([Band.relations["records"]])
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
describe ".define_dependency!" do
|
|
37
|
+
|
|
38
|
+
let(:klass) do
|
|
39
|
+
Class.new.tap { |c| c.send(:include, Mongoid::Document) }
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
context "when the association metadata doesnt exist" do
|
|
43
|
+
|
|
44
|
+
before do
|
|
45
|
+
klass.dependents.push("nothing")
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
it "does not raise an error" do
|
|
49
|
+
expect {
|
|
50
|
+
klass.new.apply_delete_dependencies!
|
|
51
|
+
}.not_to raise_error
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
context "when a dependent option is provided" do
|
|
56
|
+
|
|
57
|
+
let!(:association) do
|
|
58
|
+
klass.has_many :posts, dependent: :destroy
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
after do
|
|
62
|
+
klass.relations.delete(association.name.to_s)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
it "adds the relation to the dependents" do
|
|
66
|
+
expect(klass.dependents).to include(klass.relations["posts"])
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
context "when no dependent option is provided" do
|
|
71
|
+
|
|
72
|
+
let!(:association) do
|
|
73
|
+
klass.has_many :posts
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
after do
|
|
77
|
+
klass.relations.delete(association.name.to_s)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
it "does not add a relation to the dependents" do
|
|
81
|
+
expect(klass.dependents).to_not include(association)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
context 'when the class is defined more than once' do
|
|
86
|
+
|
|
87
|
+
let!(:association) do
|
|
88
|
+
klass.has_many :posts, dependent: :destroy
|
|
89
|
+
klass.has_many :posts, dependent: :destroy
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
it 'only creates the dependency once' do
|
|
93
|
+
expect(klass.dependents.size).to eq(1)
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
[:delete, :destroy].each do |method|
|
|
99
|
+
|
|
100
|
+
describe "##{method}" do
|
|
101
|
+
|
|
102
|
+
context "when cascading removals" do
|
|
103
|
+
|
|
104
|
+
context "when strategy is delete" do
|
|
105
|
+
|
|
106
|
+
let(:person) do
|
|
107
|
+
Person.create
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
let!(:post) do
|
|
111
|
+
person.posts.create(title: "Testing")
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
before do
|
|
115
|
+
person.send(method)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
it "deletes the associated documents" do
|
|
119
|
+
expect {
|
|
120
|
+
Post.find(post.id)
|
|
121
|
+
}.to raise_error(Mongoid::Errors::DocumentNotFound)
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
context "when strategy is destroy" do
|
|
126
|
+
|
|
127
|
+
let(:person) do
|
|
128
|
+
Person.create
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
let!(:game) do
|
|
132
|
+
person.create_game(name: "Pong")
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
before do
|
|
136
|
+
person.send(method)
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
it "destroys the associated documents" do
|
|
140
|
+
expect {
|
|
141
|
+
Game.find(game.id)
|
|
142
|
+
}.to raise_error(Mongoid::Errors::DocumentNotFound)
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
context "when strategy is nullify" do
|
|
147
|
+
|
|
148
|
+
context "when nullifying a references many" do
|
|
149
|
+
|
|
150
|
+
let(:movie) do
|
|
151
|
+
Movie.create(title: "Bladerunner")
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
let!(:rating) do
|
|
155
|
+
movie.ratings.create(value: 10)
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
let(:from_db) do
|
|
159
|
+
Rating.find(rating.id)
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
before do
|
|
163
|
+
movie.send(method)
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
it "removes the references to the removed document" do
|
|
167
|
+
expect(from_db.ratable_id).to be_nil
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
context "when nullifying a references one" do
|
|
172
|
+
|
|
173
|
+
context "when the relation exists" do
|
|
174
|
+
|
|
175
|
+
let(:book) do
|
|
176
|
+
Book.create(title: "Neuromancer")
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
let!(:rating) do
|
|
180
|
+
book.create_rating(value: 10)
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
let(:from_db) do
|
|
184
|
+
Rating.find(rating.id)
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
before do
|
|
188
|
+
book.send(method)
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
it "removes the references to the removed document" do
|
|
192
|
+
expect(from_db.ratable_id).to be_nil
|
|
193
|
+
end
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
context "when the relation is nil" do
|
|
197
|
+
|
|
198
|
+
let(:book) do
|
|
199
|
+
Book.create(title: "Neuromancer")
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
it "returns nil" do
|
|
203
|
+
expect(book.send(method)).to be true
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
context "when nullifying a many to many" do
|
|
209
|
+
|
|
210
|
+
let(:person) do
|
|
211
|
+
Person.create
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
let!(:preference) do
|
|
215
|
+
person.preferences.create(name: "Setting")
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
let(:from_db) do
|
|
219
|
+
Preference.find(preference.id)
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
before do
|
|
223
|
+
person.send(method)
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
it "removes the references from the removed document" do
|
|
227
|
+
expect(person.preference_ids).to_not include(preference.id)
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
it "removes the references to the removed document" do
|
|
231
|
+
expect(from_db.person_ids).to_not include(person.id)
|
|
232
|
+
end
|
|
233
|
+
end
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
context "when dependent is restrict_with_error" do
|
|
237
|
+
|
|
238
|
+
context "when restricting a references many" do
|
|
239
|
+
|
|
240
|
+
let!(:association) do
|
|
241
|
+
Person.has_many :drugs, dependent: :restrict_with_exception
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
after do
|
|
245
|
+
Person.dependents.delete(association)
|
|
246
|
+
Person.has_many :drugs, validate: false
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
context "when the relation is empty" do
|
|
250
|
+
|
|
251
|
+
let(:person) do
|
|
252
|
+
Person.new drugs: []
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
it "raises no error" do
|
|
256
|
+
expect { person.send(method) }.to_not raise_error
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
it "deletes the parent" do
|
|
260
|
+
person.send(method)
|
|
261
|
+
expect(person).to be_destroyed
|
|
262
|
+
end
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
context "when the relation is not empty" do
|
|
266
|
+
|
|
267
|
+
let(:person) do
|
|
268
|
+
Person.new drugs: [Drug.new]
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
it "raises DeleteRestriction error" do
|
|
272
|
+
expect { person.send(method) }.to raise_error(Mongoid::Errors::DeleteRestriction)
|
|
273
|
+
end
|
|
274
|
+
end
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
context "when restricting a references one" do
|
|
278
|
+
|
|
279
|
+
let!(:association) do
|
|
280
|
+
Person.has_one :account, dependent: :restrict_with_exception
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
after do
|
|
284
|
+
Person.dependents.delete(association)
|
|
285
|
+
Person.has_one :account, validate: false
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
context "when the relation is empty" do
|
|
289
|
+
|
|
290
|
+
let(:person) do
|
|
291
|
+
Person.new account: nil
|
|
292
|
+
end
|
|
293
|
+
|
|
294
|
+
it "raises no error" do
|
|
295
|
+
expect { person.send(method) }.to_not raise_error
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
it "deletes the parent" do
|
|
299
|
+
person.send(method)
|
|
300
|
+
expect(person).to be_destroyed
|
|
301
|
+
end
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
context "when the relation is not empty" do
|
|
305
|
+
|
|
306
|
+
let(:person) do
|
|
307
|
+
Person.new account: Account.new(name: 'test')
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
it "raises DeleteRestriction error" do
|
|
311
|
+
expect { person.send(method) }.to raise_error(Mongoid::Errors::DeleteRestriction)
|
|
312
|
+
end
|
|
313
|
+
end
|
|
314
|
+
end
|
|
315
|
+
|
|
316
|
+
context "when restricting a many to many" do
|
|
317
|
+
|
|
318
|
+
let!(:association) do
|
|
319
|
+
Person.has_and_belongs_to_many :houses, dependent: :restrict_with_exception
|
|
320
|
+
end
|
|
321
|
+
|
|
322
|
+
after do
|
|
323
|
+
Person.dependents.delete(association)
|
|
324
|
+
Person.has_and_belongs_to_many :houses, validate: false
|
|
325
|
+
end
|
|
326
|
+
|
|
327
|
+
context "when the relation is empty" do
|
|
328
|
+
|
|
329
|
+
let(:person) do
|
|
330
|
+
Person.new houses: []
|
|
331
|
+
end
|
|
332
|
+
|
|
333
|
+
it "raises no error" do
|
|
334
|
+
expect { person.send(method) }.to_not raise_error
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
it "deletes the parent" do
|
|
338
|
+
person.send(method)
|
|
339
|
+
expect(person).to be_destroyed
|
|
340
|
+
end
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
context "when the relation is not empty" do
|
|
344
|
+
|
|
345
|
+
let(:person) do
|
|
346
|
+
Person.new houses: [House.new]
|
|
347
|
+
end
|
|
348
|
+
|
|
349
|
+
it "raises DeleteRestriction error" do
|
|
350
|
+
expect { person.send(method) }.to raise_error(Mongoid::Errors::DeleteRestriction)
|
|
351
|
+
end
|
|
352
|
+
end
|
|
353
|
+
end
|
|
354
|
+
end
|
|
355
|
+
end
|
|
356
|
+
end
|
|
357
|
+
end
|
|
358
|
+
|
|
359
|
+
context 'when the strategy is :delete_all' do
|
|
360
|
+
|
|
361
|
+
let(:person) do
|
|
362
|
+
Person.create
|
|
363
|
+
end
|
|
364
|
+
|
|
365
|
+
context "when cascading a has one" do
|
|
366
|
+
|
|
367
|
+
context "when the relation exists" do
|
|
368
|
+
|
|
369
|
+
let!(:home) do
|
|
370
|
+
person.create_home
|
|
371
|
+
end
|
|
372
|
+
|
|
373
|
+
before do
|
|
374
|
+
person.delete
|
|
375
|
+
end
|
|
376
|
+
|
|
377
|
+
it "deletes the relation" do
|
|
378
|
+
expect(home).to be_destroyed
|
|
379
|
+
end
|
|
380
|
+
|
|
381
|
+
it "persists the deletion" do
|
|
382
|
+
expect {
|
|
383
|
+
home.reload
|
|
384
|
+
}.to raise_error(Mongoid::Errors::DocumentNotFound)
|
|
385
|
+
end
|
|
386
|
+
end
|
|
387
|
+
|
|
388
|
+
context "when the relation does not exist" do
|
|
389
|
+
|
|
390
|
+
before do
|
|
391
|
+
person.delete
|
|
392
|
+
end
|
|
393
|
+
|
|
394
|
+
it "deletes the base document" do
|
|
395
|
+
expect(person).to be_destroyed
|
|
396
|
+
end
|
|
397
|
+
end
|
|
398
|
+
end
|
|
399
|
+
|
|
400
|
+
context "when cascading a has many" do
|
|
401
|
+
|
|
402
|
+
context "when the relation has documents" do
|
|
403
|
+
|
|
404
|
+
let!(:post_one) do
|
|
405
|
+
person.posts.create(title: "one")
|
|
406
|
+
end
|
|
407
|
+
|
|
408
|
+
let!(:post_two) do
|
|
409
|
+
person.posts.create(title: "two")
|
|
410
|
+
end
|
|
411
|
+
|
|
412
|
+
context "when the documents are in memory" do
|
|
413
|
+
|
|
414
|
+
before do
|
|
415
|
+
expect(post_one).to receive(:delete).never
|
|
416
|
+
expect(post_two).to receive(:delete).never
|
|
417
|
+
person.delete
|
|
418
|
+
end
|
|
419
|
+
|
|
420
|
+
it "deletes the first document" do
|
|
421
|
+
expect(post_one).to be_destroyed
|
|
422
|
+
end
|
|
423
|
+
|
|
424
|
+
it "deletes the second document" do
|
|
425
|
+
expect(post_two).to be_destroyed
|
|
426
|
+
end
|
|
427
|
+
|
|
428
|
+
it "unbinds the first document" do
|
|
429
|
+
expect(post_one.person).to be_nil
|
|
430
|
+
end
|
|
431
|
+
|
|
432
|
+
it "unbinds the second document" do
|
|
433
|
+
expect(post_two.person).to be_nil
|
|
434
|
+
end
|
|
435
|
+
|
|
436
|
+
it "removes the documents from the relation" do
|
|
437
|
+
expect(person.posts).to be_empty
|
|
438
|
+
end
|
|
439
|
+
|
|
440
|
+
it "persists the first deletion" do
|
|
441
|
+
expect {
|
|
442
|
+
post_one.reload
|
|
443
|
+
}.to raise_error(Mongoid::Errors::DocumentNotFound)
|
|
444
|
+
end
|
|
445
|
+
|
|
446
|
+
it "persists the second deletion" do
|
|
447
|
+
expect {
|
|
448
|
+
post_two.reload
|
|
449
|
+
}.to raise_error(Mongoid::Errors::DocumentNotFound)
|
|
450
|
+
end
|
|
451
|
+
end
|
|
452
|
+
end
|
|
453
|
+
end
|
|
454
|
+
end
|
|
455
|
+
|
|
456
|
+
context 'when the strategy is :destroy' do
|
|
457
|
+
|
|
458
|
+
let!(:association) do
|
|
459
|
+
Person.has_many :destroyable_posts, class_name: "Post", dependent: :destroy
|
|
460
|
+
end
|
|
461
|
+
|
|
462
|
+
after do
|
|
463
|
+
Person.dependents.delete(association)
|
|
464
|
+
end
|
|
465
|
+
|
|
466
|
+
let(:person) do
|
|
467
|
+
Person.new
|
|
468
|
+
end
|
|
469
|
+
|
|
470
|
+
let(:post) do
|
|
471
|
+
Post.new
|
|
472
|
+
end
|
|
473
|
+
|
|
474
|
+
context "when the documents exist" do
|
|
475
|
+
|
|
476
|
+
before do
|
|
477
|
+
expect(post).to receive(:destroy)
|
|
478
|
+
person.destroyable_posts << post
|
|
479
|
+
end
|
|
480
|
+
|
|
481
|
+
it "destroys all documents in the relation" do
|
|
482
|
+
person.delete
|
|
483
|
+
end
|
|
484
|
+
end
|
|
485
|
+
|
|
486
|
+
context "when no documents exist" do
|
|
487
|
+
|
|
488
|
+
before do
|
|
489
|
+
expect(post).to receive(:destroy).never
|
|
490
|
+
end
|
|
491
|
+
|
|
492
|
+
it "it does not destroy the relation" do
|
|
493
|
+
person.delete
|
|
494
|
+
end
|
|
495
|
+
end
|
|
496
|
+
end
|
|
497
|
+
|
|
498
|
+
context 'when the strategy is :nullify' do
|
|
499
|
+
|
|
500
|
+
let!(:association) do
|
|
501
|
+
Person.has_many :nullifyable_posts, class_name: "Post", dependent: :nullify
|
|
502
|
+
end
|
|
503
|
+
|
|
504
|
+
after do
|
|
505
|
+
Person.dependents.delete(association)
|
|
506
|
+
end
|
|
507
|
+
|
|
508
|
+
let(:person) do
|
|
509
|
+
Person.new
|
|
510
|
+
end
|
|
511
|
+
|
|
512
|
+
let(:posts_relation) do
|
|
513
|
+
person.posts
|
|
514
|
+
end
|
|
515
|
+
|
|
516
|
+
before do
|
|
517
|
+
allow(person).to receive(:nullifyable_posts).and_return(posts_relation)
|
|
518
|
+
expect(posts_relation).to receive(:nullify)
|
|
519
|
+
end
|
|
520
|
+
|
|
521
|
+
it "nullifies the relation" do
|
|
522
|
+
person.delete
|
|
523
|
+
end
|
|
524
|
+
end
|
|
525
|
+
|
|
526
|
+
context 'when the strategy is :restrict_with_exception' do
|
|
527
|
+
|
|
528
|
+
let(:person) do
|
|
529
|
+
Person.new
|
|
530
|
+
end
|
|
531
|
+
|
|
532
|
+
let(:post) do
|
|
533
|
+
Post.new
|
|
534
|
+
end
|
|
535
|
+
|
|
536
|
+
let!(:association) do
|
|
537
|
+
Person.has_many :restrictable_posts, class_name: "Post", dependent: :restrict_with_exception
|
|
538
|
+
end
|
|
539
|
+
|
|
540
|
+
after do
|
|
541
|
+
Person.dependents.delete(association)
|
|
542
|
+
end
|
|
543
|
+
|
|
544
|
+
context 'when there are related objects' do
|
|
545
|
+
|
|
546
|
+
before do
|
|
547
|
+
person.restrictable_posts << post
|
|
548
|
+
expect(post).to receive(:delete).never
|
|
549
|
+
expect(post).to receive(:destroy).never
|
|
550
|
+
end
|
|
551
|
+
|
|
552
|
+
it 'raises an exception and leaves the related one intact' do
|
|
553
|
+
expect { person.delete }.to raise_exception(Mongoid::Errors::DeleteRestriction)
|
|
554
|
+
end
|
|
555
|
+
end
|
|
556
|
+
|
|
557
|
+
context 'when there are no related objects' do
|
|
558
|
+
|
|
559
|
+
before do
|
|
560
|
+
expect(post).to receive(:delete).never
|
|
561
|
+
expect(post).to receive(:destroy).never
|
|
562
|
+
end
|
|
563
|
+
|
|
564
|
+
it 'deletes the object and leaves the other one intact' do
|
|
565
|
+
expect(person.delete).to be(true)
|
|
566
|
+
end
|
|
567
|
+
end
|
|
568
|
+
end
|
|
569
|
+
|
|
570
|
+
context 'when the strategy is :restrict_with_error' do
|
|
571
|
+
|
|
572
|
+
let(:person) do
|
|
573
|
+
Person.new
|
|
574
|
+
end
|
|
575
|
+
|
|
576
|
+
let(:post) do
|
|
577
|
+
Post.new
|
|
578
|
+
end
|
|
579
|
+
|
|
580
|
+
let!(:association) do
|
|
581
|
+
Person.has_many :restrictable_posts, class_name: "Post", dependent: :restrict_with_error
|
|
582
|
+
end
|
|
583
|
+
|
|
584
|
+
after do
|
|
585
|
+
Person.dependents.delete(association)
|
|
586
|
+
end
|
|
587
|
+
|
|
588
|
+
context 'when there are related objects' do
|
|
589
|
+
|
|
590
|
+
before do
|
|
591
|
+
person.restrictable_posts << post
|
|
592
|
+
end
|
|
593
|
+
|
|
594
|
+
it 'adds an error to the parent object' do
|
|
595
|
+
expect(person.delete).to be(false)
|
|
596
|
+
expect(person.errors[:restrictable_posts].first).to be(
|
|
597
|
+
Mongoid::Association::Depending::RESTRICT_ERROR_MSG)
|
|
598
|
+
end
|
|
599
|
+
end
|
|
600
|
+
|
|
601
|
+
context 'when there are no related objects' do
|
|
602
|
+
|
|
603
|
+
before do
|
|
604
|
+
expect(post).to receive(:delete).never
|
|
605
|
+
expect(post).to receive(:destroy).never
|
|
606
|
+
end
|
|
607
|
+
|
|
608
|
+
it 'deletes the object and leaves the other one intact' do
|
|
609
|
+
expect(person.delete).to be(true)
|
|
610
|
+
end
|
|
611
|
+
end
|
|
612
|
+
end
|
|
613
|
+
end
|