Skip to content

Commit d914924

Browse files
committed
Merge remote-tracking branch 'remotes/origin/master' into evolve-rework
2 parents c714cca + 6407218 commit d914924

File tree

93 files changed

+2215
-400
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+2215
-400
lines changed

docs/contributing/code-documentation.txt

Lines changed: 80 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Structure
3535
class CardboardBox
3636

3737
- **Methods:** All method definitions should be preceded by a documentation comment.
38-
Use ``@param`` and ``@return`` to specify input(s) and output respectively.
38+
Use ``@param``, ``@yield``, and ``@return`` to specify inputs and output.
3939
For further details, refer to
4040
:ref:`Type Declaration <code-documentation-type-declaration>` below.
4141

@@ -48,33 +48,30 @@ Structure
4848
# @return [ Tiger ] The transmogrified result.
4949
def transmogrify(person)
5050

51+
- **Errors:** Use ``@raise`` to explain errors specific to the method.
52+
53+
.. code-block:: ruby
54+
55+
# @raise [ Errors::Validations ] If validation failed.
56+
def validate!
57+
5158
- **Private Methods:** Private methods should be documented unless they are
5259
so brief and straightforward that it is obvious what they do. Note that,
5360
for example, a method may be brief and straightforward but the type of
54-
its parameter may not be obvious, in which case the parameter needs to
55-
be appropriately documented.
61+
its parameter may not be obvious, in which case the parameter must be
62+
appropriately documented.
5663

5764
.. code-block:: ruby
5865

5966
private
6067

6168
# Documentation is optional here.
62-
def my_internal_method
63-
64-
- **Notes:** Use the ``@note`` macro to explain caveats, edge cases,
65-
and behavior which may surprise users.
66-
67-
.. code-block:: ruby
68-
69-
# Clear all stored data.
70-
#
71-
# @note This operation deletes data in the database.
72-
def erase_data!
69+
def do_something_obvious
7370

7471
- **API Private:** Classes and public methods which are not intended for
7572
external usage should be marked ``@api private``. This macro does not
7673
require a comment.
77-
74+
7875
Note that, because Mongoid's modules are mixed into application classes,
7976
``private`` visibility of a method does not necessarily indicate its
8077
status as an API private method.
@@ -86,6 +83,18 @@ Structure
8683
# @api private
8784
def dont_call_me_from_outside
8885

86+
- **Notes and TODOs:** Use ``@note`` to explain caveats, edge cases,
87+
and behavior which may surprise users. Use ``@todo`` to record
88+
follow-ups and suggestions for future improvement.
89+
90+
.. code-block:: ruby
91+
92+
# Clear all stored data.
93+
#
94+
# @note This operation deletes data in the database.
95+
# @todo Refactor this method for performance.
96+
def erase_data!
97+
8998
- **Deprecation:** Use the ``@deprecated`` macro to indicate deprecated
9099
functionality. This macro does not require a comment.
91100

@@ -286,3 +295,59 @@ Type Declaration
286295
# @option **kwargs [ String | Array<String> ] :items The items(s) as Strings to include.
287296
# @option **kwargs [ Integer ] :limit An Integer denoting the limit.
288297
def buy_groceries(**kwargs)
298+
299+
- **Blocks:** Use ``@yield`` to specify when the method yields to a block.
300+
301+
.. code-block:: ruby
302+
303+
# @yield [ Symbol, Symbol, Symbol ] Evaluate the guess of who did the crime.
304+
# Must take the person, location, and weapon used. Must return true or false.
305+
def whodunit
306+
yield(:mustard, :ballroom, :candlestick)
307+
end
308+
309+
- **Blocks:** If the method explicitly specifies a block argument, specify the block
310+
argument using ``@param`` preceded by an ampersand ``&``, and also specify ``@yield``.
311+
Note ``@yield`` should be used even when method calls ``block.call`` rather than
312+
``yield`` internally.
313+
314+
.. code-block:: ruby
315+
316+
# @param &block The block.
317+
# @yield [ Symbol, Symbol, Symbol ] Evaluate the guess of who did the crime.
318+
# Must take the person, location, and weapon used. Must return true or false.
319+
def whodunit(&block)
320+
yield(:scarlet, :library, :rope)
321+
end
322+
323+
# @param &block The block.
324+
# @yield [ Symbol, Symbol, Symbol ] Evaluate the guess of who did the crime.
325+
# Must take the person, location, and weapon used. Must return true or false.
326+
def whodunit(&block)
327+
block.call(:plum, :kitchen, :pipe)
328+
end
329+
330+
- **Blocks:** Use ``@yieldparam`` and ``@yieldreturn`` instead of ``@yield`` where
331+
beneficial for clarity.
332+
333+
.. code-block:: ruby
334+
335+
# @param &block The block.
336+
# @yieldparam [ Symbol ] The person.
337+
# @yieldparam [ Symbol ] The location.
338+
# @yieldparam [ Symbol ] The weapon used.
339+
# @yieldreturn [ true | false ] Whether the guess is correct.
340+
def whodunit(&block)
341+
yield(:peacock, :conservatory, :wrench)
342+
end
343+
344+
- **Proc Args:** Proc arguments should use ``@param`` (not ``@yield``). The
345+
inputs to the proc may be specified as subtype(s).
346+
347+
.. code-block:: ruby
348+
349+
# @param [ Proc<Integer, Integer, Integer> ] my_proc Proc argument which must
350+
# take 3 integers and must return true or false whether the guess is valid.
351+
def guess_three(my_proc)
352+
my_proc.call(42, 7, 88)
353+
end

