Skip to content

Commit 0ace00c

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 0d84f91 commit 0ace00c

File tree

9 files changed

+71
-2
lines changed

9 files changed

+71
-2
lines changed

docs/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

docs/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

docs/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.

lib/mongoid/config.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ module Config
119119
# using and's instead of overwriting them.
120120
option :overwrite_chained_operators, default: false
121121

122+
# When this flag is true, the attributes method on a document will return
123+
# a BSON::Document when that document is retrieved from the database, and
124+
# a Hash otherwise. When this flag is false, the attributes method will
125+
# always return a Hash.
126+
option :legacy_attributes, default: false
127+
122128
# Has Mongoid been configured? This is checking that at least a valid
123129
# client config exists.
124130
#

lib/mongoid/document.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,12 @@ def instantiate(attrs = nil, selected_fields = nil, &block)
309309
#
310310
# @api private
311311
def instantiate_document(attrs = nil, selected_fields = nil, execute_callbacks: true)
312-
attributes = attrs&.to_h || {}
312+
attributes = if Mongoid.legacy_attributes
313+
attrs
314+
else
315+
attrs&.to_h
316+
end || {}
317+
313318
doc = allocate
314319
doc.__selected_fields = selected_fields
315320
doc.instance_variable_set(:@attributes, attributes)

spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3815,6 +3815,8 @@ class Distributor
38153815
# a BSON::Document, which applies a transformation to the array before
38163816
# storing it.
38173817
context "when executing concat on foreign key array from the db" do
3818+
config_override :legacy_attributes, false
3819+
38183820
before do
38193821
HabtmmContract.create!
38203822
HabtmmSignature.create!

spec/mongoid/association/referenced/has_many/proxy_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4140,6 +4140,8 @@
41404140
end
41414141

41424142
context "when executing concat on foreign key array from the db" do
4143+
config_override :legacy_attributes, false
4144+
41434145
before do
41444146
Agent.create!
41454147
Basic.create!

spec/mongoid/attributes_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,6 +1678,7 @@
16781678
end
16791679

16801680
context "when comparing the object_ids of the written value" do
1681+
config_override :legacy_attributes, false
16811682

16821683
before do
16831684
Person.create!

spec/mongoid/config_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,13 @@
342342
it_behaves_like "a config option"
343343
end
344344

345+
context 'when setting the legacy_attributes option in the config' do
346+
let(:option) { :legacy_attributes }
347+
let(:default) { false }
348+
349+
it_behaves_like "a config option"
350+
end
351+
345352
describe "#load!" do
346353

347354
before(:all) do

0 commit comments

Comments
 (0)