Skip to content

Commit 8ff0dd5

Browse files
authored
MONGOID-5028 demongoize on pluck (#5176)
* MONGOID-5028 demongoize on pluck * MONGOID-5028 add demongoize to distinct * MONGOID-5028 add feature flag and tests. Revamped config tests * MONGOID-5028 remove byebug * MONGOID-5028 update docs and release notes * MONGOID-5028 add back broken scoping * MONGOID-5028 change language * MONGOID-5028 update tests * MONGOID-5028 sort test results * MONGOID-5028 revert config spec * MONGOID-5028 make tests more explicit * MONGOID-5028 expand more tests
1 parent 3020932 commit 8ff0dd5

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

source/reference/configuration.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ for details on driver options.
182182
# determined by the driver will be used. Please refer to:
183183
# https://docs.mongodb.com/ruby-driver/current/reference/authentication/#auth-source
184184
auth_source: admin
185-
185+
186186
# Connect directly to and perform all operations on the specified
187187
# server, bypassing replica set node discovery and monitoring.
188188
# Exactly one host address must be specified. (default: false)
@@ -367,6 +367,12 @@ for details on driver options.
367367
# to parent contexts by default. (default: false)
368368
join_contexts: false
369369

370+
# Maintain legacy behavior of pluck and distinct, which does not demongoize
371+
# values on returning them. Setting this option to false will cause
372+
# pluck and distinct to return demongoized values.
373+
# (default: true in Mongoid 7, will change to false in Mongoid 8)
374+
legacy_pluck_distinct: true
375+
370376
# Maintain legacy behavior of === on Mongoid document classes, which
371377
# returns true in a number of cases where Ruby's === implementation would
372378
# return false. Note that the behavior of === on Mongoid document

source/release-notes/mongoid-7.4.txt

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ in Mongoid 7.4 for backwards compatibility. In Mongoid 8 the default value
179179
will change to ``true``.
180180

181181

182-
Respect Field Aliases In Embedded Documents When Using ``distinct`` And ``pluck``
182+
Respect Field Aliases In Embedded Documents When Using ``distinct`` And ``pluck``
183183
---------------------------------------------------------------------------------
184184

185185
When ``distinct`` and ``pluck`` are used with aliased fields in embedded
@@ -273,10 +273,10 @@ and then to a non-nil value, for example:
273273

274274
class Canvas
275275
include Mongoid::Document
276-
276+
277277
embeds_one :palette
278278
end
279-
279+
280280
canvas.palette = palette
281281
canvas.palette = nil
282282
canvas.palette = palette
@@ -345,7 +345,7 @@ For example:
345345
Band.with_scope(active: true) do
346346
# ...
347347
end
348-
348+
349349
# {year: 2020} condition is applied here
350350
end
351351

@@ -359,11 +359,51 @@ all scopes are cleared:
359359
Band.with_scope(active: true) do
360360
# ...
361361
end
362-
362+
363363
# No scope is applied here
364364
end
365365

366366
.. note::
367367

368368
The value of ``Mongoid.broken_scoping`` option will change to ``false``
369369
by default in Mongoid 8.0.
370+
371+
372+
Demongoize Values Returned from Pluck and Distinct
373+
--------------------------------------------------
374+
375+
Mongoid 7.4 with the ``Mongoid.legacy_pluck_distinct`` option set to ``false``
376+
will demongoize values returned from the pluck and distinct methods.
377+
For example:
378+
379+
.. code-block:: ruby
380+
381+
class Band
382+
include Mongoid::Document
383+
384+
field :sales, type: BigDecimal
385+
end
386+
387+
Band.create!(sales: "1E2")
388+
Band.create!(sales: "2E2")
389+
Band.pluck(:sales)
390+
# => [0.1e3, 0.2e3]
391+
Band.distinct(:sales)
392+
# => [0.1e3, 0.2e3]
393+
394+
395+
In Mongoid 7.3 and earlier, and in 7.4 with the ``Mongoid.legacy_pluck_distinct``
396+
option set to ``true`` (the default), the value returned from the pluck and
397+
distinct methods will not be demongoized. For example:
398+
399+
.. code-block:: ruby
400+
401+
Band.pluck(:sales)
402+
# => ["1E2", "2E2"]
403+
Band.distinct(:sales)
404+
# => ["1E2", "2E2"]
405+
406+
.. note::
407+
408+
The value of ``Mongoid.legacy_pluck_distinct`` option will change to ``false``
409+
by default in Mongoid 8.0.

0 commit comments

Comments
 (0)