Skip to content

Commit 67e7d8b

Browse files
committed
MONGOID-5222 always return nil on mongoize
1 parent 2e1e490 commit 67e7d8b

File tree

23 files changed

+49
-180
lines changed

23 files changed

+49
-180
lines changed

docs/reference/configuration.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -434,11 +434,6 @@ for details on driver options.
434434
# (default: false)
435435
use_utc: false
436436

437-
# Raise an error on the assignment of a value that cannot be cast to
438-
# the assigning field type. If this flag is off, no error will be raised
439-
# and nil will be written.
440-
validate_attribute_types: true
441-
442437
# (Deprecated) In MongoDB 4.0 and earlier, set whether to create
443438
# indexes in the background by default. (default: false)
444439
background_indexing: false

docs/reference/fields.txt

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -847,14 +847,13 @@ coerced to the type of the field. For example:
847847
User.new(name: [ "hello" ])
848848

849849
Assigning an array to a field of type Integer doesn't work since an array can't
850-
be coerced to an Integer. To deal with this case, Mongoid has introduced the
851-
``validate_attribute_types`` flag.
850+
be coerced to an Integer. The assignment of uncastable values to a field will
851+
cause a ``nil`` to be written:
852852

853-
When the ``validate_attribute_types`` flag is turned on, the assignment of uncastable
854-
values to a field will cause a ``Mongoid::Errors::InvalidValue`` exception being raised.
853+
.. code::
855854

856-
When the ``validate_attribute_types`` flag is turned off, the assignment of uncastable
857-
values to a field will cause a ``nil`` to be written.
855+
User.new(name: [ "hello" ])
856+
# => #<User _id: 62b222d43282a47bf73e3264, name: nil>
858857

859858

860859
.. _customizing-field-behavior:
@@ -1009,11 +1008,9 @@ setter methods for fields of your custom type.
10091008

10101009
.. note::
10111010

1012-
The ``mongoize`` method should raise a ``Mongoid::Errors::InvalidValue`` on values that
1013-
are uncastable to your custom type. Mongoid will handle rescuing that error
1014-
and writing ``nil`` if the ``validate_attribute_types`` flag is turned off.
1015-
See the secion on :ref:`Uncastable Values <uncastable-values>` for more
1016-
details.
1011+
The ``mongoize`` method should return ``nil`` on values that are uncastable to
1012+
your custom type. See the secion on :ref:`Uncastable Values <uncastable-values>`
1013+
for more details.
10171014

10181015
The class method ``demongoize`` does the inverse of ``mongoize``. It takes the raw object
10191016
from the MongoDB Ruby driver and converts it to an instance of your custom type.

docs/release-notes/mongoid-8.0.txt

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,12 @@ changed in Mongoid 8.0:
5757
Please refer to :ref:`configuration option <configuration-options>` for
5858
the description and effects of each of these options.
5959

60-
Storing Uncastable Types and the ``validate_attribute_types`` Flag
61-
------------------------------------------------------------------
60+
Storing Uncastable Value
61+
------------------------
6262

63-
In Mongoid 8, Mongoid deals with "uncastable values" based on the values of the
64-
``validates_attribute_types`` flag. If the ``validates_attribute_types`` flag
65-
is set to true, a ``Mongoid:Errors::InvalidValue`` exception is raised, and if it is set to false
66-
a ``nil`` is written. See the secion on :ref:`Uncastable Values <uncastable-values>`
67-
for more details.
63+
In Mongoid 8, Mongoid standardizes the storing of "uncastable values." On
64+
attempting to write an uncastable value, a ``nil`` is written instead. See the
65+
secion on :ref:`Uncastable Values <uncastable-values>` for more details.
6866

6967
Some ``mongoize`` methods were also changed to perform consistently with rails
7068
and the other mongoize methods. The following is a table of the changes in
@@ -73,18 +71,18 @@ functionality:
7371
+--------------+------------------------+------------------------+-------------------+
7472
| Field Type | Situation | Previous Functionality | New Functionality |
7573
+==============+========================+========================+===================+
76-
| Boolean | When a non-boolean | return ``false`` | return ``nil`` or |
77-
| | string is assigned: | | raise |
78-
| | "bogus value" | | ``InvalidError`` |
74+
| Boolean | When a non-boolean | return ``false`` | return ``nil`` |
75+
| | string is assigned: | | |
76+
| | "bogus value" | | |
7977
+--------------+------------------------+------------------------+-------------------+
80-
| Symbol | When a value that does | return ``nil`` | return ``nil`` or |
81-
| | not respond to | | raise |
82-
| | ``to_sym`` is | | ``InvalidError`` |
78+
| Symbol | When a value that does | return ``nil`` | return ``nil`` |
79+
| | not respond to | | |
80+
| | ``to_sym`` is | | |
8381
| | assigned: ``[]`` | | |
8482
+--------------+------------------------+------------------------+-------------------+
85-
| All Other | When an uncastable | undefined behavior, | return ``nil`` or |
86-
| Types | value is assigned | occasionally raises | raise |
87-
| | | ``NoMethodError`` | ``InvalidError`` |
83+
| All Other | When an uncastable | undefined behavior, | return ``nil`` |
84+
| Types | value is assigned | occasionally raises | |
85+
| | | ``NoMethodError`` | |
8886
+--------------+------------------------+------------------------+-------------------+
8987

