mongoid 8.0.1 → 8.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/lib/config/locales/en.yml +1 -0
- data/lib/mongoid/association/accessors.rb +6 -6
- data/lib/mongoid/association/bindable.rb +2 -2
- data/lib/mongoid/association/constrainable.rb +0 -1
- data/lib/mongoid/association/embedded/batchable.rb +5 -5
- data/lib/mongoid/association/embedded/cyclic.rb +1 -1
- data/lib/mongoid/association/embedded/embedded_in/proxy.rb +2 -2
- data/lib/mongoid/association/embedded/embedded_in.rb +1 -1
- data/lib/mongoid/association/embedded/embeds_many/proxy.rb +10 -10
- data/lib/mongoid/association/embedded/embeds_many.rb +2 -2
- data/lib/mongoid/association/embedded/embeds_one/proxy.rb +2 -2
- data/lib/mongoid/association/embedded/embeds_one.rb +3 -3
- data/lib/mongoid/association/many.rb +6 -7
- data/lib/mongoid/association/nested/many.rb +3 -3
- data/lib/mongoid/association/nested/nested_buildable.rb +4 -4
- data/lib/mongoid/association/nested/one.rb +5 -5
- data/lib/mongoid/association/one.rb +2 -2
- data/lib/mongoid/association/options.rb +9 -9
- data/lib/mongoid/association/proxy.rb +2 -3
- data/lib/mongoid/association/referenced/auto_save.rb +1 -1
- data/lib/mongoid/association/referenced/belongs_to/proxy.rb +5 -6
- data/lib/mongoid/association/referenced/belongs_to.rb +1 -1
- data/lib/mongoid/association/referenced/counter_cache.rb +2 -2
- data/lib/mongoid/association/referenced/eager.rb +2 -2
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb +2 -2
- data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +2 -2
- data/lib/mongoid/association/referenced/has_many/enumerable.rb +10 -10
- data/lib/mongoid/association/referenced/has_many/proxy.rb +12 -9
- data/lib/mongoid/association/referenced/has_many.rb +3 -3
- data/lib/mongoid/association/referenced/has_one/nested_builder.rb +5 -5
- data/lib/mongoid/association/referenced/has_one/proxy.rb +1 -1
- data/lib/mongoid/association/referenced/has_one.rb +3 -3
- data/lib/mongoid/association/referenced/syncable.rb +2 -2
- data/lib/mongoid/association/reflections.rb +2 -2
- data/lib/mongoid/association/relatable.rb +6 -6
- data/lib/mongoid/association.rb +5 -5
- data/lib/mongoid/atomic/modifiers.rb +2 -2
- data/lib/mongoid/attributes/dynamic.rb +3 -3
- data/lib/mongoid/attributes/nested.rb +5 -5
- data/lib/mongoid/attributes/processing.rb +1 -1
- data/lib/mongoid/attributes/projector.rb +1 -1
- data/lib/mongoid/attributes/readonly.rb +2 -2
- data/lib/mongoid/attributes.rb +13 -13
- data/lib/mongoid/cacheable.rb +2 -2
- data/lib/mongoid/changeable.rb +5 -5
- data/lib/mongoid/clients/options.rb +1 -1
- data/lib/mongoid/clients/validators/storage.rb +3 -3
- data/lib/mongoid/config/validators/client.rb +6 -6
- data/lib/mongoid/config.rb +7 -7
- data/lib/mongoid/contextual/aggregable/memory.rb +1 -1
- data/lib/mongoid/contextual/aggregable/mongo.rb +4 -4
- data/lib/mongoid/contextual/aggregable/none.rb +1 -1
- data/lib/mongoid/contextual/atomic.rb +1 -1
- data/lib/mongoid/contextual/geo_near.rb +7 -7
- data/lib/mongoid/contextual/memory.rb +5 -5
- data/lib/mongoid/contextual/mongo.rb +14 -14
- data/lib/mongoid/contextual/none.rb +2 -2
- data/lib/mongoid/contextual/queryable.rb +1 -1
- data/lib/mongoid/contextual.rb +2 -2
- data/lib/mongoid/criteria/findable.rb +7 -4
- data/lib/mongoid/criteria/includable.rb +1 -1
- data/lib/mongoid/criteria/permission.rb +1 -1
- data/lib/mongoid/criteria/queryable/aggregable.rb +2 -2
- data/lib/mongoid/criteria/queryable/extensions/boolean.rb +1 -1
- data/lib/mongoid/criteria/queryable/extensions/regexp.rb +2 -2
- data/lib/mongoid/criteria/queryable/extensions/set.rb +1 -1
- data/lib/mongoid/criteria/queryable/extensions/string.rb +3 -3
- data/lib/mongoid/criteria/queryable/extensions/symbol.rb +1 -1
- data/lib/mongoid/criteria/queryable/key.rb +3 -3
- data/lib/mongoid/criteria/queryable/optional.rb +2 -2
- data/lib/mongoid/criteria/queryable/options.rb +1 -1
- data/lib/mongoid/criteria/queryable/pipeline.rb +1 -1
- data/lib/mongoid/criteria/queryable/selectable.rb +3 -3
- data/lib/mongoid/criteria/queryable/selector.rb +3 -3
- data/lib/mongoid/criteria/queryable/smash.rb +1 -1
- data/lib/mongoid/criteria/queryable.rb +1 -1
- data/lib/mongoid/criteria/scopable.rb +2 -2
- data/lib/mongoid/criteria.rb +14 -9
- data/lib/mongoid/deprecable.rb +1 -1
- data/lib/mongoid/document.rb +2 -2
- data/lib/mongoid/equality.rb +12 -12
- data/lib/mongoid/errors/document_not_found.rb +5 -5
- data/lib/mongoid/errors/invalid_config_option.rb +1 -1
- data/lib/mongoid/errors/invalid_dependent_strategy.rb +1 -1
- data/lib/mongoid/errors/invalid_field.rb +1 -1
- data/lib/mongoid/errors/invalid_relation.rb +1 -1
- data/lib/mongoid/errors/invalid_relation_option.rb +1 -1
- data/lib/mongoid/errors/invalid_session_use.rb +1 -1
- data/lib/mongoid/errors/invalid_storage_options.rb +1 -1
- data/lib/mongoid/errors/mongoid_error.rb +3 -3
- data/lib/mongoid/errors/nested_attributes_metadata_not_found.rb +1 -1
- data/lib/mongoid/errors/no_client_database.rb +1 -1
- data/lib/mongoid/errors/no_client_hosts.rb +1 -1
- data/lib/mongoid/errors/readonly_attribute.rb +1 -1
- data/lib/mongoid/errors/unknown_attribute.rb +1 -1
- data/lib/mongoid/extensions/array.rb +1 -1
- data/lib/mongoid/extensions/big_decimal.rb +4 -0
- data/lib/mongoid/extensions/false_class.rb +1 -1
- data/lib/mongoid/extensions/float.rb +6 -2
- data/lib/mongoid/extensions/hash.rb +1 -1
- data/lib/mongoid/extensions/integer.rb +6 -2
- data/lib/mongoid/extensions/module.rb +1 -1
- data/lib/mongoid/extensions/object.rb +6 -6
- data/lib/mongoid/extensions/string.rb +9 -9
- data/lib/mongoid/extensions/symbol.rb +1 -1
- data/lib/mongoid/extensions/true_class.rb +1 -1
- data/lib/mongoid/fields/foreign_key.rb +4 -4
- data/lib/mongoid/fields/localized.rb +9 -4
- data/lib/mongoid/fields/standard.rb +7 -7
- data/lib/mongoid/fields.rb +9 -9
- data/lib/mongoid/findable.rb +7 -6
- data/lib/mongoid/indexable/specification.rb +1 -1
- data/lib/mongoid/indexable/validators/options.rb +2 -1
- data/lib/mongoid/interceptable.rb +4 -4
- data/lib/mongoid/matchable.rb +1 -1
- data/lib/mongoid/matcher.rb +12 -7
- data/lib/mongoid/persistable/creatable.rb +4 -4
- data/lib/mongoid/persistable/deletable.rb +1 -1
- data/lib/mongoid/persistable/destroyable.rb +1 -1
- data/lib/mongoid/persistable/savable.rb +2 -2
- data/lib/mongoid/persistable/unsettable.rb +1 -1
- data/lib/mongoid/persistable/updatable.rb +7 -7
- data/lib/mongoid/persistable/upsertable.rb +1 -1
- data/lib/mongoid/persistable.rb +3 -3
- data/lib/mongoid/persistence_context.rb +44 -8
- data/lib/mongoid/query_cache.rb +2 -2
- data/lib/mongoid/scopable.rb +26 -22
- data/lib/mongoid/serializable.rb +10 -6
- data/lib/mongoid/stateful.rb +10 -10
- data/lib/mongoid/tasks/database.rb +0 -2
- data/lib/mongoid/threaded/lifecycle.rb +5 -5
- data/lib/mongoid/threaded.rb +12 -12
- data/lib/mongoid/timestamps/updated.rb +1 -1
- data/lib/mongoid/traversable.rb +3 -3
- data/lib/mongoid/validatable/localizable.rb +1 -1
- data/lib/mongoid/validatable/macros.rb +0 -2
- data/lib/mongoid/validatable/presence.rb +2 -2
- data/lib/mongoid/validatable/uniqueness.rb +7 -7
- data/lib/mongoid/validatable.rb +6 -6
- data/lib/mongoid/version.rb +1 -1
- data/spec/integration/i18n_fallbacks_spec.rb +1 -17
- data/spec/mongoid/cacheable_spec.rb +3 -3
- data/spec/mongoid/clients_spec.rb +25 -0
- data/spec/mongoid/contextual/memory_spec.rb +4 -5
- data/spec/mongoid/contextual/mongo_spec.rb +2 -4
- data/spec/mongoid/criteria_projection_spec.rb +0 -1
- data/spec/mongoid/criteria_spec.rb +1 -1
- data/spec/mongoid/equality_spec.rb +12 -12
- data/spec/mongoid/extensions/big_decimal_spec.rb +15 -0
- data/spec/mongoid/extensions/float_spec.rb +10 -3
- data/spec/mongoid/extensions/integer_spec.rb +10 -3
- data/spec/mongoid/fields/localized_spec.rb +37 -12
- data/spec/mongoid/indexable_spec.rb +23 -1
- data/spec/mongoid/scopable_spec.rb +34 -16
- data/spec/mongoid/validatable/uniqueness_spec.rb +0 -1
- data/spec/support/macros.rb +16 -0
- data.tar.gz.sig +0 -0
- metadata +643 -643
- metadata.gz.sig +0 -0
@@ -72,10 +72,14 @@ module Mongoid
|
|
72
72
|
BSON::Decimal128.new(object)
|
73
73
|
elsif object.numeric?
|
74
74
|
BSON::Decimal128.new(object.to_s)
|
75
|
+
elsif !object.is_a?(String)
|
76
|
+
object.try(:to_d)
|
75
77
|
end
|
76
78
|
else
|
77
79
|
if object.is_a?(BSON::Decimal128) || object.numeric?
|
78
80
|
object.to_s
|
81
|
+
elsif !object.is_a?(String)
|
82
|
+
object.try(:to_d)
|
79
83
|
end
|
80
84
|
end
|
81
85
|
end
|
@@ -21,7 +21,7 @@ module Mongoid
|
|
21
21
|
#
|
22
22
|
# @param [ Class ] other The class to check.
|
23
23
|
#
|
24
|
-
# @return [ true
|
24
|
+
# @return [ true | false ] If the other is a boolean.
|
25
25
|
def is_a?(other)
|
26
26
|
if other == Mongoid::Boolean || other.class == Mongoid::Boolean
|
27
27
|
return true
|
@@ -37,8 +37,12 @@ module Mongoid
|
|
37
37
|
# @return [ Float | nil ] The object mongoized or nil.
|
38
38
|
def mongoize(object)
|
39
39
|
return if object.blank?
|
40
|
-
if object.
|
41
|
-
object.
|
40
|
+
if object.is_a?(String)
|
41
|
+
if object.numeric?
|
42
|
+
object.to_f
|
43
|
+
end
|
44
|
+
else
|
45
|
+
object.try(:to_f)
|
42
46
|
end
|
43
47
|
end
|
44
48
|
alias :demongoize :mongoize
|
@@ -190,7 +190,7 @@ module Mongoid
|
|
190
190
|
#
|
191
191
|
# @param [ String ] operator The operator.
|
192
192
|
# @param [ Class ] klass The model class.
|
193
|
-
# @param [ String
|
193
|
+
# @param [ String | Symbol ] key The field key.
|
194
194
|
# @param [ Object ] value The value to mongoize.
|
195
195
|
#
|
196
196
|
# @return [ Object ] The mongoized value.
|
@@ -45,8 +45,12 @@ module Mongoid
|
|
45
45
|
# @return [ Integer | nil ] The object mongoized or nil.
|
46
46
|
def mongoize(object)
|
47
47
|
return if object.blank?
|
48
|
-
if object.
|
49
|
-
object.
|
48
|
+
if object.is_a?(String)
|
49
|
+
if object.numeric?
|
50
|
+
object.to_i
|
51
|
+
end
|
52
|
+
else
|
53
|
+
object.try(:to_i)
|
50
54
|
end
|
51
55
|
end
|
52
56
|
alias :demongoize :mongoize
|
@@ -90,10 +90,10 @@ module Mongoid
|
|
90
90
|
# @example Do or do not.
|
91
91
|
# object.do_or_do_not(:use, "The Force")
|
92
92
|
#
|
93
|
-
# @param [ String
|
93
|
+
# @param [ String | Symbol ] name The method name.
|
94
94
|
# @param [ Array ] args The arguments.
|
95
95
|
#
|
96
|
-
# @return [ Object
|
96
|
+
# @return [ Object | nil ] The result of the method call or nil if the
|
97
97
|
# method does not exist.
|
98
98
|
def do_or_do_not(name, *args)
|
99
99
|
send(name, *args) if name && respond_to?(name)
|
@@ -106,7 +106,7 @@ module Mongoid
|
|
106
106
|
#
|
107
107
|
# @param [ String ] name The name of the variable.
|
108
108
|
#
|
109
|
-
# @return [ Object
|
109
|
+
# @return [ Object | false ] The value or false.
|
110
110
|
def ivar(name)
|
111
111
|
var_name = "@_#{name}"
|
112
112
|
if instance_variable_defined?(var_name)
|
@@ -154,7 +154,7 @@ module Mongoid
|
|
154
154
|
#
|
155
155
|
# @param [ String ] name The name of the variable.
|
156
156
|
#
|
157
|
-
# @return [ true
|
157
|
+
# @return [ true | false ] If the variable was defined.
|
158
158
|
def remove_ivar(name)
|
159
159
|
if instance_variable_defined?("@_#{name}")
|
160
160
|
return remove_instance_variable("@_#{name}")
|
@@ -189,10 +189,10 @@ module Mongoid
|
|
189
189
|
# @example You must perform this execution.
|
190
190
|
# object.you_must(:use, "The Force")
|
191
191
|
#
|
192
|
-
# @param [ String
|
192
|
+
# @param [ String | Symbol ] name The method name.
|
193
193
|
# @param [ Array ] args The arguments.
|
194
194
|
#
|
195
|
-
# @return [ Object
|
195
|
+
# @return [ Object | nil ] The result of the method call or nil if the
|
196
196
|
# method does not exist. Nil if the object is frozen.
|
197
197
|
def you_must(name, *args)
|
198
198
|
frozen? ? nil : do_or_do_not(name, *args)
|
@@ -12,7 +12,7 @@ module Mongoid
|
|
12
12
|
# @example Evolve the string.
|
13
13
|
# "test".__evolve_object_id__
|
14
14
|
#
|
15
|
-
# @return [ String
|
15
|
+
# @return [ String | BSON::ObjectId ] The evolved string.
|
16
16
|
def __evolve_object_id__
|
17
17
|
convert_to_object_id
|
18
18
|
end
|
@@ -22,7 +22,7 @@ module Mongoid
|
|
22
22
|
# @example Evolve the string.
|
23
23
|
# "test".__mongoize_object_id__
|
24
24
|
#
|
25
|
-
# @return [ String
|
25
|
+
# @return [ String | BSON::ObjectId | nil ] The mongoized string.
|
26
26
|
def __mongoize_object_id__
|
27
27
|
convert_to_object_id unless blank?
|
28
28
|
end
|
@@ -65,7 +65,7 @@ module Mongoid
|
|
65
65
|
# @example Is the string an id value?
|
66
66
|
# "_id".mongoid_id?
|
67
67
|
#
|
68
|
-
# @return [ true
|
68
|
+
# @return [ true | false ] If the string is id or _id.
|
69
69
|
def mongoid_id?
|
70
70
|
self =~ /\A(|_)id\z/
|
71
71
|
end
|
@@ -76,7 +76,7 @@ module Mongoid
|
|
76
76
|
# @example Is the string a number.
|
77
77
|
# "1234.23".numeric?
|
78
78
|
#
|
79
|
-
# @return [ true
|
79
|
+
# @return [ true | false ] If the string is a number.
|
80
80
|
def numeric?
|
81
81
|
!!Float(self)
|
82
82
|
rescue ArgumentError
|
@@ -98,7 +98,7 @@ module Mongoid
|
|
98
98
|
# @example Is the string a setter method?
|
99
99
|
# "model=".writer?
|
100
100
|
#
|
101
|
-
# @return [ true
|
101
|
+
# @return [ true | false ] If the string contains "=".
|
102
102
|
def writer?
|
103
103
|
include?("=")
|
104
104
|
end
|
@@ -108,7 +108,7 @@ module Mongoid
|
|
108
108
|
# @example Is the string a valid Ruby identifier for use as a method name
|
109
109
|
# "model=".valid_method_name?
|
110
110
|
#
|
111
|
-
# @return [ true
|
111
|
+
# @return [ true | false ] If the string contains a valid Ruby identifier.
|
112
112
|
def valid_method_name?
|
113
113
|
/[@$"-]/ !~ self
|
114
114
|
end
|
@@ -118,7 +118,7 @@ module Mongoid
|
|
118
118
|
# @example Is the string a setter method?
|
119
119
|
# "price_before_type_cast".before_type_cast?
|
120
120
|
#
|
121
|
-
# @return [ true
|
121
|
+
# @return [ true | false ] If the string ends with "_before_type_cast"
|
122
122
|
def before_type_cast?
|
123
123
|
ends_with?("_before_type_cast")
|
124
124
|
end
|
@@ -128,7 +128,7 @@ module Mongoid
|
|
128
128
|
# @example Is the object unconvertable?
|
129
129
|
# object.unconvertable_to_bson?
|
130
130
|
#
|
131
|
-
# @return [ true
|
131
|
+
# @return [ true | false ] If the object is unconvertable.
|
132
132
|
def unconvertable_to_bson?
|
133
133
|
@unconvertable_to_bson ||= false
|
134
134
|
end
|
@@ -142,7 +142,7 @@ module Mongoid
|
|
142
142
|
# @example Convert to the object id.
|
143
143
|
# string.convert_to_object_id
|
144
144
|
#
|
145
|
-
# @return [ String
|
145
|
+
# @return [ String | BSON::ObjectId ] The string or the id.
|
146
146
|
def convert_to_object_id
|
147
147
|
BSON::ObjectId.legal?(self) ? BSON::ObjectId.from_string(self) : self
|
148
148
|
end
|
@@ -21,7 +21,7 @@ module Mongoid
|
|
21
21
|
#
|
22
22
|
# @param [ Class ] other The class to check.
|
23
23
|
#
|
24
|
-
# @return [ true
|
24
|
+
# @return [ true | false ] If the other is a boolean.
|
25
25
|
def is_a?(other)
|
26
26
|
if other == Mongoid::Boolean || other.class == Mongoid::Boolean
|
27
27
|
return true
|
@@ -43,7 +43,7 @@ module Mongoid
|
|
43
43
|
# @example Is the field a foreign key?
|
44
44
|
# field.foreign_key?
|
45
45
|
#
|
46
|
-
# @return [ true
|
46
|
+
# @return [ true | false ] If the field is a foreign key.
|
47
47
|
def foreign_key?
|
48
48
|
true
|
49
49
|
end
|
@@ -75,7 +75,7 @@ module Mongoid
|
|
75
75
|
# @example Is the field lazy?
|
76
76
|
# field.lazy?
|
77
77
|
#
|
78
|
-
# @return [ true
|
78
|
+
# @return [ true | false ] If the field is lazy.
|
79
79
|
def lazy?
|
80
80
|
type.resizable?
|
81
81
|
end
|
@@ -101,7 +101,7 @@ module Mongoid
|
|
101
101
|
# @example Is the field a BSON::ObjectId?
|
102
102
|
# field.object_id_field?
|
103
103
|
#
|
104
|
-
# @return [ true
|
104
|
+
# @return [ true | false ] If the field is a BSON::ObjectId.
|
105
105
|
def object_id_field?
|
106
106
|
@object_id_field ||=
|
107
107
|
association.polymorphic? ? true : association.klass.using_object_ids?
|
@@ -112,7 +112,7 @@ module Mongoid
|
|
112
112
|
# @example Is the field resizable?
|
113
113
|
# field.resizable?
|
114
114
|
#
|
115
|
-
# @return [ true
|
115
|
+
# @return [ true | false ] If the field is resizable.
|
116
116
|
def resizable?
|
117
117
|
type.resizable?
|
118
118
|
end
|
@@ -14,7 +14,9 @@ module Mongoid
|
|
14
14
|
#
|
15
15
|
# @return [ Object ] The value for the current locale.
|
16
16
|
def demongoize(object)
|
17
|
-
if object
|
17
|
+
return if object.nil?
|
18
|
+
case object
|
19
|
+
when Hash
|
18
20
|
type.demongoize(lookup(object))
|
19
21
|
end
|
20
22
|
end
|
@@ -24,7 +26,7 @@ module Mongoid
|
|
24
26
|
# @example Is the field localized?
|
25
27
|
# field.localized?
|
26
28
|
#
|
27
|
-
# @return [ true
|
29
|
+
# @return [ true | false ] If the field is localized.
|
28
30
|
def localized?
|
29
31
|
true
|
30
32
|
end
|
@@ -50,7 +52,7 @@ module Mongoid
|
|
50
52
|
# @example Should fallbacks be used.
|
51
53
|
# field.fallbacks?
|
52
54
|
#
|
53
|
-
# @return [ true
|
55
|
+
# @return [ true | false ] If fallbacks should be used.
|
54
56
|
def fallbacks?
|
55
57
|
return true if options[:fallbacks].nil?
|
56
58
|
!!options[:fallbacks]
|
@@ -76,7 +78,10 @@ module Mongoid
|
|
76
78
|
end
|
77
79
|
return value unless value.nil?
|
78
80
|
if fallbacks? && ::I18n.respond_to?(:fallbacks)
|
79
|
-
|
81
|
+
fallback_key = ::I18n.fallbacks[locale].find do |loc|
|
82
|
+
object.key?(loc.to_s) || object.key?(loc)
|
83
|
+
end
|
84
|
+
object[fallback_key.to_s] || object[fallback_key]
|
80
85
|
end
|
81
86
|
end
|
82
87
|
end
|
@@ -48,7 +48,7 @@ module Mongoid
|
|
48
48
|
# @example Is the field a foreign key?
|
49
49
|
# field.foreign_key?
|
50
50
|
#
|
51
|
-
# @return [ true
|
51
|
+
# @return [ true | false ] If the field is a foreign key.
|
52
52
|
def foreign_key?
|
53
53
|
false
|
54
54
|
end
|
@@ -82,7 +82,7 @@ module Mongoid
|
|
82
82
|
# @example Is the field lazy?
|
83
83
|
# field.lazy?
|
84
84
|
#
|
85
|
-
# @return [ true
|
85
|
+
# @return [ true | false ] If the field is lazy.
|
86
86
|
def lazy?
|
87
87
|
false
|
88
88
|
end
|
@@ -92,7 +92,7 @@ module Mongoid
|
|
92
92
|
# @example Is the field localized?
|
93
93
|
# field.localized?
|
94
94
|
#
|
95
|
-
# @return [ true
|
95
|
+
# @return [ true | false ] If the field is localized.
|
96
96
|
def localized?
|
97
97
|
false
|
98
98
|
end
|
@@ -112,7 +112,7 @@ module Mongoid
|
|
112
112
|
# @example Is the field a BSON::ObjectId?
|
113
113
|
# field.object_id_field?
|
114
114
|
#
|
115
|
-
# @return [ true
|
115
|
+
# @return [ true | false ] If the field is a BSON::ObjectId.
|
116
116
|
def object_id_field?
|
117
117
|
@object_id_field ||= (type == BSON::ObjectId)
|
118
118
|
end
|
@@ -122,7 +122,7 @@ module Mongoid
|
|
122
122
|
# @example Does the field pre-process the default?
|
123
123
|
# field.pre_processed?
|
124
124
|
#
|
125
|
-
# @return [ true
|
125
|
+
# @return [ true | false ] If the field's default is pre-processed.
|
126
126
|
def pre_processed?
|
127
127
|
@pre_processed ||=
|
128
128
|
(options[:pre_processed] || (default_val && !default_val.is_a?(::Proc)))
|
@@ -161,7 +161,7 @@ module Mongoid
|
|
161
161
|
#
|
162
162
|
# @note Ruby's instance_exec was just too slow.
|
163
163
|
#
|
164
|
-
# @param [ Class
|
164
|
+
# @param [ Class | Module ] object The class or module the field is
|
165
165
|
# defined on.
|
166
166
|
def define_default_method(object)
|
167
167
|
object.__send__(:define_method, default_name, default_val)
|
@@ -177,7 +177,7 @@ module Mongoid
|
|
177
177
|
#
|
178
178
|
# @param [ Hash ] fields The field limitations.
|
179
179
|
#
|
180
|
-
# @return [ true
|
180
|
+
# @return [ true | false ] If the field was included.
|
181
181
|
def included?(fields)
|
182
182
|
(fields.values.first == 1 && fields[name.to_s] == 1) ||
|
183
183
|
(fields.values.first == 0 && !fields.has_key?(name.to_s))
|
data/lib/mongoid/fields.rb
CHANGED
@@ -147,7 +147,7 @@ module Mongoid
|
|
147
147
|
# @example Apply all the non-proc defaults.
|
148
148
|
# model.apply_pre_processed_defaults
|
149
149
|
#
|
150
|
-
# @return [ Array<String ] The names of the non-proc defaults.
|
150
|
+
# @return [ Array<String> ] The names of the non-proc defaults.
|
151
151
|
def apply_pre_processed_defaults
|
152
152
|
pre_processed_defaults.each do |name|
|
153
153
|
apply_default(name)
|
@@ -159,7 +159,7 @@ module Mongoid
|
|
159
159
|
# @example Apply all the proc defaults.
|
160
160
|
# model.apply_post_processed_defaults
|
161
161
|
#
|
162
|
-
# @return [ Array<String ] The names of the proc defaults.
|
162
|
+
# @return [ Array<String> ] The names of the proc defaults.
|
163
163
|
def apply_post_processed_defaults
|
164
164
|
pending_callbacks.delete(:apply_post_processed_defaults)
|
165
165
|
post_processed_defaults.each do |name|
|
@@ -214,7 +214,7 @@ module Mongoid
|
|
214
214
|
# @example Get the database field name.
|
215
215
|
# model.database_field_name(:authorization)
|
216
216
|
#
|
217
|
-
# @param [ String
|
217
|
+
# @param [ String | Symbol ] name The name to get.
|
218
218
|
#
|
219
219
|
# @return [ String ] The name of the field as it's stored in the db.
|
220
220
|
def database_field_name(name)
|
@@ -229,7 +229,7 @@ module Mongoid
|
|
229
229
|
# @param [ Field ] field The field.
|
230
230
|
# @param [ Object ] value The current value.
|
231
231
|
#
|
232
|
-
# @return [ true
|
232
|
+
# @return [ true | false ] If we set the field lazily.
|
233
233
|
def lazy_settable?(field, value)
|
234
234
|
!frozen? && value.nil? && field.lazy?
|
235
235
|
end
|
@@ -241,7 +241,7 @@ module Mongoid
|
|
241
241
|
# @example Is the document using object ids?
|
242
242
|
# model.using_object_ids?
|
243
243
|
#
|
244
|
-
# @return [ true
|
244
|
+
# @return [ true | false ] Using object ids.
|
245
245
|
def using_object_ids?
|
246
246
|
self.class.using_object_ids?
|
247
247
|
end
|
@@ -252,7 +252,7 @@ module Mongoid
|
|
252
252
|
#
|
253
253
|
# @param [ String ] name The field name.
|
254
254
|
#
|
255
|
-
# @return [ true
|
255
|
+
# @return [ true | false ] If this field is dotted or dollared.
|
256
256
|
def dot_dollar_field?(name)
|
257
257
|
n = aliased_fields[name] || name
|
258
258
|
fields.key?(n) && (n.include?('.') || n.start_with?('$'))
|
@@ -396,7 +396,7 @@ module Mongoid
|
|
396
396
|
# If the belongs_to association is the last part of the name, we will
|
397
397
|
# pass back the _id field.
|
398
398
|
#
|
399
|
-
# @param [ String
|
399
|
+
# @param [ String | Symbol ] name The name to get.
|
400
400
|
# @param [ Hash ] relations The associations.
|
401
401
|
# @param [ Hash ] alaiased_fields The aliased fields.
|
402
402
|
# @param [ Hash ] alaiased_associations The aliased associations.
|
@@ -455,7 +455,7 @@ module Mongoid
|
|
455
455
|
# Get the name of the provided field as it is stored in the database.
|
456
456
|
# Used in determining if the field is aliased or not.
|
457
457
|
#
|
458
|
-
# @param [ String
|
458
|
+
# @param [ String | Symbol ] name The name to get.
|
459
459
|
#
|
460
460
|
# @return [ String ] The name of the field as it's stored in the db.
|
461
461
|
def database_field_name(name)
|
@@ -507,7 +507,7 @@ module Mongoid
|
|
507
507
|
# @example Does this class use object ids?
|
508
508
|
# person.using_object_ids?
|
509
509
|
#
|
510
|
-
# @return [ true
|
510
|
+
# @return [ true | false ] If the class uses BSON::ObjectIds for the id.
|
511
511
|
def using_object_ids?
|
512
512
|
fields["_id"].object_id_field?
|
513
513
|
end
|
data/lib/mongoid/findable.rb
CHANGED
@@ -76,7 +76,7 @@ module Mongoid
|
|
76
76
|
# @example Are there no saved documents for this model?
|
77
77
|
# Person.empty?
|
78
78
|
#
|
79
|
-
# @return [ true
|
79
|
+
# @return [ true | false ] If the collection is empty.
|
80
80
|
def empty?
|
81
81
|
count == 0
|
82
82
|
end
|
@@ -87,7 +87,7 @@ module Mongoid
|
|
87
87
|
# @example Do any documents exist for the conditions?
|
88
88
|
# Person.exists?
|
89
89
|
#
|
90
|
-
# @return [ true
|
90
|
+
# @return [ true | false ] If any documents exist for the conditions.
|
91
91
|
def exists?
|
92
92
|
with_default_scope.exists?
|
93
93
|
end
|
@@ -132,8 +132,10 @@ module Mongoid
|
|
132
132
|
# The +find+ method takes into account the default scope defined on the
|
133
133
|
# model class, if any.
|
134
134
|
#
|
135
|
-
# @
|
136
|
-
# array
|
135
|
+
# @note Each argument can be an individual id, an array of ids or
|
136
|
+
# a nested array. Each array will be flattened.
|
137
|
+
#
|
138
|
+
# @param [ Object | Array<Object> ] *args The _id value(s) to find.
|
137
139
|
#
|
138
140
|
# @return [ Document | Array<Document> | nil ] A document or matching documents.
|
139
141
|
#
|
@@ -161,7 +163,7 @@ module Mongoid
|
|
161
163
|
# @raise [ Errors::DocumentNotFound ] If no document found
|
162
164
|
# and Mongoid.raise_not_found_error is true.
|
163
165
|
#
|
164
|
-
# @return [ Document
|
166
|
+
# @return [ Document | nil ] A matching document.
|
165
167
|
def find_by(attrs = {})
|
166
168
|
result = where(attrs).find_first
|
167
169
|
if result.nil? && Mongoid.raise_not_found_error
|
@@ -182,7 +184,6 @@ module Mongoid
|
|
182
184
|
# @raise [ Errors::DocumentNotFound ] If no document found.
|
183
185
|
#
|
184
186
|
# @return [ Document ] A matching document.
|
185
|
-
#
|
186
187
|
def find_by!(attrs = {})
|
187
188
|
result = where(attrs).find_first
|
188
189
|
raise(Errors::DocumentNotFound.new(self, attrs)) unless result
|
@@ -27,7 +27,7 @@ module Mongoid
|
|
27
27
|
#
|
28
28
|
# @param [ Specification ] other The spec to compare against.
|
29
29
|
#
|
30
|
-
# @return [ true
|
30
|
+
# @return [ true | false ] If the specs are equal.
|
31
31
|
def ==(other)
|
32
32
|
fields == other.fields && key == other.key
|
33
33
|
end
|
@@ -53,7 +53,7 @@ module Mongoid
|
|
53
53
|
#
|
54
54
|
# @param [ Symbol ] kind The type of callback.
|
55
55
|
#
|
56
|
-
# @return [ true
|
56
|
+
# @return [ true | false ] If the callback can be executed.
|
57
57
|
def callback_executable?(kind)
|
58
58
|
respond_to?("_#{kind}_callbacks")
|
59
59
|
end
|
@@ -66,7 +66,7 @@ module Mongoid
|
|
66
66
|
#
|
67
67
|
# @param [ Symbol ] kind The callback kind.
|
68
68
|
#
|
69
|
-
# @return [ true
|
69
|
+
# @return [ true | false ] If the document is in a callback state.
|
70
70
|
def in_callback_state?(kind)
|
71
71
|
[ :create, :destroy ].include?(kind) || new_record? || flagged_for_destroy? || changed?
|
72
72
|
end
|
@@ -195,7 +195,7 @@ module Mongoid
|
|
195
195
|
# @example Was a before callback halted?
|
196
196
|
# document.before_callback_halted?
|
197
197
|
#
|
198
|
-
# @return [ true
|
198
|
+
# @return [ true | false ] If a before callback was halted.
|
199
199
|
def before_callback_halted?
|
200
200
|
!!@before_callback_halted
|
201
201
|
end
|
@@ -235,7 +235,7 @@ module Mongoid
|
|
235
235
|
# @param [ Symbol ] kind The type of callback.
|
236
236
|
# @param [ Document ] child The child document.
|
237
237
|
#
|
238
|
-
# @return [ true
|
238
|
+
# @return [ true | false ] If the child should fire the callback.
|
239
239
|
def cascadable_child?(kind, child, association)
|
240
240
|
return false if kind == :initialize || kind == :find || kind == :touch
|
241
241
|
return false if kind == :validate && association.validate?
|
data/lib/mongoid/matchable.rb
CHANGED
@@ -15,7 +15,7 @@ module Mongoid
|
|
15
15
|
#
|
16
16
|
# @param [ Hash ] selector The MongoDB selector.
|
17
17
|
#
|
18
|
-
# @return [ true
|
18
|
+
# @return [ true | false ] True if matches, false if not.
|
19
19
|
def _matches?(selector)
|
20
20
|
Matcher::Expression.matches?(self, selector)
|
21
21
|
end
|
data/lib/mongoid/matcher.rb
CHANGED
@@ -24,17 +24,22 @@ module Mongoid
|
|
24
24
|
# an array of values of the `bar` field in each of the hashes in the
|
25
25
|
# `foo` array.
|
26
26
|
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
#
|
30
|
-
#
|
27
|
+
# This method can return an individual field value in some document
|
28
|
+
# or an array of values from multiple documents. The array can be returned
|
29
|
+
# because a field value in the specified path is an array of primitive
|
30
|
+
# values (e.g. integers) or because a field value in the specified path
|
31
|
+
# is an array of documents (e.g. a one-to-many embedded association),
|
32
|
+
# in which case the leaf value may be a scalar for each individual document.
|
33
|
+
# If the leaf value is an array and a one-to-many association was traversed,
|
34
|
+
# the return value will be an array of arrays. Note that an individual
|
35
|
+
# field value can also be an array and this case is indistinguishable
|
36
|
+
# from and behaves identically to association traversal for the purposes
|
37
|
+
# of, for example, subsequent array element retrieval.
|
31
38
|
#
|
32
39
|
# @param [ Document | Hash ] document The document to extract from.
|
33
40
|
# @param [ String ] key The key path to extract.
|
34
41
|
#
|
35
|
-
# @return [
|
36
|
-
# Whether the value existed in the document, the extracted value
|
37
|
-
# and the array expansion flag.
|
42
|
+
# @return [ Object | Array ] Field value or values.
|
38
43
|
module_function def extract_attribute(document, key)
|
39
44
|
if document.respond_to?(:as_attributes, true)
|
40
45
|
# If a document has hash fields, as_attributes would keep those fields
|
@@ -132,10 +132,10 @@ module Mongoid
|
|
132
132
|
# @example Create multiple new documents.
|
133
133
|
# Person.create({ title: "Mr" }, { title: "Mrs" })
|
134
134
|
#
|
135
|
-
# @param [ Hash
|
135
|
+
# @param [ Hash | Array ] attributes The attributes to create with, or an
|
136
136
|
# Array of multiple attributes for multiple documents.
|
137
137
|
#
|
138
|
-
# @return [ Document
|
138
|
+
# @return [ Document | Array<Document> ] The newly created document(s).
|
139
139
|
def create(attributes = nil, &block)
|
140
140
|
_creating do
|
141
141
|
if attributes.is_a?(::Array)
|
@@ -159,10 +159,10 @@ module Mongoid
|
|
159
159
|
# @example Create multiple new documents.
|
160
160
|
# Person.create!({ title: "Mr" }, { title: "Mrs" })
|
161
161
|
#
|
162
|
-
# @param [ Hash
|
162
|
+
# @param [ Hash | Array ] attributes The attributes to create with, or an
|
163
163
|
# Array of multiple attributes for multiple documents.
|
164
164
|
#
|
165
|
-
# @return [ Document
|
165
|
+
# @return [ Document | Array<Document> ] The newly created document(s).
|
166
166
|
def create!(attributes = nil, &block)
|
167
167
|
_creating do
|
168
168
|
if attributes.is_a?(::Array)
|
@@ -86,7 +86,7 @@ module Mongoid
|
|
86
86
|
#
|
87
87
|
# @param [ Hash ] options The delete options.
|
88
88
|
#
|
89
|
-
# @return [ true
|
89
|
+
# @return [ true | false ] If the parent should be notified.
|
90
90
|
def notifying_parent?(options = {})
|
91
91
|
!options.delete(:suppress)
|
92
92
|
end
|
@@ -14,7 +14,7 @@ module Mongoid
|
|
14
14
|
#
|
15
15
|
# @param [ Hash ] options Options to pass to destroy.
|
16
16
|
#
|
17
|
-
# @return [ true
|
17
|
+
# @return [ true | false ] True if successful, false if not.
|
18
18
|
def destroy(options = nil)
|
19
19
|
raise Errors::ReadonlyDocument.new(self.class) if readonly?
|
20
20
|
self.flagged_for_destroy = true
|
@@ -14,7 +14,7 @@ module Mongoid
|
|
14
14
|
#
|
15
15
|
# @param [ Hash ] options Options to pass to the save.
|
16
16
|
#
|
17
|
-
# @return [ true
|
17
|
+
# @return [ true | false ] True is success, false if not.
|
18
18
|
def save(options = {})
|
19
19
|
if new_record?
|
20
20
|
!insert(options).new_record?
|
@@ -34,7 +34,7 @@ module Mongoid
|
|
34
34
|
# @raise [ Errors::Validations ] If validation failed.
|
35
35
|
# @raise [ Errors::Callback ] If a callback returns false.
|
36
36
|
#
|
37
|
-
# @return [ true
|
37
|
+
# @return [ true | false ] True if validation passed.
|
38
38
|
def save!(options = {})
|
39
39
|
unless save(options)
|
40
40
|
fail_due_to_validation! unless errors.empty?
|
@@ -13,7 +13,7 @@ module Mongoid
|
|
13
13
|
# @example Unset the values.
|
14
14
|
# document.unset(:first_name, :last_name, :middle)
|
15
15
|
#
|
16
|
-
# @param [ Array<String
|
16
|
+
# @param [ Array<String | Symbol> ] fields The names of the fields to
|
17
17
|
# unset.
|
18
18
|
#
|
19
19
|
# @return [ Document ] The document.
|