@@ -63,7 +63,7 @@ Mongoid 7.4 with ``Mongoid.legacy_triple_equals`` set to ``false`` behavior:
63
63
64
64
class Band
65
65
include Mongoid::Document
66
-
66
+
67
67
has_many :members
68
68
end
69
69
@@ -72,7 +72,7 @@ Mongoid 7.4 with ``Mongoid.legacy_triple_equals`` set to ``false`` behavior:
72
72
73
73
class Member
74
74
include Mongoid::Document
75
-
75
+
76
76
belongs_to :band
77
77
end
78
78
@@ -90,10 +90,10 @@ Mongoid 7.4 with ``Mongoid.legacy_triple_equals`` set to ``false`` behavior:
90
90
91
91
CoverBand === Band
92
92
# => false
93
-
93
+
94
94
band.members === Array
95
95
# => false
96
-
96
+
97
97
band.members === Mongoid::Association::Referenced::HasMany::Enumerable
98
98
# => false
99
99
@@ -113,10 +113,10 @@ behavior:
113
113
114
114
CoverBand === Band
115
115
# => true
116
-
116
+
117
117
band.members === Array
118
118
# => true
119
-
119
+
120
120
band.members === Mongoid::Association::Referenced::HasMany::Enumerable
121
121
# => true
122
122
@@ -207,55 +207,6 @@ in Mongoid 7.4 for backwards compatibility. In Mongoid 8 the default value
207
207
will change to ``true``.
208
208
209
209
210
- Respect Field Aliases In Embedded Documents When Using ``distinct`` And ``pluck``
211
- ---------------------------------------------------------------------------------
212
-
213
- When ``distinct`` and ``pluck`` are used with aliased fields in embedded
214
- documents, the aliases can be expanded if the ``Mongoid.broken_alias_handling``
215
- option is set to ``false``. By default, for backwards compatibility, in
216
- Mongoid 7.4 this option is set to true, yielding Mongoid 7.3 and earlier
217
- behavior. Given the following definitions:
218
-
219
- .. code-block:: ruby
220
-
221
- class Band
222
- include Mongoid::Document
223
- embeds_many :managers
224
- end
225
-
226
- class Manager
227
- include Mongoid::Document
228
- embedded_in :band
229
-
230
- field :name, as: :n
231
- end
232
-
233
- Mongoid 7.4 behavior with ``Mongoid.broken_alias_handling`` set to ``false``:
234
-
235
- .. code-block:: ruby
236
-
237
- # Expands out to "managers.name" in the query:
238
- Band.distinct('managers.n')
239
- Band.pluck('managers.n')
240
-
241
- Mongoid 7.3 and 7.4 with ``Mongoid.broken_alias_handling`` set to ``true`` behavior:
242
-
243
- .. code-block:: ruby
244
-
245
- # Sends "managers.n" without expanding the alias:
246
- Band.distinct('managers.n')
247
- Band.pluck('managers.n')
248
-
249
- .. note::
250
-
251
- The alias expansion for top-level fields has already been done by Mongoid 7.3.
252
-
253
- .. note::
254
-
255
- The value of ``Mongoid.broken_alias_handling`` option will change to ``false``
256
- by default in Mongoid 8.0.
257
-
258
-
259
210
``count``, ``sum``, ``avg``, ``min``, ``max`` Ignore Sort If Not Limiting/Skipping
260
211
----------------------------------------------------------------------------------
261
212
@@ -397,12 +348,64 @@ all scopes are cleared:
397
348
by default in Mongoid 8.0.
398
349
399
350
400
- Demongoize Values Returned from Pluck and Distinct
401
- --------------------------------------------------
351
+ Changes to ``distinct`` and ``pluck``
352
+ -------------------------------------
353
+
354
+ Respect Field Aliases In Embedded Documents When Using ``distinct`` and ``pluck``
355
+ `````````````````````````````````````````````````````````````````````````````````
356
+
357
+ When ``distinct`` and ``pluck`` are used with aliased fields in embedded
358
+ documents, the aliases can be expanded if the ``Mongoid.broken_alias_handling``
359
+ option is set to ``false``. By default, for backwards compatibility, in
360
+ Mongoid 7.4 this option is set to true, yielding Mongoid 7.3 and earlier
361
+ behavior. Given the following definitions:
362
+
363
+ .. code-block:: ruby
364
+
365
+ class Band
366
+ include Mongoid::Document
367
+ embeds_many :managers
368
+ end
369
+
370
+ class Manager
371
+ include Mongoid::Document
372
+ embedded_in :band
373
+
374
+ field :name, as: :n
375
+ end
376
+
377
+ Mongoid 7.4 behavior with ``Mongoid.broken_alias_handling`` set to ``false``:
378
+
379
+ .. code-block:: ruby
380
+
381
+ # Expands out to "managers.n" in the query:
382
+ Band.distinct('managers.name')
383
+ Band.pluck('managers.name')
384
+
385
+ Mongoid 7.3 and 7.4 with ``Mongoid.broken_alias_handling`` set to ``true`` behavior:
386
+
387
+ .. code-block:: ruby
388
+
389
+ # Sends "managers.name" without expanding the alias:
390
+ Band.distinct('managers.name')
391
+ Band.pluck('managers.name')
392
+
393
+ .. note::
394
+
395
+ The alias expansion for top-level fields has already been done by Mongoid 7.3.
396
+
397
+ .. note::
398
+
399
+ The value of ``Mongoid.broken_alias_handling`` option will change to ``false``
400
+ by default in Mongoid 8.0.
401
+
402
+
403
+ Demongoize Values Returned from ``pluck`` and ``distinct``
404
+ ``````````````````````````````````````````````````````````
402
405
403
406
Mongoid 7.4 with the ``Mongoid.legacy_pluck_distinct`` option set to ``false``
404
- will demongoize values returned from the pluck and distinct methods.
405
- For example :
407
+ will demongoize values returned from the `` pluck`` and `` distinct`` methods.
408
+ Given the following definitions :
406
409
407
410
.. code-block:: ruby
408
411
@@ -414,6 +417,11 @@ For example:
414
417
415
418
Band.create!(sales: "1E2")
416
419
Band.create!(sales: "2E2")
420
+
421
+ Mongoid 7.4 behavior with ``Mongoid.legacy_pluck_distinct`` set to ``false``:
422
+
423
+ .. code-block:: ruby
424
+
417
425
Band.pluck(:sales)
418
426
# => [0.1e3, 0.2e3]
419
427
Band.distinct(:sales)
@@ -435,3 +443,95 @@ distinct methods will not be demongoized. For example:
435
443
436
444
The value of ``Mongoid.legacy_pluck_distinct`` option will change to ``false``
437
445
by default in Mongoid 8.0.
446
+
447
+ Localized Fields with ``pluck`` and ``distinct``
448
+ ````````````````````````````````````````````````
449
+ Mongoid 7.4 with the ``Mongoid.legacy_pluck_distinct`` option set to ``false``
450
+ changes the behavior of using ``pluck`` and ``distinct`` with localized fields.
451
+ Now, when retrieving a localized field using these methods, the translation for
452
+ the current locale will be returned. To get the full translations hash the
453
+ ``_translations`` field can be used. Given the following definitions:
454
+
455
+ .. code-block:: ruby
456
+
457
+ class Band
458
+ include Mongoid::Document
459
+
460
+ field :name, localize: true
461
+ end
462
+
463
+ I18n.locale = :en
464
+ band = Band.create!(name: 'english-name')
465
+ I18n.locale = :de
466
+ band.name = 'deutsch-name'
467
+ band.save!
468
+
469
+ Mongoid 7.4 behavior with ``Mongoid.legacy_pluck_distinct`` set to ``false``:
470
+
471
+ .. code-block:: ruby
472
+
473
+ Band.pluck(:name)
474
+ # => ["deutsch-name"]
475
+ Band.pluck(:name_translations)
476
+ # => [{"en"=>"english-name", "de"=>"deutsch-name"}, {"en"=>"english-name", "de"=>"deutsch-name"}]
477
+
478
+ In Mongoid 7.3 and earlier, and in 7.4 with the ``Mongoid.legacy_pluck_distinct``
479
+ option set to ``true`` (the default), inputting a localized field returns the
480
+ full translations hash. Inputting the ``_translations`` field will return ``nil``.
481
+ For example:
482
+
483
+ .. code-block:: ruby
484
+
485
+ Band.pluck(:name)
486
+ # => [{"en"=>"english-name", "de"=>"deutsch-name"}, {"en"=>"english-name", "de"=>"deutsch-name"}]
487
+ Band.pluck(:name_translations)
488
+ # => [nil, nil]
489
+
490
+ .. note::
491
+
492
+ The value of ``Mongoid.legacy_pluck_distinct`` option will change to ``false``
493
+ by default in Mongoid 8.0.
494
+
495
+
496
+ Embedded Fields with ``pluck``
497
+ ``````````````````````````````
498
+ Mongoid 7.4 with the ``Mongoid.legacy_pluck_distinct`` option set to ``false``
499
+ returns the embedded values themselves, i.e. not inside a hash. Given the
500
+ following definitions:
501
+
502
+ .. code-block:: ruby
503
+
504
+ class Band
505
+ include Mongoid::Document
506
+
507
+ embeds_one :label
508
+ end
509
+
510
+ class Label
511
+ include Mongoid::Document
512
+
513
+ embedded_in :band
514
+ field :sales, type: BigDecimal
515
+ end
516
+
517
+ Mongoid 7.4 behavior with ``Mongoid.legacy_pluck_distinct`` set to ``false``:
518
+
519
+ .. code-block:: ruby
520
+
521
+ Band.pluck("label.sales")
522
+ # => [0.1e3]
523
+
524
+ In Mongoid 7.3 and earlier, and in 7.4 with the ``Mongoid.legacy_pluck_distinct``
525
+ option set to ``true`` (the default), plucking embedded attributes returns them
526
+ inside a hash. For example:
527
+
528
+ .. code-block:: ruby
529
+
530
+ Band.pluck("label.sales")
531
+ # => [{"sales"=>"1E2"}]
532
+
533
+ .. note::
534
+
535
+ The value of ``Mongoid.legacy_pluck_distinct`` option will change to ``false``
536
+ by default in Mongoid 8.0.
537
+
0 commit comments