9088

lib/config/locales/en.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,7 @@ en:
375375
invalid_value:
376376
message: "The value %{value} cannot be written to a field of type %{field_class}."
377377
summary: "The value %{value} could not be coerced to type %{field_class}."
378-
resolution: "Verify that the value to be set corresponds to the field definition,
379-
or turn off the Mongoid.validate_attribute_types flag to have nil written."
378+
resolution: "Verify that the value to be set corresponds to the field definition."
380379
mixed_relations:
381380
message: "Referencing a(n) %{embedded} document from the %{root}
382381
document via a non-embedded association is not allowed since the

lib/mongoid/attributes.rb

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,7 @@ def write_attribute(name, value)
172172
_assigning do
173173
localized = fields[field_name].try(:localized?)
174174
attributes_before_type_cast[name.to_s] = value
175-
176-
typed_value = if Mongoid.validate_attribute_types
177-
typed_value_for(field_name, value)
178-
else
179-
begin
180-
typed_value_for(field_name, value)
181-
rescue Errors::InvalidValue
182-
nil
183-
end
184-
end
185-
175+
typed_value = typed_value_for(field_name, value)
186176
unless attributes[field_name] == typed_value || attribute_changed?(field_name)
187177
attribute_will_change!(field_name)
188178
end

lib/mongoid/config.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,6 @@ module Config
125125
# always return a Hash.
126126
option :legacy_attributes, default: false
127127

128-
# When this flag is true, Mongoid will validate the type of the value
129-
# assigned to a field, such that it can be coerced to a value of the field
130-
# type. If it is "uncastable," an InvalidValue error will be raised. If this
131-
# feature flag is off, nil will be written to the attribute and the database
132-
# on save.
133-
option :validate_attribute_types, default: true
134-
135128
# Has Mongoid been configured? This is checking that at least a valid
136129
# client config exists.
137130
#

lib/mongoid/extensions/array.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,11 @@ def __mongoize_fk__(association, object)
144144
#
145145
# @param [ Object ] object The object to mongoize.
146146
#
147-
# @raise [ Errors::InvalidValue ] if the value is uncastable.
148-
#
149147
# @return [ Array | nil ] The object mongoized or nil.
150148
def mongoize(object)
151149
return if object.nil?
152150
if object.is_a?(::Array)
153151
evolve(object).collect{ |obj| obj.class.mongoize(obj) }
154-
else
155-
raise Errors::InvalidValue.new(self, object)
156152
end
157153
end
158154

lib/mongoid/extensions/big_decimal.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ def demongoize(object)
6262
#
6363
# @param [ Object ] object The object to Mongoize
6464
#
65-
# @raise [ Errors::InvalidValue ] if the value is uncastable.
66-
#
6765
# @return [ String | BSON::Decimal128 | nil ] A String or Decimal128
6866
# representing the object or nil. String if Mongoid.map_big_decimal_to_decimal128
6967
# is false, BSON::Decimal128 otherwise.
@@ -84,10 +82,6 @@ def mongoize(object)
8482
elsif object.numeric?
8583
object.to_d.to_s
8684
end
87-
end.tap do |res|
88-
if res.nil?
89-
raise Errors::InvalidValue.new(self, object)
90-
end
9185
end
9286
end
9387
end

lib/mongoid/extensions/binary.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,12 @@ module ClassMethods
2424
#
2525
# @param [ Object ] object The object to Mongoize
2626
#
27-
# @raise [ Errors::InvalidValue ] if the value is uncastable.
28-
#
2927
# @return [ BSON::Binary | nil ] A Binary representing the object or nil.
3028
def mongoize(object)
3129
return if object.nil?
3230
case object
3331
when BSON::Binary then object
3432
when String, Symbol then BSON::Binary.new(object.to_s)
35-
else raise Errors::InvalidValue.new(self, object)
3633
end
3734
end
3835
end

lib/mongoid/extensions/boolean.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,13 @@ class << self
1111
# @example Mongoize the object.
1212
# Boolean.mongoize("123.11")
1313
#
14-
# @raise [ Errors::InvalidValue ] if the value is uncastable.
15-
#
1614
# @return [ true | false | nil ] The object mongoized or nil.
1715
def mongoize(object)
1816
return if object.nil?
1917
if object.to_s =~ (/\A(true|t|yes|y|on|1|1.0)\z/i)
2018
true
2119
elsif object.to_s =~ (/\A(false|f|no|n|off|0|0.0)\z/i)
2220
false
23-
else
24-
raise Errors::InvalidValue.new(self, object)
2521
end
2622
end
2723
end

0 commit comments

Comments
 (0)