mongoid 6.4.8 → 7.0.0
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
- checksums.yaml.gz.sig +0 -0
- data/Rakefile +0 -26
- 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 +116 -0
- data/lib/mongoid/{relations/eager.rb → association/eager_loadable.rb} +11 -11
- data/lib/mongoid/{relations → association}/embedded/batchable.rb +21 -21
- 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 +529 -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 +20 -50
- 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 +159 -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_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 +415 -0
- data/lib/mongoid/association/touchable.rb +97 -0
- data/lib/mongoid/association.rb +150 -0
- 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 +10 -10
- 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 +1 -1
- data/lib/mongoid/clients/sessions.rb +2 -2
- data/lib/mongoid/composable.rb +4 -4
- data/lib/mongoid/config.rb +1 -0
- data/lib/mongoid/contextual/aggregable/mongo.rb +1 -1
- data/lib/mongoid/contextual/atomic.rb +1 -1
- data/lib/mongoid/contextual/geo_near.rb +1 -1
- data/lib/mongoid/contextual/map_reduce.rb +4 -4
- data/lib/mongoid/contextual/memory.rb +25 -7
- data/lib/mongoid/contextual/mongo.rb +13 -11
- data/lib/mongoid/copyable.rb +6 -6
- data/lib/mongoid/criteria/includable.rb +14 -14
- data/lib/mongoid/criteria/modifiable.rb +8 -14
- 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 +10 -34
- data/lib/mongoid/criteria.rb +2 -2
- data/lib/mongoid/document.rb +6 -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 +0 -1
- data/lib/mongoid/extensions/string.rb +1 -3
- 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 +8 -5
- 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.rb +0 -3
- data/lib/mongoid/persistable/creatable.rb +2 -2
- data/lib/mongoid/persistable/deletable.rb +9 -8
- 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 -1
- data/lib/mongoid/persistable.rb +5 -6
- data/lib/mongoid/persistence_context.rb +1 -5
- data/lib/mongoid/query_cache.rb +19 -64
- 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 +4 -4
- data/lib/mongoid/shardable.rb +1 -1
- data/lib/mongoid/tasks/database.rb +2 -2
- data/lib/mongoid/threaded.rb +0 -36
- 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/mongoid.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/person.rb +5 -2
- data/spec/app/models/shipment_address.rb +1 -0
- 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/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 -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 +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} +26 -11
- data/spec/mongoid/{relations/targets → association/referenced/has_many}/enumerable_spec.rb +122 -1
- 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 +2 -2
- data/spec/mongoid/atomic_spec.rb +4 -4
- data/spec/mongoid/attributes/nested_spec.rb +14 -12
- data/spec/mongoid/attributes/readonly_spec.rb +80 -125
- data/spec/mongoid/attributes_spec.rb +38 -2
- data/spec/mongoid/clients/factory_spec.rb +28 -52
- data/spec/mongoid/clients/options_spec.rb +65 -69
- data/spec/mongoid/clients/sessions_spec.rb +4 -13
- data/spec/mongoid/config_spec.rb +24 -0
- data/spec/mongoid/contextual/geo_near_spec.rb +0 -1
- data/spec/mongoid/contextual/memory_spec.rb +19 -0
- data/spec/mongoid/contextual/mongo_spec.rb +30 -5
- data/spec/mongoid/copyable_spec.rb +34 -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 +3 -42
- 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/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 +0 -23
- data/spec/mongoid/extensions/string_spec.rb +7 -35
- 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_spec.rb +1 -26
- 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 +1 -35
- data/spec/mongoid/persistable_spec.rb +21 -6
- data/spec/mongoid/query_cache_spec.rb +16 -99
- data/spec/mongoid/relations/proxy_spec.rb +124 -124
- data/spec/mongoid/scopable_spec.rb +0 -13
- data/spec/mongoid/shardable_spec.rb +32 -12
- 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 +12 -14
- data.tar.gz.sig +1 -1
- metadata +192 -207
- metadata.gz.sig +0 -0
- data/lib/mongoid/matchable/nor.rb +0 -37
- data/lib/mongoid/railties/controller_runtime.rb +0 -86
- 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/lib/mongoid/relations.rb +0 -148
- 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/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
- /data/spec/mongoid/{relations → association}/embedded/dirty_spec.rb +0 -0
|
@@ -0,0 +1,415 @@
|
|
|
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
|
+
create_extension!(&block)
|
|
61
|
+
validate!
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Compare this association to another.
|
|
65
|
+
#
|
|
66
|
+
# @return [ Object ] The object to compare to this association.
|
|
67
|
+
#
|
|
68
|
+
# @since 7.0
|
|
69
|
+
def ==(other)
|
|
70
|
+
relation_class_name == other.relation_class_name &&
|
|
71
|
+
inverse_class_name == other.inverse_class_name &&
|
|
72
|
+
name == other.name &&
|
|
73
|
+
options == other.options
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Get the callbacks for a given type.
|
|
77
|
+
#
|
|
78
|
+
# @param [ Symbol ] callback_type The type of callback type.
|
|
79
|
+
#
|
|
80
|
+
# @return [ Array<Proc, Symbol> ] A list of the callbacks, either method
|
|
81
|
+
# names or Procs.
|
|
82
|
+
#
|
|
83
|
+
# @since 7.0
|
|
84
|
+
def get_callbacks(callback_type)
|
|
85
|
+
Array(options[callback_type])
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# Get the type setter.
|
|
89
|
+
# @note Only relevant for polymorphic relations that take the :as option.
|
|
90
|
+
#
|
|
91
|
+
# @return [ String ] The type setter method.
|
|
92
|
+
#
|
|
93
|
+
# @since 7.0
|
|
94
|
+
def type_setter
|
|
95
|
+
@type_setter ||= type.__setter__
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# Whether trying to bind an object using this association should raise
|
|
99
|
+
# an error.
|
|
100
|
+
#
|
|
101
|
+
# @param [ Document ] doc The document to be bound.
|
|
102
|
+
#
|
|
103
|
+
# @return [ true, false ] Whether the document can be bound.
|
|
104
|
+
#
|
|
105
|
+
# @since 7.0
|
|
106
|
+
def bindable?(doc); false; end
|
|
107
|
+
|
|
108
|
+
# Get the inverse names.
|
|
109
|
+
#
|
|
110
|
+
# @param [ Object ] other The other model class or model object to use when
|
|
111
|
+
# determining inverses.
|
|
112
|
+
#
|
|
113
|
+
# @return [ Array<Symbol> ] The list of inverse names.
|
|
114
|
+
#
|
|
115
|
+
# @since 7.0
|
|
116
|
+
def inverses(other = nil)
|
|
117
|
+
return [ inverse_of ] if inverse_of
|
|
118
|
+
if polymorphic?
|
|
119
|
+
polymorphic_inverses(other)
|
|
120
|
+
else
|
|
121
|
+
determine_inverses(other)
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# Get the inverse's association metadata.
|
|
126
|
+
#
|
|
127
|
+
# @param [ Object ] other The other model class or model object to use when
|
|
128
|
+
# determining inverses.
|
|
129
|
+
#
|
|
130
|
+
# @return [ Association ] The inverse's association metadata.
|
|
131
|
+
#
|
|
132
|
+
# @since 7.0
|
|
133
|
+
def inverse_association(other = nil)
|
|
134
|
+
(other || relation_class).relations[inverse(other)]
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# Get the inverse type.
|
|
138
|
+
#
|
|
139
|
+
# @return [ nil ] Default is nil for an association.
|
|
140
|
+
#
|
|
141
|
+
# @since 7.0
|
|
142
|
+
def inverse_type; end
|
|
143
|
+
|
|
144
|
+
# The class name of the relation object(s).
|
|
145
|
+
#
|
|
146
|
+
# @return [ String ] The relation objects' class name.
|
|
147
|
+
#
|
|
148
|
+
# @since 7.0
|
|
149
|
+
def relation_class_name
|
|
150
|
+
@class_name ||= @options[:class_name] || ActiveSupport::Inflector.classify(name)
|
|
151
|
+
end
|
|
152
|
+
alias :class_name :relation_class_name
|
|
153
|
+
|
|
154
|
+
# The class of the relation object(s).
|
|
155
|
+
#
|
|
156
|
+
# @return [ String ] The relation objects' class.
|
|
157
|
+
#
|
|
158
|
+
# @since 7.0
|
|
159
|
+
def klass
|
|
160
|
+
@klass ||= relation_class_name.constantize
|
|
161
|
+
end
|
|
162
|
+
alias :relation_class :klass
|
|
163
|
+
|
|
164
|
+
# The class name of the object owning this relation.
|
|
165
|
+
#
|
|
166
|
+
# @return [ String ] The owning objects' class name.
|
|
167
|
+
#
|
|
168
|
+
# @since 7.0
|
|
169
|
+
def inverse_class_name
|
|
170
|
+
@inverse_class_name ||= @owner_class.name
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# The class of the object owning this relation.
|
|
174
|
+
#
|
|
175
|
+
# @return [ String ] The owning objects' class.
|
|
176
|
+
#
|
|
177
|
+
# @since 7.0
|
|
178
|
+
def inverse_class
|
|
179
|
+
@owner_class
|
|
180
|
+
end
|
|
181
|
+
alias :inverse_klass :inverse_class
|
|
182
|
+
|
|
183
|
+
# The foreign key field if this relation stores a foreign key.
|
|
184
|
+
# Otherwise, the primary key.
|
|
185
|
+
#
|
|
186
|
+
# @return [ Symbol, String ] The primary key.
|
|
187
|
+
#
|
|
188
|
+
# @since 7.0
|
|
189
|
+
def key
|
|
190
|
+
stores_foreign_key? ? foreign_key : primary_key
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
# The name of the setter on this object for assigning an associated object.
|
|
194
|
+
#
|
|
195
|
+
# @return [ String ] The setter name.
|
|
196
|
+
#
|
|
197
|
+
# @since 7.0
|
|
198
|
+
def setter
|
|
199
|
+
@setter ||= "#{name}="
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
# The name of the inverse setter method.
|
|
203
|
+
#
|
|
204
|
+
# @return [ String ] The name of the inverse setter.
|
|
205
|
+
#
|
|
206
|
+
# @since 7.0
|
|
207
|
+
def inverse_setter(other = nil)
|
|
208
|
+
@inverse_setter ||= "#{inverses(other).first}=" unless inverses(other).blank?
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
# The name of the foreign key setter method.
|
|
212
|
+
#
|
|
213
|
+
# @return [ String ] The name of the foreign key setter.
|
|
214
|
+
#
|
|
215
|
+
# @since 7.0
|
|
216
|
+
def foreign_key_setter
|
|
217
|
+
# note: You can't check if this association stores foreign key
|
|
218
|
+
# See HasOne and HasMany binding, they referenced foreign_key_setter
|
|
219
|
+
@foreign_key_setter ||= "#{foreign_key}=" if foreign_key
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
# The atomic path for this relation.
|
|
223
|
+
#
|
|
224
|
+
# @return [ Mongoid::Atomic::Paths::Root ] The atomic path object.
|
|
225
|
+
#
|
|
226
|
+
# @since 7.0
|
|
227
|
+
def path(document)
|
|
228
|
+
relation.path(document)
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
# Gets the setter for the field that sets the type of document on a
|
|
232
|
+
# polymorphic relation.
|
|
233
|
+
#
|
|
234
|
+
# @example Get the inverse type setter.
|
|
235
|
+
# association.inverse_type_setter
|
|
236
|
+
#
|
|
237
|
+
# @return [ String ] The name of the setter.
|
|
238
|
+
#
|
|
239
|
+
# @since 7.0
|
|
240
|
+
def inverse_type_setter
|
|
241
|
+
@inverse_type_setter ||= inverse_type.__setter__
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
# Get the name of the method to check if the foreign key has changed.
|
|
245
|
+
#
|
|
246
|
+
# @example Get the foreign key check method.
|
|
247
|
+
# association.foreign_key_check
|
|
248
|
+
#
|
|
249
|
+
# @return [ String ] The foreign key check.
|
|
250
|
+
#
|
|
251
|
+
# @since 7.0
|
|
252
|
+
def foreign_key_check
|
|
253
|
+
@foreign_key_check ||= "#{foreign_key}_changed?" if (stores_foreign_key? && foreign_key)
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
# Create a relation proxy object using the owner and target.
|
|
257
|
+
#
|
|
258
|
+
# @param [ Document ] owner The document this relation hangs off of.
|
|
259
|
+
# @param [ Document, Array<Document> ] target The target (parent) of the
|
|
260
|
+
# relation.
|
|
261
|
+
#
|
|
262
|
+
# @return [ Proxy ]
|
|
263
|
+
#
|
|
264
|
+
# @since 7.0
|
|
265
|
+
def create_relation(owner, target)
|
|
266
|
+
relation.new(owner, target, self)
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
# Whether the dependent method is destructive.
|
|
270
|
+
#
|
|
271
|
+
# @return [ true, false ] If the dependent method is destructive.
|
|
272
|
+
#
|
|
273
|
+
# @since 7.0
|
|
274
|
+
def destructive?
|
|
275
|
+
@destructive ||= !!(dependent && (dependent == :delete_all || dependent == :destroy))
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
# Get the counter cache column name.
|
|
279
|
+
#
|
|
280
|
+
# @return [ String ] The counter cache column name.
|
|
281
|
+
#
|
|
282
|
+
# @since 7.0
|
|
283
|
+
def counter_cache_column_name
|
|
284
|
+
@counter_cache_column_name ||= (@options[:counter_cache].is_a?(String) ||
|
|
285
|
+
@options[:counter_cache].is_a?(Symbol)) ?
|
|
286
|
+
@options[:counter_cache] : "#{inverse || inverse_class_name.demodulize.underscore.pluralize}_count"
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
# Get the extension.
|
|
290
|
+
#
|
|
291
|
+
# @return [ Module ] The extension module, if one has been defined.
|
|
292
|
+
#
|
|
293
|
+
# @since 7.0
|
|
294
|
+
def extension
|
|
295
|
+
@extension ||= @options[:extend]
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
# Get the inverse name.
|
|
299
|
+
#
|
|
300
|
+
# @return [ Symbol ] The inverse name.
|
|
301
|
+
#
|
|
302
|
+
# @since 7.0
|
|
303
|
+
def inverse(other = nil)
|
|
304
|
+
candidates = inverses(other)
|
|
305
|
+
candidates.detect { |c| c } if candidates
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
# Whether the associated object(s) should be validated.
|
|
309
|
+
#
|
|
310
|
+
# @return [ true, false ] If the associated object(s)
|
|
311
|
+
# should be validated.
|
|
312
|
+
#
|
|
313
|
+
# @since 7.0
|
|
314
|
+
def validate?
|
|
315
|
+
@validate ||= if @options[:validate].nil?
|
|
316
|
+
validation_default
|
|
317
|
+
else
|
|
318
|
+
!!@options[:validate]
|
|
319
|
+
end
|
|
320
|
+
end
|
|
321
|
+
|
|
322
|
+
private
|
|
323
|
+
|
|
324
|
+
def setup_index!
|
|
325
|
+
@owner_class.index(index_spec, background: true) if indexed?
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
def define_touchable!
|
|
329
|
+
if touchable?
|
|
330
|
+
Association::Touchable.define_touchable!(self)
|
|
331
|
+
end
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
def define_autosaver!
|
|
335
|
+
if autosave?
|
|
336
|
+
Association::Referenced::AutoSave.define_autosave!(self)
|
|
337
|
+
end
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
def define_builder!
|
|
341
|
+
Association::Builders.define_builder!(self)
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
def define_creator!
|
|
345
|
+
Association::Builders.define_creator!(self)
|
|
346
|
+
end
|
|
347
|
+
|
|
348
|
+
def define_getter!
|
|
349
|
+
Association::Accessors.define_getter!(self)
|
|
350
|
+
end
|
|
351
|
+
|
|
352
|
+
def define_setter!
|
|
353
|
+
Association::Accessors.define_setter!(self)
|
|
354
|
+
end
|
|
355
|
+
|
|
356
|
+
def define_existence_check!
|
|
357
|
+
Association::Accessors.define_existence_check!(self)
|
|
358
|
+
end
|
|
359
|
+
|
|
360
|
+
def define_ids_getter!
|
|
361
|
+
Association::Accessors.define_ids_getter!(self)
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
def define_ids_setter!
|
|
365
|
+
Association::Accessors.define_ids_setter!(self)
|
|
366
|
+
end
|
|
367
|
+
|
|
368
|
+
def define_counter_cache_callbacks!
|
|
369
|
+
if counter_cached?
|
|
370
|
+
Association::Referenced::CounterCache.define_callbacks!(self)
|
|
371
|
+
end
|
|
372
|
+
end
|
|
373
|
+
|
|
374
|
+
def define_dependency!
|
|
375
|
+
if dependent
|
|
376
|
+
Association::Depending.define_dependency!(self)
|
|
377
|
+
end
|
|
378
|
+
end
|
|
379
|
+
|
|
380
|
+
def validate!
|
|
381
|
+
@options.keys.each do |opt|
|
|
382
|
+
unless self.class::VALID_OPTIONS.include?(opt)
|
|
383
|
+
raise Errors::InvalidRelationOption.new(@owner_class, name, opt, self.class::VALID_OPTIONS)
|
|
384
|
+
end
|
|
385
|
+
end
|
|
386
|
+
|
|
387
|
+
[name, "#{name}?".to_sym, "#{name}=".to_sym].each do |n|
|
|
388
|
+
if Mongoid.destructive_fields.include?(n)
|
|
389
|
+
raise Errors::InvalidRelation.new(@owner_class, n)
|
|
390
|
+
end
|
|
391
|
+
end
|
|
392
|
+
end
|
|
393
|
+
|
|
394
|
+
def polymorph!
|
|
395
|
+
if polymorphic?
|
|
396
|
+
@owner_class.polymorphic = true
|
|
397
|
+
end
|
|
398
|
+
end
|
|
399
|
+
|
|
400
|
+
def create_extension!(&block)
|
|
401
|
+
if block
|
|
402
|
+
extension_module_name = "#{@owner_class.to_s.demodulize}#{name.to_s.camelize}RelationExtension"
|
|
403
|
+
silence_warnings do
|
|
404
|
+
@owner_class.const_set(extension_module_name, Module.new(&block))
|
|
405
|
+
end
|
|
406
|
+
@extension = "#{@owner_class}::#{extension_module_name}".constantize
|
|
407
|
+
end
|
|
408
|
+
end
|
|
409
|
+
|
|
410
|
+
def default_inverse
|
|
411
|
+
@default_inverse ||= klass.relations[inverse_klass.name.underscore]
|
|
412
|
+
end
|
|
413
|
+
end
|
|
414
|
+
end
|
|
415
|
+
end
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid
|
|
3
|
+
module Association
|
|
4
|
+
module Touchable
|
|
5
|
+
|
|
6
|
+
module InstanceMethods
|
|
7
|
+
|
|
8
|
+
# Touch the document, in effect updating its updated_at timestamp and
|
|
9
|
+
# optionally the provided field to the current time. If any belongs_to
|
|
10
|
+
# relations exist with a touch option, they will be updated as well.
|
|
11
|
+
#
|
|
12
|
+
# @example Update the updated_at timestamp.
|
|
13
|
+
# document.touch
|
|
14
|
+
#
|
|
15
|
+
# @example Update the updated_at and provided timestamps.
|
|
16
|
+
# document.touch(:audited)
|
|
17
|
+
#
|
|
18
|
+
# @note This will not autobuild relations if those options are set.
|
|
19
|
+
#
|
|
20
|
+
# @param [ Symbol ] field The name of an additional field to update.
|
|
21
|
+
#
|
|
22
|
+
# @return [ true/false ] false if record is new_record otherwise true.
|
|
23
|
+
#
|
|
24
|
+
# @since 3.0.0
|
|
25
|
+
def touch(field = nil)
|
|
26
|
+
return false if _root.new_record?
|
|
27
|
+
current = Time.now
|
|
28
|
+
field = database_field_name(field)
|
|
29
|
+
write_attribute(:updated_at, current) if respond_to?("updated_at=")
|
|
30
|
+
write_attribute(field, current) if field
|
|
31
|
+
|
|
32
|
+
touches = touch_atomic_updates(field)
|
|
33
|
+
unless touches["$set"].blank?
|
|
34
|
+
selector = atomic_selector
|
|
35
|
+
_root.collection.find(selector).update_one(positionally(selector, touches), session: session)
|
|
36
|
+
end
|
|
37
|
+
run_callbacks(:touch)
|
|
38
|
+
true
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
extend self
|
|
43
|
+
|
|
44
|
+
# Add the association to the touchable relations if the touch option was
|
|
45
|
+
# provided.
|
|
46
|
+
#
|
|
47
|
+
# @example Add the touchable.
|
|
48
|
+
# Model.define_touchable!(assoc)
|
|
49
|
+
#
|
|
50
|
+
# @param [ Association ] association The association metadata.
|
|
51
|
+
#
|
|
52
|
+
# @return [ Class ] The model class.
|
|
53
|
+
#
|
|
54
|
+
# @since 3.0.0
|
|
55
|
+
def define_touchable!(association)
|
|
56
|
+
name = association.name
|
|
57
|
+
method_name = define_relation_touch_method(name, association)
|
|
58
|
+
association.inverse_class.tap do |klass|
|
|
59
|
+
klass.after_save method_name
|
|
60
|
+
klass.after_destroy method_name
|
|
61
|
+
klass.after_touch method_name
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
private
|
|
66
|
+
|
|
67
|
+
# Define the method that will get called for touching belongs_to
|
|
68
|
+
# relations.
|
|
69
|
+
#
|
|
70
|
+
# @api private
|
|
71
|
+
#
|
|
72
|
+
# @example Define the touch relation.
|
|
73
|
+
# Model.define_relation_touch_method(:band)
|
|
74
|
+
# Model.define_relation_touch_method(:band, :band_updated_at)
|
|
75
|
+
#
|
|
76
|
+
# @param [ Symbol ] name The name of the relation.
|
|
77
|
+
# @param [ Association ] association The association metadata.
|
|
78
|
+
#
|
|
79
|
+
# @since 3.1.0
|
|
80
|
+
#
|
|
81
|
+
# @return [ Symbol ] The method name.
|
|
82
|
+
def define_relation_touch_method(name, association)
|
|
83
|
+
association.relation_class.send(:include, InstanceMethods)
|
|
84
|
+
method_name = "touch_#{name}_after_create_or_destroy"
|
|
85
|
+
association.inverse_class.class_eval <<-TOUCH, __FILE__, __LINE__ + 1
|
|
86
|
+
def #{method_name}
|
|
87
|
+
without_autobuild do
|
|
88
|
+
relation = __send__(:#{name})
|
|
89
|
+
relation.touch #{":#{association.touch_field}" if association.touch_field} if relation
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
TOUCH
|
|
93
|
+
method_name.to_sym
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
@@ -0,0 +1,150 @@
|
|
|
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'
|
|
6
|
+
require 'mongoid/association/touchable'
|
|
7
|
+
|
|
8
|
+
require 'mongoid/association/many'
|
|
9
|
+
require 'mongoid/association/one'
|
|
10
|
+
require 'mongoid/association/relatable'
|
|
11
|
+
require 'mongoid/association/nested'
|
|
12
|
+
require 'mongoid/association/referenced'
|
|
13
|
+
require 'mongoid/association/embedded'
|
|
14
|
+
require 'mongoid/association/macros'
|
|
15
|
+
|
|
16
|
+
require 'mongoid/association/reflections'
|
|
17
|
+
require 'mongoid/association/eager_loadable'
|
|
18
|
+
|
|
19
|
+
module Mongoid
|
|
20
|
+
module Association
|
|
21
|
+
extend ActiveSupport::Concern
|
|
22
|
+
include Embedded::Cyclic
|
|
23
|
+
include Referenced::AutoSave
|
|
24
|
+
include Referenced::CounterCache
|
|
25
|
+
include Referenced::Syncable
|
|
26
|
+
include Accessors
|
|
27
|
+
include Depending
|
|
28
|
+
include Builders
|
|
29
|
+
include Macros
|
|
30
|
+
include Reflections
|
|
31
|
+
|
|
32
|
+
# Map the macros to their corresponding Association classes.
|
|
33
|
+
#
|
|
34
|
+
# @return [ Hash ] The mapping from macros to their Association class.
|
|
35
|
+
#
|
|
36
|
+
# @since 7.0
|
|
37
|
+
MACRO_MAPPING = {
|
|
38
|
+
embeds_one: Association::Embedded::EmbedsOne,
|
|
39
|
+
embeds_many: Association::Embedded::EmbedsMany,
|
|
40
|
+
embedded_in: Association::Embedded::EmbeddedIn,
|
|
41
|
+
has_one: Association::Referenced::HasOne,
|
|
42
|
+
has_many: Association::Referenced::HasMany,
|
|
43
|
+
has_and_belongs_to_many: Association::Referenced::HasAndBelongsToMany,
|
|
44
|
+
belongs_to: Association::Referenced::BelongsTo,
|
|
45
|
+
}.freeze
|
|
46
|
+
|
|
47
|
+
attr_accessor :_association
|
|
48
|
+
|
|
49
|
+
included do
|
|
50
|
+
class_attribute :polymorphic
|
|
51
|
+
self.polymorphic = false
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Determine if the document itself is embedded in another document via the
|
|
55
|
+
# proper channels. (If it has a parent document.)
|
|
56
|
+
#
|
|
57
|
+
# @example Is the document embedded?
|
|
58
|
+
# address.embedded?
|
|
59
|
+
#
|
|
60
|
+
# @return [ true, false ] True if the document has a parent document.
|
|
61
|
+
#
|
|
62
|
+
# @since 2.0.0.rc.1
|
|
63
|
+
def embedded?
|
|
64
|
+
@embedded ||= (cyclic ? _parent.present? : self.class.embedded?)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Determine if the document is part of an embeds_many relation.
|
|
68
|
+
#
|
|
69
|
+
# @example Is the document in an embeds many?
|
|
70
|
+
# address.embedded_many?
|
|
71
|
+
#
|
|
72
|
+
# @return [ true, false ] True if in an embeds many.
|
|
73
|
+
#
|
|
74
|
+
# @since 2.0.0.rc.1
|
|
75
|
+
def embedded_many?
|
|
76
|
+
_association && _association.is_a?(Association::Embedded::EmbedsMany)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Determine if the document is part of an embeds_one relation.
|
|
80
|
+
#
|
|
81
|
+
# @example Is the document in an embeds one?
|
|
82
|
+
# address.embedded_one?
|
|
83
|
+
#
|
|
84
|
+
# @return [ true, false ] True if in an embeds one.
|
|
85
|
+
#
|
|
86
|
+
# @since 2.0.0.rc.1
|
|
87
|
+
def embedded_one?
|
|
88
|
+
_association && _association.is_a?(Association::Embedded::EmbedsOne)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Get the association name for this document. If no association was defined
|
|
92
|
+
# an error will be raised.
|
|
93
|
+
#
|
|
94
|
+
# @example Get the association name.
|
|
95
|
+
# document.association_name
|
|
96
|
+
#
|
|
97
|
+
# @raise [ Errors::NoMetadata ] If no association metadata is present.
|
|
98
|
+
#
|
|
99
|
+
# @return [ Symbol ] The association name.
|
|
100
|
+
#
|
|
101
|
+
# @since 3.0.0
|
|
102
|
+
def association_name
|
|
103
|
+
raise Errors::NoMetadata.new(self.class.name) unless _association
|
|
104
|
+
_association.name
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# Determine if the document is part of an references_many relation.
|
|
108
|
+
#
|
|
109
|
+
# @example Is the document in a references many?
|
|
110
|
+
# post.referenced_many?
|
|
111
|
+
#
|
|
112
|
+
# @return [ true, false ] True if in a references many.
|
|
113
|
+
#
|
|
114
|
+
# @since 2.0.0.rc.1
|
|
115
|
+
def referenced_many?
|
|
116
|
+
_association && _association.is_a?(Association::Referenced::HasMany)
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# Determine if the document is part of an references_one relation.
|
|
120
|
+
#
|
|
121
|
+
# @example Is the document in a references one?
|
|
122
|
+
# address.referenced_one?
|
|
123
|
+
#
|
|
124
|
+
# @return [ true, false ] True if in a references one.
|
|
125
|
+
#
|
|
126
|
+
# @since 2.0.0.rc.1
|
|
127
|
+
def referenced_one?
|
|
128
|
+
_association && _association.is_a?(Association::Referenced::HasOne)
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# Convenience method for iterating through the loaded relations and
|
|
132
|
+
# reloading them.
|
|
133
|
+
#
|
|
134
|
+
# @example Reload the relations.
|
|
135
|
+
# document.reload_relations
|
|
136
|
+
#
|
|
137
|
+
# @return [ Hash ] The association metadata.
|
|
138
|
+
#
|
|
139
|
+
# @since 2.1.6
|
|
140
|
+
def reload_relations
|
|
141
|
+
relations.each_pair do |name, meta|
|
|
142
|
+
if instance_variable_defined?("@_#{name}")
|
|
143
|
+
if _parent.nil? || instance_variable_get("@_#{name}") != _parent
|
|
144
|
+
remove_instance_variable("@_#{name}")
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
end
|
|
@@ -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
|