mongoid 6.4.2 → 7.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/config/locales/en.yml +17 -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 +137 -0
- data/lib/mongoid/{relations/eager.rb → association/eager_loadable.rb} +11 -11
- data/lib/mongoid/{relations → association}/embedded/batchable.rb +19 -19
- data/lib/mongoid/association/embedded/cyclic.rb +109 -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/embedded_in.rb +154 -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 +552 -0
- data/lib/mongoid/association/embedded/embeds_many.rb +210 -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/embedded/embeds_one.rb +173 -0
- data/lib/mongoid/association/embedded.rb +4 -0
- data/lib/mongoid/association/macros.rb +204 -0
- data/lib/mongoid/{relations → association}/many.rb +19 -49
- data/lib/mongoid/{relations → association}/marshalable.rb +6 -4
- 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/association/nested.rb +15 -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/auto_save.rb +79 -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/belongs_to.rb +248 -0
- data/lib/mongoid/association/referenced/counter_cache.rb +163 -0
- data/lib/mongoid/association/referenced/eager.rb +162 -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 +312 -0
- data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +290 -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 +510 -0
- data/lib/mongoid/association/referenced/has_many/proxy.rb +578 -0
- data/lib/mongoid/association/referenced/has_many.rb +275 -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/has_one.rb +204 -0
- data/lib/mongoid/association/referenced/syncable.rb +170 -0
- data/lib/mongoid/association/referenced.rb +7 -0
- data/lib/mongoid/{relations → association}/reflections.rb +21 -17
- data/lib/mongoid/association/relatable.rb +509 -0
- data/lib/mongoid/{relations.rb → association.rb} +57 -56
- data/lib/mongoid/atomic/paths/embedded/many.rb +1 -1
- data/lib/mongoid/atomic/paths/embedded/one.rb +1 -1
- data/lib/mongoid/atomic.rb +4 -4
- data/lib/mongoid/attributes/dynamic.rb +2 -2
- data/lib/mongoid/attributes/nested.rb +22 -11
- data/lib/mongoid/attributes/processing.rb +2 -2
- data/lib/mongoid/attributes/readonly.rb +2 -4
- data/lib/mongoid/attributes.rb +22 -13
- data/lib/mongoid/changeable.rb +1 -1
- data/lib/mongoid/clients/options.rb +7 -5
- data/lib/mongoid/composable.rb +4 -4
- data/lib/mongoid/config.rb +1 -0
- data/lib/mongoid/contextual/atomic.rb +1 -1
- data/lib/mongoid/contextual/geo_near.rb +1 -1
- data/lib/mongoid/contextual/memory.rb +21 -3
- data/lib/mongoid/contextual/mongo.rb +10 -8
- data/lib/mongoid/copyable.rb +7 -6
- data/lib/mongoid/criteria/includable.rb +14 -14
- data/lib/mongoid/criteria/modifiable.rb +8 -14
- data/lib/mongoid/criteria/options.rb +2 -2
- data/lib/mongoid/criteria/queryable/extensions/string.rb +1 -1
- data/lib/mongoid/criteria/queryable/pipeline.rb +10 -5
- data/lib/mongoid/criteria/queryable/selectable.rb +37 -7
- data/lib/mongoid/criteria.rb +2 -2
- data/lib/mongoid/document.rb +15 -6
- 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/errors.rb +3 -0
- data/lib/mongoid/extensions/array.rb +5 -5
- data/lib/mongoid/extensions/big_decimal.rb +1 -1
- data/lib/mongoid/extensions/hash.rb +5 -2
- data/lib/mongoid/extensions/object.rb +4 -4
- data/lib/mongoid/extensions/range.rb +1 -0
- data/lib/mongoid/extensions/regexp.rb +1 -0
- data/lib/mongoid/extensions.rb +0 -4
- data/lib/mongoid/factory.rb +13 -3
- 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/fields.rb +3 -3
- data/lib/mongoid/indexable.rb +4 -1
- data/lib/mongoid/interceptable.rb +5 -5
- data/lib/mongoid/matchable/and.rb +1 -1
- data/lib/mongoid/matchable/elem_match.rb +9 -3
- data/lib/mongoid/matchable/eq.rb +22 -0
- data/lib/mongoid/matchable/ne.rb +1 -1
- data/lib/mongoid/matchable.rb +3 -1
- data/lib/mongoid/persistable/deletable.rb +7 -6
- data/lib/mongoid/persistable/incrementable.rb +1 -1
- data/lib/mongoid/persistable/logical.rb +1 -1
- data/lib/mongoid/persistable/settable.rb +57 -12
- data/lib/mongoid/persistable.rb +4 -5
- data/lib/mongoid/persistence_context.rb +20 -5
- data/lib/mongoid/query_cache.rb +8 -4
- data/lib/mongoid/railtie.rb +17 -0
- data/lib/mongoid/railties/controller_runtime.rb +86 -0
- data/lib/mongoid/scopable.rb +3 -3
- data/lib/mongoid/serializable.rb +4 -4
- data/lib/mongoid/shardable.rb +1 -1
- data/lib/mongoid/threaded.rb +36 -0
- data/lib/mongoid/touchable.rb +102 -0
- data/lib/mongoid/traversable.rb +3 -3
- data/lib/mongoid/validatable/presence.rb +2 -2
- data/lib/mongoid/validatable/uniqueness.rb +4 -4
- data/lib/mongoid/validatable.rb +8 -8
- data/lib/mongoid/version.rb +1 -1
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +3 -0
- data/spec/app/models/animal.rb +2 -1
- data/spec/app/models/bomb.rb +1 -1
- data/spec/app/models/message.rb +1 -1
- data/spec/app/models/minim.rb +7 -0
- data/spec/app/models/person.rb +5 -2
- data/spec/app/models/shipment_address.rb +1 -0
- data/spec/app/models/store_as_dup_test3.rb +7 -0
- data/spec/app/models/store_as_dup_test4.rb +7 -0
- data/spec/app/models/updatable.rb +7 -0
- data/spec/app/models/vertex.rb +6 -0
- data/spec/app/models/wiki_page.rb +1 -1
- data/spec/config/mongoid.yml +13 -3
- data/spec/integration/associations/belongs_to_spec.rb +13 -0
- data/spec/lite_spec_helper.rb +56 -0
- data/spec/mongoid/{relations → association}/accessors_spec.rb +40 -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 +866 -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/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} +124 -182
- 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 -73
- 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 +66 -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} +33 -14
- 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 +2006 -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} +26 -7
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_persistence_spec.rb +56 -0
- 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_models.rb +26 -0
- 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} +26 -11
- data/spec/mongoid/{relations/targets → association/referenced/has_many}/enumerable_spec.rb +122 -1
- data/spec/mongoid/association/referenced/has_many/proxy_query_spec.rb +23 -0
- data/spec/mongoid/{relations/referenced/many_spec.rb → association/referenced/has_many/proxy_spec.rb} +28 -93
- data/spec/mongoid/association/referenced/has_many_models.rb +37 -0
- 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_models.rb +48 -0
- data/spec/mongoid/association/referenced/has_one_spec.rb +1350 -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_spec.rb → association_spec.rb} +1 -1
- data/spec/mongoid/atomic/modifiers_spec.rb +2 -2
- data/spec/mongoid/atomic_spec.rb +4 -4
- data/spec/mongoid/attributes/nested_spec.rb +29 -11
- data/spec/mongoid/attributes/readonly_spec.rb +80 -125
- data/spec/mongoid/attributes_spec.rb +38 -2
- data/spec/mongoid/clients/factory_spec.rb +24 -18
- data/spec/mongoid/clients/options_spec.rb +58 -44
- data/spec/mongoid/clients/sessions_spec.rb +1 -1
- data/spec/mongoid/clients/transactions_spec.rb +369 -0
- data/spec/mongoid/clients_spec.rb +68 -8
- data/spec/mongoid/config_spec.rb +27 -1
- data/spec/mongoid/contextual/memory_spec.rb +19 -0
- data/spec/mongoid/contextual/mongo_spec.rb +33 -5
- data/spec/mongoid/copyable_spec.rb +90 -6
- data/spec/mongoid/copyable_spec_models.rb +17 -0
- 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/extensions/string_spec.rb +43 -0
- data/spec/mongoid/criteria/queryable/pipeline_spec.rb +12 -0
- data/spec/mongoid/criteria/queryable/selectable_spec.rb +42 -3
- data/spec/mongoid/criteria/queryable/selector_spec.rb +2 -2
- data/spec/mongoid/criteria/scopable_spec.rb +81 -0
- data/spec/mongoid/criteria_spec.rb +33 -18
- data/spec/mongoid/document_spec.rb +83 -4
- data/spec/mongoid/extensions/array_spec.rb +11 -15
- data/spec/mongoid/extensions/big_decimal_spec.rb +9 -9
- data/spec/mongoid/extensions/hash_spec.rb +18 -1
- 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 +23 -0
- data/spec/mongoid/factory_spec.rb +19 -1
- 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 +22 -1
- data/spec/mongoid/matchable/elem_match_spec.rb +20 -0
- data/spec/mongoid/matchable/eq_spec.rb +48 -0
- data/spec/mongoid/persistable/incrementable_spec.rb +6 -6
- data/spec/mongoid/persistable/savable_spec.rb +2 -2
- data/spec/mongoid/persistable/settable_spec.rb +95 -10
- data/spec/mongoid/persistable_spec.rb +21 -6
- data/spec/mongoid/persistence_context_spec.rb +1 -1
- data/spec/mongoid/query_cache_spec.rb +61 -22
- data/spec/mongoid/relations/proxy_spec.rb +124 -124
- data/spec/mongoid/scopable_spec.rb +13 -0
- data/spec/mongoid/shardable_spec.rb +32 -12
- data/spec/mongoid/threaded_spec.rb +68 -0
- data/spec/mongoid/{relations/touchable_spec.rb → touchable_spec.rb} +40 -1
- 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/rails/controller_extension/controller_runtime_spec.rb +110 -0
- data/spec/spec_helper.rb +51 -25
- data/spec/support/constraints.rb +101 -0
- data/spec/support/macros.rb +20 -0
- data/spec/support/spec_config.rb +39 -0
- metadata +602 -582
- checksums.yaml.gz.sig +0 -2
- data/lib/mongoid/relations/accessors.rb +0 -267
- data/lib/mongoid/relations/auto_save.rb +0 -94
- 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/bindings.rb +0 -9
- data/lib/mongoid/relations/builder.rb +0 -57
- 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/builders.rb +0 -106
- 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/cascading.rb +0 -56
- 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/enumerable.rb +0 -493
- data/lib/mongoid/relations/targets.rb +0 -2
- data/lib/mongoid/relations/touchable.rb +0 -97
- 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/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.tar.gz.sig +0 -0
- metadata.gz.sig +0 -0
- /data/spec/mongoid/{relations → association}/embedded/dirty_spec.rb +0 -0
@@ -0,0 +1,866 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Mongoid::Association::Depending do
|
4
|
+
|
5
|
+
describe '#self.included' do
|
6
|
+
|
7
|
+
context 'when a destroy dependent is defined' do
|
8
|
+
|
9
|
+
context 'when the model is a subclass' do
|
10
|
+
|
11
|
+
context 'when transitive dependents are defined' do
|
12
|
+
|
13
|
+
let(:define_classes) do
|
14
|
+
class DependentReportCard
|
15
|
+
include Mongoid::Document
|
16
|
+
|
17
|
+
belongs_to :dependent_student
|
18
|
+
end
|
19
|
+
|
20
|
+
class DependentUser
|
21
|
+
include Mongoid::Document
|
22
|
+
end
|
23
|
+
|
24
|
+
class DependentStudent < DependentUser
|
25
|
+
belongs_to :dependent_teacher
|
26
|
+
has_many :dependent_report_cards, dependent: :destroy
|
27
|
+
end
|
28
|
+
|
29
|
+
class DependentDerivedStudent < DependentStudent; end
|
30
|
+
|
31
|
+
class DependentTeacher
|
32
|
+
include Mongoid::Document
|
33
|
+
|
34
|
+
has_many :dependent_students, dependent: :destroy
|
35
|
+
end
|
36
|
+
|
37
|
+
class DependentCollegeUser < DependentUser; end
|
38
|
+
end
|
39
|
+
|
40
|
+
it "does not add the dependent to superclass" do
|
41
|
+
define_classes
|
42
|
+
|
43
|
+
expect(DependentUser.dependents).to be_empty
|
44
|
+
|
45
|
+
u = DependentUser.create!
|
46
|
+
expect(u.dependents).to be_empty
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'does not impede destroying the superclass' do
|
50
|
+
define_classes
|
51
|
+
|
52
|
+
u = DependentUser.create!
|
53
|
+
expect { u.destroy! }.not_to raise_error
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'adds the dependent' do
|
57
|
+
define_classes
|
58
|
+
|
59
|
+
expect(DependentStudent.dependents.length).to be(1)
|
60
|
+
expect(DependentStudent.dependents.first.name).to be(:dependent_report_cards)
|
61
|
+
|
62
|
+
s = DependentStudent.create!
|
63
|
+
expect(s.dependents.length).to be(1)
|
64
|
+
expect(s.dependents.first.name).to be(:dependent_report_cards)
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'facilitates proper destroying of the object' do
|
68
|
+
define_classes
|
69
|
+
|
70
|
+
s = DependentStudent.create!
|
71
|
+
r = DependentReportCard.create!(dependent_student: s)
|
72
|
+
s.destroy!
|
73
|
+
|
74
|
+
expect { DependentReportCard.find(r.id) }.to raise_error(Mongoid::Errors::DocumentNotFound)
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'facilitates proper transitive destroying of the object' do
|
78
|
+
define_classes
|
79
|
+
|
80
|
+
t = DependentTeacher.create!
|
81
|
+
s = DependentStudent.create!(dependent_teacher: t)
|
82
|
+
r = DependentReportCard.create!(dependent_student: s)
|
83
|
+
s.destroy!
|
84
|
+
|
85
|
+
expect { DependentReportCard.find(r.id) }.to raise_error(Mongoid::Errors::DocumentNotFound)
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'adds the dependent to subclasses' do
|
89
|
+
define_classes
|
90
|
+
|
91
|
+
expect(DependentDerivedStudent.dependents.length).to be(1)
|
92
|
+
expect(DependentDerivedStudent.dependents.first.name).to be(:dependent_report_cards)
|
93
|
+
|
94
|
+
s = DependentDerivedStudent.create!
|
95
|
+
expect(s.dependents.length).to be(1)
|
96
|
+
expect(s.dependents.first.name).to be(:dependent_report_cards)
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'facilitates proper destroying of the object from subclasses' do
|
100
|
+
define_classes
|
101
|
+
|
102
|
+
s = DependentDerivedStudent.create!
|
103
|
+
r = DependentReportCard.create!(dependent_student: s)
|
104
|
+
s.destroy!
|
105
|
+
|
106
|
+
expect { DependentReportCard.find(r.id) }.to raise_error(Mongoid::Errors::DocumentNotFound)
|
107
|
+
end
|
108
|
+
|
109
|
+
it "doesn't add the dependent to sibling classes" do
|
110
|
+
define_classes
|
111
|
+
|
112
|
+
expect(DependentCollegeUser.dependents).to be_empty
|
113
|
+
|
114
|
+
c = DependentCollegeUser.create!
|
115
|
+
expect(c.dependents).to be_empty
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'does not impede destroying the sibling class' do
|
119
|
+
define_classes
|
120
|
+
|
121
|
+
c = DependentCollegeUser.create!
|
122
|
+
expect { c.destroy! }.not_to raise_error
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
context 'when a superclass is reopened and a new dependent is added' do
|
127
|
+
let(:define_classes) do
|
128
|
+
class DependentOwnedOne
|
129
|
+
include Mongoid::Document
|
130
|
+
|
131
|
+
belongs_to :dependent_superclass
|
132
|
+
end
|
133
|
+
|
134
|
+
class DependentOwnedTwo
|
135
|
+
include Mongoid::Document
|
136
|
+
|
137
|
+
belongs_to :dependent_superclass
|
138
|
+
end
|
139
|
+
|
140
|
+
class DependentSuperclass
|
141
|
+
include Mongoid::Document
|
142
|
+
has_one :dependent_owned_one
|
143
|
+
has_one :dependent_owned_two
|
144
|
+
end
|
145
|
+
|
146
|
+
class DependentSubclass < DependentSuperclass
|
147
|
+
has_one :dependent_owned_two, dependent: :nullify
|
148
|
+
end
|
149
|
+
|
150
|
+
class DependentSuperclass
|
151
|
+
has_one :dependent_owned_one, dependent: :destroy
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
it 'defines the dependent from the reopened superclass on the subclass' do
|
156
|
+
define_classes
|
157
|
+
|
158
|
+
DependentSubclass.create!.destroy!
|
159
|
+
|
160
|
+
expect(DependentSubclass.dependents.length).to be(1)
|
161
|
+
expect(DependentSubclass.dependents.last.name).to be(:dependent_owned_two)
|
162
|
+
expect(DependentSubclass.dependents.last.options[:dependent]).to be(:nullify)
|
163
|
+
|
164
|
+
subclass = DependentSubclass.create!
|
165
|
+
expect(subclass.dependents.last.name).to be(:dependent_owned_two)
|
166
|
+
expect(subclass.dependents.last.options[:dependent]).to be(:nullify)
|
167
|
+
end
|
168
|
+
|
169
|
+
it 'causes the destruction of the inherited destroy dependent' do
|
170
|
+
define_classes
|
171
|
+
|
172
|
+
subclass = DependentSubclass.create!
|
173
|
+
owned = DependentOwnedOne.create!(dependent_superclass: subclass)
|
174
|
+
subclass.destroy!
|
175
|
+
|
176
|
+
expect {
|
177
|
+
DependentOwnedOne.find(owned.id)
|
178
|
+
}.to raise_error(Mongoid::Errors::DocumentNotFound)
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
context 'when a separate subclass overrides the destroy dependent' do
|
183
|
+
let(:define_classes) do
|
184
|
+
class Dep
|
185
|
+
include Mongoid::Document
|
186
|
+
|
187
|
+
belongs_to :double_assoc
|
188
|
+
end
|
189
|
+
|
190
|
+
class DoubleAssoc
|
191
|
+
include Mongoid::Document
|
192
|
+
|
193
|
+
has_many :deps, dependent: :destroy
|
194
|
+
end
|
195
|
+
|
196
|
+
class DoubleAssocOne < DoubleAssoc
|
197
|
+
has_many :deps, dependent: :nullify, inverse_of: :double_assoc
|
198
|
+
end
|
199
|
+
|
200
|
+
class DoubleAssocTwo < DoubleAssocOne
|
201
|
+
has_many :deps, dependent: :destroy, inverse_of: :double_assoc
|
202
|
+
end
|
203
|
+
|
204
|
+
class DoubleAssocThree < DoubleAssoc; end
|
205
|
+
end
|
206
|
+
|
207
|
+
it 'adds the non-destroy dependent correctly to the subclass with the override' do
|
208
|
+
define_classes
|
209
|
+
|
210
|
+
expect(DoubleAssocOne.dependents.length).to be(1)
|
211
|
+
expect(DoubleAssocOne.dependents.first.name).to be(:deps)
|
212
|
+
expect(DoubleAssocOne.dependents.first.options[:dependent]).to be(:nullify)
|
213
|
+
|
214
|
+
one = DoubleAssocOne.create!
|
215
|
+
expect(one.dependents.length).to be(1)
|
216
|
+
expect(one.dependents.first.name).to be(:deps)
|
217
|
+
expect(one.dependents.first.options[:dependent]).to be(:nullify)
|
218
|
+
end
|
219
|
+
|
220
|
+
it 'does not cause the destruction of the non-destroy dependent' do
|
221
|
+
define_classes
|
222
|
+
|
223
|
+
one = DoubleAssocOne.create!
|
224
|
+
dep = Dep.create!(double_assoc: one)
|
225
|
+
one.destroy!
|
226
|
+
|
227
|
+
expect { Dep.find(dep.id) }.not_to raise_error
|
228
|
+
expect(dep.double_assoc).to be_nil
|
229
|
+
end
|
230
|
+
|
231
|
+
it 'adds the destroy dependent correctly to the subclass without the override' do
|
232
|
+
define_classes
|
233
|
+
|
234
|
+
expect(DoubleAssocTwo.dependents.length).to be(1)
|
235
|
+
expect(DoubleAssocTwo.dependents.first.name).to be(:deps)
|
236
|
+
expect(DoubleAssocTwo.dependents.first.options[:dependent]).to be(:destroy)
|
237
|
+
|
238
|
+
two = DoubleAssocTwo.create!
|
239
|
+
expect(two.dependents.length).to be(1)
|
240
|
+
expect(two.dependents.first.name).to be(:deps)
|
241
|
+
expect(two.dependents.first.options[:dependent]).to be(:destroy)
|
242
|
+
end
|
243
|
+
|
244
|
+
it 'causes the destruction of the destroy dependent' do
|
245
|
+
define_classes
|
246
|
+
|
247
|
+
two = DoubleAssocTwo.create!
|
248
|
+
dep = Dep.create!(double_assoc: two)
|
249
|
+
two.destroy!
|
250
|
+
|
251
|
+
expect { Dep.find(dep.id) }.to raise_error(Mongoid::Errors::DocumentNotFound)
|
252
|
+
end
|
253
|
+
end
|
254
|
+
end
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
around(:each) do |example|
|
259
|
+
relations_before = Person.relations
|
260
|
+
example.run
|
261
|
+
Person.relations = relations_before
|
262
|
+
end
|
263
|
+
|
264
|
+
describe "#apply_delete_dependencies!" do
|
265
|
+
|
266
|
+
let(:band) do
|
267
|
+
Band.new
|
268
|
+
end
|
269
|
+
|
270
|
+
context "when the association exists in the list of dependencies" do
|
271
|
+
|
272
|
+
context "when the association has no dependent strategy" do
|
273
|
+
|
274
|
+
before do
|
275
|
+
band.dependents.push(Band.relations["records"])
|
276
|
+
end
|
277
|
+
|
278
|
+
after do
|
279
|
+
band.dependents.delete(Band.relations["records"])
|
280
|
+
end
|
281
|
+
|
282
|
+
it "ignores the dependency" do
|
283
|
+
expect(band.apply_delete_dependencies!).to eq([Band.relations["records"]])
|
284
|
+
end
|
285
|
+
end
|
286
|
+
end
|
287
|
+
end
|
288
|
+
|
289
|
+
describe ".define_dependency!" do
|
290
|
+
|
291
|
+
let(:klass) do
|
292
|
+
Class.new.tap { |c| c.send(:include, Mongoid::Document) }
|
293
|
+
end
|
294
|
+
|
295
|
+
context "when the association metadata doesnt exist" do
|
296
|
+
|
297
|
+
before do
|
298
|
+
klass.dependents.push("nothing")
|
299
|
+
end
|
300
|
+
|
301
|
+
it "does not raise an error" do
|
302
|
+
expect {
|
303
|
+
klass.new.apply_delete_dependencies!
|
304
|
+
}.not_to raise_error
|
305
|
+
end
|
306
|
+
end
|
307
|
+
|
308
|
+
context "when a dependent option is provided" do
|
309
|
+
|
310
|
+
let!(:association) do
|
311
|
+
klass.has_many :posts, dependent: :destroy
|
312
|
+
end
|
313
|
+
|
314
|
+
after do
|
315
|
+
klass.relations.delete(association.name.to_s)
|
316
|
+
end
|
317
|
+
|
318
|
+
it "adds the relation to the dependents" do
|
319
|
+
expect(klass.dependents).to include(klass.relations["posts"])
|
320
|
+
end
|
321
|
+
end
|
322
|
+
|
323
|
+
context "when no dependent option is provided" do
|
324
|
+
|
325
|
+
let!(:association) do
|
326
|
+
klass.has_many :posts
|
327
|
+
end
|
328
|
+
|
329
|
+
after do
|
330
|
+
klass.relations.delete(association.name.to_s)
|
331
|
+
end
|
332
|
+
|
333
|
+
it "does not add a relation to the dependents" do
|
334
|
+
expect(klass.dependents).to_not include(association)
|
335
|
+
end
|
336
|
+
end
|
337
|
+
|
338
|
+
context 'when the class is defined more than once' do
|
339
|
+
|
340
|
+
let!(:association) do
|
341
|
+
klass.has_many :posts, dependent: :destroy
|
342
|
+
klass.has_many :posts, dependent: :destroy
|
343
|
+
end
|
344
|
+
|
345
|
+
it 'only creates the dependency once' do
|
346
|
+
expect(klass.dependents.size).to eq(1)
|
347
|
+
end
|
348
|
+
end
|
349
|
+
end
|
350
|
+
|
351
|
+
[:delete, :destroy].each do |method|
|
352
|
+
|
353
|
+
describe "##{method}" do
|
354
|
+
|
355
|
+
context "when cascading removals" do
|
356
|
+
|
357
|
+
context "when strategy is delete" do
|
358
|
+
|
359
|
+
let(:person) do
|
360
|
+
Person.create
|
361
|
+
end
|
362
|
+
|
363
|
+
let!(:post) do
|
364
|
+
person.posts.create(title: "Testing")
|
365
|
+
end
|
366
|
+
|
367
|
+
before do
|
368
|
+
person.send(method)
|
369
|
+
end
|
370
|
+
|
371
|
+
it "deletes the associated documents" do
|
372
|
+
expect {
|
373
|
+
Post.find(post.id)
|
374
|
+
}.to raise_error(Mongoid::Errors::DocumentNotFound)
|
375
|
+
end
|
376
|
+
end
|
377
|
+
|
378
|
+
context "when strategy is destroy" do
|
379
|
+
|
380
|
+
let(:person) do
|
381
|
+
Person.create
|
382
|
+
end
|
383
|
+
|
384
|
+
let!(:game) do
|
385
|
+
person.create_game(name: "Pong")
|
386
|
+
end
|
387
|
+
|
388
|
+
before do
|
389
|
+
person.send(method)
|
390
|
+
end
|
391
|
+
|
392
|
+
it "destroys the associated documents" do
|
393
|
+
expect {
|
394
|
+
Game.find(game.id)
|
395
|
+
}.to raise_error(Mongoid::Errors::DocumentNotFound)
|
396
|
+
end
|
397
|
+
end
|
398
|
+
|
399
|
+
context "when strategy is nullify" do
|
400
|
+
|
401
|
+
context "when nullifying a references many" do
|
402
|
+
|
403
|
+
let(:movie) do
|
404
|
+
Movie.create(title: "Bladerunner")
|
405
|
+
end
|
406
|
+
|
407
|
+
let!(:rating) do
|
408
|
+
movie.ratings.create(value: 10)
|
409
|
+
end
|
410
|
+
|
411
|
+
let(:from_db) do
|
412
|
+
Rating.find(rating.id)
|
413
|
+
end
|
414
|
+
|
415
|
+
before do
|
416
|
+
movie.send(method)
|
417
|
+
end
|
418
|
+
|
419
|
+
it "removes the references to the removed document" do
|
420
|
+
expect(from_db.ratable_id).to be_nil
|
421
|
+
end
|
422
|
+
end
|
423
|
+
|
424
|
+
context "when nullifying a references one" do
|
425
|
+
|
426
|
+
context "when the relation exists" do
|
427
|
+
|
428
|
+
let(:book) do
|
429
|
+
Book.create(title: "Neuromancer")
|
430
|
+
end
|
431
|
+
|
432
|
+
let!(:rating) do
|
433
|
+
book.create_rating(value: 10)
|
434
|
+
end
|
435
|
+
|
436
|
+
let(:from_db) do
|
437
|
+
Rating.find(rating.id)
|
438
|
+
end
|
439
|
+
|
440
|
+
before do
|
441
|
+
book.send(method)
|
442
|
+
end
|
443
|
+
|
444
|
+
it "removes the references to the removed document" do
|
445
|
+
expect(from_db.ratable_id).to be_nil
|
446
|
+
end
|
447
|
+
end
|
448
|
+
|
449
|
+
context "when the relation is nil" do
|
450
|
+
|
451
|
+
let(:book) do
|
452
|
+
Book.create(title: "Neuromancer")
|
453
|
+
end
|
454
|
+
|
455
|
+
it "returns nil" do
|
456
|
+
expect(book.send(method)).to be true
|
457
|
+
end
|
458
|
+
end
|
459
|
+
end
|
460
|
+
|
461
|
+
context "when nullifying a many to many" do
|
462
|
+
|
463
|
+
let(:person) do
|
464
|
+
Person.create
|
465
|
+
end
|
466
|
+
|
467
|
+
let!(:preference) do
|
468
|
+
person.preferences.create(name: "Setting")
|
469
|
+
end
|
470
|
+
|
471
|
+
let(:from_db) do
|
472
|
+
Preference.find(preference.id)
|
473
|
+
end
|
474
|
+
|
475
|
+
before do
|
476
|
+
person.send(method)
|
477
|
+
end
|
478
|
+
|
479
|
+
it "removes the references from the removed document" do
|
480
|
+
expect(person.preference_ids).to_not include(preference.id)
|
481
|
+
end
|
482
|
+
|
483
|
+
it "removes the references to the removed document" do
|
484
|
+
expect(from_db.person_ids).to_not include(person.id)
|
485
|
+
end
|
486
|
+
end
|
487
|
+
end
|
488
|
+
|
489
|
+
context "when dependent is restrict_with_error" do
|
490
|
+
|
491
|
+
context "when restricting a references many" do
|
492
|
+
|
493
|
+
let!(:association) do
|
494
|
+
Person.has_many :drugs, dependent: :restrict_with_exception
|
495
|
+
end
|
496
|
+
|
497
|
+
after do
|
498
|
+
Person.dependents.delete(association)
|
499
|
+
Person.has_many :drugs, validate: false
|
500
|
+
end
|
501
|
+
|
502
|
+
context "when the relation is empty" do
|
503
|
+
|
504
|
+
let(:person) do
|
505
|
+
Person.new drugs: []
|
506
|
+
end
|
507
|
+
|
508
|
+
it "raises no error" do
|
509
|
+
expect { person.send(method) }.to_not raise_error
|
510
|
+
end
|
511
|
+
|
512
|
+
it "deletes the parent" do
|
513
|
+
person.send(method)
|
514
|
+
expect(person).to be_destroyed
|
515
|
+
end
|
516
|
+
end
|
517
|
+
|
518
|
+
context "when the relation is not empty" do
|
519
|
+
|
520
|
+
let(:person) do
|
521
|
+
Person.new drugs: [Drug.new]
|
522
|
+
end
|
523
|
+
|
524
|
+
it "raises DeleteRestriction error" do
|
525
|
+
expect { person.send(method) }.to raise_error(Mongoid::Errors::DeleteRestriction)
|
526
|
+
end
|
527
|
+
end
|
528
|
+
end
|
529
|
+
|
530
|
+
context "when restricting a references one" do
|
531
|
+
|
532
|
+
let!(:association) do
|
533
|
+
Person.has_one :account, dependent: :restrict_with_exception
|
534
|
+
end
|
535
|
+
|
536
|
+
after do
|
537
|
+
Person.dependents.delete(association)
|
538
|
+
Person.has_one :account, validate: false
|
539
|
+
end
|
540
|
+
|
541
|
+
context "when the relation is empty" do
|
542
|
+
|
543
|
+
let(:person) do
|
544
|
+
Person.new account: nil
|
545
|
+
end
|
546
|
+
|
547
|
+
it "raises no error" do
|
548
|
+
expect { person.send(method) }.to_not raise_error
|
549
|
+
end
|
550
|
+
|
551
|
+
it "deletes the parent" do
|
552
|
+
person.send(method)
|
553
|
+
expect(person).to be_destroyed
|
554
|
+
end
|
555
|
+
end
|
556
|
+
|
557
|
+
context "when the relation is not empty" do
|
558
|
+
|
559
|
+
let(:person) do
|
560
|
+
Person.new account: Account.new(name: 'test')
|
561
|
+
end
|
562
|
+
|
563
|
+
it "raises DeleteRestriction error" do
|
564
|
+
expect { person.send(method) }.to raise_error(Mongoid::Errors::DeleteRestriction)
|
565
|
+
end
|
566
|
+
end
|
567
|
+
end
|
568
|
+
|
569
|
+
context "when restricting a many to many" do
|
570
|
+
|
571
|
+
let!(:association) do
|
572
|
+
Person.has_and_belongs_to_many :houses, dependent: :restrict_with_exception
|
573
|
+
end
|
574
|
+
|
575
|
+
after do
|
576
|
+
Person.dependents.delete(association)
|
577
|
+
Person.has_and_belongs_to_many :houses, validate: false
|
578
|
+
end
|
579
|
+
|
580
|
+
context "when the relation is empty" do
|
581
|
+
|
582
|
+
let(:person) do
|
583
|
+
Person.new houses: []
|
584
|
+
end
|
585
|
+
|
586
|
+
it "raises no error" do
|
587
|
+
expect { person.send(method) }.to_not raise_error
|
588
|
+
end
|
589
|
+
|
590
|
+
it "deletes the parent" do
|
591
|
+
person.send(method)
|
592
|
+
expect(person).to be_destroyed
|
593
|
+
end
|
594
|
+
end
|
595
|
+
|
596
|
+
context "when the relation is not empty" do
|
597
|
+
|
598
|
+
let(:person) do
|
599
|
+
Person.new houses: [House.new]
|
600
|
+
end
|
601
|
+
|
602
|
+
it "raises DeleteRestriction error" do
|
603
|
+
expect { person.send(method) }.to raise_error(Mongoid::Errors::DeleteRestriction)
|
604
|
+
end
|
605
|
+
end
|
606
|
+
end
|
607
|
+
end
|
608
|
+
end
|
609
|
+
end
|
610
|
+
end
|
611
|
+
|
612
|
+
context 'when the strategy is :delete_all' do
|
613
|
+
|
614
|
+
let(:person) do
|
615
|
+
Person.create
|
616
|
+
end
|
617
|
+
|
618
|
+
context "when cascading a has one" do
|
619
|
+
|
620
|
+
context "when the relation exists" do
|
621
|
+
|
622
|
+
let!(:home) do
|
623
|
+
person.create_home
|
624
|
+
end
|
625
|
+
|
626
|
+
before do
|
627
|
+
person.delete
|
628
|
+
end
|
629
|
+
|
630
|
+
it "deletes the relation" do
|
631
|
+
expect(home).to be_destroyed
|
632
|
+
end
|
633
|
+
|
634
|
+
it "persists the deletion" do
|
635
|
+
expect {
|
636
|
+
home.reload
|
637
|
+
}.to raise_error(Mongoid::Errors::DocumentNotFound)
|
638
|
+
end
|
639
|
+
end
|
640
|
+
|
641
|
+
context "when the relation does not exist" do
|
642
|
+
|
643
|
+
before do
|
644
|
+
person.delete
|
645
|
+
end
|
646
|
+
|
647
|
+
it "deletes the base document" do
|
648
|
+
expect(person).to be_destroyed
|
649
|
+
end
|
650
|
+
end
|
651
|
+
end
|
652
|
+
|
653
|
+
context "when cascading a has many" do
|
654
|
+
|
655
|
+
context "when the relation has documents" do
|
656
|
+
|
657
|
+
let!(:post_one) do
|
658
|
+
person.posts.create(title: "one")
|
659
|
+
end
|
660
|
+
|
661
|
+
let!(:post_two) do
|
662
|
+
person.posts.create(title: "two")
|
663
|
+
end
|
664
|
+
|
665
|
+
context "when the documents are in memory" do
|
666
|
+
|
667
|
+
before do
|
668
|
+
expect(post_one).to receive(:delete).never
|
669
|
+
expect(post_two).to receive(:delete).never
|
670
|
+
person.delete
|
671
|
+
end
|
672
|
+
|
673
|
+
it "deletes the first document" do
|
674
|
+
expect(post_one).to be_destroyed
|
675
|
+
end
|
676
|
+
|
677
|
+
it "deletes the second document" do
|
678
|
+
expect(post_two).to be_destroyed
|
679
|
+
end
|
680
|
+
|
681
|
+
it "unbinds the first document" do
|
682
|
+
expect(post_one.person).to be_nil
|
683
|
+
end
|
684
|
+
|
685
|
+
it "unbinds the second document" do
|
686
|
+
expect(post_two.person).to be_nil
|
687
|
+
end
|
688
|
+
|
689
|
+
it "removes the documents from the relation" do
|
690
|
+
expect(person.posts).to be_empty
|
691
|
+
end
|
692
|
+
|
693
|
+
it "persists the first deletion" do
|
694
|
+
expect {
|
695
|
+
post_one.reload
|
696
|
+
}.to raise_error(Mongoid::Errors::DocumentNotFound)
|
697
|
+
end
|
698
|
+
|
699
|
+
it "persists the second deletion" do
|
700
|
+
expect {
|
701
|
+
post_two.reload
|
702
|
+
}.to raise_error(Mongoid::Errors::DocumentNotFound)
|
703
|
+
end
|
704
|
+
end
|
705
|
+
end
|
706
|
+
end
|
707
|
+
end
|
708
|
+
|
709
|
+
context 'when the strategy is :destroy' do
|
710
|
+
|
711
|
+
let!(:association) do
|
712
|
+
Person.has_many :destroyable_posts, class_name: "Post", dependent: :destroy
|
713
|
+
end
|
714
|
+
|
715
|
+
after do
|
716
|
+
Person.dependents.delete(association)
|
717
|
+
end
|
718
|
+
|
719
|
+
let(:person) do
|
720
|
+
Person.new
|
721
|
+
end
|
722
|
+
|
723
|
+
let(:post) do
|
724
|
+
Post.new
|
725
|
+
end
|
726
|
+
|
727
|
+
context "when the documents exist" do
|
728
|
+
|
729
|
+
before do
|
730
|
+
expect(post).to receive(:destroy)
|
731
|
+
person.destroyable_posts << post
|
732
|
+
end
|
733
|
+
|
734
|
+
it "destroys all documents in the relation" do
|
735
|
+
person.delete
|
736
|
+
end
|
737
|
+
end
|
738
|
+
|
739
|
+
context "when no documents exist" do
|
740
|
+
|
741
|
+
before do
|
742
|
+
expect(post).to receive(:destroy).never
|
743
|
+
end
|
744
|
+
|
745
|
+
it "it does not destroy the relation" do
|
746
|
+
person.delete
|
747
|
+
end
|
748
|
+
end
|
749
|
+
end
|
750
|
+
|
751
|
+
context 'when the strategy is :nullify' do
|
752
|
+
|
753
|
+
let!(:association) do
|
754
|
+
Person.has_many :nullifyable_posts, class_name: "Post", dependent: :nullify
|
755
|
+
end
|
756
|
+
|
757
|
+
after do
|
758
|
+
Person.dependents.delete(association)
|
759
|
+
end
|
760
|
+
|
761
|
+
let(:person) do
|
762
|
+
Person.new
|
763
|
+
end
|
764
|
+
|
765
|
+
let(:posts_relation) do
|
766
|
+
person.posts
|
767
|
+
end
|
768
|
+
|
769
|
+
before do
|
770
|
+
allow(person).to receive(:nullifyable_posts).and_return(posts_relation)
|
771
|
+
expect(posts_relation).to receive(:nullify)
|
772
|
+
end
|
773
|
+
|
774
|
+
it "nullifies the relation" do
|
775
|
+
person.delete
|
776
|
+
end
|
777
|
+
end
|
778
|
+
|
779
|
+
context 'when the strategy is :restrict_with_exception' do
|
780
|
+
|
781
|
+
let(:person) do
|
782
|
+
Person.new
|
783
|
+
end
|
784
|
+
|
785
|
+
let(:post) do
|
786
|
+
Post.new
|
787
|
+
end
|
788
|
+
|
789
|
+
let!(:association) do
|
790
|
+
Person.has_many :restrictable_posts, class_name: "Post", dependent: :restrict_with_exception
|
791
|
+
end
|
792
|
+
|
793
|
+
after do
|
794
|
+
Person.dependents.delete(association)
|
795
|
+
end
|
796
|
+
|
797
|
+
context 'when there are related objects' do
|
798
|
+
|
799
|
+
before do
|
800
|
+
person.restrictable_posts << post
|
801
|
+
expect(post).to receive(:delete).never
|
802
|
+
expect(post).to receive(:destroy).never
|
803
|
+
end
|
804
|
+
|
805
|
+
it 'raises an exception and leaves the related one intact' do
|
806
|
+
expect { person.delete }.to raise_exception(Mongoid::Errors::DeleteRestriction)
|
807
|
+
end
|
808
|
+
end
|
809
|
+
|
810
|
+
context 'when there are no related objects' do
|
811
|
+
|
812
|
+
before do
|
813
|
+
expect(post).to receive(:delete).never
|
814
|
+
expect(post).to receive(:destroy).never
|
815
|
+
end
|
816
|
+
|
817
|
+
it 'deletes the object and leaves the other one intact' do
|
818
|
+
expect(person.delete).to be(true)
|
819
|
+
end
|
820
|
+
end
|
821
|
+
end
|
822
|
+
|
823
|
+
context 'when the strategy is :restrict_with_error' do
|
824
|
+
|
825
|
+
let(:person) do
|
826
|
+
Person.new
|
827
|
+
end
|
828
|
+
|
829
|
+
let(:post) do
|
830
|
+
Post.new
|
831
|
+
end
|
832
|
+
|
833
|
+
let!(:association) do
|
834
|
+
Person.has_many :restrictable_posts, class_name: "Post", dependent: :restrict_with_error
|
835
|
+
end
|
836
|
+
|
837
|
+
after do
|
838
|
+
Person.dependents.delete(association)
|
839
|
+
end
|
840
|
+
|
841
|
+
context 'when there are related objects' do
|
842
|
+
|
843
|
+
before do
|
844
|
+
person.restrictable_posts << post
|
845
|
+
end
|
846
|
+
|
847
|
+
it 'adds an error to the parent object' do
|
848
|
+
expect(person.delete).to be(false)
|
849
|
+
expect(person.errors[:restrictable_posts].first).to be(
|
850
|
+
Mongoid::Association::Depending::RESTRICT_ERROR_MSG)
|
851
|
+
end
|
852
|
+
end
|
853
|
+
|
854
|
+
context 'when there are no related objects' do
|
855
|
+
|
856
|
+
before do
|
857
|
+
expect(post).to receive(:delete).never
|
858
|
+
expect(post).to receive(:destroy).never
|
859
|
+
end
|
860
|
+
|
861
|
+
it 'deletes the object and leaves the other one intact' do
|
862
|
+
expect(person.delete).to be(true)
|
863
|
+
end
|
864
|
+
end
|
865
|
+
end
|
866
|
+
end
|