@@ -1208,16 +1208,16 @@ Custom Field Options
1208
1208
You may define custom options for the ``field`` macro function
1209
1209
which extend its behavior at the your time model classes are loaded.
1210
1210
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
1212
1212
validator for the field. First, declare the new field option in an initializer,
1213
1213
specifiying its handler function as a block:
1214
1214
1215
1215
.. code-block:: ruby
1216
1216
1217
1217
# in /config/initializers/mongoid_custom_fields.rb
1218
1218
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
1221
1221
end
1222
1222
1223
1223
Then, use it your model class:
@@ -1227,7 +1227,7 @@ Then, use it your model class:
1227
1227
class Person
1228
1228
include Mongoid::Document
1229
1229
1230
- field :name, type: String, required: true
1230
+ field :name, type: String, max_length: 10
1231
1231
end
1232
1232
1233
1233
Note that the handler function will be invoked whenever the option is used
0 commit comments