mongoid 6.1.0 → 6.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/mongoid/atomic.rb +3 -3
- data/lib/mongoid/attributes.rb +1 -2
- data/lib/mongoid/attributes/dynamic.rb +3 -2
- data/lib/mongoid/attributes/nested.rb +1 -1
- data/lib/mongoid/attributes/processing.rb +0 -4
- data/lib/mongoid/changeable.rb +1 -1
- data/lib/mongoid/clients/options.rb +1 -1
- data/lib/mongoid/composable.rb +3 -1
- data/lib/mongoid/contextual/atomic.rb +1 -1
- data/lib/mongoid/contextual/geo_near.rb +3 -2
- data/lib/mongoid/contextual/memory.rb +3 -3
- data/lib/mongoid/contextual/mongo.rb +5 -5
- data/lib/mongoid/contextual/none.rb +14 -2
- data/lib/mongoid/copyable.rb +1 -5
- data/lib/mongoid/criteria.rb +2 -2
- data/lib/mongoid/criteria/findable.rb +3 -2
- data/lib/mongoid/criteria/includable.rb +2 -2
- data/lib/mongoid/criteria/queryable/aggregable.rb +1 -1
- data/lib/mongoid/criteria/queryable/extensions/array.rb +1 -1
- data/lib/mongoid/criteria/queryable/extensions/boolean.rb +1 -1
- data/lib/mongoid/criteria/queryable/extensions/hash.rb +4 -4
- data/lib/mongoid/criteria/queryable/extensions/object.rb +4 -4
- data/lib/mongoid/criteria/queryable/extensions/regexp.rb +34 -0
- data/lib/mongoid/criteria/queryable/options.rb +16 -0
- data/lib/mongoid/criteria/queryable/selector.rb +1 -1
- data/lib/mongoid/document.rb +1 -1
- data/lib/mongoid/errors/delete_restriction.rb +2 -2
- data/lib/mongoid/errors/inverse_not_found.rb +1 -1
- data/lib/mongoid/extensions/hash.rb +3 -2
- data/lib/mongoid/extensions/object.rb +2 -2
- data/lib/mongoid/extensions/time.rb +1 -1
- data/lib/mongoid/factory.rb +0 -1
- data/lib/mongoid/fields/foreign_key.rb +2 -2
- data/lib/mongoid/findable.rb +1 -1
- data/lib/mongoid/indexable.rb +3 -2
- data/lib/mongoid/indexable/specification.rb +1 -1
- data/lib/mongoid/interceptable.rb +3 -9
- data/lib/mongoid/loggable.rb +1 -1
- data/lib/mongoid/matchable.rb +16 -9
- data/lib/mongoid/matchable/all.rb +2 -2
- data/lib/mongoid/matchable/and.rb +3 -3
- data/lib/mongoid/matchable/default.rb +2 -2
- data/lib/mongoid/matchable/elem_match.rb +3 -3
- data/lib/mongoid/matchable/exists.rb +2 -2
- data/lib/mongoid/matchable/gt.rb +2 -2
- data/lib/mongoid/matchable/gte.rb +2 -2
- data/lib/mongoid/matchable/in.rb +2 -2
- data/lib/mongoid/matchable/lt.rb +2 -2
- data/lib/mongoid/matchable/lte.rb +2 -2
- data/lib/mongoid/matchable/ne.rb +2 -2
- data/lib/mongoid/matchable/nin.rb +2 -2
- data/lib/mongoid/matchable/or.rb +3 -3
- data/lib/mongoid/matchable/regexp.rb +27 -0
- data/lib/mongoid/matchable/size.rb +2 -2
- data/lib/mongoid/persistable.rb +0 -3
- data/lib/mongoid/persistable/creatable.rb +2 -4
- data/lib/mongoid/persistable/deletable.rb +1 -1
- data/lib/mongoid/persistable/settable.rb +1 -1
- data/lib/mongoid/persistable/upsertable.rb +1 -1
- data/lib/mongoid/persistence_context.rb +2 -3
- data/lib/mongoid/relations/accessors.rb +1 -2
- data/lib/mongoid/relations/bindings/embedded/in.rb +0 -4
- data/lib/mongoid/relations/bindings/embedded/many.rb +1 -8
- data/lib/mongoid/relations/bindings/embedded/one.rb +0 -10
- data/lib/mongoid/relations/bindings/referenced/many.rb +0 -4
- data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +4 -4
- data/lib/mongoid/relations/builders/embedded/one.rb +1 -1
- data/lib/mongoid/relations/builders/nested_attributes/many.rb +1 -1
- data/lib/mongoid/relations/conversions.rb +1 -1
- data/lib/mongoid/relations/counter_cache.rb +10 -10
- data/lib/mongoid/relations/eager.rb +6 -11
- data/lib/mongoid/relations/eager/base.rb +4 -4
- data/lib/mongoid/relations/embedded/batchable.rb +3 -3
- data/lib/mongoid/relations/embedded/in.rb +1 -3
- data/lib/mongoid/relations/embedded/many.rb +12 -9
- data/lib/mongoid/relations/embedded/one.rb +1 -3
- data/lib/mongoid/relations/macros.rb +3 -2
- data/lib/mongoid/relations/metadata.rb +3 -3
- data/lib/mongoid/relations/nested_builder.rb +1 -1
- data/lib/mongoid/relations/proxy.rb +3 -2
- data/lib/mongoid/relations/referenced/in.rb +1 -4
- data/lib/mongoid/relations/referenced/many.rb +5 -7
- data/lib/mongoid/relations/referenced/many_to_many.rb +2 -2
- data/lib/mongoid/relations/referenced/one.rb +0 -2
- data/lib/mongoid/relations/reflections.rb +2 -2
- data/lib/mongoid/relations/synchronization.rb +11 -11
- data/lib/mongoid/scopable.rb +2 -2
- data/lib/mongoid/serializable.rb +4 -3
- data/lib/mongoid/tasks/database.rb +2 -1
- data/lib/mongoid/threaded.rb +4 -4
- data/lib/mongoid/validatable.rb +1 -1
- data/lib/mongoid/validatable/macros.rb +4 -2
- data/lib/mongoid/version.rb +1 -1
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +12 -2
- data/spec/app/models/book.rb +2 -1
- data/spec/app/models/page.rb +1 -1
- data/spec/mongoid/contextual/none_spec.rb +15 -0
- data/spec/mongoid/criteria/queryable/extensions/regexp_raw_spec.rb +89 -0
- data/spec/mongoid/criteria/queryable/selectable_spec.rb +15 -0
- data/spec/mongoid/criteria_spec.rb +6 -2
- data/spec/mongoid/interceptable_spec.rb +19 -0
- data/spec/mongoid/matchable/all_spec.rb +4 -4
- data/spec/mongoid/matchable/and_spec.rb +10 -10
- data/spec/mongoid/matchable/default_spec.rb +12 -12
- data/spec/mongoid/matchable/elem_match_spec.rb +11 -11
- data/spec/mongoid/matchable/exists_spec.rb +5 -5
- data/spec/mongoid/matchable/gt_spec.rb +8 -8
- data/spec/mongoid/matchable/gte_spec.rb +8 -8
- data/spec/mongoid/matchable/in_spec.rb +5 -5
- data/spec/mongoid/matchable/lt_spec.rb +8 -8
- data/spec/mongoid/matchable/lte_spec.rb +8 -8
- data/spec/mongoid/matchable/ne_spec.rb +5 -5
- data/spec/mongoid/matchable/nin_spec.rb +5 -5
- data/spec/mongoid/matchable/or_spec.rb +7 -7
- data/spec/mongoid/matchable/regexp_spec.rb +59 -0
- data/spec/mongoid/matchable/size_spec.rb +3 -3
- data/spec/mongoid/matchable_spec.rb +108 -58
- data/spec/mongoid/persistable/deletable_spec.rb +15 -0
- data/spec/mongoid/persistable/settable_spec.rb +16 -0
- data/spec/mongoid/persistence_context_spec.rb +22 -1
- data/spec/mongoid/relations/bindings/referenced/many_to_many_spec.rb +4 -4
- data/spec/mongoid/relations/embedded/many_spec.rb +24 -6
- data/spec/mongoid/relations/referenced/many_to_many_spec.rb +1 -1
- data/spec/mongoid/relations/synchronization_spec.rb +48 -2
- data/spec/mongoid/relations/touchable_spec.rb +40 -0
- data/spec/mongoid/validatable/uniqueness_spec.rb +2 -0
- data/spec/spec_helper.rb +1 -1
- metadata +9 -26
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -1
- metadata.gz.sig +0 -1
@@ -8,12 +8,12 @@ module Mongoid
|
|
8
8
|
# Return true if the attribute is less than or equal to the value.
|
9
9
|
#
|
10
10
|
# @example Do the values match?
|
11
|
-
# matcher.
|
11
|
+
# matcher._matches?({ :key => 10 })
|
12
12
|
#
|
13
13
|
# @param [ Hash ] value The values to check.
|
14
14
|
#
|
15
15
|
# @return [ true, false ] If a value exists.
|
16
|
-
def
|
16
|
+
def _matches?(value)
|
17
17
|
determine(value, :<=)
|
18
18
|
rescue ArgumentError
|
19
19
|
false
|
data/lib/mongoid/matchable/ne.rb
CHANGED
@@ -8,12 +8,12 @@ module Mongoid
|
|
8
8
|
# Return true if the attribute and first value are not equal.
|
9
9
|
#
|
10
10
|
# @example Do the values match?
|
11
|
-
# matcher.
|
11
|
+
# matcher._matches?({ :key => 10 })
|
12
12
|
#
|
13
13
|
# @param [ Hash ] value The values to check.
|
14
14
|
#
|
15
15
|
# @return [ true, false ] If a value exists.
|
16
|
-
def
|
16
|
+
def _matches?(value)
|
17
17
|
!super(value.values.first)
|
18
18
|
end
|
19
19
|
end
|
@@ -8,12 +8,12 @@ module Mongoid
|
|
8
8
|
# Return true if the attribute is not in the value list.
|
9
9
|
#
|
10
10
|
# @example Do the values match?
|
11
|
-
# matcher.
|
11
|
+
# matcher._matches?({ :key => 10 })
|
12
12
|
#
|
13
13
|
# @param [ Hash ] value The values to check.
|
14
14
|
#
|
15
15
|
# @return [ true, false ] If a value exists.
|
16
|
-
def
|
16
|
+
def _matches?(value)
|
17
17
|
attribute_array = @attribute.nil? ? [nil] : Array.wrap(@attribute)
|
18
18
|
attribute_array.none? { |e| value.values.first.include?(e) }
|
19
19
|
end
|
data/lib/mongoid/matchable/or.rb
CHANGED
@@ -8,20 +8,20 @@ module Mongoid
|
|
8
8
|
# Does the supplied query match the attribute?
|
9
9
|
#
|
10
10
|
# @example Does this match?
|
11
|
-
# matcher.
|
11
|
+
# matcher._matches?("$or" => [ { field => value } ])
|
12
12
|
#
|
13
13
|
# @param [ Array ] conditions The or expression.
|
14
14
|
#
|
15
15
|
# @return [ true, false ] True if matches, false if not.
|
16
16
|
#
|
17
17
|
# @since 2.0.0.rc.7
|
18
|
-
def
|
18
|
+
def _matches?(conditions)
|
19
19
|
conditions.each do |condition|
|
20
20
|
res = true
|
21
21
|
condition.keys.each do |k|
|
22
22
|
key = k
|
23
23
|
value = condition[k]
|
24
|
-
res &&= document.
|
24
|
+
res &&= document._matches?(key => value)
|
25
25
|
break unless res
|
26
26
|
end
|
27
27
|
return res if res
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Matchable
|
3
|
+
|
4
|
+
# Defines behavior for handling regular expressions in embedded documents.
|
5
|
+
class Regexp < Default
|
6
|
+
|
7
|
+
# Does the supplied query match the attribute?
|
8
|
+
#
|
9
|
+
# @example Does this match?
|
10
|
+
# matcher._matches?(/^Em/)
|
11
|
+
# matcher._matches?(BSON::Regex::Raw.new("^Em"))
|
12
|
+
#
|
13
|
+
# @param [ BSON::Regexp::Raw, Regexp ] regexp The regular expression object.
|
14
|
+
#
|
15
|
+
# @return [ true, false ] True if matches, false if not.
|
16
|
+
#
|
17
|
+
# @since 5.2.1
|
18
|
+
def _matches?(regexp)
|
19
|
+
if native_regexp = regexp.try(:compile)
|
20
|
+
super(native_regexp)
|
21
|
+
else
|
22
|
+
super
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -8,12 +8,12 @@ module Mongoid
|
|
8
8
|
# Return true if the attribute size is equal to the first value.
|
9
9
|
#
|
10
10
|
# @example Do the values match?
|
11
|
-
# matcher.
|
11
|
+
# matcher._matches?({ :key => 10 })
|
12
12
|
#
|
13
13
|
# @param [ Hash ] value The values to check.
|
14
14
|
#
|
15
15
|
# @return [ true, false ] If a value exists.
|
16
|
-
def
|
16
|
+
def _matches?(value)
|
17
17
|
@attribute.size == value.values.first
|
18
18
|
end
|
19
19
|
end
|
data/lib/mongoid/persistable.rb
CHANGED
@@ -69,8 +69,6 @@ module Mongoid
|
|
69
69
|
# @example Raise the validation error.
|
70
70
|
# Person.fail_due_to_validation!(person)
|
71
71
|
#
|
72
|
-
# @param [ Document ] document The document to fail.
|
73
|
-
#
|
74
72
|
# @raise [ Errors::Validations ] The validation error.
|
75
73
|
#
|
76
74
|
# @since 4.0.0
|
@@ -83,7 +81,6 @@ module Mongoid
|
|
83
81
|
# @example Raise the callback error.
|
84
82
|
# Person.fail_due_to_callback!(person, :create!)
|
85
83
|
#
|
86
|
-
# @param [ Document ] document The document to fail.
|
87
84
|
# @param [ Symbol ] method The method being called.
|
88
85
|
#
|
89
86
|
# @raise [ Errors::Callback ] The callback error.
|
@@ -42,7 +42,7 @@ module Mongoid
|
|
42
42
|
#
|
43
43
|
# @since 2.1.0
|
44
44
|
def atomic_inserts
|
45
|
-
{ atomic_insert_modifier => { atomic_position =>
|
45
|
+
{ atomic_insert_modifier => { atomic_position => as_attributes }}
|
46
46
|
end
|
47
47
|
|
48
48
|
# Insert the embedded document.
|
@@ -76,7 +76,7 @@ module Mongoid
|
|
76
76
|
#
|
77
77
|
# @since 4.0.0
|
78
78
|
def insert_as_root
|
79
|
-
collection.insert_one(
|
79
|
+
collection.insert_one(as_attributes)
|
80
80
|
end
|
81
81
|
|
82
82
|
# Post process an insert, which sets the new record attribute to false
|
@@ -165,8 +165,6 @@ module Mongoid
|
|
165
165
|
#
|
166
166
|
# @param [ Hash, Array ] attributes The attributes to create with, or an
|
167
167
|
# Array of multiple attributes for multiple documents.
|
168
|
-
# @param [ Hash ] options A mass-assignment protection options. Supports
|
169
|
-
# :as and :without_protection
|
170
168
|
#
|
171
169
|
# @return [ Document, Array<Document> ] The newly created document(s).
|
172
170
|
#
|
@@ -28,7 +28,7 @@ module Mongoid
|
|
28
28
|
|
29
29
|
if fields[field] && fields[field].type == Hash && attributes.key?(field)
|
30
30
|
merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
|
31
|
-
value = attributes[field].merge(field_and_value_hash[field], &merger)
|
31
|
+
value = (attributes[field] || {}).merge(field_and_value_hash[field], &merger)
|
32
32
|
process_attribute(field.to_s, value)
|
33
33
|
else
|
34
34
|
process_attribute(field.to_s, field_and_value_hash[field])
|
@@ -44,7 +44,7 @@ module Mongoid
|
|
44
44
|
# @example Create a new persistence context.
|
45
45
|
# PersistenceContext.new(model, collection: 'other')
|
46
46
|
#
|
47
|
-
# @param [ Object ]
|
47
|
+
# @param [ Object ] object The class or model instance for which a persistence context
|
48
48
|
# should be created.
|
49
49
|
# @param [ Hash ] opts The persistence context options.
|
50
50
|
#
|
@@ -67,8 +67,7 @@ module Mongoid
|
|
67
67
|
#
|
68
68
|
# @since 6.0.0
|
69
69
|
def collection(parent = nil)
|
70
|
-
|
71
|
-
client[name.to_sym]
|
70
|
+
parent ? parent.collection.with(client_options) : client[collection_name.to_sym]
|
72
71
|
end
|
73
72
|
|
74
73
|
# Get the collection name for this persistence context.
|
@@ -17,7 +17,6 @@ module Mongoid
|
|
17
17
|
# @param [ String, Symbol ] name The name of the relation.
|
18
18
|
# @param [ Hash, BSON::ObjectId ] object The id or attributes to use.
|
19
19
|
# @param [ Metadata ] metadata The relation's metadata.
|
20
|
-
# @param [ true, false ] building If we are in a build operation.
|
21
20
|
#
|
22
21
|
# @return [ Proxy ] The relation.
|
23
22
|
#
|
@@ -32,7 +31,7 @@ module Mongoid
|
|
32
31
|
# @example Create the relation.
|
33
32
|
# person.create_relation(document, metadata)
|
34
33
|
#
|
35
|
-
# @param [ Document, Array<Document ] object The relation target.
|
34
|
+
# @param [ Document, Array<Document> ] object The relation target.
|
36
35
|
# @param [ Metadata ] metadata The relation metadata.
|
37
36
|
#
|
38
37
|
# @return [ Proxy ] The relation.
|
@@ -17,8 +17,6 @@ module Mongoid
|
|
17
17
|
# name.person.bind(:continue => true)
|
18
18
|
# name.person = Person.new
|
19
19
|
#
|
20
|
-
# @param [ Hash ] options The binding options.
|
21
|
-
#
|
22
20
|
# @option options [ true, false ] :continue Continue binding the inverse.
|
23
21
|
# @option options [ true, false ] :binding Are we in build mode?
|
24
22
|
#
|
@@ -42,8 +40,6 @@ module Mongoid
|
|
42
40
|
# name.person.unbind(:continue => true)
|
43
41
|
# name.person = nil
|
44
42
|
#
|
45
|
-
# @param [ Hash ] options The options to pass through.
|
46
|
-
#
|
47
43
|
# @option options [ true, false ] :continue Do we continue unbinding?
|
48
44
|
#
|
49
45
|
# @since 2.0.0.rc.1
|
@@ -14,10 +14,6 @@ module Mongoid
|
|
14
14
|
# person.addresses.bind_one(address)
|
15
15
|
#
|
16
16
|
# @param [ Document ] doc The single document to bind.
|
17
|
-
# @param [ Hash ] options The binding options.
|
18
|
-
#
|
19
|
-
# @option options [ true, false ] :continue Continue binding the inverse.
|
20
|
-
# @option options [ true, false ] :binding Are we in build mode?
|
21
17
|
#
|
22
18
|
# @since 2.0.0.rc.1
|
23
19
|
def bind_one(doc)
|
@@ -32,10 +28,7 @@ module Mongoid
|
|
32
28
|
# @example Unbind the document.
|
33
29
|
# person.addresses.unbind_one(document)
|
34
30
|
#
|
35
|
-
# @param [
|
36
|
-
#
|
37
|
-
# @option options [ true, false ] :continue Continue binding the inverse.
|
38
|
-
# @option options [ true, false ] :binding Are we in build mode?
|
31
|
+
# @param [ Document ] doc The single document to unbind.
|
39
32
|
#
|
40
33
|
# @since 2.0.0.rc.1
|
41
34
|
def unbind_one(doc)
|
@@ -17,11 +17,6 @@ module Mongoid
|
|
17
17
|
# person.name.bind(:continue => true)
|
18
18
|
# person.name = Name.new
|
19
19
|
#
|
20
|
-
# @param [ Hash ] options The options to pass through.
|
21
|
-
#
|
22
|
-
# @option options [ true, false ] :continue Do we continue binding?
|
23
|
-
# @option options [ true, false ] :binding Are we in build mode?
|
24
|
-
#
|
25
20
|
# @since 2.0.0.rc.1
|
26
21
|
def bind_one
|
27
22
|
target.parentize(base)
|
@@ -37,11 +32,6 @@ module Mongoid
|
|
37
32
|
# person.name.unbind(:continue => true)
|
38
33
|
# person.name = nil
|
39
34
|
#
|
40
|
-
# @param [ Hash ] options The options to pass through.
|
41
|
-
#
|
42
|
-
# @option options [ true, false ] :continue Do we continue unbinding?
|
43
|
-
# @option options [ true, false ] :binding Are we in build mode?
|
44
|
-
#
|
45
35
|
# @since 2.0.0.rc.1
|
46
36
|
def unbind_one
|
47
37
|
binding do
|
@@ -13,8 +13,6 @@ module Mongoid
|
|
13
13
|
# @example Bind one document.
|
14
14
|
# person.posts.bind_one(post)
|
15
15
|
#
|
16
|
-
# @param [ Document ] doc The single document to bind.
|
17
|
-
#
|
18
16
|
# @since 2.0.0.rc.1
|
19
17
|
def bind_one(doc)
|
20
18
|
binding do
|
@@ -27,8 +25,6 @@ module Mongoid
|
|
27
25
|
# @example Unbind the document.
|
28
26
|
# person.posts.unbind_one(document)
|
29
27
|
#
|
30
|
-
# @param [ Document ] document The document to unbind.
|
31
|
-
#
|
32
28
|
# @since 2.0.0.rc.1
|
33
29
|
def unbind_one(doc)
|
34
30
|
binding do
|
@@ -26,8 +26,8 @@ module Mongoid
|
|
26
26
|
end
|
27
27
|
doc.reset_relation_criteria(metadata.inverse)
|
28
28
|
end
|
29
|
-
base.
|
30
|
-
doc.
|
29
|
+
base._synced[metadata.foreign_key] = true
|
30
|
+
doc._synced[metadata.inverse_foreign_key] = true
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
@@ -45,8 +45,8 @@ module Mongoid
|
|
45
45
|
inverse_keys.delete_one(inverse_record_id(doc))
|
46
46
|
doc.reset_relation_criteria(metadata.inverse)
|
47
47
|
end
|
48
|
-
base.
|
49
|
-
doc.
|
48
|
+
base._synced[metadata.foreign_key] = true
|
49
|
+
doc._synced[metadata.inverse_foreign_key] = true
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
@@ -12,7 +12,7 @@ module Mongoid
|
|
12
12
|
# @example Build the document.
|
13
13
|
# Builder.new(meta, attrs).build
|
14
14
|
#
|
15
|
-
# @param [ String ]
|
15
|
+
# @param [ String ] _type Not used in this context.
|
16
16
|
#
|
17
17
|
# @return [ Document ] A single document.
|
18
18
|
def build(_type = nil)
|
@@ -60,7 +60,7 @@ module Mongoid
|
|
60
60
|
# @example Is the document destroyable?
|
61
61
|
# destroyable?({ :_destroy => "1" })
|
62
62
|
#
|
63
|
-
# @
|
63
|
+
# @param [ Hash ] attributes The attributes to pull the flag from.
|
64
64
|
#
|
65
65
|
# @return [ true, false ] If the relation can potentially be deleted.
|
66
66
|
def destroyable?(attributes)
|
@@ -13,7 +13,7 @@ module Mongoid
|
|
13
13
|
# Conversions.flag(metadata, 15)
|
14
14
|
#
|
15
15
|
# @param [ Object ] object The object to flag.
|
16
|
-
# @param [ Metadata ] The relation metadata.
|
16
|
+
# @param [ Metadata ] metadata The relation metadata.
|
17
17
|
#
|
18
18
|
# @return [ Object ] The provided object.
|
19
19
|
#
|
@@ -11,7 +11,7 @@ module Mongoid
|
|
11
11
|
# @example Reset the given counter cache
|
12
12
|
# post.reset_counters(:comments)
|
13
13
|
#
|
14
|
-
# @param [ Symbol, Array ] One or more counter caches to reset
|
14
|
+
# @param [ Symbol, Array ] counters One or more counter caches to reset
|
15
15
|
#
|
16
16
|
# @since 4.0.0
|
17
17
|
def reset_counters(*counters)
|
@@ -29,8 +29,8 @@ module Mongoid
|
|
29
29
|
# @example Reset the given counter cache
|
30
30
|
# Post.reset_counters('50e0edd97c71c17ea9000001', :comments)
|
31
31
|
#
|
32
|
-
# @param [ String ] The id of the object that will be reset.
|
33
|
-
# @param [ Symbol, Array ] One or more counter caches to reset
|
32
|
+
# @param [ String ] id The id of the object that will be reset.
|
33
|
+
# @param [ Symbol, Array ] counters One or more counter caches to reset
|
34
34
|
#
|
35
35
|
# @since 3.1.0
|
36
36
|
def reset_counters(id, *counters)
|
@@ -51,8 +51,8 @@ module Mongoid
|
|
51
51
|
# Post.update_counters('50e0edd97c71c17ea9000001',
|
52
52
|
# :comments_count => 5, :likes_count => -2)
|
53
53
|
#
|
54
|
-
# @param [ String ] The id of the object to update.
|
55
|
-
# @param [ Hash ]
|
54
|
+
# @param [ String ] id The id of the object to update.
|
55
|
+
# @param [ Hash ] counters
|
56
56
|
#
|
57
57
|
# @since 3.1.0
|
58
58
|
def update_counters(id, counters)
|
@@ -66,8 +66,8 @@ module Mongoid
|
|
66
66
|
# @example Increment comments counter
|
67
67
|
# Post.increment_counter(:comments_count, '50e0edd97c71c17ea9000001')
|
68
68
|
#
|
69
|
-
# @param [ Symbol ] Counter cache name
|
70
|
-
# @param [ String ] The id of the object that will have its counter incremented.
|
69
|
+
# @param [ Symbol ] counter_name Counter cache name
|
70
|
+
# @param [ String ] id The id of the object that will have its counter incremented.
|
71
71
|
#
|
72
72
|
# @since 3.1.0
|
73
73
|
def increment_counter(counter_name, id)
|
@@ -81,8 +81,8 @@ module Mongoid
|
|
81
81
|
# @example Decrement comments counter
|
82
82
|
# Post.decrement_counter(:comments_count, '50e0edd97c71c17ea9000001')
|
83
83
|
#
|
84
|
-
# @param [ Symbol ] Counter cache name
|
85
|
-
# @param [ String ] The id of the object that will have its counter decremented.
|
84
|
+
# @param [ Symbol ] counter_name Counter cache name
|
85
|
+
# @param [ String ] id The id of the object that will have its counter decremented.
|
86
86
|
#
|
87
87
|
# @since 3.1.0
|
88
88
|
def decrement_counter(counter_name, id)
|
@@ -98,7 +98,7 @@ module Mongoid
|
|
98
98
|
# @example Add the touchable.
|
99
99
|
# Person.add_counter_cache_callbacks(meta)
|
100
100
|
#
|
101
|
-
# @param [ Metadata ]
|
101
|
+
# @param [ Metadata ] meta The metadata for the relation.
|
102
102
|
#
|
103
103
|
# @since 3.1.0
|
104
104
|
def add_counter_cache_callbacks(meta)
|
@@ -22,19 +22,14 @@ module Mongoid
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def preload(relations, docs)
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
25
|
+
relations.group_by(&:inverse_class_name)
|
26
|
+
.values
|
27
|
+
.each do |associations|
|
28
|
+
associations.group_by(&:relation)
|
29
|
+
.each do |relation, association|
|
30
|
+
relation.eager_load_klass.new(association, docs).run
|
31
31
|
end
|
32
32
|
end
|
33
|
-
grouped_relations.each do |_klass, associations|
|
34
|
-
docs = associations.collect do |_relation, association|
|
35
|
-
_relation.eager_load_klass.new(association, docs).run
|
36
|
-
end.flatten
|
37
|
-
end
|
38
33
|
end
|
39
34
|
end
|
40
35
|
end
|