Skip to content

Commit 257b61c

Browse files
authored
MONGOID-5260 Add _translations fields and dot-navigation to pluck and distinct (#5183)
* MONGOID-5260 add translation tests for pluck * MONGOID-5260 initial implementation * MONGOID-5260 revamp pluck method * MONGOID-5260 test callbacks * MONGOID-5260 fix dotted translation fields * MONGOID-5260 fix errors for pluck * MONGOID-5260 revamp the distinct method * MONGOID-5260 fix failing tests * MONGOID-5260 don't trigger callbacks * MOGOID-5260 use execute_callbacks * MONGOID-5260 add release notes * MONGOID-5260 fix selecting from localized fields * MONGOID-5260 add fallbacks tests for i18n * MONGOID-5260 change locale to fix failing tests * MONGOID-5260 test embedded localized fields and aliased associations * MONGOID-5260 remove includes for fallbacks * MONGOID-5260 refactor pluck to not instantiate a model * MONGOID-5260 move comment * MONGOID-5260 update comment * MONGOID-5260 demongoize value * MONGOID-5260 require fallbacks constraint * MONGOID-5260 finish rebase * MONGOID-5260 fix failing test with feature flag flip * empty commit * MONGOID-5260 minor doc change * MONGOID-5260 update comments, style * MONGOID-5260 update failing tests * MONGOID-5260 add files to fallbacks tests
1 parent 1ef890e commit 257b61c

File tree

14 files changed

+996
-162
lines changed

14 files changed

+996
-162
lines changed

.evergreen/run-tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ set +e
9999
if test -n "$TEST_CMD"; then
100100
eval $TEST_CMD
101101
elif test -n "$TEST_I18N_FALLBACKS"; then
102-
bundle exec rspec spec/integration/i18n_fallbacks_spec.rb
102+
bundle exec rspec spec/integration/i18n_fallbacks_spec.rb spec/mongoid/criteria_spec.rb spec/mongoid/contextual/mongo_spec.rb
103103
elif test -n "$APP_TESTS"; then
104104
# Need recent node for rails
105105
export N_PREFIX=$HOME/.n

docs/reference/configuration.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,10 @@ for details on driver options.
368368

369369
# Maintain legacy behavior of pluck and distinct, which does not demongoize
370370
# values on returning them. Setting this option to false will cause
371-
# pluck and distinct to return demongoized values.
371+
# pluck and distinct to return demongoized values. Setting this option to
372+
# false will also allow retrieving *_translations fields from pluck and
373+
# distinct and will return embedded values themselves (i.e. without
374+
# putting them in a hash).
372375
# (default: false)
373376
#legacy_pluck_distinct: true
374377

docs/release-notes/mongoid-7.4.txt

Lines changed: 159 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Mongoid 7.4 with ``Mongoid.legacy_triple_equals`` set to ``false`` behavior:
6363

6464
class Band
6565
include Mongoid::Document
66-
66+
6767
has_many :members
6868
end
6969

@@ -72,7 +72,7 @@ Mongoid 7.4 with ``Mongoid.legacy_triple_equals`` set to ``false`` behavior:
7272

7373
class Member
7474
include Mongoid::Document
75-
75+
7676
belongs_to :band
7777
end
7878

@@ -90,10 +90,10 @@ Mongoid 7.4 with ``Mongoid.legacy_triple_equals`` set to ``false`` behavior:
9090

9191
CoverBand === Band
9292
# => false
93-
93+
9494
band.members === Array
9595
# => false
96-
96+
9797
band.members === Mongoid::Association::Referenced::HasMany::Enumerable
9898
# => false
9999

@@ -113,10 +113,10 @@ behavior:
113113

114114
CoverBand === Band
115115
# => true
116-
116+
117117
band.members === Array
118118
# => true
119-
119+
120120
band.members === Mongoid::Association::Referenced::HasMany::Enumerable
121121
# => true
122122

@@ -207,55 +207,6 @@ in Mongoid 7.4 for backwards compatibility. In Mongoid 8 the default value
207207
will change to ``true``.
208208

209209

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-
259210
``count``, ``sum``, ``avg``, ``min``, ``max`` Ignore Sort If Not Limiting/Skipping
260211
----------------------------------------------------------------------------------
261212

@@ -397,12 +348,64 @@ all scopes are cleared:
397348
by default in Mongoid 8.0.
398349

399350

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+
``````````````````````````````````````````````````````````
402405

403406
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:
406409

407410
.. code-block:: ruby
408411

@@ -414,6 +417,11 @@ For example:
414417

415418
Band.create!(sales: "1E2")
416419
Band.create!(sales: "2E2")
420+
421+
Mongoid 7.4 behavior with ``Mongoid.legacy_pluck_distinct`` set to ``false``:
422+
423+
.. code-block:: ruby
424+
417425
Band.pluck(:sales)
418426
# => [0.1e3, 0.2e3]
419427
Band.distinct(:sales)
@@ -435,3 +443,95 @@ distinct methods will not be demongoized. For example:
435443

436444
The value of ``Mongoid.legacy_pluck_distinct`` option will change to ``false``
437445
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

Comments
 (0)