Skip to content

Commit b42af93

Browse files
committed
wip
1 parent 226df34 commit b42af93

File tree

6 files changed

+224
-9
lines changed

6 files changed

+224
-9
lines changed

source/add-existing.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,10 @@ for each Rails component, as shown in the following sample
130130
.. note::
131131

132132
Because they rely on Active Record, the `ActionText
133-
<https://guides.rubyonrails.org/action_text_overview.html>`__,
133+
<{+active-record-docs+}/action_text_overview.html>`__,
134134
`ActiveStorage <https://edgeguides.rubyonrails.org/active_storage_overview.html>`__, and
135135
`ActionMailbox
136-
<https://guides.rubyonrails.org/action_mailbox_basics.html>`__
136+
<{+active-record-docs+}/action_mailbox_basics.html>`__
137137
adapters cannot be used alongside {+odm+}.
138138

139139
Disable Active Record Adapters

source/data-modeling.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ Model Your Data
2222

2323
In this section, you can learn how to model data in {+odm+}.
2424

25-
In this section, you can learn how to model data in {+odm+}.
26-
2725
- :ref:`mongoid-modeling-documents`: Learn about the ``Document``
2826
module.
2927

source/data-modeling/callbacks.txt

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,143 @@ more, see `Callbacks
3232
<{+active-record-docs+}/active_record_callbacks.html>`__ in the
3333
Active Record documentation.
3434

35+
Supported Callbacks
36+
-------------------
37+
38+
Mongoid supports the following callbacks on model classes that implement
39+
the :ref:`Document <mongoid-modeling-documents>` module:
40+
41+
- ``after_initialize``
42+
- ``after_build``
43+
- ``before_validation``
44+
- ``after_validation``
45+
- ``before_create``
46+
- ``around_create``
47+
- ``after_create``
48+
- ``after_find``
49+
- ``before_update``
50+
- ``around_update``
51+
- ``after_update``
52+
- ``before_upsert``
53+
- ``around_upsert``
54+
- ``after_upsert``
55+
- ``before_save``
56+
- ``around_save``
57+
- ``after_save``
58+
- ``before_destroy``
59+
- ``around_destroy``
60+
- ``after_destroy``
61+
62+
To learn more about any of the preceding callback types, see the
63+
`ActiveRecord::Callbacks
64+
<https://api.rubyonrails.org/{+rails-8-version-docs+}/classes/ActiveRecord/Callbacks.html>`__
65+
reference in the Rails API documentation.
66+
67+
You can implement a callback both top-level and embedded documents.
68+
69+
.. note:: Callback Invocation Behavior
70+
71+
For efficiency, {+odm+} invokes the callback only on the document
72+
that you performed the persistence action on. This behavior enables
73+
{+odm+} to support large hierarchies and handle optimized atomic
74+
updates efficiently by not invoking callbacks throughout the document
75+
hierarchy.
76+
77+
Take precautions and ensure testability when implementing callbacks for
78+
domain logic, because these designs can lead to unexpected errors when
79+
callbacks in the chain halt execution. We recommend using callbacks for
80+
cross-cutting concerns, such as queueing up background jobs.
81+
82+
Document Callbacks
83+
------------------
84+
85+
You must implement and register callbacks on your model classes.
86+
You can register a callback by using ordinary methods, blocks and
87+
``Proc`` objects, or by defining custom callback objects that use
88+
classes or modules.
89+
90+
This example demonstrates how to register callbacks on the ``Contact``
91+
model class in the following ways:
92+
93+
- Includes the ``before_save`` class method which triggers the
94+
``process_phone`` method before a ``Contact`` instance is saved to
95+
MongoDB. The ``process_phone`` method is defined separately in the class.
96+
97+
- Includes the ``after_destroy`` class method and uses a block to print a
98+
message when a ``Contact`` instance is deleted.
99+
100+
.. literalinclude:: /includes/data-modeling/callbacks.rb
101+
:start-after: start-doc-callback
102+
:end-before: end-doc-callback
103+
:language: ruby
104+
:emphasize-lines: 8, 11-13, 16-18
105+
:dedent:
106+
107+
The following code performs data operations that demonstrate the
108+
callback actions:
109+
110+
.. literalinclude:: /includes/data-modeling/callbacks.rb
111+
:start-after: start-doc-ops
112+
:end-before: end-doc-ops
113+
:language: ruby
114+
:dedent:
115+
116+
Because callback functionality comes from Active Support, you can use
117+
the ``set_callback`` class method syntax to register callbacks, as shown
118+
in the following code:
119+
120+
.. literalinclude:: /includes/data-modeling/callbacks.rb
121+
:start-after: start-doc-set-syntax
122+
:end-before: end-doc-set-syntax
123+
:language: ruby
124+
:dedent:
125+
126+
Association Callbacks
127+
---------------------
128+
129+
{+odm+} provides the following association callbacks:
130+
131+
- ``after_add``
132+
- ``after_remove``
133+
- ``before_add``
134+
- ``before_remove``
135+
136+
If you register an association callback on your model class, it is
137+
invoked whenever you add or remove a document from any of the following
138+
associations:
139+
140+
- ``embeds_many``
141+
- ``has_many``
142+
- ``has_and_belongs_to_many``
143+
144+
Specify association callbacks as options on the respective association.
145+
You must pass the added or removed document as the parameter to the
146+
specified callback.
147+
148+
The following code demonstrates how to register an association callback
149+
on a ``User`` model class that embeds multiple ``SavedArticle``
150+
instances:
151+
152+
.. literalinclude:: /includes/data-modeling/callbacks.rb
153+
:start-after: start-association-callback
154+
:end-before: end-association-callback
155+
:language: ruby
156+
:emphasize-lines: 8-13
157+
:dedent:
158+
159+
Additional Information
160+
----------------------
161+
162+
To learn how to prevent {+odm+} from running callbacks, see the
163+
following references in the Active Record documentation:
164+
165+
- `Skipping Callbacks <{+active-record-docs+}/active_record_callbacks.html#skipping-callbacks>`__
166+
- `Suppressing Callbacks <{+active-record-docs+}/active_record_callbacks.html#suppressing-callbacks>`__
167+
168+
To learn about how {+odm+} manages callbacks in transactions, see the
169+
:ref:`mongoid-data-txn` guide.
170+
171+
To learn how to access and change your MongoDB data, see the
172+
:ref:`mongoid-interact-data` guides.
173+
174+
.. TODO Add link to field types guide.

