Skip to content

Commit ae05909

Browse files
authored
MONGOID-5365 Change #attributes return value to Hash with a flag (#5301)
* MONGOID-5365 Change #attributes return value to Hash with a flag * MONGOID-5365 add config_override to tests * MONGOID-5365 rename flag and flip default * MONGOID-5365 fix old flag ref
1 parent 1420e21 commit ae05909

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

source/reference/configuration.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,12 @@ for details on driver options.
368368
# to parent contexts by default. (default: false)
369369
join_contexts: false
370370

371+
# When this flag is true, the attributes method on a document will return
372+
# a BSON::Document when that document is retrieved from the database, and
373+
# a Hash otherwise. When this flag is false, the attributes method will
374+
# always return a Hash. (default: false)
375+
#legacy_attributes: true
376+
371377
# Maintain legacy behavior of pluck and distinct, which does not demongoize
372378
# values on returning them. Setting this option to false will cause
373379
# pluck and distinct to return demongoized values. Setting this option to

source/release-notes/mongoid-7.5.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,42 @@ The following functions are affected by this change:
8787
Mongoid 7.5 fixes incorrect usage of the driver's ``update_one`` method from
8888
Mongoid's ``upsert`` method. Mongoid's ``upsert`` actually performs a
8989
replacing upsert, and Mongoid 7.5 correctly calls ``replace_one``.
90+
91+
92+
Force the ``attributes`` Method to Always Return a ``Hash``
93+
```````````````````````````````````````````````````````````
94+
95+
Mongoid 7.5 with the ``Mongoid.legacy_attributes`` option set to ``false``
96+
will always return a ``Hash`` when calling the ``attributes`` method.
97+
For example:
98+
99+
.. code-block:: ruby
100+
101+
class Band
102+
include Mongoid::Document
103+
104+
field :name
105+
end
106+
107+
band = Band.create!(name: "The Rolling Stones")
108+
p band.attributes.class
109+
# => Hash
110+
111+
band = Band.first
112+
p band.attributes.class
113+
# => Hash
114+
115+
In Mongoid 7.4 and earlier, and in 7.5 with the ``Mongoid.legacy_attributes``
116+
option set to ``true``, the ``attributes`` method on a document will return a
117+
``BSON::Document`` when retrieving that document from the database, but will
118+
return a ``Hash`` when instantiating a new document:
119+
120+
.. code-block:: ruby
121+
122+
band = Band.create!(name: "The Rolling Stones")
123+
p band.attributes.class
124+
# => Hash
125+
126+
band = Band.first
127+
p band.attributes.class
128+
# => BSON::Document

source/release-notes/mongoid-8.0.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,18 @@ Default Option Values Changed
3131
**Breaking change:** The following options have had their default values
3232
changed in Mongoid 8.0:
3333

34-
- ``:overwrite_chained_operators`` => ``false``
3534
- ``:broken_aggregables`` => ``false``
3635
- ``:broken_alias_handling`` => ``false``
3736
- ``:broken_and`` => ``false``
3837
- ``:broken_scoping`` => ``false``
3938
- ``:broken_updates`` => ``false``
4039
- ``:compare_time_by_ms`` => ``true``
40+
- ``:legacy_attributes`` => true
4141
- ``:legacy_pluck_distinct`` => ``false``
4242
- ``:legacy_triple_equals`` => ``false``
4343
- ``:map_big_decimal_to_decimal128`` => ``true``
4444
- ``:object_id_as_json_oid`` => ``false``
45+
- ``:overwrite_chained_operators`` => ``false``
4546

4647
Please refer to :ref:`configuration option <configuration-options>` for
4748
the description and effects of each of these options.

0 commit comments

Comments
 (0)