Skip to content

Commit 78c5ee9

Browse files
committed
MONGOID-5222 add documentation
1 parent 699e087 commit 78c5ee9

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

docs/reference/configuration.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,11 @@ 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 casted 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+
437442
# (Deprecated) In MongoDB 4.0 and earlier, set whether to create
438443
# indexes in the background by default. (default: false)
439444
background_indexing: false

docs/reference/fields.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,35 @@ 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 assigning 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. To deal with this case, Mongoid has introduced the
851+
``validate_attribute_types`` flag.
852+
853+
When the ``validate_attribute_types`` flag is turned on, the assignment of uncastable
854+
values to a field will cause an ``InvalidValue`` error to be raised.
855+
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.
858+
830859

831860
.. _customizing-field-behavior:
832861

docs/release-notes/mongoid-8.0.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,40 @@ 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+
------------------------------------------------------------------
62+
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, an ``InvalidValue`` error is raise, 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.
68+
69+
Some ``mongoize`` methods were also changed to perform consistently with rails
70+
and the other mongoize methods. The following is a table of the changes in
71+
functionality:
72+
73+
+--------------+------------------------+------------------------+-------------------+
74+
| Field Type | Situation | Previous Functionality | New Functionality |
75+
+==============+========================+========================+===================+
76+
| Integer/Float| When a non-numeric | return ``nil`` | return ``42`` |
77+
| | string starts with a | | |
78+
| | number: "42bogus" | | |
79+
+--------------+------------------------+------------------------+-------------------+
80+
| Boolean | When a non-boolean | return ``false`` | return ``nil`` or |
81+
| | string is assigned: | | raise |
82+
| | "bogus value" | | ``InvalidError`` |
83+
+--------------+------------------------+------------------------+-------------------+
84+
| Symbol | When a value that does | return ``nil`` | return ``nil`` or |
85+
| | not respond to | | raise |
86+
| | ``to_sym`` is | | ``InvalidError`` |
87+
| | assigned: ``[]`` | | |
88+
+--------------+------------------------+------------------------+-------------------+
89+
| All Other | When an uncastable | undefined behavior, | return ``nil`` or |
90+
| Types | value is assigned | occasionally raises | raise |
91+
| | | ``NoMethodError`` | ``InvalidError`` |
92+
+--------------+------------------------+------------------------+-------------------+
93+
6094

6195
Order of Callback Invocation
6296
----------------------------

0 commit comments

Comments
 (0)