source/data-modeling/validation.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ of document fields in your collections.
2929
{+odm+} includes ``ActiveModel::Validations`` from Active Record to
3030
provide validation functionality, including an associated and uniqueness
3131
validator. To learn more, see the `Active Record Validations
32-
<https://guides.rubyonrails.org/active_record_validations.html>`__
32+
<{+active-record-docs+}/active_record_validations.html>`__
3333
Rails guide and `ActiveModel::Validations
3434
<https://api.rubyonrails.org/classes/ActiveModel/Validations.html>`__
3535
Rails API documentation.
@@ -284,14 +284,14 @@ Custom Validation Rules
284284
You can use the ``validates_each`` and ``validates_with`` helpers to
285285
create custom validators. To learn more about these helpers and view
286286
examples, see the `validates_each
287-
<https://guides.rubyonrails.org/active_record_validations.html#validates-each>`__
287+
<{+active-record-docs+}/active_record_validations.html#validates-each>`__
288288
and `validates_with
289-
<https://guides.rubyonrails.org/active_record_validations.html#validates-with>`__
289+
<{+active-record-docs+}/active_record_validations.html#validates-with>`__
290290
references in the Active Record documentation.
291291

292292
To learn more about custom validators, see `Performing Custom
293293
Validations
294-
<https://guides.rubyonrails.org/active_record_validations.html#performing-custom-validations>`__
294+
<{+active-record-docs+}/active_record_validations.html#performing-custom-validations>`__
295295
in the Active Record documentation.
296296

297297
Behavior
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# start-doc-callback
2+
class Contact
3+
include Mongoid::Document
4+
5+
field :name, type: String
6+
field :phone, type: String
7+
8+
# Creates a callback to clean phone numbers before saving
9+
before_save :process_phone
10+
11+
protected
12+
def process_phone
13+
self.phone = phone.gsub(/[^0-9]/, "") if attribute_present?("phone")
14+
end
15+
16+
# Creates a callback to send a message about object deletion
17+
after_destroy do
18+
p "deleted the contact for #{name}"
19+
end
20+
end
21+
# end-doc-callback
22+
23+
# start-doc-ops
24+
Contact.create(name: 'Serena Atherton', phone: '999 555-3030')
25+
# => `phone` field saved as '9995553030'
26+
Contact.create(name: 'Zayba Haq', phone: '999 123?5050')
27+
# => `phone` field saved as '9991235050'
28+
29+
Contact.first.destroy
30+
# => Console message: "deleted the contact for Serena Atherton"
31+
# end-doc-ops
32+
33+
# start-doc-set-syntax
34+
class Contact
35+
include Mongoid::Document
36+
37+
field :name, type: String
38+
field :phone, type: String
39+
field :aliases, type: Array, default: []
40+
41+
set_callback(:update, :before) do |document|
42+
if document.name_changed?
43+
document.push(aliases: document.name_was)
44+
end
45+
end
46+
end
47+
48+
Contact.create(name: 'Xavier Bloom', phone: '4447779999')
49+
Contact.first.update(name: 'Xav - coworker')
50+
# Saved document in MongoDB:
51+
# {"aliases":["Xavier Bloom"],"name":"Xav - coworker","phone":"4447779999"}
52+
# end-doc-set-syntax
53+
54+
# start-association-callback
55+
class User
56+
include Mongoid::Document
57+
58+
field :username, type: String
59+
embeds_many :saved_articles, before_add: :send_message
60+
61+
protected
62+
# Passes the association document as a parameter to the callback
63+
def send_message(saved_article)
64+
if saved_articles.count >= 10
65+
p "you can't save more than 10 articles at a time"
66+
throw(:abort)
67+
end
68+
end
69+
end
70+
71+
class SavedArticle
72+
include Mongoid::Document
73+
embedded_in :user
74+
75+
field :url, type: String
76+
end
77+
# end-association-callback

source/quick-start-rails.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ modeled and displayed. {+odm+} replaces the default Active Record
4545
adapter for data modeling in Rails.
4646

4747
To learn more about Ruby on Rails, see the `Getting Started
48-
with Rails <https://guides.rubyonrails.org/getting_started.html>`__
48+
with Rails <{+active-record-docs+}/getting_started.html>`__
4949
guide in the Rails documentation.
5050

5151
MongoDB Atlas is a fully managed cloud database service that hosts your

0 commit comments

Comments
 (0)