File tree Expand file tree Collapse file tree 2 files changed +53
-0
lines changed Expand file tree Collapse file tree 2 files changed +53
-0
lines changed Original file line number Diff line number Diff 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+
13991435Fallbacks
14001436---------
14011437
Original file line number Diff line number Diff line change @@ -99,3 +99,20 @@ specific version:
9999This is helpful for upgrading between versions. See the section on
100100:ref:`Version Based Default Configuration <load-defaults>` for more details on
101101how 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.
You can’t perform that action at this time.
0 commit comments