mongoid 6.4.2 → 7.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/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,509 @@
|
|
|
1
|
+
require 'mongoid/association/constrainable'
|
|
2
|
+
require 'mongoid/association/options'
|
|
3
|
+
|
|
4
|
+
module Mongoid
|
|
5
|
+
module Association
|
|
6
|
+
|
|
7
|
+
# This module provides behaviors shared between Association types.
|
|
8
|
+
#
|
|
9
|
+
# @since 7.0
|
|
10
|
+
module Relatable
|
|
11
|
+
include Constrainable
|
|
12
|
+
include Options
|
|
13
|
+
|
|
14
|
+
# The options shared between all association types.
|
|
15
|
+
#
|
|
16
|
+
# @return [ Array<Symbol> ] The shared options.
|
|
17
|
+
#
|
|
18
|
+
# @since 7.0
|
|
19
|
+
SHARED_OPTIONS = [
|
|
20
|
+
:class_name,
|
|
21
|
+
:inverse_of,
|
|
22
|
+
:validate,
|
|
23
|
+
:extend
|
|
24
|
+
].freeze
|
|
25
|
+
|
|
26
|
+
# The primary key default.
|
|
27
|
+
#
|
|
28
|
+
# @return [ String ] The primary key field default.
|
|
29
|
+
#
|
|
30
|
+
# @since 7.0
|
|
31
|
+
PRIMARY_KEY_DEFAULT = '_id'.freeze
|
|
32
|
+
|
|
33
|
+
# The name of the association.
|
|
34
|
+
#
|
|
35
|
+
# @return [ Symbol ] The name of the relation.
|
|
36
|
+
#
|
|
37
|
+
# @since 7.0
|
|
38
|
+
attr_reader :name
|
|
39
|
+
|
|
40
|
+
# The options on this association.
|
|
41
|
+
#
|
|
42
|
+
# @return [ Hash ] The options.
|
|
43
|
+
#
|
|
44
|
+
# @since 7.0
|
|
45
|
+
attr_reader :options
|
|
46
|
+
|
|
47
|
+
# Initialize the Association.
|
|
48
|
+
#
|
|
49
|
+
# @param [ Class ] _class The class of the model who owns this relation.
|
|
50
|
+
# @param [ Symbol ] name The name of the association.
|
|
51
|
+
# @param [ Hash ] opts The relation options.
|
|
52
|
+
# @param [ Block ] block The optional block.
|
|
53
|
+
#
|
|
54
|
+
# @since 7.0
|
|
55
|
+
def initialize(_class, name, opts = {}, &block)
|
|
56
|
+
@owner_class = _class
|
|
57
|
+
@name = name
|
|
58
|
+
@options = opts
|
|
59
|
+
@extension = nil
|
|
60
|
+
|
|
61
|
+
@module_path = _class.name ? _class.name.split('::')[0..-2].join('::') : ''
|
|
62
|
+
@module_path << '::' unless @module_path.empty?
|
|
63
|
+
|
|
64
|
+
create_extension!(&block)
|
|
65
|
+
validate!
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Compare this association to another.
|
|
69
|
+
#
|
|
70
|
+
# @return [ Object ] The object to compare to this association.
|
|
71
|
+
#
|
|
72
|
+
# @since 7.0
|
|
73
|
+
def ==(other)
|
|
74
|
+
relation_class_name == other.relation_class_name &&
|
|
75
|
+
inverse_class_name == other.inverse_class_name &&
|
|
76
|
+
name == other.name &&
|
|
77
|
+
options == other.options
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Get the callbacks for a given type.
|
|
81
|
+
#
|
|
82
|
+
# @param [ Symbol ] callback_type The type of callback type.
|
|
83
|
+
#
|
|
84
|
+
# @return [ Array<Proc, Symbol> ] A list of the callbacks, either method
|
|
85
|
+
# names or Procs.
|
|
86
|
+
#
|
|
87
|
+
# @since 7.0
|
|
88
|
+
def get_callbacks(callback_type)
|
|
89
|
+
Array(options[callback_type])
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# Get the type setter.
|
|
93
|
+
# @note Only relevant for polymorphic relations that take the :as option.
|
|
94
|
+
#
|
|
95
|
+
# @return [ String ] The type setter method.
|
|
96
|
+
#
|
|
97
|
+
# @since 7.0
|
|
98
|
+
def type_setter
|
|
99
|
+
@type_setter ||= type.__setter__
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# Whether trying to bind an object using this association should raise
|
|
103
|
+
# an error.
|
|
104
|
+
#
|
|
105
|
+
# @param [ Document ] doc The document to be bound.
|
|
106
|
+
#
|
|
107
|
+
# @return [ true, false ] Whether the document can be bound.
|
|
108
|
+
#
|
|
109
|
+
# @since 7.0
|
|
110
|
+
def bindable?(doc); false; end
|
|
111
|
+
|
|
112
|
+
# Get the inverse names.
|
|
113
|
+
#
|
|
114
|
+
# @param [ Object ] other The other model class or model object to use when
|
|
115
|
+
# determining inverses.
|
|
116
|
+
#
|
|
117
|
+
# @return [ Array<Symbol> ] The list of inverse names.
|
|
118
|
+
#
|
|
119
|
+
# @since 7.0
|
|
120
|
+
def inverses(other = nil)
|
|
121
|
+
return [ inverse_of ] if inverse_of
|
|
122
|
+
if polymorphic?
|
|
123
|
+
polymorphic_inverses(other)
|
|
124
|
+
else
|
|
125
|
+
determine_inverses(other)
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# Get the inverse's association metadata.
|
|
130
|
+
#
|
|
131
|
+
# @param [ Object ] other The other model class or model object to use when
|
|
132
|
+
# determining inverses.
|
|
133
|
+
#
|
|
134
|
+
# @return [ Association ] The inverse's association metadata.
|
|
135
|
+
#
|
|
136
|
+
# @since 7.0
|
|
137
|
+
def inverse_association(other = nil)
|
|
138
|
+
(other || relation_class).relations[inverse(other)]
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# Get the inverse type.
|
|
142
|
+
#
|
|
143
|
+
# @return [ nil ] Default is nil for an association.
|
|
144
|
+
#
|
|
145
|
+
# @since 7.0
|
|
146
|
+
def inverse_type; end
|
|
147
|
+
|
|
148
|
+
# The class name of the relation object(s).
|
|
149
|
+
#
|
|
150
|
+
# The class name may be fully qualified or may be specified relative
|
|
151
|
+
# to the class on which the association is defined (this class is
|
|
152
|
+
# accessible as inverse_class). If :class_name option is given in the
|
|
153
|
+
# association, the exact value of that option is returned here for
|
|
154
|
+
# backwards compatibility reasons. If :class_name option is not given,
|
|
155
|
+
# the name of the class computed by Mongoid to be the association target
|
|
156
|
+
# is returned, and it will be fully qualified.
|
|
157
|
+
#
|
|
158
|
+
# The class name returned by this method may not correspond to a defined
|
|
159
|
+
# class. The return value of the method is the class name that Mongoid
|
|
160
|
+
# would reference, relative to the host document class, when it needs to
|
|
161
|
+
# perform operations on the association target.
|
|
162
|
+
#
|
|
163
|
+
# @note The return value of this method should not be used to determine
|
|
164
|
+
# whether two associations have the same target class, because the
|
|
165
|
+
# return value is not always a fully qualified class name. To compare
|
|
166
|
+
# classes, retrieve the class instance of the association target using
|
|
167
|
+
# the +relation_class+ method.
|
|
168
|
+
#
|
|
169
|
+
# @return [ String ] The association objects' class name.
|
|
170
|
+
#
|
|
171
|
+
# @since 7.0
|
|
172
|
+
def relation_class_name
|
|
173
|
+
@class_name ||= @options[:class_name] || begin
|
|
174
|
+
cls_name = ActiveSupport::Inflector.classify(name)
|
|
175
|
+
begin
|
|
176
|
+
cls_name = resolve_name(inverse_class, cls_name).name
|
|
177
|
+
rescue NameError
|
|
178
|
+
# ignore
|
|
179
|
+
end
|
|
180
|
+
cls_name
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
alias :class_name :relation_class_name
|
|
184
|
+
|
|
185
|
+
# The class of the relation object(s).
|
|
186
|
+
#
|
|
187
|
+
# This method returns the class instance corresponding to
|
|
188
|
+
# +relation_class_name+, resolved relative to the host document class.
|
|
189
|
+
#
|
|
190
|
+
# If the class does not exist, this method raises NameError. This can
|
|
191
|
+
# happen because the target class has not yet been defined. Note that
|
|
192
|
+
# polymorphic associations generally do not have a well defined target
|
|
193
|
+
# class because the target class can change from one object to another,
|
|
194
|
+
# and calling this method on a polymorphic association will generally
|
|
195
|
+
# fail with a NameError or produce misleading results (if a class does
|
|
196
|
+
# happen to be defined with the same name as the association name).
|
|
197
|
+
#
|
|
198
|
+
# @return [ String ] The association objects' class.
|
|
199
|
+
#
|
|
200
|
+
# @since 7.0
|
|
201
|
+
def relation_class
|
|
202
|
+
@klass ||= begin
|
|
203
|
+
cls_name = @options[:class_name] || ActiveSupport::Inflector.classify(name)
|
|
204
|
+
resolve_name(inverse_class, cls_name)
|
|
205
|
+
end
|
|
206
|
+
end
|
|
207
|
+
alias :klass :relation_class
|
|
208
|
+
|
|
209
|
+
# The class name of the object owning this relation.
|
|
210
|
+
#
|
|
211
|
+
# @return [ String ] The owning objects' class name.
|
|
212
|
+
#
|
|
213
|
+
# @since 7.0
|
|
214
|
+
def inverse_class_name
|
|
215
|
+
@inverse_class_name ||= @owner_class.name
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
# The class of the object owning this relation.
|
|
219
|
+
#
|
|
220
|
+
# @return [ String ] The owning objects' class.
|
|
221
|
+
#
|
|
222
|
+
# @since 7.0
|
|
223
|
+
def inverse_class
|
|
224
|
+
@owner_class
|
|
225
|
+
end
|
|
226
|
+
alias :inverse_klass :inverse_class
|
|
227
|
+
|
|
228
|
+
# The foreign key field if this relation stores a foreign key.
|
|
229
|
+
# Otherwise, the primary key.
|
|
230
|
+
#
|
|
231
|
+
# @return [ Symbol, String ] The primary key.
|
|
232
|
+
#
|
|
233
|
+
# @since 7.0
|
|
234
|
+
def key
|
|
235
|
+
stores_foreign_key? ? foreign_key : primary_key
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
# The name of the setter on this object for assigning an associated object.
|
|
239
|
+
#
|
|
240
|
+
# @return [ String ] The setter name.
|
|
241
|
+
#
|
|
242
|
+
# @since 7.0
|
|
243
|
+
def setter
|
|
244
|
+
@setter ||= "#{name}="
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
# The name of the inverse setter method.
|
|
248
|
+
#
|
|
249
|
+
# @return [ String ] The name of the inverse setter.
|
|
250
|
+
#
|
|
251
|
+
# @since 7.0
|
|
252
|
+
def inverse_setter(other = nil)
|
|
253
|
+
@inverse_setter ||= "#{inverses(other).first}=" unless inverses(other).blank?
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
# The name of the foreign key setter method.
|
|
257
|
+
#
|
|
258
|
+
# @return [ String ] The name of the foreign key setter.
|
|
259
|
+
#
|
|
260
|
+
# @since 7.0
|
|
261
|
+
def foreign_key_setter
|
|
262
|
+
# note: You can't check if this association stores foreign key
|
|
263
|
+
# See HasOne and HasMany binding, they referenced foreign_key_setter
|
|
264
|
+
@foreign_key_setter ||= "#{foreign_key}=" if foreign_key
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
# The atomic path for this relation.
|
|
268
|
+
#
|
|
269
|
+
# @return [ Mongoid::Atomic::Paths::Root ] The atomic path object.
|
|
270
|
+
#
|
|
271
|
+
# @since 7.0
|
|
272
|
+
def path(document)
|
|
273
|
+
relation.path(document)
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
# Gets the setter for the field that sets the type of document on a
|
|
277
|
+
# polymorphic relation.
|
|
278
|
+
#
|
|
279
|
+
# @example Get the inverse type setter.
|
|
280
|
+
# association.inverse_type_setter
|
|
281
|
+
#
|
|
282
|
+
# @return [ String ] The name of the setter.
|
|
283
|
+
#
|
|
284
|
+
# @since 7.0
|
|
285
|
+
def inverse_type_setter
|
|
286
|
+
@inverse_type_setter ||= inverse_type.__setter__
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
# Get the name of the method to check if the foreign key has changed.
|
|
290
|
+
#
|
|
291
|
+
# @example Get the foreign key check method.
|
|
292
|
+
# association.foreign_key_check
|
|
293
|
+
#
|
|
294
|
+
# @return [ String ] The foreign key check.
|
|
295
|
+
#
|
|
296
|
+
# @since 7.0
|
|
297
|
+
def foreign_key_check
|
|
298
|
+
@foreign_key_check ||= "#{foreign_key}_changed?" if (stores_foreign_key? && foreign_key)
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
# Create a relation proxy object using the owner and target.
|
|
302
|
+
#
|
|
303
|
+
# @param [ Document ] owner The document this relation hangs off of.
|
|
304
|
+
# @param [ Document, Array<Document> ] target The target (parent) of the
|
|
305
|
+
# relation.
|
|
306
|
+
#
|
|
307
|
+
# @return [ Proxy ]
|
|
308
|
+
#
|
|
309
|
+
# @since 7.0
|
|
310
|
+
def create_relation(owner, target)
|
|
311
|
+
relation.new(owner, target, self)
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
# Whether the dependent method is destructive.
|
|
315
|
+
#
|
|
316
|
+
# @return [ true, false ] If the dependent method is destructive.
|
|
317
|
+
#
|
|
318
|
+
# @since 7.0
|
|
319
|
+
def destructive?
|
|
320
|
+
@destructive ||= !!(dependent && (dependent == :delete_all || dependent == :destroy))
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
# Get the counter cache column name.
|
|
324
|
+
#
|
|
325
|
+
# @return [ String ] The counter cache column name.
|
|
326
|
+
#
|
|
327
|
+
# @since 7.0
|
|
328
|
+
def counter_cache_column_name
|
|
329
|
+
@counter_cache_column_name ||= (@options[:counter_cache].is_a?(String) ||
|
|
330
|
+
@options[:counter_cache].is_a?(Symbol)) ?
|
|
331
|
+
@options[:counter_cache] : "#{inverse || inverse_class_name.demodulize.underscore.pluralize}_count"
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
# Get the extension.
|
|
335
|
+
#
|
|
336
|
+
# @return [ Module ] The extension module, if one has been defined.
|
|
337
|
+
#
|
|
338
|
+
# @since 7.0
|
|
339
|
+
def extension
|
|
340
|
+
@extension ||= @options[:extend]
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
# Get the inverse name.
|
|
344
|
+
#
|
|
345
|
+
# @return [ Symbol ] The inverse name.
|
|
346
|
+
#
|
|
347
|
+
# @since 7.0
|
|
348
|
+
def inverse(other = nil)
|
|
349
|
+
candidates = inverses(other)
|
|
350
|
+
candidates.detect { |c| c } if candidates
|
|
351
|
+
end
|
|
352
|
+
|
|
353
|
+
# Whether the associated object(s) should be validated.
|
|
354
|
+
#
|
|
355
|
+
# @return [ true, false ] If the associated object(s)
|
|
356
|
+
# should be validated.
|
|
357
|
+
#
|
|
358
|
+
# @since 7.0
|
|
359
|
+
def validate?
|
|
360
|
+
@validate ||= if @options[:validate].nil?
|
|
361
|
+
validation_default
|
|
362
|
+
else
|
|
363
|
+
!!@options[:validate]
|
|
364
|
+
end
|
|
365
|
+
end
|
|
366
|
+
|
|
367
|
+
private
|
|
368
|
+
|
|
369
|
+
# Gets the model classes with inverse associations of this model. This is used to determine
|
|
370
|
+
# the classes on the other end of polymorphic relations with models.
|
|
371
|
+
def inverse_association_classes
|
|
372
|
+
Mongoid::Config.models.map { |m| inverse_association(m) }.compact.map(&:inverse_class)
|
|
373
|
+
end
|
|
374
|
+
|
|
375
|
+
def setup_index!
|
|
376
|
+
@owner_class.index(index_spec, background: true) if indexed?
|
|
377
|
+
end
|
|
378
|
+
|
|
379
|
+
def define_touchable!
|
|
380
|
+
if touchable?
|
|
381
|
+
Touchable.define_touchable!(self)
|
|
382
|
+
end
|
|
383
|
+
end
|
|
384
|
+
|
|
385
|
+
def define_autosaver!
|
|
386
|
+
if autosave?
|
|
387
|
+
Association::Referenced::AutoSave.define_autosave!(self)
|
|
388
|
+
end
|
|
389
|
+
end
|
|
390
|
+
|
|
391
|
+
def define_builder!
|
|
392
|
+
Association::Builders.define_builder!(self)
|
|
393
|
+
end
|
|
394
|
+
|
|
395
|
+
def define_creator!
|
|
396
|
+
Association::Builders.define_creator!(self)
|
|
397
|
+
end
|
|
398
|
+
|
|
399
|
+
def define_getter!
|
|
400
|
+
Association::Accessors.define_getter!(self)
|
|
401
|
+
end
|
|
402
|
+
|
|
403
|
+
def define_setter!
|
|
404
|
+
Association::Accessors.define_setter!(self)
|
|
405
|
+
end
|
|
406
|
+
|
|
407
|
+
def define_existence_check!
|
|
408
|
+
Association::Accessors.define_existence_check!(self)
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
def define_ids_getter!
|
|
412
|
+
Association::Accessors.define_ids_getter!(self)
|
|
413
|
+
end
|
|
414
|
+
|
|
415
|
+
def define_ids_setter!
|
|
416
|
+
Association::Accessors.define_ids_setter!(self)
|
|
417
|
+
end
|
|
418
|
+
|
|
419
|
+
def define_counter_cache_callbacks!
|
|
420
|
+
if counter_cached?
|
|
421
|
+
Association::Referenced::CounterCache.define_callbacks!(self)
|
|
422
|
+
end
|
|
423
|
+
end
|
|
424
|
+
|
|
425
|
+
def define_dependency!
|
|
426
|
+
if dependent
|
|
427
|
+
Association::Depending.define_dependency!(self)
|
|
428
|
+
end
|
|
429
|
+
end
|
|
430
|
+
|
|
431
|
+
def validate!
|
|
432
|
+
@options.keys.each do |opt|
|
|
433
|
+
unless self.class::VALID_OPTIONS.include?(opt)
|
|
434
|
+
raise Errors::InvalidRelationOption.new(@owner_class, name, opt, self.class::VALID_OPTIONS)
|
|
435
|
+
end
|
|
436
|
+
end
|
|
437
|
+
|
|
438
|
+
[name, "#{name}?".to_sym, "#{name}=".to_sym].each do |n|
|
|
439
|
+
if Mongoid.destructive_fields.include?(n)
|
|
440
|
+
raise Errors::InvalidRelation.new(@owner_class, n)
|
|
441
|
+
end
|
|
442
|
+
end
|
|
443
|
+
end
|
|
444
|
+
|
|
445
|
+
def polymorph!
|
|
446
|
+
if polymorphic?
|
|
447
|
+
@owner_class.polymorphic = true
|
|
448
|
+
end
|
|
449
|
+
end
|
|
450
|
+
|
|
451
|
+
def create_extension!(&block)
|
|
452
|
+
if block
|
|
453
|
+
extension_module_name = "#{@owner_class.to_s.demodulize}#{name.to_s.camelize}RelationExtension"
|
|
454
|
+
silence_warnings do
|
|
455
|
+
@owner_class.const_set(extension_module_name, Module.new(&block))
|
|
456
|
+
end
|
|
457
|
+
@extension = "#{@owner_class}::#{extension_module_name}".constantize
|
|
458
|
+
end
|
|
459
|
+
end
|
|
460
|
+
|
|
461
|
+
def default_inverse
|
|
462
|
+
@default_inverse ||= klass.relations[inverse_klass.name.underscore]
|
|
463
|
+
end
|
|
464
|
+
|
|
465
|
+
# Returns an array of classes/modules forming the namespace hierarchy
|
|
466
|
+
# where symbols referenced in the provided class/module would be looked
|
|
467
|
+
# up by Ruby. For example, if mod is Foo::Bar, this method would return
|
|
468
|
+
# [Foo::Bar, Foo, Object].
|
|
469
|
+
def namespace_hierarchy(mod)
|
|
470
|
+
parent = Object
|
|
471
|
+
hier = [parent]
|
|
472
|
+
mod.name.split('::').each do |part|
|
|
473
|
+
parent = parent.const_get(part)
|
|
474
|
+
hier << parent
|
|
475
|
+
end
|
|
476
|
+
hier.reverse
|
|
477
|
+
end
|
|
478
|
+
|
|
479
|
+
# Resolves the given class/module name in the context of the specified
|
|
480
|
+
# module, as Ruby would when a constant is referenced in the source.
|
|
481
|
+
def resolve_name(mod, name)
|
|
482
|
+
cls = exc = nil
|
|
483
|
+
parts = name.to_s.split('::')
|
|
484
|
+
namespace_hierarchy(mod).each do |ns|
|
|
485
|
+
begin
|
|
486
|
+
parts.each do |part|
|
|
487
|
+
# Simple const_get sometimes pulls names out of weird scopes,
|
|
488
|
+
# perhaps confusing the receiver (ns in this case) with the
|
|
489
|
+
# local scope. Walk the class hierarchy ourselves one node
|
|
490
|
+
# at a time by specifying false as the second argument.
|
|
491
|
+
ns = ns.const_get(part, false)
|
|
492
|
+
end
|
|
493
|
+
cls = ns
|
|
494
|
+
break
|
|
495
|
+
rescue NameError => e
|
|
496
|
+
if exc.nil?
|
|
497
|
+
exc = e
|
|
498
|
+
end
|
|
499
|
+
end
|
|
500
|
+
end
|
|
501
|
+
if cls.nil?
|
|
502
|
+
# Raise the first exception, this is from the most specific namespace
|
|
503
|
+
raise exc
|
|
504
|
+
end
|
|
505
|
+
cls
|
|
506
|
+
end
|
|
507
|
+
end
|
|
508
|
+
end
|
|
509
|
+
end
|
|
@@ -1,53 +1,54 @@
|
|
|
1
|
-
|
|
2
|
-
require
|
|
3
|
-
require
|
|
4
|
-
require
|
|
5
|
-
require
|
|
6
|
-
require "mongoid/relations/conversions"
|
|
7
|
-
require "mongoid/relations/counter_cache"
|
|
8
|
-
require "mongoid/relations/cyclic"
|
|
9
|
-
require "mongoid/relations/proxy"
|
|
10
|
-
require "mongoid/relations/bindings"
|
|
11
|
-
require "mongoid/relations/builders"
|
|
12
|
-
require "mongoid/relations/many"
|
|
13
|
-
require "mongoid/relations/one"
|
|
14
|
-
require "mongoid/relations/options"
|
|
15
|
-
require "mongoid/relations/polymorphic"
|
|
16
|
-
require "mongoid/relations/targets/enumerable"
|
|
17
|
-
require "mongoid/relations/embedded/in"
|
|
18
|
-
require "mongoid/relations/embedded/many"
|
|
19
|
-
require "mongoid/relations/embedded/one"
|
|
20
|
-
require "mongoid/relations/referenced/in"
|
|
21
|
-
require "mongoid/relations/referenced/many"
|
|
22
|
-
require "mongoid/relations/referenced/many_to_many"
|
|
23
|
-
require "mongoid/relations/referenced/one"
|
|
24
|
-
require "mongoid/relations/reflections"
|
|
25
|
-
require "mongoid/relations/synchronization"
|
|
26
|
-
require "mongoid/relations/touchable"
|
|
27
|
-
require "mongoid/relations/metadata"
|
|
28
|
-
require "mongoid/relations/macros"
|
|
1
|
+
require 'mongoid/association/accessors'
|
|
2
|
+
require 'mongoid/association/builders'
|
|
3
|
+
require 'mongoid/association/bindable'
|
|
4
|
+
require 'mongoid/association/depending'
|
|
5
|
+
require 'mongoid/association/proxy'
|
|
29
6
|
|
|
30
|
-
|
|
7
|
+
require 'mongoid/association/many'
|
|
8
|
+
require 'mongoid/association/one'
|
|
9
|
+
require 'mongoid/association/relatable'
|
|
10
|
+
require 'mongoid/association/nested'
|
|
11
|
+
require 'mongoid/association/referenced'
|
|
12
|
+
require 'mongoid/association/embedded'
|
|
13
|
+
require 'mongoid/association/macros'
|
|
14
|
+
|
|
15
|
+
require 'mongoid/association/reflections'
|
|
16
|
+
require 'mongoid/association/eager_loadable'
|
|
31
17
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
# associations.
|
|
35
|
-
module Relations
|
|
18
|
+
module Mongoid
|
|
19
|
+
module Association
|
|
36
20
|
extend ActiveSupport::Concern
|
|
21
|
+
include Embedded::Cyclic
|
|
22
|
+
include Referenced::AutoSave
|
|
23
|
+
include Referenced::CounterCache
|
|
24
|
+
include Referenced::Syncable
|
|
37
25
|
include Accessors
|
|
38
|
-
include
|
|
39
|
-
include Cascading
|
|
40
|
-
include Cyclic
|
|
26
|
+
include Depending
|
|
41
27
|
include Builders
|
|
42
28
|
include Macros
|
|
43
|
-
include Polymorphic
|
|
44
29
|
include Reflections
|
|
45
|
-
include Synchronization
|
|
46
|
-
include Touchable
|
|
47
|
-
include CounterCache
|
|
48
30
|
|
|
49
|
-
|
|
50
|
-
|
|
31
|
+
# Map the macros to their corresponding Association classes.
|
|
32
|
+
#
|
|
33
|
+
# @return [ Hash ] The mapping from macros to their Association class.
|
|
34
|
+
#
|
|
35
|
+
# @since 7.0
|
|
36
|
+
MACRO_MAPPING = {
|
|
37
|
+
embeds_one: Association::Embedded::EmbedsOne,
|
|
38
|
+
embeds_many: Association::Embedded::EmbedsMany,
|
|
39
|
+
embedded_in: Association::Embedded::EmbeddedIn,
|
|
40
|
+
has_one: Association::Referenced::HasOne,
|
|
41
|
+
has_many: Association::Referenced::HasMany,
|
|
42
|
+
has_and_belongs_to_many: Association::Referenced::HasAndBelongsToMany,
|
|
43
|
+
belongs_to: Association::Referenced::BelongsTo,
|
|
44
|
+
}.freeze
|
|
45
|
+
|
|
46
|
+
attr_accessor :_association
|
|
47
|
+
|
|
48
|
+
included do
|
|
49
|
+
class_attribute :polymorphic
|
|
50
|
+
self.polymorphic = false
|
|
51
|
+
end
|
|
51
52
|
|
|
52
53
|
# Determine if the document itself is embedded in another document via the
|
|
53
54
|
# proper channels. (If it has a parent document.)
|
|
@@ -71,7 +72,7 @@ module Mongoid
|
|
|
71
72
|
#
|
|
72
73
|
# @since 2.0.0.rc.1
|
|
73
74
|
def embedded_many?
|
|
74
|
-
|
|
75
|
+
_association && _association.is_a?(Association::Embedded::EmbedsMany)
|
|
75
76
|
end
|
|
76
77
|
|
|
77
78
|
# Determine if the document is part of an embeds_one relation.
|
|
@@ -83,23 +84,23 @@ module Mongoid
|
|
|
83
84
|
#
|
|
84
85
|
# @since 2.0.0.rc.1
|
|
85
86
|
def embedded_one?
|
|
86
|
-
|
|
87
|
+
_association && _association.is_a?(Association::Embedded::EmbedsOne)
|
|
87
88
|
end
|
|
88
89
|
|
|
89
|
-
# Get the
|
|
90
|
-
# will
|
|
90
|
+
# Get the association name for this document. If no association was defined
|
|
91
|
+
# an error will be raised.
|
|
91
92
|
#
|
|
92
|
-
# @example Get the
|
|
93
|
-
# document.
|
|
93
|
+
# @example Get the association name.
|
|
94
|
+
# document.association_name
|
|
94
95
|
#
|
|
95
|
-
# @raise [ Errors::NoMetadata ] If no metadata is present.
|
|
96
|
+
# @raise [ Errors::NoMetadata ] If no association metadata is present.
|
|
96
97
|
#
|
|
97
|
-
# @return [ Symbol ] The
|
|
98
|
+
# @return [ Symbol ] The association name.
|
|
98
99
|
#
|
|
99
100
|
# @since 3.0.0
|
|
100
|
-
def
|
|
101
|
-
raise Errors::NoMetadata.new(self.class.name) unless
|
|
102
|
-
|
|
101
|
+
def association_name
|
|
102
|
+
raise Errors::NoMetadata.new(self.class.name) unless _association
|
|
103
|
+
_association.name
|
|
103
104
|
end
|
|
104
105
|
|
|
105
106
|
# Determine if the document is part of an references_many relation.
|
|
@@ -111,7 +112,7 @@ module Mongoid
|
|
|
111
112
|
#
|
|
112
113
|
# @since 2.0.0.rc.1
|
|
113
114
|
def referenced_many?
|
|
114
|
-
|
|
115
|
+
_association && _association.is_a?(Association::Referenced::HasMany)
|
|
115
116
|
end
|
|
116
117
|
|
|
117
118
|
# Determine if the document is part of an references_one relation.
|
|
@@ -123,7 +124,7 @@ module Mongoid
|
|
|
123
124
|
#
|
|
124
125
|
# @since 2.0.0.rc.1
|
|
125
126
|
def referenced_one?
|
|
126
|
-
|
|
127
|
+
_association && _association.is_a?(Association::Referenced::HasOne)
|
|
127
128
|
end
|
|
128
129
|
|
|
129
130
|
# Convenience method for iterating through the loaded relations and
|
|
@@ -132,7 +133,7 @@ module Mongoid
|
|
|
132
133
|
# @example Reload the relations.
|
|
133
134
|
# document.reload_relations
|
|
134
135
|
#
|
|
135
|
-
# @return [ Hash ] The
|
|
136
|
+
# @return [ Hash ] The association metadata.
|
|
136
137
|
#
|
|
137
138
|
# @since 2.1.6
|
|
138
139
|
def reload_relations
|
|
@@ -35,7 +35,7 @@ module Mongoid
|
|
|
35
35
|
def position
|
|
36
36
|
pos = parent.atomic_position
|
|
37
37
|
locator = document.new_record? ? "" : ".#{document._index}"
|
|
38
|
-
"#{pos}#{"." unless pos.blank?}#{document.
|
|
38
|
+
"#{pos}#{"." unless pos.blank?}#{document._association.store_as}#{locator}"
|
|
39
39
|
end
|
|
40
40
|
end
|
|
41
41
|
end
|