Skip to content

Commit 0405e00

Browse files
neilshwekyp-mongo
andauthored
MONGOID-5264 add :present option to localized fields (#5408)
* MONGOID-5264 add :present option to localize * MONGOID-5264 change present? to localize_present? * MONGOID-5264 add tests and fix present * MONGOID-5264 add docs and release notes * MONGOID-5264 potentially fix tests * MONGOID-5264 fix docs * MONGOID-5264 update the docstring * MONGOID-5264 reset fallbacks after setting * Update docs/reference/fields.txt Co-authored-by: Oleg Pudeyev <39304720+p-mongo@users.noreply.github.com> * MONGOID-5264 answer comments * Update lib/mongoid/attributes.rb Co-authored-by: Oleg Pudeyev <39304720+p-mongo@users.noreply.github.com> Co-authored-by: Oleg Pudeyev <39304720+p-mongo@users.noreply.github.com>
1 parent 05ad359 commit 0405e00

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

source/reference/fields.txt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,6 +1396,42 @@ You can get and set all the translations at once by using the corresponding ``_t
13961396
{ "en" => "Marvelous!", "de" => "Wunderbar!" }
13971397

13981398

1399+
.. _present-fields:
1400+
1401+
Localize ``:present`` Field Option
1402+
----------------------------------
1403+
1404+
Mongoid supports the ``:present`` option when creating a localized field:
1405+
1406+
.. code-block:: ruby
1407+
1408+
class Product
1409+
include Mongoid::Document
1410+
field :description, localize: :present
1411+
end
1412+
1413+
This option automatically removes ``blank`` values (i.e. those that return true
1414+
for the ``blank?`` method) from the ``_translations`` hash:
1415+
1416+
.. code-block:: ruby
1417+
1418+
I18n.default_locale = :en
1419+
product = Product.new
1420+
product.description = "Marvelous!"
1421+
I18n.locale = :de
1422+
product.description = "Fantastisch!"
1423+
1424+
product.description_translations
1425+
# { "en" => "Marvelous!", "de" => "Fantastisch!" }
1426+
1427+
product.description = ""
1428+
product.description_translations
1429+
# { "en" => "Marvelous!" }
1430+
1431+
When the empty string is written for the ``:de`` locale, the ``"de"`` key is
1432+
removed from the ``_translations`` hash instead of writing the empty string.
1433+
1434+
13991435
Fallbacks
14001436
---------
14011437

source/release-notes/mongoid-8.1.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,20 @@ specific version:
9999
This is helpful for upgrading between versions. See the section on
100100
:ref:`Version Based Default Configuration <load-defaults>` for more details on
101101
how to use this feature to make upgrading between Mongoid versions easier.
102+
103+
104+
Added ``:present`` option to localized fields
105+
---------------------------------------------
106+
107+
The ``:present`` option was added to localized fields for removing blank values
108+
from the ``_translations`` hash:
109+
110+
.. code-block:: ruby
111+
112+
class Product
113+
include Mongoid::Document
114+
field :description, localize: :present
115+
end
116+
117+
See the section on :ref:`Localize :present Field Option <present-fields>` for
118+
more details on how these are used.

0 commit comments

Comments
 (0)