Skip to content

Commit 19c7d03

Browse files
neilshwekyp-mongo
andauthored
MONGOID-5222 always return nil on mongoize (#5336)
* MONGOID-5222 add feature flag * MONGOID-5222 first batch of type fixes * MONGOID-5222 rebase * MONGOID-5222 add sets * MONGOID-5222 fix failures * MONGOID-5222 fix most tests * MONGOID-5222 fix to always raise in mongoize * MONGOID-5222 use new architecture * MONGOID-5222 fix many tests * MONGOID-5222 fix rest of tests * MONGOID-5222 remove dead code * MONGOID-5222 fix binary tests * MONGOID-5222 fix mongoizable tests * MONGOID-5222 fix the rest of tests * MONGOID-5222 unpend all mongoizable tests * MONGOID-5222 add evolve to mongoizable spec * comment out most evolve methods * Revert "comment out most evolve methods" This reverts commit f60e4b4c55a4cd0505a8a91564ef4feb57809946. * MONGOID-5222 remove evolve tests * MONGOID-5222 get rid of unnecessary test flags. Change integer to check for to_i * MONGOID-5222 change float as well * MONGOID-5222 fix range tests * MONGOID-5222 rename wrap_mognoize * MONGOID-5222 remove mongoize_safe * MONGOID-5222 fix BigDecimal mongoization * MONGOID-5222 add documentation * MONGOID-5222 update error message and docs * MONGOID-5222 fix tests * MONGOID-5222 add note to custom types section * MONGOID-5222 fix grammatical error * MONGOID-5222 remove _mongoid_wrap_mongoize * Update docs/release-notes/mongoid-8.0.txt Co-authored-by: Oleg Pudeyev <[email protected]> * Update docs/reference/fields.txt Co-authored-by: Oleg Pudeyev <[email protected]> * Update docs/reference/fields.txt Co-authored-by: Oleg Pudeyev <[email protected]> * MONGOID-5222 answer comments * MONGOID-5222 fix tests * MONGOID-5222 update error message * MONGOID-5222 fix regexp mongoization * MONGOID-5222 get rid of 42bogus * MONGOID-5222 update mongoize update strings * MONGOID-5222 fix range docs * MONGOID-5222 always return nil on mongoize * MONGOID-5222 remove InvalidValue from tests * MONGOID-5222 fix array, set, regexp mongoize * MONGOID-5222 fix more comments and update compatibility release note * MONGOID-5222 remove invalid_value error * MONGOID-5222 add another case to release notes * MONGOID-5222 fix docs * MONGOID-5222 remove byebug * MONGOID-5222 fix boolean mongoize * Update docs/reference/fields.txt * MONGOID-5222 test nps * MONGOID-5222 add reference to attributes_before_type_cast * MONGOID-5222 answer some comments * MONGOID-5222 move ArgumentError * MONGOID-5222 update release notes Co-authored-by: Oleg Pudeyev <[email protected]>
1 parent c8e6dc3 commit 19c7d03

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

source/reference/fields.txt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,42 @@ Mongoid also defines the ``id`` field aliased to ``_id``. The ``id``
827827
alias can :ref:`be removed <unalias-id>` if desired (such as to integrate
828828
with systems that use the ``id`` field to store value different from ``_id``.
829829

830+
.. _uncastable-values:
831+
832+
Assigning Uncastable Values
833+
---------------------------
834+
835+
In Mongoid 8, Mongoid has standardized the treatment of the assignment of
836+
"uncastable" values. A value is considered "uncastable" when it cannot be
837+
coerced to the type of the field. For example:
838+
839+
.. code::
840+
841+
class User
842+
include Mongoid::Document
843+
844+
field :name, type: Integer
845+
end
846+
847+
User.new(name: [ "hello" ])
848+
849+
Assigning an array to a field of type Integer doesn't work since an array can't
850+
be coerced to an Integer. The assignment of uncastable values to a field will
851+
cause a ``nil`` to be written:
852+
853+
.. code::
854+
855+
user = User.new(name: [ "hello" ])
856+
# => #<User _id: 62b222d43282a47bf73e3264, name: nil>
857+
858+
Note that the original uncastable values will be stored in the
859+
``attributes_before_type_cast`` hash with their field names:
860+
861+
.. code::
862+
863+
user.attributes_before_type_cast["name"]
864+
# => ["hello"]
865+
830866

831867
.. _customizing-field-behavior:
832868

@@ -978,6 +1014,12 @@ setter methods for fields of your custom type.
9781014
venue = Venue.new(location: point) # This uses the Point#mongoize instance method.
9791015
venue = Venue.new(location: [ 12, 24 ]) # This uses the Point.mongoize class method.
9801016

1017+
.. note::
1018+
1019+
The ``mongoize`` method should return ``nil`` on values that are uncastable to
1020+
your custom type. See the section on :ref:`Uncastable Values <uncastable-values>`
1021+
for more details.
1022+
9811023
The class method ``demongoize`` does the inverse of ``mongoize``. It takes the raw object
9821024
from the MongoDB Ruby driver and converts it to an instance of your custom type.
9831025
In this case, the database driver returns an ``Array`` and we instantiate a ``Point`` from it.

source/release-notes/mongoid-8.0.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,34 @@ 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 Value
61+
------------------------
62+
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.
66+
67+
Some ``mongoize`` methods were also changed to perform consistently with rails
68+
and the other mongoize methods. The following is a table of the changes in
69+
functionality:
70+
71+
+--------------+------------------------+------------------------+-------------------+
72+
| Field Type | Situation | Previous Functionality | New Functionality |
73+
+==============+========================+========================+===================+
74+
| Boolean | When a non-boolean | return ``false`` | return ``nil`` |
75+
| | string is assigned: | | |
76+
| | "bogus value" | | |
77+
+--------------+------------------------+------------------------+-------------------+
78+
| Array/Set | When a value that is | raise ``InvalidValue`` | return ``nil`` |
79+
| | not an array or set is | error | |
80+
| | assigned, and does NOT | | |
81+
| | respond to ``to_a``: 1 | | |
82+
+--------------+------------------------+------------------------+-------------------+
83+
| All Other | When an uncastable | undefined behavior, | return ``nil`` |
84+
| Types | value is assigned | occasionally raises | |
85+
| | | ``NoMethodError`` | |
86+
+--------------+------------------------+------------------------+-------------------+
87+
6088

6189
Order of Callback Invocation
6290
----------------------------

0 commit comments

Comments
 (0)