mongoid 6.4.8 → 7.0.0.beta
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/Rakefile +0 -26
- data/lib/config/locales/en.yml +17 -21
- data/lib/mongoid.rb +2 -2
- data/lib/mongoid/association.rb +150 -0
- data/lib/mongoid/association/accessors.rb +339 -0
- data/lib/mongoid/{relations/binding.rb → association/bindable.rb} +32 -52
- data/lib/mongoid/association/builders.rb +92 -0
- data/lib/mongoid/{relations/constraint.rb → association/constrainable.rb} +11 -22
- data/lib/mongoid/association/depending.rb +116 -0
- data/lib/mongoid/{relations/eager.rb → association/eager_loadable.rb} +11 -11
- data/lib/mongoid/association/embedded.rb +4 -0
- data/lib/mongoid/{relations → association}/embedded/batchable.rb +27 -53
- data/lib/mongoid/association/embedded/cyclic.rb +109 -0
- data/lib/mongoid/association/embedded/embedded_in.rb +154 -0
- data/lib/mongoid/association/embedded/embedded_in/binding.rb +56 -0
- data/lib/mongoid/{relations/builders/embedded/in.rb → association/embedded/embedded_in/buildable.rb} +12 -6
- data/lib/mongoid/association/embedded/embedded_in/proxy.rb +121 -0
- data/lib/mongoid/association/embedded/embeds_many.rb +210 -0
- data/lib/mongoid/{relations/bindings/embedded/many.rb → association/embedded/embeds_many/binding.rb} +11 -9
- data/lib/mongoid/{relations/builders/embedded/many.rb → association/embedded/embeds_many/buildable.rb} +13 -7
- data/lib/mongoid/association/embedded/embeds_many/proxy.rb +529 -0
- data/lib/mongoid/association/embedded/embeds_one.rb +173 -0
- data/lib/mongoid/{relations/bindings/embedded/one.rb → association/embedded/embeds_one/binding.rb} +12 -10
- data/lib/mongoid/{relations/builders/embedded/one.rb → association/embedded/embeds_one/buildable.rb} +13 -7
- data/lib/mongoid/association/embedded/embeds_one/proxy.rb +130 -0
- data/lib/mongoid/association/macros.rb +204 -0
- data/lib/mongoid/{relations → association}/many.rb +18 -52
- data/lib/mongoid/{relations → association}/marshalable.rb +6 -4
- data/lib/mongoid/association/nested.rb +15 -0
- data/lib/mongoid/association/nested/many.rb +200 -0
- data/lib/mongoid/association/nested/nested_buildable.rb +72 -0
- data/lib/mongoid/association/nested/one.rb +127 -0
- data/lib/mongoid/{relations → association}/one.rb +6 -6
- data/lib/mongoid/association/options.rb +152 -0
- data/lib/mongoid/{relations → association}/proxy.rb +31 -58
- data/lib/mongoid/association/referenced.rb +7 -0
- data/lib/mongoid/association/referenced/auto_save.rb +79 -0
- data/lib/mongoid/association/referenced/belongs_to.rb +248 -0
- data/lib/mongoid/association/referenced/belongs_to/binding.rb +87 -0
- data/lib/mongoid/association/referenced/belongs_to/buildable.rb +46 -0
- data/lib/mongoid/association/referenced/belongs_to/eager.rb +36 -0
- data/lib/mongoid/association/referenced/belongs_to/proxy.rb +136 -0
- data/lib/mongoid/association/referenced/counter_cache.rb +163 -0
- data/lib/mongoid/association/referenced/eager.rb +159 -0
- data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +290 -0
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/binding.rb +71 -0
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb +40 -0
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb +52 -0
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb +310 -0
- data/lib/mongoid/association/referenced/has_many.rb +273 -0
- data/lib/mongoid/{relations/bindings/referenced/many.rb → association/referenced/has_many/binding.rb} +6 -5
- data/lib/mongoid/association/referenced/has_many/buildable.rb +38 -0
- data/lib/mongoid/association/referenced/has_many/eager.rb +43 -0
- data/lib/mongoid/association/referenced/has_many/enumerable.rb +479 -0
- data/lib/mongoid/association/referenced/has_many/proxy.rb +577 -0
- data/lib/mongoid/association/referenced/has_one.rb +204 -0
- data/lib/mongoid/{relations/bindings/referenced/one.rb → association/referenced/has_one/binding.rb} +11 -8
- data/lib/mongoid/association/referenced/has_one/buildable.rb +60 -0
- data/lib/mongoid/association/referenced/has_one/eager.rb +35 -0
- data/lib/mongoid/{relations/builders/nested_attributes/one.rb → association/referenced/has_one/nested_builder.rb} +9 -9
- data/lib/mongoid/association/referenced/has_one/proxy.rb +113 -0
- data/lib/mongoid/association/referenced/syncable.rb +170 -0
- data/lib/mongoid/{relations → association}/reflections.rb +21 -17
- data/lib/mongoid/association/relatable.rb +415 -0
- data/lib/mongoid/association/touchable.rb +97 -0
- data/lib/mongoid/atomic.rb +6 -6
- data/lib/mongoid/atomic/modifiers.rb +8 -12
- data/lib/mongoid/atomic/paths/embedded/many.rb +1 -1
- data/lib/mongoid/atomic/paths/embedded/one.rb +1 -1
- data/lib/mongoid/attributes.rb +2 -1
- data/lib/mongoid/attributes/nested.rb +10 -10
- data/lib/mongoid/attributes/processing.rb +2 -2
- data/lib/mongoid/attributes/readonly.rb +2 -4
- data/lib/mongoid/clients.rb +0 -2
- data/lib/mongoid/clients/options.rb +1 -1
- data/lib/mongoid/clients/storage_options.rb +0 -1
- data/lib/mongoid/composable.rb +3 -4
- data/lib/mongoid/config.rb +1 -0
- data/lib/mongoid/contextual/aggregable/mongo.rb +1 -1
- data/lib/mongoid/contextual/atomic.rb +3 -6
- data/lib/mongoid/contextual/map_reduce.rb +3 -7
- data/lib/mongoid/contextual/memory.rb +5 -10
- data/lib/mongoid/contextual/mongo.rb +10 -27
- data/lib/mongoid/copyable.rb +6 -6
- data/lib/mongoid/criteria.rb +1 -2
- data/lib/mongoid/criteria/includable.rb +14 -14
- data/lib/mongoid/criteria/modifiable.rb +8 -14
- data/lib/mongoid/criteria/queryable/mergeable.rb +1 -3
- data/lib/mongoid/criteria/queryable/pipeline.rb +10 -5
- data/lib/mongoid/criteria/queryable/selectable.rb +10 -34
- data/lib/mongoid/document.rb +6 -6
- data/lib/mongoid/errors.rb +3 -1
- data/lib/mongoid/errors/invalid_dependent_strategy.rb +32 -0
- data/lib/mongoid/errors/invalid_relation_option.rb +29 -0
- data/lib/mongoid/errors/unknown_model.rb +25 -0
- data/lib/mongoid/extensions/array.rb +5 -5
- data/lib/mongoid/extensions/big_decimal.rb +1 -1
- data/lib/mongoid/extensions/object.rb +4 -4
- data/lib/mongoid/extensions/range.rb +1 -0
- data/lib/mongoid/extensions/regexp.rb +0 -1
- data/lib/mongoid/extensions/string.rb +1 -3
- data/lib/mongoid/factory.rb +4 -3
- data/lib/mongoid/fields.rb +1 -1
- data/lib/mongoid/fields/foreign_key.rb +5 -5
- data/lib/mongoid/fields/standard.rb +2 -14
- data/lib/mongoid/fields/validators/macro.rb +1 -1
- data/lib/mongoid/indexable.rb +8 -5
- data/lib/mongoid/interceptable.rb +5 -5
- data/lib/mongoid/matchable.rb +0 -3
- data/lib/mongoid/persistable.rb +4 -5
- data/lib/mongoid/persistable/creatable.rb +2 -4
- data/lib/mongoid/persistable/deletable.rb +9 -10
- data/lib/mongoid/persistable/destroyable.rb +5 -1
- data/lib/mongoid/persistable/incrementable.rb +1 -1
- data/lib/mongoid/persistable/logical.rb +1 -1
- data/lib/mongoid/persistable/settable.rb +5 -5
- data/lib/mongoid/persistable/updatable.rb +2 -2
- data/lib/mongoid/persistable/upsertable.rb +1 -2
- data/lib/mongoid/persistence_context.rb +4 -9
- data/lib/mongoid/query_cache.rb +18 -65
- data/lib/mongoid/railtie.rb +0 -17
- data/lib/mongoid/reloadable.rb +1 -1
- data/lib/mongoid/scopable.rb +3 -3
- data/lib/mongoid/serializable.rb +3 -3
- data/lib/mongoid/tasks/database.rb +2 -3
- data/lib/mongoid/threaded.rb +0 -74
- data/lib/mongoid/traversable.rb +2 -2
- data/lib/mongoid/validatable.rb +8 -8
- data/lib/mongoid/validatable/presence.rb +2 -2
- data/lib/mongoid/validatable/uniqueness.rb +4 -4
- data/lib/mongoid/version.rb +1 -1
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +3 -4
- data/spec/app/models/animal.rb +2 -1
- data/spec/app/models/band.rb +0 -1
- data/spec/app/models/bomb.rb +1 -1
- data/spec/app/models/message.rb +1 -1
- data/spec/app/models/person.rb +5 -2
- data/spec/app/models/vertex.rb +6 -0
- data/spec/app/models/wiki_page.rb +1 -1
- data/spec/config/mongoid.yml +1 -0
- data/spec/mongoid/{relations → association}/accessors_spec.rb +1 -1
- data/spec/mongoid/{relations → association}/auto_save_spec.rb +60 -12
- data/spec/mongoid/{relations → association}/builders_spec.rb +1 -1
- data/spec/mongoid/association/constrainable_spec.rb +115 -0
- data/spec/mongoid/{relations → association}/counter_cache_spec.rb +1 -1
- data/spec/mongoid/association/depending_spec.rb +613 -0
- data/spec/mongoid/{relations → association}/eager_spec.rb +12 -12
- data/spec/mongoid/{relations → association/embedded}/cyclic_spec.rb +1 -1
- data/spec/mongoid/{relations → association}/embedded/dirty_spec.rb +0 -0
- data/spec/mongoid/{relations/bindings/embedded/in_spec.rb → association/embedded/embedded_in/binding_spec.rb} +13 -13
- data/spec/mongoid/{relations/builders/embedded/in_spec.rb → association/embedded/embedded_in/buildable_spec.rb} +9 -9
- data/spec/mongoid/{relations/embedded/in_spec.rb → association/embedded/embedded_in/proxy_spec.rb} +5 -77
- data/spec/mongoid/association/embedded/embedded_in_spec.rb +843 -0
- data/spec/mongoid/{relations/bindings/embedded/many_spec.rb → association/embedded/embeds_many/binding_spec.rb} +3 -3
- data/spec/mongoid/{relations/builders/embedded/many_spec.rb → association/embedded/embeds_many/buildable_spec.rb} +17 -45
- data/spec/mongoid/{relations/embedded/many_spec.rb → association/embedded/embeds_many/proxy_spec.rb} +140 -428
- data/spec/mongoid/association/embedded/embeds_many_spec.rb +852 -0
- data/spec/mongoid/{relations/bindings/embedded/one_spec.rb → association/embedded/embeds_one/binding_spec.rb} +4 -4
- data/spec/mongoid/{relations/builders/embedded/one_spec.rb → association/embedded/embeds_one/buildable_spec.rb} +14 -34
- data/spec/mongoid/{relations/embedded/one_spec.rb → association/embedded/embeds_one/proxy_spec.rb} +39 -84
- data/spec/mongoid/association/embedded/embeds_one_spec.rb +908 -0
- data/spec/mongoid/{relations → association}/macros_spec.rb +148 -93
- data/spec/mongoid/{relations/builders/nested_attributes → association/nested}/many_spec.rb +16 -19
- data/spec/mongoid/{relations/builders/nested_attributes → association/nested}/one_spec.rb +17 -20
- data/spec/mongoid/association/options_spec.rb +1321 -0
- data/spec/mongoid/{relations → association}/polymorphic_spec.rb +7 -34
- data/spec/mongoid/{relations/bindings/referenced/in_spec.rb → association/referenced/belongs_to/binding_spec.rb} +7 -7
- data/spec/mongoid/{relations/builders/referenced/in_spec.rb → association/referenced/belongs_to/buildable_spec.rb} +46 -79
- data/spec/mongoid/{relations/eager/belongs_to_spec.rb → association/referenced/belongs_to/eager_spec.rb} +9 -9
- data/spec/mongoid/{relations/referenced/in_spec.rb → association/referenced/belongs_to/proxy_spec.rb} +57 -91
- data/spec/mongoid/association/referenced/belongs_to_spec.rb +1963 -0
- data/spec/mongoid/{relations/bindings/referenced/many_to_many_spec.rb → association/referenced/has_and_belongs_to_many/binding_spec.rb} +5 -5
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/buildable_spec.rb +121 -0
- data/spec/mongoid/{relations/eager/has_and_belongs_to_many_spec.rb → association/referenced/has_and_belongs_to_many/eager_spec.rb} +5 -5
- data/spec/mongoid/{relations/referenced/many_to_many_spec.rb → association/referenced/has_and_belongs_to_many/proxy_spec.rb} +107 -98
- data/spec/mongoid/association/referenced/has_and_belongs_to_many_spec.rb +1027 -0
- data/spec/mongoid/{relations/bindings/referenced/many_spec.rb → association/referenced/has_many/binding_spec.rb} +5 -5
- data/spec/mongoid/association/referenced/has_many/buildable_spec.rb +119 -0
- data/spec/mongoid/{relations/eager/has_many_spec.rb → association/referenced/has_many/eager_spec.rb} +11 -11
- data/spec/mongoid/{relations/targets → association/referenced/has_many}/enumerable_spec.rb +1 -109
- data/spec/mongoid/{relations/referenced/many_spec.rb → association/referenced/has_many/proxy_spec.rb} +28 -93
- data/spec/mongoid/association/referenced/has_many_spec.rb +1225 -0
- data/spec/mongoid/{relations/bindings/referenced/one_spec.rb → association/referenced/has_one/binding_spec.rb} +4 -4
- data/spec/mongoid/association/referenced/has_one/buildable_spec.rb +113 -0
- data/spec/mongoid/{relations/eager/has_one_spec.rb → association/referenced/has_one/eager_spec.rb} +10 -10
- data/spec/mongoid/{relations/referenced/one_spec.rb → association/referenced/has_one/proxy_spec.rb} +9 -109
- data/spec/mongoid/association/referenced/has_one_spec.rb +1244 -0
- data/spec/mongoid/{relations → association}/reflections_spec.rb +1 -12
- data/spec/mongoid/{relations/synchronization_spec.rb → association/syncable_spec.rb} +4 -2
- data/spec/mongoid/{relations → association}/touchable_spec.rb +19 -1
- data/spec/mongoid/{relations_spec.rb → association_spec.rb} +1 -1
- data/spec/mongoid/atomic/modifiers_spec.rb +17 -17
- data/spec/mongoid/atomic_spec.rb +17 -17
- data/spec/mongoid/attributes/nested_spec.rb +14 -12
- data/spec/mongoid/attributes/readonly_spec.rb +80 -125
- data/spec/mongoid/clients/factory_spec.rb +28 -52
- data/spec/mongoid/clients/options_spec.rb +65 -69
- data/spec/mongoid/config_spec.rb +24 -0
- data/spec/mongoid/contextual/geo_near_spec.rb +0 -1
- data/spec/mongoid/contextual/mongo_spec.rb +4 -112
- data/spec/mongoid/criteria/modifiable_spec.rb +183 -60
- data/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb +3 -3
- data/spec/mongoid/criteria/queryable/pipeline_spec.rb +12 -0
- data/spec/mongoid/criteria/queryable/selectable_spec.rb +6 -74
- data/spec/mongoid/criteria/queryable/selector_spec.rb +2 -2
- data/spec/mongoid/criteria/scopable_spec.rb +0 -81
- data/spec/mongoid/criteria_spec.rb +16 -19
- data/spec/mongoid/document_spec.rb +2 -56
- data/spec/mongoid/extensions/array_spec.rb +11 -15
- data/spec/mongoid/extensions/big_decimal_spec.rb +9 -9
- data/spec/mongoid/extensions/object_spec.rb +7 -11
- data/spec/mongoid/extensions/range_spec.rb +7 -0
- data/spec/mongoid/extensions/regexp_spec.rb +0 -23
- data/spec/mongoid/extensions/string_spec.rb +7 -35
- data/spec/mongoid/factory_spec.rb +18 -11
- data/spec/mongoid/fields/foreign_key_spec.rb +24 -32
- data/spec/mongoid/fields_spec.rb +2 -2
- data/spec/mongoid/findable_spec.rb +1 -1
- data/spec/mongoid/indexable_spec.rb +18 -8
- data/spec/mongoid/interceptable_spec.rb +21 -2
- data/spec/mongoid/matchable_spec.rb +1 -26
- data/spec/mongoid/persistable/deletable_spec.rb +0 -19
- data/spec/mongoid/persistable/destroyable_spec.rb +0 -19
- data/spec/mongoid/persistable/incrementable_spec.rb +6 -6
- data/spec/mongoid/persistable/savable_spec.rb +3 -3
- data/spec/mongoid/persistable/settable_spec.rb +1 -35
- data/spec/mongoid/persistable/updatable_spec.rb +2 -2
- data/spec/mongoid/persistable_spec.rb +16 -16
- data/spec/mongoid/persistence_context_spec.rb +0 -14
- data/spec/mongoid/positional_spec.rb +10 -10
- data/spec/mongoid/query_cache_spec.rb +18 -87
- data/spec/mongoid/relations/proxy_spec.rb +124 -124
- data/spec/mongoid/scopable_spec.rb +0 -13
- data/spec/mongoid/threaded_spec.rb +0 -68
- data/spec/mongoid/validatable/associated_spec.rb +1 -1
- data/spec/mongoid/validatable/presence_spec.rb +7 -6
- data/spec/mongoid/validatable_spec.rb +1 -1
- data/spec/spec_helper.rb +7 -83
- metadata +586 -611
- metadata.gz.sig +2 -5
- data/lib/mongoid/clients/sessions.rb +0 -113
- data/lib/mongoid/errors/invalid_session_use.rb +0 -24
- data/lib/mongoid/matchable/nor.rb +0 -37
- data/lib/mongoid/railties/controller_runtime.rb +0 -86
- data/lib/mongoid/relations.rb +0 -148
- data/lib/mongoid/relations/accessors.rb +0 -267
- data/lib/mongoid/relations/auto_save.rb +0 -94
- data/lib/mongoid/relations/bindings.rb +0 -9
- data/lib/mongoid/relations/bindings/embedded/in.rb +0 -59
- data/lib/mongoid/relations/bindings/referenced/in.rb +0 -65
- data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +0 -70
- data/lib/mongoid/relations/builder.rb +0 -57
- data/lib/mongoid/relations/builders.rb +0 -106
- data/lib/mongoid/relations/builders/nested_attributes/many.rb +0 -199
- data/lib/mongoid/relations/builders/referenced/in.rb +0 -26
- data/lib/mongoid/relations/builders/referenced/many.rb +0 -26
- data/lib/mongoid/relations/builders/referenced/many_to_many.rb +0 -39
- data/lib/mongoid/relations/builders/referenced/one.rb +0 -26
- data/lib/mongoid/relations/cascading.rb +0 -56
- data/lib/mongoid/relations/cascading/delete.rb +0 -44
- data/lib/mongoid/relations/cascading/destroy.rb +0 -43
- data/lib/mongoid/relations/cascading/nullify.rb +0 -35
- data/lib/mongoid/relations/cascading/restrict.rb +0 -39
- data/lib/mongoid/relations/conversions.rb +0 -34
- data/lib/mongoid/relations/counter_cache.rb +0 -160
- data/lib/mongoid/relations/cyclic.rb +0 -107
- data/lib/mongoid/relations/eager/base.rb +0 -153
- data/lib/mongoid/relations/eager/belongs_to.rb +0 -31
- data/lib/mongoid/relations/eager/has_and_belongs_to_many.rb +0 -47
- data/lib/mongoid/relations/eager/has_many.rb +0 -38
- data/lib/mongoid/relations/eager/has_one.rb +0 -30
- data/lib/mongoid/relations/embedded/in.rb +0 -241
- data/lib/mongoid/relations/embedded/many.rb +0 -683
- data/lib/mongoid/relations/embedded/one.rb +0 -235
- data/lib/mongoid/relations/macros.rb +0 -367
- data/lib/mongoid/relations/metadata.rb +0 -1179
- data/lib/mongoid/relations/nested_builder.rb +0 -74
- data/lib/mongoid/relations/options.rb +0 -49
- data/lib/mongoid/relations/polymorphic.rb +0 -39
- data/lib/mongoid/relations/referenced/in.rb +0 -304
- data/lib/mongoid/relations/referenced/many.rb +0 -812
- data/lib/mongoid/relations/referenced/many_to_many.rb +0 -479
- data/lib/mongoid/relations/referenced/one.rb +0 -290
- data/lib/mongoid/relations/synchronization.rb +0 -169
- data/lib/mongoid/relations/targets.rb +0 -2
- data/lib/mongoid/relations/targets/enumerable.rb +0 -493
- data/lib/mongoid/relations/touchable.rb +0 -97
- data/spec/app/models/array_field.rb +0 -7
- data/spec/app/models/delegating_patient.rb +0 -16
- data/spec/integration/document_spec.rb +0 -22
- data/spec/mongoid/clients/sessions_spec.rb +0 -334
- data/spec/mongoid/fields/internal/foreign_keys/array_spec.rb +0 -184
- data/spec/mongoid/fields/internal/foreign_keys/object_spec.rb +0 -201
- data/spec/mongoid/matchable/nor_spec.rb +0 -209
- data/spec/mongoid/relations/builders/referenced/many_spec.rb +0 -137
- data/spec/mongoid/relations/builders/referenced/many_to_many_spec.rb +0 -178
- data/spec/mongoid/relations/builders/referenced/one_spec.rb +0 -111
- data/spec/mongoid/relations/cascading/delete_spec.rb +0 -101
- data/spec/mongoid/relations/cascading/destroy_spec.rb +0 -47
- data/spec/mongoid/relations/cascading/nullify_spec.rb +0 -32
- data/spec/mongoid/relations/cascading/restrict_spec.rb +0 -68
- data/spec/mongoid/relations/cascading_spec.rb +0 -355
- data/spec/mongoid/relations/constraint_spec.rb +0 -75
- data/spec/mongoid/relations/conversions_spec.rb +0 -128
- data/spec/mongoid/relations/metadata_spec.rb +0 -1985
- data/spec/mongoid/relations/options_spec.rb +0 -35
- data/spec/rails/controller_extension/controller_runtime_spec.rb +0 -110
- data/spec/support/cluster_config.rb +0 -158
- data/spec/support/constraints.rb +0 -101
- data/spec/support/macros.rb +0 -20
- data/spec/support/session_registry.rb +0 -50
- data/spec/support/spec_config.rb +0 -42
@@ -1,812 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
module Mongoid
|
3
|
-
module Relations
|
4
|
-
module Referenced
|
5
|
-
|
6
|
-
# This class defines the behaviour for all relations that are a
|
7
|
-
# one-to-many between documents in different collections.
|
8
|
-
class Many < Relations::Many
|
9
|
-
|
10
|
-
delegate :count, to: :criteria
|
11
|
-
delegate :first, :in_memory, :last, :reset, :uniq, to: :target
|
12
|
-
|
13
|
-
# The allowed options when defining this relation.
|
14
|
-
#
|
15
|
-
# @return [ Array<Symbol> ] The allowed options when defining this relation.
|
16
|
-
#
|
17
|
-
# @since 6.0.0
|
18
|
-
VALID_OPTIONS = [
|
19
|
-
:after_add,
|
20
|
-
:after_remove,
|
21
|
-
:as,
|
22
|
-
:autosave,
|
23
|
-
:before_add,
|
24
|
-
:before_remove,
|
25
|
-
:dependent,
|
26
|
-
:foreign_key,
|
27
|
-
:order,
|
28
|
-
:primary_key
|
29
|
-
].freeze
|
30
|
-
|
31
|
-
# Appends a document or array of documents to the relation. Will set
|
32
|
-
# the parent and update the index in the process.
|
33
|
-
#
|
34
|
-
# @example Append a document.
|
35
|
-
# person.posts << post
|
36
|
-
#
|
37
|
-
# @example Push a document.
|
38
|
-
# person.posts.push(post)
|
39
|
-
#
|
40
|
-
# @example Concat with other documents.
|
41
|
-
# person.posts.concat([ post_one, post_two ])
|
42
|
-
#
|
43
|
-
# @param [ Document, Array<Document> ] args Any number of documents.
|
44
|
-
#
|
45
|
-
# @return [ Array<Document> ] The loaded docs.
|
46
|
-
#
|
47
|
-
# @since 2.0.0.beta.1
|
48
|
-
def <<(*args)
|
49
|
-
docs = args.flatten
|
50
|
-
return concat(docs) if docs.size > 1
|
51
|
-
if doc = docs.first
|
52
|
-
append(doc)
|
53
|
-
doc.save if persistable? && !_assigning? && !doc.validated?
|
54
|
-
end
|
55
|
-
self
|
56
|
-
end
|
57
|
-
alias :push :<<
|
58
|
-
|
59
|
-
# Appends an array of documents to the relation. Performs a batch
|
60
|
-
# insert of the documents instead of persisting one at a time.
|
61
|
-
#
|
62
|
-
# @example Concat with other documents.
|
63
|
-
# person.posts.concat([ post_one, post_two ])
|
64
|
-
#
|
65
|
-
# @param [ Array<Document> ] documents The docs to add.
|
66
|
-
#
|
67
|
-
# @return [ Array<Document> ] The documents.
|
68
|
-
#
|
69
|
-
# @since 2.4.0
|
70
|
-
def concat(documents)
|
71
|
-
docs, inserts = [], []
|
72
|
-
documents.each do |doc|
|
73
|
-
next unless doc
|
74
|
-
append(doc)
|
75
|
-
save_or_delay(doc, docs, inserts) if persistable?
|
76
|
-
end
|
77
|
-
persist_delayed(docs, inserts)
|
78
|
-
self
|
79
|
-
end
|
80
|
-
|
81
|
-
# Build a new document from the attributes and append it to this
|
82
|
-
# relation without saving.
|
83
|
-
#
|
84
|
-
# @example Build a new document on the relation.
|
85
|
-
# person.posts.build(:title => "A new post")
|
86
|
-
#
|
87
|
-
# @overload build(attributes = {}, options = {}, type = nil)
|
88
|
-
# @param [ Hash ] attributes The attributes of the new document.
|
89
|
-
# @param [ Hash ] options The scoped assignment options.
|
90
|
-
# @param [ Class ] type The optional subclass to build.
|
91
|
-
#
|
92
|
-
# @overload build(attributes = {}, type = nil)
|
93
|
-
# @param [ Hash ] attributes The attributes of the new document.
|
94
|
-
# @param [ Class ] type The optional subclass to build.
|
95
|
-
#
|
96
|
-
# @return [ Document ] The new document.
|
97
|
-
#
|
98
|
-
# @since 2.0.0.beta.1
|
99
|
-
def build(attributes = {}, type = nil)
|
100
|
-
doc = Factory.build(type || klass, attributes)
|
101
|
-
append(doc)
|
102
|
-
doc.apply_post_processed_defaults
|
103
|
-
yield(doc) if block_given?
|
104
|
-
doc.run_callbacks(:build) { doc }
|
105
|
-
doc
|
106
|
-
end
|
107
|
-
alias :new :build
|
108
|
-
|
109
|
-
# Delete the document from the relation. This will set the foreign key
|
110
|
-
# on the document to nil. If the dependent options on the relation are
|
111
|
-
# :delete or :destroy the appropriate removal will occur.
|
112
|
-
#
|
113
|
-
# @example Delete the document.
|
114
|
-
# person.posts.delete(post)
|
115
|
-
#
|
116
|
-
# @param [ Document ] document The document to remove.
|
117
|
-
#
|
118
|
-
# @return [ Document ] The matching document.
|
119
|
-
#
|
120
|
-
# @since 2.1.0
|
121
|
-
def delete(document)
|
122
|
-
execute_callback :before_remove, document
|
123
|
-
target.delete(document) do |doc|
|
124
|
-
if doc
|
125
|
-
unbind_one(doc)
|
126
|
-
cascade!(doc) if !_assigning?
|
127
|
-
end
|
128
|
-
execute_callback :after_remove, doc
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
# Deletes all related documents from the database given the supplied
|
133
|
-
# conditions.
|
134
|
-
#
|
135
|
-
# @example Delete all documents in the relation.
|
136
|
-
# person.posts.delete_all
|
137
|
-
#
|
138
|
-
# @example Conditonally delete all documents in the relation.
|
139
|
-
# person.posts.delete_all({ :title => "Testing" })
|
140
|
-
#
|
141
|
-
# @param [ Hash ] conditions Optional conditions to delete with.
|
142
|
-
#
|
143
|
-
# @return [ Integer ] The number of documents deleted.
|
144
|
-
#
|
145
|
-
# @since 2.0.0.beta.1
|
146
|
-
def delete_all(conditions = nil)
|
147
|
-
remove_all(conditions, :delete_all)
|
148
|
-
end
|
149
|
-
|
150
|
-
# Destroys all related documents from the database given the supplied
|
151
|
-
# conditions.
|
152
|
-
#
|
153
|
-
# @example Destroy all documents in the relation.
|
154
|
-
# person.posts.destroy_all
|
155
|
-
#
|
156
|
-
# @example Conditonally destroy all documents in the relation.
|
157
|
-
# person.posts.destroy_all({ :title => "Testing" })
|
158
|
-
#
|
159
|
-
# @param [ Hash ] conditions Optional conditions to destroy with.
|
160
|
-
#
|
161
|
-
# @return [ Integer ] The number of documents destroyd.
|
162
|
-
#
|
163
|
-
# @since 2.0.0.beta.1
|
164
|
-
def destroy_all(conditions = nil)
|
165
|
-
remove_all(conditions, :destroy_all)
|
166
|
-
end
|
167
|
-
|
168
|
-
# Iterate over each document in the relation and yield to the provided
|
169
|
-
# block.
|
170
|
-
#
|
171
|
-
# @note This will load the entire relation into memory.
|
172
|
-
#
|
173
|
-
# @example Iterate over the documents.
|
174
|
-
# person.posts.each do |post|
|
175
|
-
# post.save
|
176
|
-
# end
|
177
|
-
#
|
178
|
-
# @return [ Array<Document> ] The loaded docs.
|
179
|
-
#
|
180
|
-
# @since 2.1.0
|
181
|
-
def each
|
182
|
-
if block_given?
|
183
|
-
target.each { |doc| yield(doc) }
|
184
|
-
else
|
185
|
-
to_enum
|
186
|
-
end
|
187
|
-
end
|
188
|
-
|
189
|
-
# Determine if any documents in this relation exist in the database.
|
190
|
-
#
|
191
|
-
# @example Are there persisted documents?
|
192
|
-
# person.posts.exists?
|
193
|
-
#
|
194
|
-
# @return [ true, false ] True is persisted documents exist, false if not.
|
195
|
-
def exists?
|
196
|
-
criteria.exists?
|
197
|
-
end
|
198
|
-
|
199
|
-
# Find the matchind document on the association, either based on id or
|
200
|
-
# conditions.
|
201
|
-
#
|
202
|
-
# @example Find by an id.
|
203
|
-
# person.posts.find(BSON::ObjectId.new)
|
204
|
-
#
|
205
|
-
# @example Find by multiple ids.
|
206
|
-
# person.posts.find([ BSON::ObjectId.new, BSON::ObjectId.new ])
|
207
|
-
#
|
208
|
-
# @note This will keep matching documents in memory for iteration
|
209
|
-
# later.
|
210
|
-
#
|
211
|
-
# @param [ BSON::ObjectId, Array<BSON::ObjectId> ] args The ids.
|
212
|
-
#
|
213
|
-
# @return [ Document, Criteria ] The matching document(s).
|
214
|
-
#
|
215
|
-
# @since 2.0.0.beta.1
|
216
|
-
def find(*args)
|
217
|
-
matching = criteria.find(*args)
|
218
|
-
Array(matching).each { |doc| target.push(doc) }
|
219
|
-
matching
|
220
|
-
end
|
221
|
-
|
222
|
-
# Instantiate a new references_many relation. Will set the foreign key
|
223
|
-
# and the base on the inverse object.
|
224
|
-
#
|
225
|
-
# @example Create the new relation.
|
226
|
-
# Referenced::Many.new(base, target, metadata)
|
227
|
-
#
|
228
|
-
# @param [ Document ] base The document this relation hangs off of.
|
229
|
-
# @param [ Array<Document> ] target The target of the relation.
|
230
|
-
# @param [ Metadata ] metadata The relation's metadata.
|
231
|
-
#
|
232
|
-
# @since 2.0.0.beta.1
|
233
|
-
def initialize(base, target, metadata)
|
234
|
-
init(base, Targets::Enumerable.new(target), metadata) do
|
235
|
-
raise_mixed if klass.embedded? && !klass.cyclic?
|
236
|
-
end
|
237
|
-
end
|
238
|
-
|
239
|
-
# Removes all associations between the base document and the target
|
240
|
-
# documents by deleting the foreign keys and the references, orphaning
|
241
|
-
# the target documents in the process.
|
242
|
-
#
|
243
|
-
# @example Nullify the relation.
|
244
|
-
# person.posts.nullify
|
245
|
-
#
|
246
|
-
# @since 2.0.0.rc.1
|
247
|
-
def nullify
|
248
|
-
criteria.update_all(foreign_key => nil)
|
249
|
-
target.clear do |doc|
|
250
|
-
unbind_one(doc)
|
251
|
-
doc.changed_attributes.delete(foreign_key)
|
252
|
-
end
|
253
|
-
end
|
254
|
-
alias :nullify_all :nullify
|
255
|
-
|
256
|
-
# Clear the relation. Will delete the documents from the db if they are
|
257
|
-
# already persisted.
|
258
|
-
#
|
259
|
-
# @example Clear the relation.
|
260
|
-
# person.posts.clear
|
261
|
-
#
|
262
|
-
# @return [ Many ] The relation emptied.
|
263
|
-
#
|
264
|
-
# @since 2.0.0.beta.1
|
265
|
-
def purge
|
266
|
-
unless __metadata.destructive?
|
267
|
-
nullify
|
268
|
-
else
|
269
|
-
after_remove_error = nil
|
270
|
-
criteria.delete_all
|
271
|
-
many = target.clear do |doc|
|
272
|
-
execute_callback :before_remove, doc
|
273
|
-
unbind_one(doc)
|
274
|
-
doc.destroyed = true
|
275
|
-
begin
|
276
|
-
execute_callback :after_remove, doc
|
277
|
-
rescue => e
|
278
|
-
after_remove_error = e
|
279
|
-
end
|
280
|
-
end
|
281
|
-
raise after_remove_error if after_remove_error
|
282
|
-
many
|
283
|
-
end
|
284
|
-
end
|
285
|
-
alias :clear :purge
|
286
|
-
|
287
|
-
# Substitutes the supplied target documents for the existing documents
|
288
|
-
# in the relation. If the new target is nil, perform the necessary
|
289
|
-
# deletion.
|
290
|
-
#
|
291
|
-
# @example Replace the relation.
|
292
|
-
# person.posts.substitute([ new_post ])
|
293
|
-
#
|
294
|
-
# @param [ Array<Document> ] replacement The replacement target.
|
295
|
-
#
|
296
|
-
# @return [ Many ] The relation.
|
297
|
-
#
|
298
|
-
# @since 2.0.0.rc.1
|
299
|
-
def substitute(replacement)
|
300
|
-
if replacement
|
301
|
-
new_docs, docs = replacement.compact, []
|
302
|
-
new_ids = new_docs.map { |doc| doc._id }
|
303
|
-
remove_not_in(new_ids)
|
304
|
-
new_docs.each do |doc|
|
305
|
-
docs.push(doc) if doc.send(foreign_key) != base._id
|
306
|
-
end
|
307
|
-
concat(docs)
|
308
|
-
else
|
309
|
-
purge
|
310
|
-
end
|
311
|
-
self
|
312
|
-
end
|
313
|
-
|
314
|
-
# Get a criteria for the documents without the default scoping
|
315
|
-
# applied.
|
316
|
-
#
|
317
|
-
# @example Get the unscoped criteria.
|
318
|
-
# person.posts.unscoped
|
319
|
-
#
|
320
|
-
# @return [ Criteria ] The unscoped criteria.
|
321
|
-
#
|
322
|
-
# @since 2.4.0
|
323
|
-
def unscoped
|
324
|
-
klass.unscoped.where(
|
325
|
-
foreign_key => Conversions.flag(base._id, __metadata)
|
326
|
-
)
|
327
|
-
end
|
328
|
-
|
329
|
-
private
|
330
|
-
|
331
|
-
# Appends the document to the target array, updating the index on the
|
332
|
-
# document at the same time.
|
333
|
-
#
|
334
|
-
# @example Append the document to the relation.
|
335
|
-
# relation.append(document)
|
336
|
-
#
|
337
|
-
# @param [ Document ] document The document to append to the target.
|
338
|
-
#
|
339
|
-
# @since 2.0.0.rc.1
|
340
|
-
def append(document)
|
341
|
-
with_add_callbacks(document, already_related?(document)) do
|
342
|
-
target.push(document)
|
343
|
-
characterize_one(document)
|
344
|
-
bind_one(document)
|
345
|
-
end
|
346
|
-
end
|
347
|
-
|
348
|
-
# Execute before/after add callbacks around the block unless the objects
|
349
|
-
# already have a persisted relation.
|
350
|
-
#
|
351
|
-
# @example Execute before/after add callbacks around the block.
|
352
|
-
# relation.with_add_callbacks(document, false)
|
353
|
-
#
|
354
|
-
# @param [ Document ] document The document to append to the target.
|
355
|
-
# @param [ true, false ] already_related Whether the document is already related
|
356
|
-
# to the target.
|
357
|
-
#
|
358
|
-
# @since 5.1.0
|
359
|
-
def with_add_callbacks(document, already_related)
|
360
|
-
execute_callback :before_add, document unless already_related
|
361
|
-
yield
|
362
|
-
execute_callback :after_add, document unless already_related
|
363
|
-
end
|
364
|
-
|
365
|
-
# Whether the document and the base already have a persisted relation.
|
366
|
-
#
|
367
|
-
# @example Is the document already related to the base.
|
368
|
-
# relation.already_related?(document)
|
369
|
-
#
|
370
|
-
# @param [ Document ] document The document to possibly append to the target.
|
371
|
-
#
|
372
|
-
# @return [ true, false ] Whether the document is already related to the base and the
|
373
|
-
# relation is persisted.
|
374
|
-
#
|
375
|
-
# @since 5.1.0
|
376
|
-
def already_related?(document)
|
377
|
-
document.persisted? &&
|
378
|
-
document.__metadata &&
|
379
|
-
document.respond_to?(document.__metadata.foreign_key) &&
|
380
|
-
document.__send__(document.__metadata.foreign_key) == base.id
|
381
|
-
end
|
382
|
-
|
383
|
-
# Instantiate the binding associated with this relation.
|
384
|
-
#
|
385
|
-
# @example Get the binding.
|
386
|
-
# relation.binding([ address ])
|
387
|
-
#
|
388
|
-
# @return [ Binding ] The binding.
|
389
|
-
#
|
390
|
-
# @since 2.0.0.rc.1
|
391
|
-
def binding
|
392
|
-
Bindings::Referenced::Many.new(base, target, __metadata)
|
393
|
-
end
|
394
|
-
|
395
|
-
# Get the collection of the relation in question.
|
396
|
-
#
|
397
|
-
# @example Get the collection of the relation.
|
398
|
-
# relation.collection
|
399
|
-
#
|
400
|
-
# @return [ Collection ] The collection of the relation.
|
401
|
-
#
|
402
|
-
# @since 2.0.2
|
403
|
-
def collection
|
404
|
-
klass.collection
|
405
|
-
end
|
406
|
-
|
407
|
-
# Returns the criteria object for the target class with its documents set
|
408
|
-
# to target.
|
409
|
-
#
|
410
|
-
# @example Get a criteria for the relation.
|
411
|
-
# relation.criteria
|
412
|
-
#
|
413
|
-
# @return [ Criteria ] A new criteria.
|
414
|
-
#
|
415
|
-
# @since 2.0.0.beta.1
|
416
|
-
def criteria
|
417
|
-
Many.criteria(
|
418
|
-
__metadata,
|
419
|
-
Conversions.flag(base.send(__metadata.primary_key), __metadata),
|
420
|
-
base.class
|
421
|
-
)
|
422
|
-
end
|
423
|
-
|
424
|
-
# Perform the necessary cascade operations for documents that just got
|
425
|
-
# deleted or nullified.
|
426
|
-
#
|
427
|
-
# @example Cascade the change.
|
428
|
-
# relation.cascade!(document)
|
429
|
-
#
|
430
|
-
# @param [ Document ] document The document to cascade on.
|
431
|
-
#
|
432
|
-
# @return [ true, false ] If the metadata is destructive.
|
433
|
-
#
|
434
|
-
# @since 2.1.0
|
435
|
-
def cascade!(document)
|
436
|
-
if persistable?
|
437
|
-
if __metadata.destructive?
|
438
|
-
document.send(__metadata.dependent)
|
439
|
-
else
|
440
|
-
document.save
|
441
|
-
end
|
442
|
-
end
|
443
|
-
end
|
444
|
-
|
445
|
-
# If the target array does not respond to the supplied method then try to
|
446
|
-
# find a named scope or criteria on the class and send the call there.
|
447
|
-
#
|
448
|
-
# If the method exists on the array, use the default proxy behavior.
|
449
|
-
#
|
450
|
-
# @param [ Symbol, String ] name The name of the method.
|
451
|
-
# @param [ Array ] args The method args
|
452
|
-
# @param [ Proc ] block Optional block to pass.
|
453
|
-
#
|
454
|
-
# @return [ Criteria, Object ] A Criteria or return value from the target.
|
455
|
-
#
|
456
|
-
# @since 2.0.0.beta.1
|
457
|
-
def method_missing(name, *args, &block)
|
458
|
-
if target.respond_to?(name)
|
459
|
-
target.send(name, *args, &block)
|
460
|
-
else
|
461
|
-
klass.send(:with_scope, criteria) do
|
462
|
-
criteria.public_send(name, *args, &block)
|
463
|
-
end
|
464
|
-
end
|
465
|
-
end
|
466
|
-
|
467
|
-
# Persist all the delayed batch inserts.
|
468
|
-
#
|
469
|
-
# @api private
|
470
|
-
#
|
471
|
-
# @example Persist the delayed batch inserts.
|
472
|
-
# relation.persist_delayed([ doc ])
|
473
|
-
#
|
474
|
-
# @param [ Array<Document> ] docs The delayed inserts.
|
475
|
-
# @param [ Array<Hash> ] inserts The raw insert document.
|
476
|
-
#
|
477
|
-
# @since 3.0.0
|
478
|
-
def persist_delayed(docs, inserts)
|
479
|
-
unless docs.empty?
|
480
|
-
collection.insert_many(inserts, session: _session)
|
481
|
-
docs.each do |doc|
|
482
|
-
doc.new_record = false
|
483
|
-
doc.run_after_callbacks(:create, :save)
|
484
|
-
doc.post_persist
|
485
|
-
end
|
486
|
-
end
|
487
|
-
end
|
488
|
-
|
489
|
-
# Are we able to persist this relation?
|
490
|
-
#
|
491
|
-
# @example Can we persist the relation?
|
492
|
-
# relation.persistable?
|
493
|
-
#
|
494
|
-
# @return [ true, false ] If the relation is persistable.
|
495
|
-
#
|
496
|
-
# @since 2.1.0
|
497
|
-
def persistable?
|
498
|
-
!_binding? && (_creating? || base.persisted? && !_building?)
|
499
|
-
end
|
500
|
-
|
501
|
-
# Deletes all related documents from the database given the supplied
|
502
|
-
# conditions.
|
503
|
-
#
|
504
|
-
# @example Delete all documents in the relation.
|
505
|
-
# person.posts.delete_all
|
506
|
-
#
|
507
|
-
# @example Conditonally delete all documents in the relation.
|
508
|
-
# person.posts.delete_all({ :title => "Testing" })
|
509
|
-
#
|
510
|
-
# @param [ Hash ] conditions Optional conditions to delete with.
|
511
|
-
# @param [ Symbol ] method The deletion method to call.
|
512
|
-
#
|
513
|
-
# @return [ Integer ] The number of documents deleted.
|
514
|
-
#
|
515
|
-
# @since 2.1.0
|
516
|
-
def remove_all(conditions = nil, method = :delete_all)
|
517
|
-
selector = conditions || {}
|
518
|
-
removed = klass.send(method, selector.merge!(criteria.selector))
|
519
|
-
target.delete_if do |doc|
|
520
|
-
if doc._matches?(selector)
|
521
|
-
unbind_one(doc) and true
|
522
|
-
end
|
523
|
-
end
|
524
|
-
removed
|
525
|
-
end
|
526
|
-
|
527
|
-
# Remove all the documents in the proxy that do not have the provided
|
528
|
-
# ids.
|
529
|
-
#
|
530
|
-
# @example Remove all documents without the ids.
|
531
|
-
# proxy.remove_not_in([ id ])
|
532
|
-
#
|
533
|
-
# @param [ Array<Object> ] ids The ids.
|
534
|
-
#
|
535
|
-
# @since 2.4.0
|
536
|
-
def remove_not_in(ids)
|
537
|
-
removed = criteria.not_in(_id: ids)
|
538
|
-
if __metadata.destructive?
|
539
|
-
removed.delete_all
|
540
|
-
else
|
541
|
-
removed.update_all(foreign_key => nil)
|
542
|
-
end
|
543
|
-
in_memory.each do |doc|
|
544
|
-
if !ids.include?(doc._id)
|
545
|
-
unbind_one(doc)
|
546
|
-
target.delete(doc)
|
547
|
-
if __metadata.destructive?
|
548
|
-
doc.destroyed = true
|
549
|
-
end
|
550
|
-
end
|
551
|
-
end
|
552
|
-
end
|
553
|
-
|
554
|
-
# Save a persisted document immediately or delay a new document for
|
555
|
-
# batch insert.
|
556
|
-
#
|
557
|
-
# @api private
|
558
|
-
#
|
559
|
-
# @example Save or delay the document.
|
560
|
-
# relation.save_or_delay(doc, [])
|
561
|
-
#
|
562
|
-
# @param [ Document ] doc The document.
|
563
|
-
# @param [ Array<Document> ] inserts The inserts.
|
564
|
-
#
|
565
|
-
# @since 3.0.0
|
566
|
-
def save_or_delay(doc, docs, inserts)
|
567
|
-
if doc.new_record? && doc.valid?(:create)
|
568
|
-
doc.run_before_callbacks(:save, :create)
|
569
|
-
docs.push(doc)
|
570
|
-
inserts.push(doc.send(:as_attributes))
|
571
|
-
else
|
572
|
-
doc.save
|
573
|
-
end
|
574
|
-
end
|
575
|
-
|
576
|
-
class << self
|
577
|
-
|
578
|
-
# Return the builder that is responsible for generating the documents
|
579
|
-
# that will be used by this relation.
|
580
|
-
#
|
581
|
-
# @example Get the builder.
|
582
|
-
# Referenced::Many.builder(meta, object)
|
583
|
-
#
|
584
|
-
# @param [ Document ] base The base document.
|
585
|
-
# @param [ Metadata ] meta The metadata of the relation.
|
586
|
-
# @param [ Document, Hash ] object A document or attributes to build
|
587
|
-
# with.
|
588
|
-
#
|
589
|
-
# @return [ Builder ] A new builder object.
|
590
|
-
#
|
591
|
-
# @since 2.0.0.rc.1
|
592
|
-
def builder(base, meta, object)
|
593
|
-
Builders::Referenced::Many.new(base, meta, object || [])
|
594
|
-
end
|
595
|
-
|
596
|
-
# Get the standard criteria used for querying this relation.
|
597
|
-
#
|
598
|
-
# @example Get the criteria.
|
599
|
-
# Proxy.criteria(meta, id, Model)
|
600
|
-
#
|
601
|
-
# @param [ Metadata ] metadata The metadata.
|
602
|
-
# @param [ Object ] object The value of the foreign key.
|
603
|
-
# @param [ Class ] type The optional type.
|
604
|
-
#
|
605
|
-
# @return [ Criteria ] The criteria.
|
606
|
-
#
|
607
|
-
# @since 2.1.0
|
608
|
-
def criteria(metadata, object, type = nil)
|
609
|
-
apply_ordering(
|
610
|
-
with_polymorphic_criterion(
|
611
|
-
metadata.klass.where(metadata.foreign_key => object),
|
612
|
-
metadata,
|
613
|
-
type
|
614
|
-
),
|
615
|
-
metadata
|
616
|
-
)
|
617
|
-
end
|
618
|
-
|
619
|
-
def eager_load_klass
|
620
|
-
Relations::Eager::HasMany
|
621
|
-
end
|
622
|
-
|
623
|
-
# Returns true if the relation is an embedded one. In this case
|
624
|
-
# always false.
|
625
|
-
#
|
626
|
-
# @example Is this relation embedded?
|
627
|
-
# Referenced::Many.embedded?
|
628
|
-
#
|
629
|
-
# @return [ false ] Always false.
|
630
|
-
#
|
631
|
-
# @since 2.0.0.rc.1
|
632
|
-
def embedded?
|
633
|
-
false
|
634
|
-
end
|
635
|
-
|
636
|
-
# Get the foreign key for the provided name.
|
637
|
-
#
|
638
|
-
# @example Get the foreign key.
|
639
|
-
# Referenced::Many.foreign_key(:person)
|
640
|
-
#
|
641
|
-
# @param [ Symbol ] name The name.
|
642
|
-
#
|
643
|
-
# @return [ String ] The foreign key.
|
644
|
-
#
|
645
|
-
# @since 3.0.0
|
646
|
-
def foreign_key(name)
|
647
|
-
"#{name}#{foreign_key_suffix}"
|
648
|
-
end
|
649
|
-
|
650
|
-
# Get the default value for the foreign key.
|
651
|
-
#
|
652
|
-
# @example Get the default.
|
653
|
-
# Referenced::Many.foreign_key_default
|
654
|
-
#
|
655
|
-
# @return [ nil ] Always nil.
|
656
|
-
#
|
657
|
-
# @since 2.0.0.rc.1
|
658
|
-
def foreign_key_default
|
659
|
-
nil
|
660
|
-
end
|
661
|
-
|
662
|
-
# Returns the suffix of the foreign key field, either "_id" or "_ids".
|
663
|
-
#
|
664
|
-
# @example Get the suffix for the foreign key.
|
665
|
-
# Referenced::Many.foreign_key_suffix
|
666
|
-
#
|
667
|
-
# @return [ String ] "_id"
|
668
|
-
#
|
669
|
-
# @since 2.0.0.rc.1
|
670
|
-
def foreign_key_suffix
|
671
|
-
"_id"
|
672
|
-
end
|
673
|
-
|
674
|
-
# Returns the macro for this relation. Used mostly as a helper in
|
675
|
-
# reflection.
|
676
|
-
#
|
677
|
-
# @example Get the macro.
|
678
|
-
# Referenced::Many.macro
|
679
|
-
#
|
680
|
-
# @return [ Symbol ] :has_many
|
681
|
-
def macro
|
682
|
-
:has_many
|
683
|
-
end
|
684
|
-
|
685
|
-
# Return the nested builder that is responsible for generating the documents
|
686
|
-
# that will be used by this relation.
|
687
|
-
#
|
688
|
-
# @example Get the nested builder.
|
689
|
-
# Referenced::Many.builder(attributes, options)
|
690
|
-
#
|
691
|
-
# @param [ Metadata ] metadata The relation metadata.
|
692
|
-
# @param [ Hash ] attributes The attributes to build with.
|
693
|
-
# @param [ Hash ] options The options for the builder.
|
694
|
-
#
|
695
|
-
# @option options [ true, false ] :allow_destroy Can documents be
|
696
|
-
# deleted?
|
697
|
-
# @option options [ Integer ] :limit Max number of documents to
|
698
|
-
# create at once.
|
699
|
-
# @option options [ Proc, Symbol ] :reject_if If documents match this
|
700
|
-
# option then they are ignored.
|
701
|
-
# @option options [ true, false ] :update_only Only existing documents
|
702
|
-
# can be modified.
|
703
|
-
#
|
704
|
-
# @return [ NestedBuilder ] A newly instantiated nested builder object.
|
705
|
-
#
|
706
|
-
# @since 2.0.0.rc.1
|
707
|
-
def nested_builder(metadata, attributes, options)
|
708
|
-
Builders::NestedAttributes::Many.new(metadata, attributes, options)
|
709
|
-
end
|
710
|
-
|
711
|
-
# Get the path calculator for the supplied document.
|
712
|
-
#
|
713
|
-
# @example Get the path calculator.
|
714
|
-
# Proxy.path(document)
|
715
|
-
#
|
716
|
-
# @param [ Document ] document The document to calculate on.
|
717
|
-
#
|
718
|
-
# @return [ Root ] The root atomic path calculator.
|
719
|
-
#
|
720
|
-
# @since 2.1.0
|
721
|
-
def path(document)
|
722
|
-
Mongoid::Atomic::Paths::Root.new(document)
|
723
|
-
end
|
724
|
-
|
725
|
-
# Tells the caller if this relation is one that stores the foreign
|
726
|
-
# key on its own objects.
|
727
|
-
#
|
728
|
-
# @example Does this relation store a foreign key?
|
729
|
-
# Referenced::Many.stores_foreign_key?
|
730
|
-
#
|
731
|
-
# @return [ false ] Always false.
|
732
|
-
#
|
733
|
-
# @since 2.0.0.rc.1
|
734
|
-
def stores_foreign_key?
|
735
|
-
false
|
736
|
-
end
|
737
|
-
|
738
|
-
# Get the valid options allowed with this relation.
|
739
|
-
#
|
740
|
-
# @example Get the valid options.
|
741
|
-
# Relation.valid_options
|
742
|
-
#
|
743
|
-
# @return [ Array<Symbol> ] The valid options.
|
744
|
-
#
|
745
|
-
# @since 2.1.0
|
746
|
-
def valid_options
|
747
|
-
VALID_OPTIONS
|
748
|
-
end
|
749
|
-
|
750
|
-
# Get the default validation setting for the relation. Determines if
|
751
|
-
# by default a validates associated will occur.
|
752
|
-
#
|
753
|
-
# @example Get the validation default.
|
754
|
-
# Proxy.validation_default
|
755
|
-
#
|
756
|
-
# @return [ true, false ] The validation default.
|
757
|
-
#
|
758
|
-
# @since 2.1.9
|
759
|
-
def validation_default
|
760
|
-
true
|
761
|
-
end
|
762
|
-
|
763
|
-
private
|
764
|
-
|
765
|
-
# Decorate the criteria with polymorphic criteria, if applicable.
|
766
|
-
#
|
767
|
-
# @api private
|
768
|
-
#
|
769
|
-
# @example Get the criteria with polymorphic criterion.
|
770
|
-
# Proxy.with_polymorphic_criterion(criteria, metadata)
|
771
|
-
#
|
772
|
-
# @param [ Criteria ] criteria The criteria to decorate.
|
773
|
-
# @param [ Metadata ] metadata The metadata.
|
774
|
-
# @param [ Class ] type The optional type.
|
775
|
-
#
|
776
|
-
# @return [ Criteria ] The criteria.
|
777
|
-
#
|
778
|
-
# @since 3.0.0
|
779
|
-
def with_polymorphic_criterion(criteria, metadata, type = nil)
|
780
|
-
if metadata.polymorphic?
|
781
|
-
criteria.where(metadata.type => type.name)
|
782
|
-
else
|
783
|
-
criteria
|
784
|
-
end
|
785
|
-
end
|
786
|
-
|
787
|
-
# Decorate the criteria with inverse field criteria, if applicable.
|
788
|
-
#
|
789
|
-
# @api private
|
790
|
-
#
|
791
|
-
# @example Get the criteria with polymorphic criterion.
|
792
|
-
# Proxy.with_inverse_field_criterion(criteria, metadata)
|
793
|
-
#
|
794
|
-
# @param [ Criteria ] criteria The criteria to decorate.
|
795
|
-
# @param [ Metadata ] metadata The metadata.
|
796
|
-
#
|
797
|
-
# @return [ Criteria ] The criteria.
|
798
|
-
#
|
799
|
-
# @since 3.0.0
|
800
|
-
def with_inverse_field_criterion(criteria, metadata)
|
801
|
-
inverse_metadata = metadata.inverse_metadata(metadata.klass)
|
802
|
-
if inverse_metadata.try(:inverse_of)
|
803
|
-
criteria.any_in(inverse_metadata.inverse_of => [ metadata.name, nil ])
|
804
|
-
else
|
805
|
-
criteria
|
806
|
-
end
|
807
|
-
end
|
808
|
-
end
|
809
|
-
end
|
810
|
-
end
|
811
|
-
end
|
812
|
-
end
|