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,31 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
module Mongoid
|
3
|
-
module Relations
|
4
|
-
module Eager
|
5
|
-
|
6
|
-
class BelongsTo < Base
|
7
|
-
|
8
|
-
def preload
|
9
|
-
raise Errors::EagerLoad.new(@metadata.name) if @metadata.polymorphic?
|
10
|
-
|
11
|
-
@docs.each do |d|
|
12
|
-
set_relation(d, nil)
|
13
|
-
end
|
14
|
-
|
15
|
-
each_loaded_document do |doc|
|
16
|
-
id = doc.send(key)
|
17
|
-
set_on_parent(id, doc)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def group_by_key
|
22
|
-
@metadata.foreign_key
|
23
|
-
end
|
24
|
-
|
25
|
-
def key
|
26
|
-
@metadata.primary_key
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
@@ -1,47 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
module Mongoid
|
3
|
-
module Relations
|
4
|
-
module Eager
|
5
|
-
|
6
|
-
class HasAndBelongsToMany < Base
|
7
|
-
|
8
|
-
def preload
|
9
|
-
@docs.each do |d|
|
10
|
-
set_relation(d, [])
|
11
|
-
end
|
12
|
-
|
13
|
-
entries = {}
|
14
|
-
each_loaded_document do |doc|
|
15
|
-
entries[doc.send(key)] = doc
|
16
|
-
end
|
17
|
-
|
18
|
-
@docs.each do |d|
|
19
|
-
keys = d.send(group_by_key)
|
20
|
-
docs = entries.values_at(*keys).compact
|
21
|
-
set_relation(d, docs)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def keys_from_docs
|
26
|
-
keys = Set.new
|
27
|
-
@docs.each do |d|
|
28
|
-
keys += d.send(group_by_key)
|
29
|
-
end
|
30
|
-
keys.to_a
|
31
|
-
end
|
32
|
-
|
33
|
-
def set_relation(doc, element)
|
34
|
-
doc.__build__(@metadata.name, element, @metadata) unless doc.blank?
|
35
|
-
end
|
36
|
-
|
37
|
-
def group_by_key
|
38
|
-
@metadata.foreign_key
|
39
|
-
end
|
40
|
-
|
41
|
-
def key
|
42
|
-
@metadata.primary_key
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
@@ -1,38 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
module Mongoid
|
3
|
-
module Relations
|
4
|
-
module Eager
|
5
|
-
|
6
|
-
class HasMany < Base
|
7
|
-
|
8
|
-
def preload
|
9
|
-
@docs.each do |d|
|
10
|
-
set_relation(d, [])
|
11
|
-
end
|
12
|
-
|
13
|
-
entries = Hash.new { |hash, key| hash[key] = [] }
|
14
|
-
each_loaded_document do |doc|
|
15
|
-
fk = doc.send(key)
|
16
|
-
entries[fk] << doc
|
17
|
-
end
|
18
|
-
|
19
|
-
entries.each do |id, docs|
|
20
|
-
set_on_parent(id, docs)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def set_relation(doc, element)
|
25
|
-
doc.__build__(@metadata.name, element, @metadata) unless doc.blank?
|
26
|
-
end
|
27
|
-
|
28
|
-
def group_by_key
|
29
|
-
@metadata.primary_key
|
30
|
-
end
|
31
|
-
|
32
|
-
def key
|
33
|
-
@metadata.foreign_key
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
@@ -1,30 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
module Mongoid
|
3
|
-
module Relations
|
4
|
-
module Eager
|
5
|
-
|
6
|
-
class HasOne < Base
|
7
|
-
|
8
|
-
def preload
|
9
|
-
@docs.each do |d|
|
10
|
-
set_relation(d, nil)
|
11
|
-
end
|
12
|
-
|
13
|
-
each_loaded_document do |doc|
|
14
|
-
id = doc.send(key)
|
15
|
-
set_on_parent(id, doc)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def group_by_key
|
20
|
-
@metadata.primary_key
|
21
|
-
end
|
22
|
-
|
23
|
-
def key
|
24
|
-
@metadata.foreign_key
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
@@ -1,241 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
module Mongoid
|
3
|
-
module Relations
|
4
|
-
module Embedded
|
5
|
-
|
6
|
-
# This class defines the behaviour necessary to handle relations that are
|
7
|
-
# embedded within another relation, either as a single document or
|
8
|
-
# multiple documents.
|
9
|
-
class In < Relations::One
|
10
|
-
|
11
|
-
# The allowed options when defining this relation.
|
12
|
-
#
|
13
|
-
# @return [ Array<Symbol> ] The allowed options when defining this relation.
|
14
|
-
#
|
15
|
-
# @since 6.0.0
|
16
|
-
VALID_OPTIONS = [
|
17
|
-
:autobuild,
|
18
|
-
:cyclic,
|
19
|
-
:polymorphic,
|
20
|
-
:touch
|
21
|
-
].freeze
|
22
|
-
|
23
|
-
# Instantiate a new embedded_in relation.
|
24
|
-
#
|
25
|
-
# @example Create the new relation.
|
26
|
-
# Embedded::In.new(name, person, metadata)
|
27
|
-
#
|
28
|
-
# @param [ Document ] base The document the relation hangs off of.
|
29
|
-
# @param [ Document ] target The target (parent) of the relation.
|
30
|
-
# @param [ Metadata ] metadata The relation metadata.
|
31
|
-
#
|
32
|
-
# @return [ In ] The proxy.
|
33
|
-
def initialize(base, target, metadata)
|
34
|
-
init(base, target, metadata) do
|
35
|
-
characterize_one(target)
|
36
|
-
bind_one
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
# Substitutes the supplied target documents for the existing document
|
41
|
-
# in the relation.
|
42
|
-
#
|
43
|
-
# @example Substitute the new document.
|
44
|
-
# person.name.substitute(new_name)
|
45
|
-
#
|
46
|
-
# @param [ Document ] replacement A document to replace the target.
|
47
|
-
#
|
48
|
-
# @return [ Document, nil ] The relation or nil.
|
49
|
-
#
|
50
|
-
# @since 2.0.0.rc.1
|
51
|
-
def substitute(replacement)
|
52
|
-
unbind_one
|
53
|
-
unless replacement
|
54
|
-
base.delete if persistable?
|
55
|
-
return nil
|
56
|
-
end
|
57
|
-
base.new_record = true
|
58
|
-
self.target = replacement
|
59
|
-
bind_one
|
60
|
-
self
|
61
|
-
end
|
62
|
-
|
63
|
-
private
|
64
|
-
|
65
|
-
# Instantiate the binding associated with this relation.
|
66
|
-
#
|
67
|
-
# @example Get the binding.
|
68
|
-
# binding([ address ])
|
69
|
-
#
|
70
|
-
# @return [ Binding ] A binding object.
|
71
|
-
#
|
72
|
-
# @since 2.0.0.rc.1
|
73
|
-
def binding
|
74
|
-
Bindings::Embedded::In.new(base, target, __metadata)
|
75
|
-
end
|
76
|
-
|
77
|
-
# Characterize the document.
|
78
|
-
#
|
79
|
-
# @example Set the base metadata.
|
80
|
-
# relation.characterize_one(document)
|
81
|
-
#
|
82
|
-
# @param [ Document ] document The document to set the metadata on.
|
83
|
-
#
|
84
|
-
# @since 2.1.0
|
85
|
-
def characterize_one(document)
|
86
|
-
unless base.__metadata
|
87
|
-
base.__metadata = __metadata.inverse_metadata(document)
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
# Are we able to persist this relation?
|
92
|
-
#
|
93
|
-
# @example Can we persist the relation?
|
94
|
-
# relation.persistable?
|
95
|
-
#
|
96
|
-
# @return [ true, false ] If the relation is persistable.
|
97
|
-
#
|
98
|
-
# @since 2.1.0
|
99
|
-
def persistable?
|
100
|
-
target.persisted? && !_binding? && !_building?
|
101
|
-
end
|
102
|
-
|
103
|
-
class << self
|
104
|
-
|
105
|
-
# Return the builder that is responsible for generating the documents
|
106
|
-
# that will be used by this relation.
|
107
|
-
#
|
108
|
-
# @example Get the builder.
|
109
|
-
# Embedded::In.builder(meta, object, person)
|
110
|
-
#
|
111
|
-
# @param [ Document ] base The base document.
|
112
|
-
# @param [ Metadata ] meta The metadata of the relation.
|
113
|
-
# @param [ Document, Hash ] object A document or attributes to build with.
|
114
|
-
#
|
115
|
-
# @return [ Builder ] A newly instantiated builder object.
|
116
|
-
#
|
117
|
-
# @since 2.0.0.rc.1
|
118
|
-
def builder(base, meta, object)
|
119
|
-
Builders::Embedded::In.new(base, meta, object)
|
120
|
-
end
|
121
|
-
|
122
|
-
# Returns true if the relation is an embedded one. In this case
|
123
|
-
# always true.
|
124
|
-
#
|
125
|
-
# @example Is this relation embedded?
|
126
|
-
# Embedded::In.embedded?
|
127
|
-
#
|
128
|
-
# @return [ true ] true.
|
129
|
-
#
|
130
|
-
# @since 2.0.0.rc.1
|
131
|
-
def embedded?
|
132
|
-
true
|
133
|
-
end
|
134
|
-
|
135
|
-
# Returns the suffix of the foreign key field, either "_id" or "_ids".
|
136
|
-
#
|
137
|
-
# @example Get the suffix for the foreign key.
|
138
|
-
# Referenced::Many.foreign_key_suffix
|
139
|
-
#
|
140
|
-
# @return [ nil ] nil.
|
141
|
-
#
|
142
|
-
# @since 3.0.0
|
143
|
-
def foreign_key_suffix
|
144
|
-
nil
|
145
|
-
end
|
146
|
-
|
147
|
-
# Returns the macro for this relation. Used mostly as a helper in
|
148
|
-
# reflection.
|
149
|
-
#
|
150
|
-
# @example Get the macro.
|
151
|
-
# Mongoid::Relations::Embedded::In.macro
|
152
|
-
#
|
153
|
-
# @return [ Symbol ] :embedded_in.
|
154
|
-
#
|
155
|
-
# @since 2.0.0.rc.1
|
156
|
-
def macro
|
157
|
-
:embedded_in
|
158
|
-
end
|
159
|
-
|
160
|
-
# Return the nested builder that is responsible for generating
|
161
|
-
# the documents that will be used by this relation.
|
162
|
-
#
|
163
|
-
# @example Get the builder.
|
164
|
-
# NestedAttributes::One.builder(attributes, options)
|
165
|
-
#
|
166
|
-
# @param [ Metadata ] metadata The relation metadata.
|
167
|
-
# @param [ Hash ] attributes The attributes to build with.
|
168
|
-
# @param [ Hash ] options The options for the builder.
|
169
|
-
#
|
170
|
-
# @option options [ true, false ] :allow_destroy Can documents be
|
171
|
-
# deleted?
|
172
|
-
# @option options [ Integer ] :limit Max number of documents to
|
173
|
-
# create at once.
|
174
|
-
# @option options [ Proc, Symbol ] :reject_if If documents match this
|
175
|
-
# option then they are ignored.
|
176
|
-
# @option options [ true, false ] :update_only Only existing documents
|
177
|
-
# can be modified.
|
178
|
-
#
|
179
|
-
# @return [ Builder ] A newly instantiated nested builder object.
|
180
|
-
#
|
181
|
-
# @since 2.0.0.rc.1
|
182
|
-
def nested_builder(metadata, attributes, options)
|
183
|
-
Builders::NestedAttributes::One.new(metadata, attributes, options)
|
184
|
-
end
|
185
|
-
|
186
|
-
# Get the path calculator for the supplied document.
|
187
|
-
#
|
188
|
-
# @example Get the path calculator.
|
189
|
-
# Proxy.path(document)
|
190
|
-
#
|
191
|
-
# @param [ Document ] document The document to calculate on.
|
192
|
-
#
|
193
|
-
# @return [ Root ] The root atomic path calculator.
|
194
|
-
#
|
195
|
-
# @since 2.1.0
|
196
|
-
def path(document)
|
197
|
-
Mongoid::Atomic::Paths::Root.new(document)
|
198
|
-
end
|
199
|
-
|
200
|
-
# Tells the caller if this relation is one that stores the foreign
|
201
|
-
# key on its own objects.
|
202
|
-
#
|
203
|
-
# @example Does this relation store a foreign key?
|
204
|
-
# Embedded::In.stores_foreign_key?
|
205
|
-
#
|
206
|
-
# @return [ false ] false.
|
207
|
-
#
|
208
|
-
# @since 2.0.0.rc.1
|
209
|
-
def stores_foreign_key?
|
210
|
-
false
|
211
|
-
end
|
212
|
-
|
213
|
-
# Get the valid options allowed with this relation.
|
214
|
-
#
|
215
|
-
# @example Get the valid options.
|
216
|
-
# Relation.valid_options
|
217
|
-
#
|
218
|
-
# @return [ Array<Symbol> ] The valid options.
|
219
|
-
#
|
220
|
-
# @since 2.1.0
|
221
|
-
def valid_options
|
222
|
-
VALID_OPTIONS
|
223
|
-
end
|
224
|
-
|
225
|
-
# Get the default validation setting for the relation. Determines if
|
226
|
-
# by default a validates associated will occur.
|
227
|
-
#
|
228
|
-
# @example Get the validation default.
|
229
|
-
# Proxy.validation_default
|
230
|
-
#
|
231
|
-
# @return [ true, false ] The validation default.
|
232
|
-
#
|
233
|
-
# @since 2.1.9
|
234
|
-
def validation_default
|
235
|
-
false
|
236
|
-
end
|
237
|
-
end
|
238
|
-
end
|
239
|
-
end
|
240
|
-
end
|
241
|
-
end
|
@@ -1,683 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
require "mongoid/relations/embedded/batchable"
|
3
|
-
|
4
|
-
module Mongoid
|
5
|
-
module Relations
|
6
|
-
module Embedded
|
7
|
-
|
8
|
-
# This class handles the behaviour for a document that embeds many other
|
9
|
-
# documents within in it as an array.
|
10
|
-
class Many < Relations::Many
|
11
|
-
include Batchable
|
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
|
-
:as,
|
20
|
-
:cascade_callbacks,
|
21
|
-
:cyclic,
|
22
|
-
:order,
|
23
|
-
:store_as,
|
24
|
-
:before_add,
|
25
|
-
:after_add,
|
26
|
-
:before_remove,
|
27
|
-
:after_remove
|
28
|
-
].freeze
|
29
|
-
|
30
|
-
# Appends a document or array of documents to the relation. Will set
|
31
|
-
# the parent and update the index in the process.
|
32
|
-
#
|
33
|
-
# @example Append a document.
|
34
|
-
# person.addresses << address
|
35
|
-
#
|
36
|
-
# @example Push a document.
|
37
|
-
# person.addresses.push(address)
|
38
|
-
#
|
39
|
-
# @param [ Document, Array<Document> ] args Any number of documents.
|
40
|
-
#
|
41
|
-
def <<(*args)
|
42
|
-
docs = args.flatten
|
43
|
-
return concat(docs) if docs.size > 1
|
44
|
-
if doc = docs.first
|
45
|
-
append(doc)
|
46
|
-
doc.save if persistable? && !_assigning?
|
47
|
-
end
|
48
|
-
self
|
49
|
-
end
|
50
|
-
alias :push :<<
|
51
|
-
|
52
|
-
# Get this relation as as its representation in the database.
|
53
|
-
#
|
54
|
-
# @example Convert the relation to an attributes hash.
|
55
|
-
# person.addresses.as_document
|
56
|
-
#
|
57
|
-
# @return [ Array<Hash> ] The relation as stored in the db.
|
58
|
-
#
|
59
|
-
# @since 2.0.0.rc.1
|
60
|
-
def as_document
|
61
|
-
as_attributes.collect { |attrs| BSON::Document.new(attrs) }
|
62
|
-
end
|
63
|
-
|
64
|
-
# Appends an array of documents to the relation. Performs a batch
|
65
|
-
# insert of the documents instead of persisting one at a time.
|
66
|
-
#
|
67
|
-
# @example Concat with other documents.
|
68
|
-
# person.addresses.concat([ address_one, address_two ])
|
69
|
-
#
|
70
|
-
# @param [ Array<Document> ] docs The docs to add.
|
71
|
-
#
|
72
|
-
# @return [ Array<Document> ] The documents.
|
73
|
-
#
|
74
|
-
# @since 2.4.0
|
75
|
-
def concat(docs)
|
76
|
-
batch_insert(docs) unless docs.empty?
|
77
|
-
self
|
78
|
-
end
|
79
|
-
|
80
|
-
# Builds a new document in the relation and appends it to the target.
|
81
|
-
# Takes an optional type if you want to specify a subclass.
|
82
|
-
#
|
83
|
-
# @example Build a new document on the relation.
|
84
|
-
# person.people.build(:name => "Bozo")
|
85
|
-
#
|
86
|
-
# @overload build(attributes = {}, options = {}, type = nil)
|
87
|
-
# @param [ Hash ] attributes The attributes to build the document with.
|
88
|
-
# @param [ Class ] type Optional class to build the document with.
|
89
|
-
#
|
90
|
-
# @overload build(attributes = {}, type = nil)
|
91
|
-
# @param [ Hash ] attributes The attributes to build the document with.
|
92
|
-
# @param [ Class ] type Optional class to build the document with.
|
93
|
-
#
|
94
|
-
# @return [ Document ] The new document.
|
95
|
-
def build(attributes = {}, type = nil)
|
96
|
-
doc = Factory.build(type || __metadata.klass, attributes)
|
97
|
-
append(doc)
|
98
|
-
doc.apply_post_processed_defaults
|
99
|
-
yield(doc) if block_given?
|
100
|
-
doc.run_callbacks(:build) { doc }
|
101
|
-
base._reset_memoized_children!
|
102
|
-
doc
|
103
|
-
end
|
104
|
-
alias :new :build
|
105
|
-
|
106
|
-
# Clear the relation. Will delete the documents from the db if they are
|
107
|
-
# already persisted.
|
108
|
-
#
|
109
|
-
# @example Clear the relation.
|
110
|
-
# person.addresses.clear
|
111
|
-
#
|
112
|
-
# @return [ Many ] The empty relation.
|
113
|
-
def clear
|
114
|
-
batch_clear(target.dup)
|
115
|
-
self
|
116
|
-
end
|
117
|
-
|
118
|
-
# Returns a count of the number of documents in the association that have
|
119
|
-
# actually been persisted to the database.
|
120
|
-
#
|
121
|
-
# Use #size if you want the total number of documents.
|
122
|
-
#
|
123
|
-
# @example Get the count of persisted documents.
|
124
|
-
# person.addresses.count
|
125
|
-
#
|
126
|
-
# @return [ Integer ] The total number of persisted embedded docs, as
|
127
|
-
# flagged by the #persisted? method.
|
128
|
-
def count
|
129
|
-
target.select { |doc| doc.persisted? }.size
|
130
|
-
end
|
131
|
-
|
132
|
-
# Delete the supplied document from the target. This method is proxied
|
133
|
-
# in order to reindex the array after the operation occurs.
|
134
|
-
#
|
135
|
-
# @example Delete the document from the relation.
|
136
|
-
# person.addresses.delete(address)
|
137
|
-
#
|
138
|
-
# @param [ Document ] document The document to be deleted.
|
139
|
-
#
|
140
|
-
# @return [ Document, nil ] The deleted document or nil if nothing deleted.
|
141
|
-
#
|
142
|
-
# @since 2.0.0.rc.1
|
143
|
-
def delete(document)
|
144
|
-
execute_callback :before_remove, document
|
145
|
-
doc = target.delete_one(document)
|
146
|
-
if doc && !_binding?
|
147
|
-
_unscoped.delete_one(doc)
|
148
|
-
if _assigning?
|
149
|
-
base.add_atomic_pull(doc)
|
150
|
-
else
|
151
|
-
doc.delete(suppress: true)
|
152
|
-
unbind_one(doc)
|
153
|
-
end
|
154
|
-
end
|
155
|
-
reindex
|
156
|
-
execute_callback :after_remove, document
|
157
|
-
doc
|
158
|
-
end
|
159
|
-
|
160
|
-
# Delete all the documents in the association without running callbacks.
|
161
|
-
#
|
162
|
-
# @example Delete all documents from the relation.
|
163
|
-
# person.addresses.delete_all
|
164
|
-
#
|
165
|
-
# @example Conditionally delete documents from the relation.
|
166
|
-
# person.addresses.delete_all({ :street => "Bond" })
|
167
|
-
#
|
168
|
-
# @param [ Hash ] conditions Conditions on which documents to delete.
|
169
|
-
#
|
170
|
-
# @return [ Integer ] The number of documents deleted.
|
171
|
-
def delete_all(conditions = {})
|
172
|
-
remove_all(conditions, :delete)
|
173
|
-
end
|
174
|
-
|
175
|
-
# Delete all the documents for which the provided block returns true.
|
176
|
-
#
|
177
|
-
# @example Delete the matching documents.
|
178
|
-
# person.addresses.delete_if do |doc|
|
179
|
-
# doc.state == "GA"
|
180
|
-
# end
|
181
|
-
#
|
182
|
-
# @return [ Many, Enumerator ] The relation or an enumerator if no
|
183
|
-
# block was provided.
|
184
|
-
#
|
185
|
-
# @since 3.1.0
|
186
|
-
def delete_if
|
187
|
-
if block_given?
|
188
|
-
dup_target = target.dup
|
189
|
-
dup_target.each do |doc|
|
190
|
-
delete(doc) if yield(doc)
|
191
|
-
end
|
192
|
-
self
|
193
|
-
else
|
194
|
-
super
|
195
|
-
end
|
196
|
-
end
|
197
|
-
|
198
|
-
# Destroy all the documents in the association whilst running callbacks.
|
199
|
-
#
|
200
|
-
# @example Destroy all documents from the relation.
|
201
|
-
# person.addresses.destroy_all
|
202
|
-
#
|
203
|
-
# @example Conditionally destroy documents from the relation.
|
204
|
-
# person.addresses.destroy_all({ :street => "Bond" })
|
205
|
-
#
|
206
|
-
# @param [ Hash ] conditions Conditions on which documents to destroy.
|
207
|
-
#
|
208
|
-
# @return [ Integer ] The number of documents destroyed.
|
209
|
-
def destroy_all(conditions = {})
|
210
|
-
remove_all(conditions, :destroy)
|
211
|
-
end
|
212
|
-
|
213
|
-
# Determine if any documents in this relation exist in the database.
|
214
|
-
#
|
215
|
-
# @example Are there persisted documents?
|
216
|
-
# person.posts.exists?
|
217
|
-
#
|
218
|
-
# @return [ true, false ] True is persisted documents exist, false if not.
|
219
|
-
def exists?
|
220
|
-
count > 0
|
221
|
-
end
|
222
|
-
|
223
|
-
# Finds a document in this association through several different
|
224
|
-
# methods.
|
225
|
-
#
|
226
|
-
# @example Find a document by its id.
|
227
|
-
# person.addresses.find(BSON::ObjectId.new)
|
228
|
-
#
|
229
|
-
# @example Find documents for multiple ids.
|
230
|
-
# person.addresses.find([ BSON::ObjectId.new, BSON::ObjectId.new ])
|
231
|
-
#
|
232
|
-
# @param [ Array<Object> ] args Various arguments.
|
233
|
-
#
|
234
|
-
# @return [ Array<Document>, Document ] A single or multiple documents.
|
235
|
-
def find(*args)
|
236
|
-
criteria.find(*args)
|
237
|
-
end
|
238
|
-
|
239
|
-
# Instantiate a new embeds_many relation.
|
240
|
-
#
|
241
|
-
# @example Create the new relation.
|
242
|
-
# Many.new(person, addresses, metadata)
|
243
|
-
#
|
244
|
-
# @param [ Document ] base The document this relation hangs off of.
|
245
|
-
# @param [ Array<Document> ] target The child documents of the relation.
|
246
|
-
# @param [ Metadata ] metadata The relation's metadata
|
247
|
-
#
|
248
|
-
# @return [ Many ] The proxy.
|
249
|
-
def initialize(base, target, metadata)
|
250
|
-
init(base, target, metadata) do
|
251
|
-
target.each_with_index do |doc, index|
|
252
|
-
integrate(doc)
|
253
|
-
doc._index = index
|
254
|
-
end
|
255
|
-
@_unscoped = target.dup
|
256
|
-
@target = scope(target)
|
257
|
-
end
|
258
|
-
end
|
259
|
-
|
260
|
-
# Get all the documents in the relation that are loaded into memory.
|
261
|
-
#
|
262
|
-
# @example Get the in memory documents.
|
263
|
-
# relation.in_memory
|
264
|
-
#
|
265
|
-
# @return [ Array<Document> ] The documents in memory.
|
266
|
-
#
|
267
|
-
# @since 2.1.0
|
268
|
-
def in_memory
|
269
|
-
target
|
270
|
-
end
|
271
|
-
|
272
|
-
# Pop documents off the relation. This can be a single document or
|
273
|
-
# multiples, and will automatically persist the changes.
|
274
|
-
#
|
275
|
-
# @example Pop a single document.
|
276
|
-
# relation.pop
|
277
|
-
#
|
278
|
-
# @example Pop multiple documents.
|
279
|
-
# relation.pop(3)
|
280
|
-
#
|
281
|
-
# @param [ Integer ] count The number of documents to pop, or 1 if not
|
282
|
-
# provided.
|
283
|
-
#
|
284
|
-
# @return [ Document, Array<Document> ] The popped document(s).
|
285
|
-
#
|
286
|
-
# @since 3.0.0
|
287
|
-
def pop(count = nil)
|
288
|
-
if count
|
289
|
-
if docs = target[target.size - count, target.size]
|
290
|
-
docs.each { |doc| delete(doc) }
|
291
|
-
end
|
292
|
-
else
|
293
|
-
delete(target[-1])
|
294
|
-
end
|
295
|
-
end
|
296
|
-
|
297
|
-
# Shift documents off the relation. This can be a single document or
|
298
|
-
# multiples, and will automatically persist the changes.
|
299
|
-
#
|
300
|
-
# @example Shift a single document.
|
301
|
-
# relation.shift
|
302
|
-
#
|
303
|
-
# @example Shift multiple documents.
|
304
|
-
# relation.shift(3)
|
305
|
-
#
|
306
|
-
# @param [ Integer ] count The number of documents to shift, or 1 if not
|
307
|
-
# provided.
|
308
|
-
#
|
309
|
-
# @return [ Document, Array<Document> ] The shifted document(s).
|
310
|
-
def shift(count = nil)
|
311
|
-
if count
|
312
|
-
if target.size > 0 && docs = target[0, count]
|
313
|
-
docs.each { |doc| delete(doc) }
|
314
|
-
end
|
315
|
-
else
|
316
|
-
delete(target[0])
|
317
|
-
end
|
318
|
-
end
|
319
|
-
|
320
|
-
# Substitutes the supplied target documents for the existing documents
|
321
|
-
# in the relation.
|
322
|
-
#
|
323
|
-
# @example Substitute the relation's target.
|
324
|
-
# person.addresses.substitute([ address ])
|
325
|
-
#
|
326
|
-
# @param [ Array<Document> ] docs The replacement docs.
|
327
|
-
#
|
328
|
-
# @return [ Many ] The proxied relation.
|
329
|
-
#
|
330
|
-
# @since 2.0.0.rc.1
|
331
|
-
def substitute(docs)
|
332
|
-
batch_replace(docs)
|
333
|
-
self
|
334
|
-
end
|
335
|
-
|
336
|
-
# Return the relation with all previous scoping removed. This is the
|
337
|
-
# exact representation of the docs in the database.
|
338
|
-
#
|
339
|
-
# @example Get the unscoped documents.
|
340
|
-
# person.addresses.unscoped
|
341
|
-
#
|
342
|
-
# @return [ Criteria ] The unscoped relation.
|
343
|
-
#
|
344
|
-
# @since 2.4.0
|
345
|
-
def unscoped
|
346
|
-
criterion = klass.unscoped
|
347
|
-
criterion.embedded = true
|
348
|
-
criterion.documents = _unscoped.delete_if(&:marked_for_destruction?)
|
349
|
-
criterion
|
350
|
-
end
|
351
|
-
|
352
|
-
private
|
353
|
-
|
354
|
-
# Appends the document to the target array, updating the index on the
|
355
|
-
# document at the same time.
|
356
|
-
#
|
357
|
-
# @example Append to the document.
|
358
|
-
# relation.append(document)
|
359
|
-
#
|
360
|
-
# @param [ Document ] document The document to append to the target.
|
361
|
-
#
|
362
|
-
# @since 2.0.0.rc.1
|
363
|
-
def append(document)
|
364
|
-
execute_callback :before_add, document
|
365
|
-
target.push(*scope([document]))
|
366
|
-
_unscoped.push(document)
|
367
|
-
integrate(document)
|
368
|
-
document._index = _unscoped.size - 1
|
369
|
-
execute_callback :after_add, document
|
370
|
-
end
|
371
|
-
|
372
|
-
# Instantiate the binding associated with this relation.
|
373
|
-
#
|
374
|
-
# @example Create the binding.
|
375
|
-
# relation.binding([ address ])
|
376
|
-
#
|
377
|
-
# @return [ Binding ] The many binding.
|
378
|
-
#
|
379
|
-
# @since 2.0.0.rc.1
|
380
|
-
def binding
|
381
|
-
Bindings::Embedded::Many.new(base, target, __metadata)
|
382
|
-
end
|
383
|
-
|
384
|
-
# Returns the criteria object for the target class with its documents set
|
385
|
-
# to target.
|
386
|
-
#
|
387
|
-
# @example Get a criteria for the relation.
|
388
|
-
# relation.criteria
|
389
|
-
#
|
390
|
-
# @return [ Criteria ] A new criteria.
|
391
|
-
def criteria
|
392
|
-
criterion = klass.scoped
|
393
|
-
criterion.embedded = true
|
394
|
-
criterion.documents = target
|
395
|
-
criterion.parent_document = base
|
396
|
-
criterion.metadata = relation_metadata
|
397
|
-
Many.apply_ordering(criterion, __metadata)
|
398
|
-
end
|
399
|
-
|
400
|
-
# Deletes one document from the target and unscoped.
|
401
|
-
#
|
402
|
-
# @api private
|
403
|
-
#
|
404
|
-
# @example Delete one document.
|
405
|
-
# relation.delete_one(doc)
|
406
|
-
#
|
407
|
-
# @param [ Document ] document The document to delete.
|
408
|
-
#
|
409
|
-
# @since 2.4.7
|
410
|
-
def delete_one(document)
|
411
|
-
target.delete_one(document)
|
412
|
-
_unscoped.delete_one(document)
|
413
|
-
reindex
|
414
|
-
end
|
415
|
-
|
416
|
-
# Integrate the document into the relation. will set its metadata and
|
417
|
-
# attempt to bind the inverse.
|
418
|
-
#
|
419
|
-
# @example Integrate the document.
|
420
|
-
# relation.integrate(document)
|
421
|
-
#
|
422
|
-
# @param [ Document ] document The document to integrate.
|
423
|
-
#
|
424
|
-
# @since 2.1.0
|
425
|
-
def integrate(document)
|
426
|
-
characterize_one(document)
|
427
|
-
bind_one(document)
|
428
|
-
end
|
429
|
-
|
430
|
-
# If the target array does not respond to the supplied method then try to
|
431
|
-
# find a named scope or criteria on the class and send the call there.
|
432
|
-
#
|
433
|
-
# If the method exists on the array, use the default proxy behavior.
|
434
|
-
#
|
435
|
-
# @param [ Symbol, String ] name The name of the method.
|
436
|
-
# @param [ Array ] args The method args
|
437
|
-
# @param [ Proc ] block Optional block to pass.
|
438
|
-
#
|
439
|
-
# @return [ Criteria, Object ] A Criteria or return value from the target.
|
440
|
-
def method_missing(name, *args, &block)
|
441
|
-
return super if target.respond_to?(name)
|
442
|
-
klass.send(:with_scope, criteria) do
|
443
|
-
criteria.public_send(name, *args, &block)
|
444
|
-
end
|
445
|
-
end
|
446
|
-
|
447
|
-
# Are we able to persist this relation?
|
448
|
-
#
|
449
|
-
# @example Can we persist the relation?
|
450
|
-
# relation.persistable?
|
451
|
-
#
|
452
|
-
# @return [ true, false ] If the relation is persistable.
|
453
|
-
#
|
454
|
-
# @since 2.1.0
|
455
|
-
def persistable?
|
456
|
-
base.persisted? && !_binding?
|
457
|
-
end
|
458
|
-
|
459
|
-
# Reindex all the target elements. This is useful when performing
|
460
|
-
# operations on the proxied target directly and the indices need to
|
461
|
-
# match that on the database side.
|
462
|
-
#
|
463
|
-
# @example Reindex the relation.
|
464
|
-
# person.addresses.reindex
|
465
|
-
#
|
466
|
-
# @since 2.0.0.rc.1
|
467
|
-
def reindex
|
468
|
-
_unscoped.each_with_index do |doc, index|
|
469
|
-
doc._index = index
|
470
|
-
end
|
471
|
-
end
|
472
|
-
|
473
|
-
# Apply the metadata ordering or the default scoping to the provided
|
474
|
-
# documents.
|
475
|
-
#
|
476
|
-
# @example Apply scoping.
|
477
|
-
# person.addresses.scope(target)
|
478
|
-
#
|
479
|
-
# @param [ Array<Document> ] docs The documents to scope.
|
480
|
-
#
|
481
|
-
# @return [ Array<Document> ] The scoped docs.
|
482
|
-
#
|
483
|
-
# @since 2.4.0
|
484
|
-
def scope(docs)
|
485
|
-
return docs unless __metadata.order || __metadata.klass.default_scoping?
|
486
|
-
crit = __metadata.klass.order_by(__metadata.order)
|
487
|
-
crit.embedded = true
|
488
|
-
crit.documents = docs
|
489
|
-
crit.entries
|
490
|
-
end
|
491
|
-
|
492
|
-
# Remove all documents from the relation, either with a delete or a
|
493
|
-
# destroy depending on what this was called through.
|
494
|
-
#
|
495
|
-
# @example Destroy documents from the relation.
|
496
|
-
# relation.remove_all({ :num => 1 }, true)
|
497
|
-
#
|
498
|
-
# @param [ Hash ] conditions Conditions to filter by.
|
499
|
-
# @param [ true, false ] method
|
500
|
-
#
|
501
|
-
# @return [ Integer ] The number of documents removed.
|
502
|
-
def remove_all(conditions = {}, method = :delete)
|
503
|
-
criteria = where(conditions || {})
|
504
|
-
removed = criteria.size
|
505
|
-
batch_remove(criteria, method)
|
506
|
-
removed
|
507
|
-
end
|
508
|
-
|
509
|
-
# Get the internal unscoped documents.
|
510
|
-
#
|
511
|
-
# @example Get the unscoped documents.
|
512
|
-
# relation._unscoped
|
513
|
-
#
|
514
|
-
# @return [ Array<Document> ] The unscoped documents.
|
515
|
-
#
|
516
|
-
# @since 2.4.0
|
517
|
-
def _unscoped
|
518
|
-
@_unscoped ||= []
|
519
|
-
end
|
520
|
-
|
521
|
-
# Set the internal unscoped documents.
|
522
|
-
#
|
523
|
-
# @example Set the unscoped documents.
|
524
|
-
# relation._unscoped = docs
|
525
|
-
#
|
526
|
-
# @param [ Array<Document> ] docs The documents.
|
527
|
-
#
|
528
|
-
# @return [ Array<Document ] The unscoped docs.
|
529
|
-
#
|
530
|
-
# @since 2.4.0
|
531
|
-
def _unscoped=(docs)
|
532
|
-
@_unscoped = docs
|
533
|
-
end
|
534
|
-
|
535
|
-
def as_attributes
|
536
|
-
attributes = []
|
537
|
-
_unscoped.each do |doc|
|
538
|
-
attributes.push(doc.as_document)
|
539
|
-
end
|
540
|
-
attributes
|
541
|
-
end
|
542
|
-
|
543
|
-
class << self
|
544
|
-
|
545
|
-
# Return the builder that is responsible for generating the documents
|
546
|
-
# that will be used by this relation.
|
547
|
-
#
|
548
|
-
# @example Get the builder.
|
549
|
-
# Embedded::Many.builder(meta, object)
|
550
|
-
#
|
551
|
-
# @param [ Document ] base The base document.
|
552
|
-
# @param [ Metadata ] meta The metadata of the relation.
|
553
|
-
# @param [ Document, Hash ] object A document or attributes to build
|
554
|
-
# with.
|
555
|
-
#
|
556
|
-
# @return [ Builder ] A newly instantiated builder object.
|
557
|
-
#
|
558
|
-
# @since 2.0.0.rc.1
|
559
|
-
def builder(base, meta, object)
|
560
|
-
Builders::Embedded::Many.new(base, meta, object)
|
561
|
-
end
|
562
|
-
|
563
|
-
# Returns true if the relation is an embedded one. In this case
|
564
|
-
# always true.
|
565
|
-
#
|
566
|
-
# @example Is the relation embedded?
|
567
|
-
# Embedded::Many.embedded?
|
568
|
-
#
|
569
|
-
# @return [ true ] true.
|
570
|
-
#
|
571
|
-
# @since 2.0.0.rc.1
|
572
|
-
def embedded?
|
573
|
-
true
|
574
|
-
end
|
575
|
-
|
576
|
-
# Returns the suffix of the foreign key field, either "_id" or "_ids".
|
577
|
-
#
|
578
|
-
# @example Get the suffix for the foreign key.
|
579
|
-
# Referenced::Many.foreign_key_suffix
|
580
|
-
#
|
581
|
-
# @return [ nil ] nil.
|
582
|
-
#
|
583
|
-
# @since 3.0.0
|
584
|
-
def foreign_key_suffix
|
585
|
-
nil
|
586
|
-
end
|
587
|
-
|
588
|
-
# Returns the macro for this relation. Used mostly as a helper in
|
589
|
-
# reflection.
|
590
|
-
#
|
591
|
-
# @example Get the relation macro.
|
592
|
-
# Mongoid::Relations::Embedded::Many.macro
|
593
|
-
#
|
594
|
-
# @return [ Symbol ] :embeds_many
|
595
|
-
#
|
596
|
-
# @since 2.0.0.rc.1
|
597
|
-
def macro
|
598
|
-
:embeds_many
|
599
|
-
end
|
600
|
-
|
601
|
-
# Return the nested builder that is responsible for generating the
|
602
|
-
# documents that will be used by this relation.
|
603
|
-
#
|
604
|
-
# @example Get the nested builder.
|
605
|
-
# NestedAttributes::Many.builder(attributes, options)
|
606
|
-
#
|
607
|
-
# @param [ Metadata ] metadata The relation metadata.
|
608
|
-
# @param [ Hash ] attributes The attributes to build with.
|
609
|
-
# @param [ Hash ] options The builder options.
|
610
|
-
#
|
611
|
-
# @option options [ true, false ] :allow_destroy Can documents be
|
612
|
-
# deleted?
|
613
|
-
# @option options [ Integer ] :limit Max number of documents to
|
614
|
-
# create at once.
|
615
|
-
# @option options [ Proc, Symbol ] :reject_if If documents match this
|
616
|
-
# option then they are ignored.
|
617
|
-
# @option options [ true, false ] :update_only Only existing documents
|
618
|
-
# can be modified.
|
619
|
-
#
|
620
|
-
# @return [ NestedBuilder ] The nested attributes builder.
|
621
|
-
#
|
622
|
-
# @since 2.0.0.rc.1
|
623
|
-
def nested_builder(metadata, attributes, options)
|
624
|
-
Builders::NestedAttributes::Many.new(metadata, attributes, options)
|
625
|
-
end
|
626
|
-
|
627
|
-
# Get the path calculator for the supplied document.
|
628
|
-
#
|
629
|
-
# @example Get the path calculator.
|
630
|
-
# Proxy.path(document)
|
631
|
-
#
|
632
|
-
# @param [ Document ] document The document to calculate on.
|
633
|
-
#
|
634
|
-
# @return [ Mongoid::Atomic::Paths::Embedded::Many ]
|
635
|
-
# The embedded many atomic path calculator.
|
636
|
-
#
|
637
|
-
# @since 2.1.0
|
638
|
-
def path(document)
|
639
|
-
Mongoid::Atomic::Paths::Embedded::Many.new(document)
|
640
|
-
end
|
641
|
-
|
642
|
-
# Tells the caller if this relation is one that stores the foreign
|
643
|
-
# key on its own objects.
|
644
|
-
#
|
645
|
-
# @example Does this relation store a foreign key?
|
646
|
-
# Embedded::Many.stores_foreign_key?
|
647
|
-
#
|
648
|
-
# @return [ false ] false.
|
649
|
-
#
|
650
|
-
# @since 2.0.0.rc.1
|
651
|
-
def stores_foreign_key?
|
652
|
-
false
|
653
|
-
end
|
654
|
-
|
655
|
-
# Get the valid options allowed with this relation.
|
656
|
-
#
|
657
|
-
# @example Get the valid options.
|
658
|
-
# Relation.valid_options
|
659
|
-
#
|
660
|
-
# @return [ Array<Symbol> ] The valid options.
|
661
|
-
#
|
662
|
-
# @since 2.1.0
|
663
|
-
def valid_options
|
664
|
-
VALID_OPTIONS
|
665
|
-
end
|
666
|
-
|
667
|
-
# Get the default validation setting for the relation. Determines if
|
668
|
-
# by default a validates associated will occur.
|
669
|
-
#
|
670
|
-
# @example Get the validation default.
|
671
|
-
# Proxy.validation_default
|
672
|
-
#
|
673
|
-
# @return [ true, false ] The validation default.
|
674
|
-
#
|
675
|
-
# @since 2.1.9
|
676
|
-
def validation_default
|
677
|
-
true
|
678
|
-
end
|
679
|
-
end
|
680
|
-
end
|
681
|
-
end
|
682
|
-
end
|
683
|
-
end
|