docs/installation.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ To install the gem with bundler, include the following in your ``Gemfile``:
2727

2828
.. code-block:: ruby
2929

30-
gem 'mongoid', '~> 7.3.0'
30+
gem 'mongoid', '~> 9.0.0'
3131

3232
Using Mongoid with a New Rails Application
3333
==========================================

docs/reference/collection-configuration.txt

Lines changed: 69 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,51 +10,96 @@ Collection Configuration
1010
:depth: 2
1111
:class: singlecol
1212

13+
Configuring a Document Collection
14+
=================================
1315

14-
Capped Collections
15-
------------------
16+
You can specify collection options for documents using the ``store_in`` macro.
17+
This macro accepts ``:collection_options`` argument, which can contain any collection
18+
options that are supported by the driver.
19+
20+
.. note::
21+
22+
In order to apply the options, the collection must be explicitly created up-front.
23+
This should be done using :ref:`Collection Management Rake Task<collection-management-task>`.
1624

17-
Mongoid does not provide a mechanism for creating capped collections on the fly - you
18-
will need to create these yourself one time up front either with the driver or via the
19-
Mongo console.
25+
Please refer to `the driver collections page
26+
<https://mongodb.com/docs/ruby-driver/current/reference/collection-tasks/>`_
27+
for the more information about collection options.
2028

21-
To create a capped collection with the driver, first retrieve the client:
29+
.. note::
30+
31+
Collection options depend on the driver version and MongoDB server version.
32+
It is possible that some options, like time series collections, are not available
33+
on older server versions.
34+
35+
Time Series Collection
36+
----------------------
2237

2338
.. code-block:: ruby
2439

25-
class Name
40+
class Measurement
2641
include Mongoid::Document
27-
end
28-
client = Name.collection.client
2942

30-
Then create the collection:
43+
field :temperature, type: Integer
44+
field :timestamp, type: Time
3145

32-
.. code-block:: ruby
46+
store_in collection_options: {
47+
time_series: {
48+
timeField: "timestamp",
49+
granularity: "minutes"
50+
},
51+
expire_after: 604800
52+
}
53+
end
3354

34-
client["names", :capped => true, :size => 1024].create
3555

36-
To create a capped collection from the Mongo console:
3756

38-
.. code-block:: javascript
57+
Capped Collections
58+
------------------
3959

40-
db.createCollection("name", { capped: true, size: 1024 });
60+
.. code-block:: ruby
4161

62+
class Name
63+
include Mongoid::Document
64+
65+
store_in collection_options: {
66+
capped: true,
67+
size: 1024
68+
}
69+
end
4270

4371
Set a Default Collation on a Collection
4472
---------------------------------------
4573

46-
Mongoid does not provide a mechanism for creating a collection with a default collation.
47-
Like capped collections, you will need to create the collection yourself one time, up-front,
48-
either with the driver or via the Mongo console.
74+
.. code-block:: ruby
4975

50-
To create a collection with a default collation with the driver:
76+
class Name
77+
include Mongoid::Document
5178

52-
.. code-block:: ruby
79+
store_in collection_options: {
80+
collation: {
81+
locale: 'fr'
82+
}
83+
}
84+
end
85+
86+
.. _collection-management-task:
87+
88+
Collection Management Rake Task
89+
===============================
5390

54-
client["name", :collation => { :locale => 'fr'}].create
91+
If you specify collection options for a document, then the corresponding collection
92+
must be explicitly created prior to use. To do so, use the provided
93+
``db:mongoid:create_collections`` Rake task:
5594

56-
To create a collection with a default collation from the Mongo console:
95+
.. code-block:: bash
5796

58-
.. code-block:: javascript
97+
$ rake db:mongoid:create_collections
98+
99+
The create collections command also works for just one model by running
100+
in Rails console:
101+
102+
.. code-block:: ruby
59103

60-
db.createCollection("name", { collation: { locale: 'fr' } });
104+
# Create collection for Model
105+
Model.create_collection

docs/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

docs/reference/crud.txt

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ Mongoid provides several ways of accessing field values.
657657
.. note::
658658

659659
All of the access methods described below raise
660-
``ActiveModel::MissingAttributeError`` when the field being accessed is
660+
``Mongoid::Errors::AttributeNotLoaded`` when the field being accessed is
661661
:ref:`projected out <projection>`, either by virtue of not being included in
662662
:ref:`only <only>` or by virtue of being included in
663663
:ref:`without <without>`. This applies to both reads and writes.
@@ -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

docs/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
================

0 commit comments

Comments
 (0)