Skip to content

Commit 028965e

Browse files
authored
MONGOID-5474/MONGOID-5478 add docs and release notes (#5457)
* MONGOID-5474 add docs and release notes * MONGOID-5474 use read-only * MONGOID-5474 add configuration docs * MOGOID-5474 switch order of flag docs * add MONGOID-5477 use case
1 parent 41cb4b7 commit 028965e

File tree

4 files changed

+121
-1
lines changed

4 files changed

+121
-1
lines changed

source/reference/configuration.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,17 @@ for details on driver options.
383383
# (default: false)
384384
#legacy_pluck_distinct: true
385385

386+
# When this flag is false, a document will become read-only only once the
387+
# #readonly! method is called, and an error will be raised on attempting
388+
# to save or update such documents, instead of just on delete. When this
389+
# flag is true, a document is only read-only if it has been projected
390+
# using #only or #without, and read-only documents will not be
391+
# deletable/destroyable, but will be savable/updatable.
392+
# When this feature flag is turned on, the read-only state will be reset on
393+
# reload, but when it is turned off, it won't be.
394+
# (default: true)
395+
legacy_readonly: false
396+
386397
# Maintain legacy behavior of === on Mongoid document classes, which
387398
# returns true in a number of cases where Ruby's === implementation would
388399
# return false. Note that the behavior of === on Mongoid document

source/reference/crud.txt

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,3 +974,69 @@ back to the model as follows:
974974

975975
band.tours
976976
# => #<Set: {"London"}>
977+
978+
979+
.. _readonly-documents:
980+
981+
Readonly Documents
982+
==================
983+
984+
Documents can be marked read-only in two ways, depending on the value of the
985+
``Mongoid.legacy_readonly`` feature flag:
986+
987+
If this flag is turned off, a document is marked read-only when the ``#readonly!``
988+
method is called on that documnet. A read-only document, with this flag turned off,
989+
will raise a ReadonlyDocument error on attempting to perform any persistence
990+
operation, including (but not limited to) saving, updating, deleting and
991+
destroying. Note that reloading does not reset the read-only state.
992+
993+
.. code:: ruby
994+
995+
band = Band.first
996+
band.readonly? # => false
997+
band.readonly!
998+
band.readonly? # => true
999+
band.name = "The Rolling Stones"
1000+
band.save # => raises ReadonlyDocument error
1001+
band.reload.readonly? # => true
1002+
1003+
If this flag is turned on, a document is marked read-only when that document
1004+
has been projected (i.e. using ``#only`` or ``#without``). A read-only document,
1005+
with this flag turned on, will not be deletable or destroyable (a
1006+
``ReadonlyDocument`` error will be raised), but will be saveable and updatable.
1007+
The read-only status is reset on reloading the document.
1008+
1009+
.. code:: ruby
1010+
1011+
class Band
1012+
include Mongoid::Document
1013+
field :name, type: String
1014+
field :genre, type: String
1015+
end
1016+
1017+
band = Band.only(:name).first
1018+
band.readonly? # => true
1019+
band.destroy # => raises ReadonlyDocument error
1020+
band.reload.readonly? # => false
1021+
1022+
1023+
Overriding ``readonly?``
1024+
------------------------
1025+
1026+
Another way to make a document read-only is by overriding the readonly? method:
1027+
1028+
.. code:: ruby
1029+
1030+
class Band
1031+
include Mongoid::Document
1032+
field :name, type: String
1033+
field :genre, type: String
1034+
1035+
def readonly?
1036+
true
1037+
end
1038+
end
1039+
1040+
band = Band.first
1041+
band.readonly? # => true
1042+
band.destroy # => raises ReadonlyDocument error

source/reference/fields.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1648,7 +1648,7 @@ Assignments to read-only attributes using their setters will be ignored:
16481648
# => "The Rolling Stones"
16491649

16501650
Calls to atomic persistence operators, like ``bit`` and ``inc``, will persist
1651-
changes to readonly fields.
1651+
changes to read-only fields.
16521652

16531653
Timestamp Fields
16541654
================

source/release-notes/mongoid-8.1.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ The complete list of releases is available `on GitHub
1717
please consult GitHub releases for detailed release notes and JIRA for
1818
the complete list of issues fixed in each release, including bug fixes.
1919

20+
2021
Added ``attribute_before_last_save``, ``saved_change_to_attribute``, ``saved_change_to_attribute?``, and ``will_save_change_to_attribute?`` methods
2122
----------------------------------------------------------------------------------------------------------------------------------------------------
2223

@@ -239,3 +240,45 @@ documents should be stored in using the ``store_in`` macro:
239240
Previously, an error was raised if this was done. See the section on
240241
:ref:`Inheritance Persistence Context <inheritance-persistence-context>`
241242
for more details.
243+
244+
245+
Added ``readonly!`` method and ``legacy_readonly`` feature flag
246+
---------------------------------------------------------------
247+
248+
Mongoid 8.1 changes the meaning of read-only documents. In Mongoid 8.1 with
249+
this feature flag turned off, a document becomes read-only when calling the
250+
``readonly!`` method:
251+
252+
.. code:: ruby
253+
254+
band = Band.first
255+
band.readonly? # => false
256+
band.readonly!
257+
band.readonly? # => true
258+
band.name = "The Rolling Stones"
259+
band.save # => raises ReadonlyDocument error
260+
261+
With this feature flag turned off, a ``ReadonlyDocument`` error will be
262+
raised when destroying or deleting, as well as when saving or updating.
263+
264+
Prior to Mongoid 8.1 and in 8.1 with the ``legacy_readonly`` feature flag
265+
turned on, documents become read-only when they are projected (i.e. using
266+
``#only`` or ``#without``).
267+
268+
.. code:: ruby
269+
270+
class Band
271+
include Mongoid::Document
272+
field :name, type: String
273+
field :genre, type: String
274+
end
275+
276+
band = Band.only(:name).first
277+
band.readonly? # => true
278+
band.destroy # => raises ReadonlyDocument error
279+
280+
Note that with this feature flag on, a ``ReadonlyDocument`` error will only be
281+
raised when destroying or deleting, and not on saving or updating. See the
282+
section on :ref:`Read-only Documents <readonly-documents>` for more details.
283+
284+

0 commit comments

Comments
 (0)