Skip to content

Commit b461b76

Browse files
neilshwekyp-mongo
andauthored
MONGOID-5461 Change custom field options example (#5438)
* MONGOID-5461 Change custom field options example * Apply suggestions from code review 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 5f825ec commit b461b76

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

docs/reference/fields.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,16 +1208,16 @@ Custom Field Options
12081208
You may define custom options for the ``field`` macro function
12091209
which extend its behavior at the your time model classes are loaded.
12101210

1211-
As an example, we will define a ``:required`` option which will add a presence
1211+
As an example, we will define a ``:max_length`` option which will add a length
12121212
validator for the field. First, declare the new field option in an initializer,
12131213
specifiying its handler function as a block:
12141214

12151215
.. code-block:: ruby
12161216

12171217
# in /config/initializers/mongoid_custom_fields.rb
12181218

1219-
Mongoid::Fields.option :required do |model, field, value|
1220-
model.validates_presence_of field if value
1219+
Mongoid::Fields.option :max_length do |model, field, value|
1220+
model.validates_length_of field.name, maximum: value
12211221
end
12221222

12231223
Then, use it your model class:
@@ -1227,7 +1227,7 @@ Then, use it your model class:
12271227
class Person
12281228
include Mongoid::Document
12291229

1230-
field :name, type: String, required: true
1230+
field :name, type: String, max_length: 10
12311231
end
12321232

12331233
Note that the handler function will be invoked whenever the option is used

0 commit comments

Comments
 (0)