mongoid-braxton 2.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.rdoc +50 -0
- data/Rakefile +51 -0
- data/lib/config/locales/bg.yml +41 -0
- data/lib/config/locales/de.yml +41 -0
- data/lib/config/locales/en.yml +45 -0
- data/lib/config/locales/es.yml +41 -0
- data/lib/config/locales/fr.yml +42 -0
- data/lib/config/locales/hu.yml +44 -0
- data/lib/config/locales/id.yml +46 -0
- data/lib/config/locales/it.yml +39 -0
- data/lib/config/locales/ja.yml +40 -0
- data/lib/config/locales/kr.yml +65 -0
- data/lib/config/locales/nl.yml +39 -0
- data/lib/config/locales/pl.yml +39 -0
- data/lib/config/locales/pt-BR.yml +40 -0
- data/lib/config/locales/pt.yml +40 -0
- data/lib/config/locales/ro.yml +46 -0
- data/lib/config/locales/ru.yml +41 -0
- data/lib/config/locales/sv.yml +40 -0
- data/lib/config/locales/vi.yml +45 -0
- data/lib/config/locales/zh-CN.yml +33 -0
- data/lib/mongoid.rb +140 -0
- data/lib/mongoid/atomicity.rb +111 -0
- data/lib/mongoid/attributes.rb +185 -0
- data/lib/mongoid/attributes/processing.rb +145 -0
- data/lib/mongoid/callbacks.rb +23 -0
- data/lib/mongoid/collection.rb +137 -0
- data/lib/mongoid/collections.rb +71 -0
- data/lib/mongoid/collections/master.rb +37 -0
- data/lib/mongoid/collections/operations.rb +42 -0
- data/lib/mongoid/collections/retry.rb +39 -0
- data/lib/mongoid/components.rb +45 -0
- data/lib/mongoid/config.rb +349 -0
- data/lib/mongoid/config/database.rb +167 -0
- data/lib/mongoid/config/replset_database.rb +78 -0
- data/lib/mongoid/contexts.rb +19 -0
- data/lib/mongoid/contexts/enumerable.rb +275 -0
- data/lib/mongoid/contexts/enumerable/sort.rb +43 -0
- data/lib/mongoid/contexts/mongo.rb +345 -0
- data/lib/mongoid/copyable.rb +46 -0
- data/lib/mongoid/criteria.rb +357 -0
- data/lib/mongoid/criterion/builder.rb +34 -0
- data/lib/mongoid/criterion/complex.rb +34 -0
- data/lib/mongoid/criterion/creational.rb +34 -0
- data/lib/mongoid/criterion/exclusion.rb +108 -0
- data/lib/mongoid/criterion/inclusion.rb +198 -0
- data/lib/mongoid/criterion/inspection.rb +22 -0
- data/lib/mongoid/criterion/optional.rb +193 -0
- data/lib/mongoid/criterion/selector.rb +143 -0
- data/lib/mongoid/criterion/unconvertable.rb +20 -0
- data/lib/mongoid/cursor.rb +86 -0
- data/lib/mongoid/default_scope.rb +36 -0
- data/lib/mongoid/dirty.rb +253 -0
- data/lib/mongoid/document.rb +284 -0
- data/lib/mongoid/errors.rb +13 -0
- data/lib/mongoid/errors/document_not_found.rb +29 -0
- data/lib/mongoid/errors/invalid_collection.rb +19 -0
- data/lib/mongoid/errors/invalid_database.rb +20 -0
- data/lib/mongoid/errors/invalid_field.rb +19 -0
- data/lib/mongoid/errors/invalid_options.rb +16 -0
- data/lib/mongoid/errors/invalid_type.rb +26 -0
- data/lib/mongoid/errors/mixed_relations.rb +37 -0
- data/lib/mongoid/errors/mongoid_error.rb +27 -0
- data/lib/mongoid/errors/too_many_nested_attribute_records.rb +21 -0
- data/lib/mongoid/errors/unsaved_document.rb +23 -0
- data/lib/mongoid/errors/unsupported_version.rb +21 -0
- data/lib/mongoid/errors/validations.rb +24 -0
- data/lib/mongoid/extensions.rb +123 -0
- data/lib/mongoid/extensions/array/conversions.rb +23 -0
- data/lib/mongoid/extensions/array/parentization.rb +13 -0
- data/lib/mongoid/extensions/big_decimal/conversions.rb +19 -0
- data/lib/mongoid/extensions/binary/conversions.rb +17 -0
- data/lib/mongoid/extensions/boolean/conversions.rb +27 -0
- data/lib/mongoid/extensions/date/conversions.rb +25 -0
- data/lib/mongoid/extensions/datetime/conversions.rb +12 -0
- data/lib/mongoid/extensions/false_class/equality.rb +13 -0
- data/lib/mongoid/extensions/float/conversions.rb +20 -0
- data/lib/mongoid/extensions/hash/conversions.rb +19 -0
- data/lib/mongoid/extensions/hash/criteria_helpers.rb +22 -0
- data/lib/mongoid/extensions/hash/scoping.rb +12 -0
- data/lib/mongoid/extensions/integer/conversions.rb +20 -0
- data/lib/mongoid/extensions/nil/collectionization.rb +12 -0
- data/lib/mongoid/extensions/object/checks.rb +32 -0
- data/lib/mongoid/extensions/object/conversions.rb +25 -0
- data/lib/mongoid/extensions/object/reflections.rb +17 -0
- data/lib/mongoid/extensions/object/yoda.rb +27 -0
- data/lib/mongoid/extensions/object_id/conversions.rb +96 -0
- data/lib/mongoid/extensions/proc/scoping.rb +12 -0
- data/lib/mongoid/extensions/range/conversions.rb +25 -0
- data/lib/mongoid/extensions/set/conversions.rb +20 -0
- data/lib/mongoid/extensions/string/conversions.rb +34 -0
- data/lib/mongoid/extensions/string/inflections.rb +97 -0
- data/lib/mongoid/extensions/symbol/conversions.rb +21 -0
- data/lib/mongoid/extensions/symbol/inflections.rb +40 -0
- data/lib/mongoid/extensions/time_conversions.rb +38 -0
- data/lib/mongoid/extensions/true_class/equality.rb +13 -0
- data/lib/mongoid/extras.rb +42 -0
- data/lib/mongoid/factory.rb +37 -0
- data/lib/mongoid/field.rb +162 -0
- data/lib/mongoid/fields.rb +183 -0
- data/lib/mongoid/finders.rb +127 -0
- data/lib/mongoid/hierarchy.rb +85 -0
- data/lib/mongoid/identity.rb +92 -0
- data/lib/mongoid/indexes.rb +38 -0
- data/lib/mongoid/inspection.rb +54 -0
- data/lib/mongoid/javascript.rb +21 -0
- data/lib/mongoid/javascript/functions.yml +37 -0
- data/lib/mongoid/json.rb +16 -0
- data/lib/mongoid/keys.rb +131 -0
- data/lib/mongoid/logger.rb +18 -0
- data/lib/mongoid/matchers.rb +32 -0
- data/lib/mongoid/matchers/all.rb +11 -0
- data/lib/mongoid/matchers/default.rb +70 -0
- data/lib/mongoid/matchers/exists.rb +13 -0
- data/lib/mongoid/matchers/gt.rb +11 -0
- data/lib/mongoid/matchers/gte.rb +11 -0
- data/lib/mongoid/matchers/in.rb +11 -0
- data/lib/mongoid/matchers/lt.rb +11 -0
- data/lib/mongoid/matchers/lte.rb +11 -0
- data/lib/mongoid/matchers/ne.rb +11 -0
- data/lib/mongoid/matchers/nin.rb +11 -0
- data/lib/mongoid/matchers/or.rb +30 -0
- data/lib/mongoid/matchers/size.rb +11 -0
- data/lib/mongoid/matchers/strategies.rb +63 -0
- data/lib/mongoid/multi_database.rb +11 -0
- data/lib/mongoid/multi_parameter_attributes.rb +82 -0
- data/lib/mongoid/named_scope.rb +137 -0
- data/lib/mongoid/nested_attributes.rb +51 -0
- data/lib/mongoid/observer.rb +67 -0
- data/lib/mongoid/paranoia.rb +103 -0
- data/lib/mongoid/paths.rb +61 -0
- data/lib/mongoid/persistence.rb +240 -0
- data/lib/mongoid/persistence/atomic.rb +88 -0
- data/lib/mongoid/persistence/atomic/add_to_set.rb +32 -0
- data/lib/mongoid/persistence/atomic/inc.rb +28 -0
- data/lib/mongoid/persistence/atomic/operation.rb +44 -0
- data/lib/mongoid/persistence/atomic/pull_all.rb +33 -0
- data/lib/mongoid/persistence/atomic/push.rb +28 -0
- data/lib/mongoid/persistence/command.rb +71 -0
- data/lib/mongoid/persistence/insert.rb +53 -0
- data/lib/mongoid/persistence/insert_embedded.rb +43 -0
- data/lib/mongoid/persistence/remove.rb +44 -0
- data/lib/mongoid/persistence/remove_all.rb +40 -0
- data/lib/mongoid/persistence/remove_embedded.rb +48 -0
- data/lib/mongoid/persistence/update.rb +77 -0
- data/lib/mongoid/railtie.rb +139 -0
- data/lib/mongoid/railties/database.rake +171 -0
- data/lib/mongoid/railties/document.rb +12 -0
- data/lib/mongoid/relations.rb +107 -0
- data/lib/mongoid/relations/accessors.rb +175 -0
- data/lib/mongoid/relations/auto_save.rb +34 -0
- data/lib/mongoid/relations/binding.rb +26 -0
- data/lib/mongoid/relations/bindings.rb +9 -0
- data/lib/mongoid/relations/bindings/embedded/in.rb +82 -0
- data/lib/mongoid/relations/bindings/embedded/many.rb +98 -0
- data/lib/mongoid/relations/bindings/embedded/one.rb +66 -0
- data/lib/mongoid/relations/bindings/referenced/in.rb +74 -0
- data/lib/mongoid/relations/bindings/referenced/many.rb +96 -0
- data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +103 -0
- data/lib/mongoid/relations/bindings/referenced/one.rb +66 -0
- data/lib/mongoid/relations/builder.rb +42 -0
- data/lib/mongoid/relations/builders.rb +79 -0
- data/lib/mongoid/relations/builders/embedded/in.rb +25 -0
- data/lib/mongoid/relations/builders/embedded/many.rb +32 -0
- data/lib/mongoid/relations/builders/embedded/one.rb +26 -0
- data/lib/mongoid/relations/builders/nested_attributes/many.rb +126 -0
- data/lib/mongoid/relations/builders/nested_attributes/one.rb +135 -0
- data/lib/mongoid/relations/builders/referenced/in.rb +29 -0
- data/lib/mongoid/relations/builders/referenced/many.rb +47 -0
- data/lib/mongoid/relations/builders/referenced/many_to_many.rb +29 -0
- data/lib/mongoid/relations/builders/referenced/one.rb +27 -0
- data/lib/mongoid/relations/cascading.rb +55 -0
- data/lib/mongoid/relations/cascading/delete.rb +19 -0
- data/lib/mongoid/relations/cascading/destroy.rb +19 -0
- data/lib/mongoid/relations/cascading/nullify.rb +18 -0
- data/lib/mongoid/relations/cascading/strategy.rb +26 -0
- data/lib/mongoid/relations/constraint.rb +42 -0
- data/lib/mongoid/relations/cyclic.rb +103 -0
- data/lib/mongoid/relations/embedded/atomic.rb +86 -0
- data/lib/mongoid/relations/embedded/atomic/operation.rb +63 -0
- data/lib/mongoid/relations/embedded/atomic/pull.rb +65 -0
- data/lib/mongoid/relations/embedded/atomic/push_all.rb +59 -0
- data/lib/mongoid/relations/embedded/atomic/set.rb +61 -0
- data/lib/mongoid/relations/embedded/atomic/unset.rb +41 -0
- data/lib/mongoid/relations/embedded/in.rb +173 -0
- data/lib/mongoid/relations/embedded/many.rb +499 -0
- data/lib/mongoid/relations/embedded/one.rb +170 -0
- data/lib/mongoid/relations/macros.rb +310 -0
- data/lib/mongoid/relations/many.rb +215 -0
- data/lib/mongoid/relations/metadata.rb +539 -0
- data/lib/mongoid/relations/nested_builder.rb +68 -0
- data/lib/mongoid/relations/one.rb +47 -0
- data/lib/mongoid/relations/polymorphic.rb +54 -0
- data/lib/mongoid/relations/proxy.rb +143 -0
- data/lib/mongoid/relations/referenced/batch.rb +71 -0
- data/lib/mongoid/relations/referenced/batch/insert.rb +57 -0
- data/lib/mongoid/relations/referenced/in.rb +216 -0
- data/lib/mongoid/relations/referenced/many.rb +516 -0
- data/lib/mongoid/relations/referenced/many_to_many.rb +396 -0
- data/lib/mongoid/relations/referenced/one.rb +222 -0
- data/lib/mongoid/relations/reflections.rb +45 -0
- data/lib/mongoid/safe.rb +23 -0
- data/lib/mongoid/safety.rb +207 -0
- data/lib/mongoid/scope.rb +31 -0
- data/lib/mongoid/serialization.rb +99 -0
- data/lib/mongoid/sharding.rb +51 -0
- data/lib/mongoid/state.rb +67 -0
- data/lib/mongoid/timestamps.rb +14 -0
- data/lib/mongoid/timestamps/created.rb +31 -0
- data/lib/mongoid/timestamps/updated.rb +33 -0
- data/lib/mongoid/validations.rb +124 -0
- data/lib/mongoid/validations/associated.rb +44 -0
- data/lib/mongoid/validations/referenced.rb +58 -0
- data/lib/mongoid/validations/uniqueness.rb +85 -0
- data/lib/mongoid/version.rb +4 -0
- data/lib/mongoid/versioning.rb +113 -0
- data/lib/rails/generators/mongoid/config/config_generator.rb +25 -0
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +20 -0
- data/lib/rails/generators/mongoid/model/model_generator.rb +24 -0
- data/lib/rails/generators/mongoid/model/templates/model.rb +19 -0
- data/lib/rails/generators/mongoid/observer/observer_generator.rb +17 -0
- data/lib/rails/generators/mongoid/observer/templates/observer.rb +4 -0
- data/lib/rails/generators/mongoid_generator.rb +70 -0
- data/lib/rails/mongoid.rb +58 -0
- metadata +406 -0
@@ -0,0 +1,173 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid # :nodoc:
|
3
|
+
module Relations #:nodoc:
|
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
|
+
# Binds the base object to the inverse of the relation. This is so we
|
12
|
+
# are referenced to the actual objects themselves and dont hit the
|
13
|
+
# database twice when setting the relations up.
|
14
|
+
#
|
15
|
+
# This is called after first creating the relation, or if a new object
|
16
|
+
# is set on the relation.
|
17
|
+
#
|
18
|
+
# @example Bind the relation.
|
19
|
+
# name.person.bind(:continue => true)
|
20
|
+
#
|
21
|
+
# @param [ Hash ] options The options to bind with.
|
22
|
+
#
|
23
|
+
# @option options [ true, false ] :binding Are we in build mode?
|
24
|
+
# @option options [ true, false ] :continue Continue binding the
|
25
|
+
# inverse?
|
26
|
+
#
|
27
|
+
# @since 2.0.0.rc.1
|
28
|
+
def bind(options = {})
|
29
|
+
binding.bind(options)
|
30
|
+
base.save if target.persisted? && !options[:binding]
|
31
|
+
end
|
32
|
+
|
33
|
+
# Instantiate a new embedded_in relation.
|
34
|
+
#
|
35
|
+
# @example Create the new relation.
|
36
|
+
# Embedded::In.new(name, person, metadata)
|
37
|
+
#
|
38
|
+
# @param [ Document ] base The document the relation hangs off of.
|
39
|
+
# @param [ Document ] target The target (parent) of the relation.
|
40
|
+
# @param [ Metadata ] metadata The relation metadata.
|
41
|
+
#
|
42
|
+
# @return [ In ] The proxy.
|
43
|
+
def initialize(base, target, metadata)
|
44
|
+
init(base, target, metadata) do
|
45
|
+
characterize_one(target)
|
46
|
+
base.parentize(target)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# Unbinds the base object to the inverse of the relation. This occurs
|
51
|
+
# when setting a side of the relation to nil.
|
52
|
+
#
|
53
|
+
# Will delete the object if necessary.
|
54
|
+
#
|
55
|
+
# @example Unbind the relation.
|
56
|
+
# name.person.unbind(:continue => false)
|
57
|
+
#
|
58
|
+
# @param [ Proxy ] old_target The previous target of the relation.
|
59
|
+
# @param [ Hash ] options The options to bind with.
|
60
|
+
#
|
61
|
+
# @option options [ true, false ] :binding Are we in build mode?
|
62
|
+
# @option options [ true, false ] :continue Continue binding the
|
63
|
+
# inverse?
|
64
|
+
#
|
65
|
+
# @since 2.0.0.rc.1
|
66
|
+
def unbind(old_target, options = {})
|
67
|
+
binding(old_target).unbind(options)
|
68
|
+
base.delete if old_target.persisted? && !base.destroyed?
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
# Instantiate the binding associated with this relation.
|
74
|
+
#
|
75
|
+
# @example Get the binding.
|
76
|
+
# binding([ address ])
|
77
|
+
#
|
78
|
+
# @param [ Proxy ] new_target The new documents to bind with.
|
79
|
+
#
|
80
|
+
# @return [ Binding ] A binding object.
|
81
|
+
#
|
82
|
+
# @since 2.0.0.rc.1
|
83
|
+
def binding(new_target = nil)
|
84
|
+
Bindings::Embedded::In.new(base, new_target || target, metadata)
|
85
|
+
end
|
86
|
+
|
87
|
+
class << self
|
88
|
+
|
89
|
+
# Return the builder that is responsible for generating the documents
|
90
|
+
# that will be used by this relation.
|
91
|
+
#
|
92
|
+
# @example Get the builder.
|
93
|
+
# Embedded::In.builder(meta, object, person)
|
94
|
+
#
|
95
|
+
# @param [ Metadata ] meta The metadata of the relation.
|
96
|
+
# @param [ Document, Hash ] object A document or attributes to build with.
|
97
|
+
#
|
98
|
+
# @return [ Builder ] A newly instantiated builder object.
|
99
|
+
#
|
100
|
+
# @since 2.0.0.rc.1
|
101
|
+
def builder(meta, object)
|
102
|
+
Builders::Embedded::In.new(meta, object)
|
103
|
+
end
|
104
|
+
|
105
|
+
# Returns true if the relation is an embedded one. In this case
|
106
|
+
# always true.
|
107
|
+
#
|
108
|
+
# @example Is this relation embedded?
|
109
|
+
# Embedded::In.embedded?
|
110
|
+
#
|
111
|
+
# @return [ true ] true.
|
112
|
+
#
|
113
|
+
# @since 2.0.0.rc.1
|
114
|
+
def embedded?
|
115
|
+
true
|
116
|
+
end
|
117
|
+
|
118
|
+
# Returns the macro for this relation. Used mostly as a helper in
|
119
|
+
# reflection.
|
120
|
+
#
|
121
|
+
# @example Get the macro.
|
122
|
+
# Mongoid::Relations::Embedded::In.macro
|
123
|
+
#
|
124
|
+
# @return [ Symbol ] :embedded_in.
|
125
|
+
#
|
126
|
+
# @since 2.0.0.rc.1
|
127
|
+
def macro
|
128
|
+
:embedded_in
|
129
|
+
end
|
130
|
+
|
131
|
+
# Return the nested builder that is responsible for generating
|
132
|
+
# the documents that will be used by this relation.
|
133
|
+
#
|
134
|
+
# @example Get the builder.
|
135
|
+
# NestedAttributes::One.builder(attributes, options)
|
136
|
+
#
|
137
|
+
# @param [ Metadata ] metadata The relation metadata.
|
138
|
+
# @param [ Hash ] attributes The attributes to build with.
|
139
|
+
# @param [ Hash ] options The options for the builder.
|
140
|
+
#
|
141
|
+
# @option options [ true, false ] :allow_destroy Can documents be
|
142
|
+
# deleted?
|
143
|
+
# @option options [ Integer ] :limit Max number of documents to
|
144
|
+
# create at once.
|
145
|
+
# @option options [ Proc, Symbol ] :reject_if If documents match this
|
146
|
+
# option then they are ignored.
|
147
|
+
# @option options [ true, false ] :update_only Only existing documents
|
148
|
+
# can be modified.
|
149
|
+
#
|
150
|
+
# @return [ Builder ] A newly instantiated nested builder object.
|
151
|
+
#
|
152
|
+
# @since 2.0.0.rc.1
|
153
|
+
def nested_builder(metadata, attributes, options)
|
154
|
+
Builders::NestedAttributes::One.new(metadata, attributes, options)
|
155
|
+
end
|
156
|
+
|
157
|
+
# Tells the caller if this relation is one that stores the foreign
|
158
|
+
# key on its own objects.
|
159
|
+
#
|
160
|
+
# @example Does this relation store a foreign key?
|
161
|
+
# Embedded::In.stores_foreign_key?
|
162
|
+
#
|
163
|
+
# @return [ false ] false.
|
164
|
+
#
|
165
|
+
# @since 2.0.0.rc.1
|
166
|
+
def stores_foreign_key?
|
167
|
+
false
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
@@ -0,0 +1,499 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid # :nodoc:
|
3
|
+
module Relations #:nodoc:
|
4
|
+
module Embedded #:nodoc:
|
5
|
+
|
6
|
+
# This class handles the behaviour for a document that embeds many other
|
7
|
+
# documents within in it as an array.
|
8
|
+
class Many < Relations::Many
|
9
|
+
include Atomic
|
10
|
+
|
11
|
+
# Appends a document or array of documents to the relation. Will set
|
12
|
+
# the parent and update the index in the process.
|
13
|
+
#
|
14
|
+
# @example Append a document.
|
15
|
+
# person.addresses << address
|
16
|
+
#
|
17
|
+
# @example Push a document.
|
18
|
+
# person.addresses.push(address)
|
19
|
+
#
|
20
|
+
# @example Concat with other documents.
|
21
|
+
# person.addresses.concat([ address_one, address_two ])
|
22
|
+
#
|
23
|
+
# @param [ Document, Array<Document> ] *args Any number of documents.
|
24
|
+
def <<(*args)
|
25
|
+
options = default_options(args)
|
26
|
+
atomically(:$pushAll) do
|
27
|
+
args.flatten.each do |doc|
|
28
|
+
return doc unless doc
|
29
|
+
append(doc, options)
|
30
|
+
doc.save if base.persisted? && !options[:binding]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Binds the base object to the inverse of the relation. This is so we
|
36
|
+
# are referenced to the actual objects themselves and dont hit the
|
37
|
+
# database twice when setting the relations up.
|
38
|
+
#
|
39
|
+
# This is called after first creating the relation, or if a new object
|
40
|
+
# is set on the relation.
|
41
|
+
#
|
42
|
+
# @example Bind the relation.
|
43
|
+
# person.addresses.bind(:continue => true)
|
44
|
+
#
|
45
|
+
# @param [ Hash ] options The options to bind with.
|
46
|
+
#
|
47
|
+
# @option options [ true, false ] :binding Are we in build mode?
|
48
|
+
# @option options [ true, false ] :continue Continue binding the
|
49
|
+
# inverse?
|
50
|
+
#
|
51
|
+
# @since 2.0.0.rc.1
|
52
|
+
def bind(options = {})
|
53
|
+
binding.bind(options)
|
54
|
+
if base.persisted? && !options[:binding]
|
55
|
+
atomically(:$set) { target.each(&:save) }
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# Bind the inverse relation between a single document in this proxy
|
60
|
+
# instead of the entire target.
|
61
|
+
#
|
62
|
+
# Used when appending to the target instead of setting the entire
|
63
|
+
# thing.
|
64
|
+
#
|
65
|
+
# @example Bind a single document.
|
66
|
+
# person.addressses.bind_one(address)
|
67
|
+
#
|
68
|
+
# @param [ Document ] document The document to bind.
|
69
|
+
#
|
70
|
+
# @since 2.0.0.rc.1
|
71
|
+
def bind_one(document, options = {})
|
72
|
+
binding.bind_one(document, options)
|
73
|
+
end
|
74
|
+
|
75
|
+
# Clear the relation. Will delete the documents from the db if they are
|
76
|
+
# already persisted.
|
77
|
+
#
|
78
|
+
# @example Clear the relation.
|
79
|
+
# person.addresses.clear
|
80
|
+
#
|
81
|
+
# @return [ Many ] The empty relation.
|
82
|
+
def clear
|
83
|
+
load! and substitute(nil)
|
84
|
+
end
|
85
|
+
|
86
|
+
# Returns a count of the number of documents in the association that have
|
87
|
+
# actually been persisted to the database.
|
88
|
+
#
|
89
|
+
# Use #size if you want the total number of documents.
|
90
|
+
#
|
91
|
+
# @example Get the count of persisted documents.
|
92
|
+
# person.addresses.count
|
93
|
+
#
|
94
|
+
# @return [ Integer ] The total number of persisted embedded docs, as
|
95
|
+
# flagged by the #persisted? method.
|
96
|
+
def count
|
97
|
+
target.select(&:persisted?).size
|
98
|
+
end
|
99
|
+
|
100
|
+
# Create a new document in the relation. This is essentially the same
|
101
|
+
# as doing a #build then #save on the new document.
|
102
|
+
#
|
103
|
+
# @example Create a new document in the relation.
|
104
|
+
# person.movies.create(:name => "Bozo")
|
105
|
+
#
|
106
|
+
# @param [ Hash ] attributes The attributes to build the document with.
|
107
|
+
# @param [ Class ] type Optional class to create the document with.
|
108
|
+
#
|
109
|
+
# @return [ Document ] The newly created document.
|
110
|
+
def create(attributes = {}, type = nil, &block)
|
111
|
+
build(attributes, type, &block).tap(&:save)
|
112
|
+
end
|
113
|
+
|
114
|
+
# Create a new document in the relation. This is essentially the same
|
115
|
+
# as doing a #build then #save on the new document. If validation
|
116
|
+
# failed on the document an error will get raised.
|
117
|
+
#
|
118
|
+
# @example Create the document.
|
119
|
+
# person.addresses.create!(:street => "Unter der Linden")</tt>
|
120
|
+
#
|
121
|
+
# @param [ Hash ] attributes The attributes to build the document with.
|
122
|
+
# @param [ Class ] type Optional class to create the document with.
|
123
|
+
#
|
124
|
+
# @raise [ Errors::Validations ] If a validation error occured.
|
125
|
+
#
|
126
|
+
# @return [ Document ] The newly created document.
|
127
|
+
def create!(attributes = {}, type = nil, &block)
|
128
|
+
build(attributes, type, &block).tap(&:save!)
|
129
|
+
end
|
130
|
+
|
131
|
+
# Delete the supplied document from the target. This method is proxied
|
132
|
+
# in order to reindex the array after the operation occurs.
|
133
|
+
#
|
134
|
+
# @example Delete the document from the relation.
|
135
|
+
# person.addresses.delete(address)
|
136
|
+
#
|
137
|
+
# @param [ Document ] document The document to be deleted.
|
138
|
+
#
|
139
|
+
# @return [ Document, nil ] The deleted document or nil if nothing deleted.
|
140
|
+
#
|
141
|
+
# @since 2.0.0.rc.1
|
142
|
+
def delete(document)
|
143
|
+
target.delete(document).tap { reindex }
|
144
|
+
end
|
145
|
+
|
146
|
+
# Delete all the documents in the association without running callbacks.
|
147
|
+
#
|
148
|
+
# @example Delete all documents from the relation.
|
149
|
+
# person.addresses.delete_all
|
150
|
+
#
|
151
|
+
# @example Conditionally delete documents from the relation.
|
152
|
+
# person.addresses.delete_all(:conditions => { :street => "Bond" })
|
153
|
+
#
|
154
|
+
# @param [ Hash ] conditions Conditions on which documents to delete.
|
155
|
+
#
|
156
|
+
# @return [ Integer ] The number of documents deleted.
|
157
|
+
def delete_all(conditions = {})
|
158
|
+
atomically(:$pull) { remove_all(conditions, :delete) }
|
159
|
+
end
|
160
|
+
|
161
|
+
# Destroy all the documents in the association whilst running callbacks.
|
162
|
+
#
|
163
|
+
# @example Destroy all documents from the relation.
|
164
|
+
# person.addresses.destroy_all
|
165
|
+
#
|
166
|
+
# @example Conditionally destroy documents from the relation.
|
167
|
+
# person.addresses.destroy_all(:conditions => { :street => "Bond" })
|
168
|
+
#
|
169
|
+
# @param [ Hash ] conditions Conditions on which documents to destroy.
|
170
|
+
#
|
171
|
+
# @return [ Integer ] The number of documents destroyed.
|
172
|
+
def destroy_all(conditions = {})
|
173
|
+
atomically(:$pull) { remove_all(conditions, :destroy) }
|
174
|
+
end
|
175
|
+
|
176
|
+
# Finds a document in this association through several different
|
177
|
+
# methods.
|
178
|
+
#
|
179
|
+
# @example Find a document by its id.
|
180
|
+
# person.addresses.find(BSON::ObjectId.new)
|
181
|
+
#
|
182
|
+
# @example Find documents for multiple ids.
|
183
|
+
# person.addresses.find([ BSON::ObjectId.new, BSON::ObjectId.new ])
|
184
|
+
#
|
185
|
+
# @example Find documents based on conditions.
|
186
|
+
# person.addresses.find(:all, :conditions => { :number => 10 })
|
187
|
+
# person.addresses.find(:first, :conditions => { :number => 10 })
|
188
|
+
# person.addresses.find(:last, :conditions => { :number => 10 })
|
189
|
+
#
|
190
|
+
# @param [ Array<Object> ] args Various arguments.
|
191
|
+
#
|
192
|
+
# @return [ Array<Document>, Document ] A single or multiple documents.
|
193
|
+
def find(*args)
|
194
|
+
criteria.find(*args)
|
195
|
+
end
|
196
|
+
|
197
|
+
# Instantiate a new embeds_many relation.
|
198
|
+
#
|
199
|
+
# @example Create the new relation.
|
200
|
+
# Many.new(person, addresses, metadata)
|
201
|
+
#
|
202
|
+
# @param [ Document ] base The document this relation hangs off of.
|
203
|
+
# @param [ Array<Document> ] target The child documents of the relation.
|
204
|
+
# @param [ Metadata ] metadata The relation's metadata
|
205
|
+
#
|
206
|
+
# @return [ Many ] The proxy.
|
207
|
+
def initialize(base, target, metadata)
|
208
|
+
init(base, target, metadata) do
|
209
|
+
target.each_with_index do |doc, index|
|
210
|
+
characterize_one(doc)
|
211
|
+
doc.parentize(base)
|
212
|
+
doc._index = index
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
# Will load the target into an array if the target had not already been
|
218
|
+
# loaded.
|
219
|
+
#
|
220
|
+
# @example Load the relation into memory.
|
221
|
+
# relation.load!
|
222
|
+
#
|
223
|
+
# @return [ Many ] The relation.
|
224
|
+
#
|
225
|
+
# @since 2.0.0.rc.5
|
226
|
+
def load!(options = {})
|
227
|
+
tap do |relation|
|
228
|
+
unless relation.loaded?
|
229
|
+
relation.bind(options)
|
230
|
+
relation.loaded = true
|
231
|
+
end
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
# Substitutes the supplied target documents for the existing documents
|
236
|
+
# in the relation.
|
237
|
+
#
|
238
|
+
# @example Substitute the relation's target.
|
239
|
+
# person.addresses.substitute([ address ])
|
240
|
+
#
|
241
|
+
# @param [ Array<Document> ] new_target The replacement array.
|
242
|
+
# @param [ true, false ] building Are we in build mode?
|
243
|
+
#
|
244
|
+
# @return [ Many ] The proxied relation.
|
245
|
+
#
|
246
|
+
# @since 2.0.0.rc.1
|
247
|
+
def substitute(new_target, options = {})
|
248
|
+
old_target = target
|
249
|
+
tap do |relation|
|
250
|
+
relation.target = new_target || []
|
251
|
+
if !new_target.blank?
|
252
|
+
atomically(:$set) { rebind(old_target, options) }
|
253
|
+
else
|
254
|
+
atomically(:$unset) { unbind(old_target, options) }
|
255
|
+
end
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
# Get this relation as as its representation in the database.
|
260
|
+
#
|
261
|
+
# @example Convert the relation to an attributes hash.
|
262
|
+
# person.addresses.as_document
|
263
|
+
#
|
264
|
+
# @return [ Array<Hash> ] The relation as stored in the db.
|
265
|
+
#
|
266
|
+
# @since 2.0.0.rc.1
|
267
|
+
def as_document
|
268
|
+
target.inject([]) do |attributes, doc|
|
269
|
+
attributes.tap do |attr|
|
270
|
+
attr << doc.as_document
|
271
|
+
end
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
275
|
+
# Unbind the inverse relation from this set of documents. Used when the
|
276
|
+
# entire proxy has been cleared, set to nil or empty, or replaced.
|
277
|
+
#
|
278
|
+
# @example Unbind the relation.
|
279
|
+
# person.addresses.unbind(target, :continue => false)
|
280
|
+
#
|
281
|
+
# @param [ Array<Document> ] old_target The relations previous target.
|
282
|
+
# @param [ Hash ] options The options to bind with.
|
283
|
+
#
|
284
|
+
# @option options [ true, false ] :binding Are we in build mode?
|
285
|
+
# @option options [ true, false ] :continue Continue binding the
|
286
|
+
# inverse?
|
287
|
+
#
|
288
|
+
# @since 2.0.0.rc.1
|
289
|
+
def unbind(old_target, options = {})
|
290
|
+
binding(old_target).unbind(options)
|
291
|
+
if base.persisted?
|
292
|
+
old_target.each do |doc|
|
293
|
+
doc.delete unless doc.destroyed?
|
294
|
+
end
|
295
|
+
end
|
296
|
+
end
|
297
|
+
|
298
|
+
private
|
299
|
+
|
300
|
+
# Appends the document to the target array, updating the index on the
|
301
|
+
# document at the same time.
|
302
|
+
#
|
303
|
+
# @example Append to the document.
|
304
|
+
# relation.append(document)
|
305
|
+
#
|
306
|
+
# @param [ Document ] document The document to append to the target.
|
307
|
+
#
|
308
|
+
# @since 2.0.0.rc.1
|
309
|
+
def append(document, options = {})
|
310
|
+
load! and target.push(document)
|
311
|
+
characterize_one(document)
|
312
|
+
bind_one(document, options)
|
313
|
+
document._index = target.size - 1
|
314
|
+
end
|
315
|
+
|
316
|
+
# Instantiate the binding associated with this relation.
|
317
|
+
#
|
318
|
+
# @example Create the binding.
|
319
|
+
# relation.binding([ address ])
|
320
|
+
#
|
321
|
+
# @param [ Array<Document> ] new_target The new documents to bind with.
|
322
|
+
#
|
323
|
+
# @return [ Binding ] The many binding.
|
324
|
+
#
|
325
|
+
# @since 2.0.0.rc.1
|
326
|
+
def binding(new_target = nil)
|
327
|
+
Bindings::Embedded::Many.new(base, new_target || target, metadata)
|
328
|
+
end
|
329
|
+
|
330
|
+
# Returns the criteria object for the target class with its documents set
|
331
|
+
# to target.
|
332
|
+
#
|
333
|
+
# @example Get a criteria for the relation.
|
334
|
+
# relation.criteria
|
335
|
+
#
|
336
|
+
# @return [ Criteria ] A new criteria.
|
337
|
+
def criteria
|
338
|
+
metadata.klass.criteria(true).tap do |criterion|
|
339
|
+
criterion.documents = target
|
340
|
+
end
|
341
|
+
end
|
342
|
+
|
343
|
+
# If the target array does not respond to the supplied method then try to
|
344
|
+
# find a named scope or criteria on the class and send the call there.
|
345
|
+
#
|
346
|
+
# If the method exists on the array, use the default proxy behavior.
|
347
|
+
#
|
348
|
+
# @param [ Symbol, String ] name The name of the method.
|
349
|
+
# @param [ Array ] args The method args
|
350
|
+
# @param [ Proc ] block Optional block to pass.
|
351
|
+
#
|
352
|
+
# @return [ Criteria, Object ] A Criteria or return value from the target.
|
353
|
+
def method_missing(name, *args, &block)
|
354
|
+
load!(:binding => true) and return super if target.respond_to?(name)
|
355
|
+
klass = metadata.klass
|
356
|
+
klass.send(:with_scope, criteria) do
|
357
|
+
criteria.send(name, *args, &block)
|
358
|
+
end
|
359
|
+
end
|
360
|
+
|
361
|
+
# Reindex all the target elements. This is useful when performing
|
362
|
+
# operations on the proxied target directly and the indices need to
|
363
|
+
# match that on the database side.
|
364
|
+
#
|
365
|
+
# @example Reindex the relation.
|
366
|
+
# person.addresses.reindex
|
367
|
+
#
|
368
|
+
# @since 2.0.0.rc.1
|
369
|
+
def reindex
|
370
|
+
target.each_with_index do |doc, index|
|
371
|
+
doc._index = index
|
372
|
+
end
|
373
|
+
end
|
374
|
+
|
375
|
+
# Remove all documents from the relation, either with a delete or a
|
376
|
+
# destroy depending on what this was called through.
|
377
|
+
#
|
378
|
+
# @example Destroy documents from the relation.
|
379
|
+
# relation.remove_all(:conditions => { :num => 1 }, true)
|
380
|
+
#
|
381
|
+
# @param [ Hash ] conditions Conditions to filter by.
|
382
|
+
# @param [ true, false ] destroy If true then destroy, else delete.
|
383
|
+
#
|
384
|
+
# @return [ Integer ] The number of documents removed.
|
385
|
+
def remove_all(conditions = {}, method = :delete)
|
386
|
+
criteria = find(:all, conditions || {})
|
387
|
+
criteria.size.tap do
|
388
|
+
criteria.each do |doc|
|
389
|
+
target.delete(doc)
|
390
|
+
doc.send(method, :suppress => true)
|
391
|
+
end
|
392
|
+
reindex
|
393
|
+
end
|
394
|
+
end
|
395
|
+
|
396
|
+
# Convenience method to clean up the substitute code. Unbinds the old
|
397
|
+
# target and reindexes.
|
398
|
+
#
|
399
|
+
# @example Rebind the relation.
|
400
|
+
# relation.rebind([])
|
401
|
+
#
|
402
|
+
# @param [ Array<Document> ] old_target The old target.
|
403
|
+
# @param [ Hash ] options The options passed to substitute.
|
404
|
+
#
|
405
|
+
# @since 2.0.0
|
406
|
+
def rebind(old_target, options)
|
407
|
+
reindex
|
408
|
+
unbind(old_target, options)
|
409
|
+
bind(options)
|
410
|
+
end
|
411
|
+
|
412
|
+
class << self
|
413
|
+
|
414
|
+
# Return the builder that is responsible for generating the documents
|
415
|
+
# that will be used by this relation.
|
416
|
+
#
|
417
|
+
# @example Get the builder.
|
418
|
+
# Embedded::Many.builder(meta, object)
|
419
|
+
#
|
420
|
+
# @param [ Metadata ] meta The metadata of the relation.
|
421
|
+
# @param [ Document, Hash ] object A document or attributes to build
|
422
|
+
# with.
|
423
|
+
#
|
424
|
+
# @return [ Builder ] A newly instantiated builder object.
|
425
|
+
#
|
426
|
+
# @since 2.0.0.rc.1
|
427
|
+
def builder(meta, object)
|
428
|
+
Builders::Embedded::Many.new(meta, object)
|
429
|
+
end
|
430
|
+
|
431
|
+
# Returns true if the relation is an embedded one. In this case
|
432
|
+
# always true.
|
433
|
+
#
|
434
|
+
# @example Is the relation embedded?
|
435
|
+
# Embedded::Many.embedded?
|
436
|
+
#
|
437
|
+
# @return [ true ] true.
|
438
|
+
#
|
439
|
+
# @since 2.0.0.rc.1
|
440
|
+
def embedded?
|
441
|
+
true
|
442
|
+
end
|
443
|
+
|
444
|
+
# Returns the macro for this relation. Used mostly as a helper in
|
445
|
+
# reflection.
|
446
|
+
#
|
447
|
+
# @example Get the relation macro.
|
448
|
+
# Mongoid::Relations::Embedded::Many.macro
|
449
|
+
#
|
450
|
+
# @return [ Symbol ] :embeds_many
|
451
|
+
#
|
452
|
+
# @since 2.0.0.rc.1
|
453
|
+
def macro
|
454
|
+
:embeds_many
|
455
|
+
end
|
456
|
+
|
457
|
+
# Return the nested builder that is responsible for generating the
|
458
|
+
# documents that will be used by this relation.
|
459
|
+
#
|
460
|
+
# @example Get the nested builder.
|
461
|
+
# NestedAttributes::Many.builder(attributes, options)
|
462
|
+
#
|
463
|
+
# @param [ Metadata ] metadata The relation metadata.
|
464
|
+
# @param [ Hash ] attributes The attributes to build with.
|
465
|
+
# @param [ Hash ] options The builder options.
|
466
|
+
#
|
467
|
+
# @option options [ true, false ] :allow_destroy Can documents be
|
468
|
+
# deleted?
|
469
|
+
# @option options [ Integer ] :limit Max number of documents to
|
470
|
+
# create at once.
|
471
|
+
# @option options [ Proc, Symbol ] :reject_if If documents match this
|
472
|
+
# option then they are ignored.
|
473
|
+
# @option options [ true, false ] :update_only Only existing documents
|
474
|
+
# can be modified.
|
475
|
+
#
|
476
|
+
# @return [ NestedBuilder ] The nested attributes builder.
|
477
|
+
#
|
478
|
+
# @since 2.0.0.rc.1
|
479
|
+
def nested_builder(metadata, attributes, options)
|
480
|
+
Builders::NestedAttributes::Many.new(metadata, attributes, options)
|
481
|
+
end
|
482
|
+
|
483
|
+
# Tells the caller if this relation is one that stores the foreign
|
484
|
+
# key on its own objects.
|
485
|
+
#
|
486
|
+
# @example Does this relation store a foreign key?
|
487
|
+
# Embedded::Many.stores_foreign_key?
|
488
|
+
#
|
489
|
+
# @return [ false ] false.
|
490
|
+
#
|
491
|
+
# @since 2.0.0.rc.1
|
492
|
+
def stores_foreign_key?
|
493
|
+
false
|
494
|
+
end
|
495
|
+
end
|
496
|
+
end
|
497
|
+
end
|
498
|
+
end
|
499
|
+
end
|