Skip to content

Commit e8046dc

Browse files
committed
Move DSL to top-level config
1 parent 3327b80 commit e8046dc

File tree

5 files changed

+71
-89
lines changed

5 files changed

+71
-89
lines changed

docs/reference/fields.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -921,8 +921,8 @@ You may optionally declare a mapping for the new field type in an initializer:
921921

922922
# in /config/initializers/mongoid_custom_fields.rb
923923

924-
Mongoid::Fields.configure do
925-
define_type :point, Point
924+
Mongoid.configure do |config|
925+
config.define_field_type :point, Point
926926
end
927927

928928

@@ -1021,8 +1021,8 @@ specifiying its handler function as a block:
10211021

10221022
# in /config/initializers/mongoid_custom_fields.rb
10231023

1024-
Mongoid::Fields.configure do
1025-
option :required do |model, field, value|
1024+
Mongoid.configure do |config|
1025+
config.define_field_option :required do |model, field, value|
10261026
model.validates_presence_of field.name if value
10271027
end
10281028
end

docs/release-notes/mongoid-8.0.txt

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,15 +322,42 @@ Mongoid 8.0 adds the ability to define custom ``field :type`` Symbol values as f
322322

323323
.. code-block:: ruby
324324

325-
# in /config/initializers/mongoid_custom_fields.rb
325+
# in /config/initializers/mongoid.rb
326326

327-
Mongoid::Fields.configure do
328-
define_type :point, Point
327+
Mongoid.configure do |config|
328+
config.define_field_type :point, Point
329329
end
330330

331331
Refer to the :ref:`docs <http://docs.mongodb.org/manual/reference/fields/#custom-field-types>` for details.
332332

333333

334+
Support for Defining Custom Field Options via Top-Level Config
335+
--------------------------------------------------------------
336+
337+
Mongoid 8.0 adds the ability to define custom ``field`` options as follows:
338+
339+
.. code-block:: ruby
340+
341+
# in /config/initializers/mongoid.rb
342+
343+
Mongoid.configure do |config|
344+
config.define_field_option :required do |model, field, value|
345+
model.validates_presence_of field.name if value
346+
end
347+
end
348+
349+
In Mongoid 7, this was possible with the following legacy syntax. Users are
350+
recommended to migrate to the Mongoid 8.0 syntax above.
351+
352+
.. code-block:: ruby
353+
354+
Mongoid::Fields.option :required do |model, field, value|
355+
model.validates_presence_of field.name if value
356+
end
357+
358+
Refer to the :ref:`docs <http://docs.mongodb.org/manual/reference/fields/#custom-field-options>` for details.
359+
360+
334361
``#pluck`` on Embedded Criteria Returns ``nil`` Values
335362
------------------------------------------------------
336363

lib/mongoid/config.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,43 @@ def running_with_passenger?
314314
@running_with_passenger ||= defined?(PhusionPassenger)
315315
end
316316

317+
# Defines a field type mapping, for later use in field :type option.
318+
#
319+
# @example
320+
# Mongoid.configure do
321+
# define_field_type :point, Point
322+
# end
323+
#
324+
# @param [ Symbol | String ] type_name The identifier of the
325+
# defined type. This identifier may be accessible as either a
326+
# Symbol or a String regardless of the type passed to this method.
327+
# @param [ Module ] klass the class of the defined type, which must
328+
# include mongoize, demongoize, and evolve methods.
329+
def define_field_type(type_name, klass)
330+
Mongoid::Fields::FieldTypes.define_type(type_name, klass)
331+
end
332+
333+
# Defines an option for the field macro, which runs the handler
334+
# provided as a block.
335+
#
336+
# No assumptions are made about what functionality the handler might
337+
# perform, so it will always be called if the `option_name` key is
338+
# provided in the field definition -- even if it is false or nil.
339+
#
340+
# @example
341+
# Mongoid.configure do
342+
# define_field_option :required do |model, field, value|
343+
# model.validates_presence_of field.name if value
344+
# end
345+
# end
346+
#
347+
# @param [ Symbol ] option_name the option name to match against
348+
# @param [ Proc ] block the handler to execute when the option is
349+
# provided.
350+
def define_field_option(option_name, &block)
351+
Mongoid::Fields.option(option_name, &block)
352+
end
353+
317354
private
318355

319356
def set_log_levels

lib/mongoid/fields.rb

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -257,33 +257,6 @@ def validate_writable_field_name!(name)
257257

258258
class << self
259259

260-
# DSL method used for configuration readability, typically in
261-
# an initializer.
262-
#
263-
# @example
264-
# Mongoid::Fields.configure do
265-
# # do configuration
266-
# end
267-
def configure(&block)
268-
instance_exec(&block)
269-
end
270-
271-
# Defines a field type mapping, for later use in field :type option.
272-
#
273-
# @example
274-
# Mongoid::Fields.configure do
275-
# define_type :point, Point
276-
# end
277-
#
278-
# @param [ Symbol | String ] field_type The identifier of the
279-
# defined type. This identifier may be accessible as either a
280-
# Symbol or a String regardless of the type passed to this method.
281-
# @param [ Module ] klass the class of the defined type, which must
282-
# include mongoize, demongoize, and evolve methods.
283-
def define_type(field_type, klass)
284-
Fields::FieldTypes.define_type(field_type, klass)
285-
end
286-
287260
# Stores the provided block to be run when the option name specified is
288261
# defined on a field.
289262
#

spec/mongoid/fields_spec.rb

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,61 +1933,6 @@ class DiscriminatorChild2 < DiscriminatorParent
19331933
end
19341934
end
19351935

1936-
describe '.configure DSL' do
1937-
1938-
context '.type method' do
1939-
around do |example|
1940-
klass = Mongoid::Fields::FieldTypes
1941-
klass.instance_variable_set(:@mapping, klass::DEFAULT_MAPPING.dup)
1942-
example.run
1943-
klass.instance_variable_set(:@mapping, klass::DEFAULT_MAPPING.dup)
1944-
end
1945-
1946-
it 'can define a custom type' do
1947-
described_class.configure do
1948-
define_type :my_type, Integer
1949-
end
1950-
1951-
expect(described_class::FieldTypes.get(:my_type)).to eq Integer
1952-
end
1953-
1954-
it 'can override and existing type' do
1955-
described_class.configure do
1956-
define_type :integer, String
1957-
end
1958-
1959-
expect(described_class::FieldTypes.get(:integer)).to eq String
1960-
end
1961-
end
1962-
1963-
context '.option method' do
1964-
after do
1965-
described_class.instance_variable_set(:@options, {})
1966-
end
1967-
1968-
it 'can define a custom field option' do
1969-
described_class.configure do
1970-
option :my_required do |model, field, value|
1971-
model.validates_presence_of field.name if value
1972-
end
1973-
end
1974-
1975-
klass = Class.new do
1976-
include Mongoid::Document
1977-
field :my_field, my_required: true
1978-
1979-
def self.model_name
1980-
OpenStruct.new(human: 'Klass')
1981-
end
1982-
end
1983-
1984-
instance = klass.new
1985-
expect(instance.valid?).to eq false
1986-
expect(instance.errors.full_messages).to eq ["My field can't be blank"]
1987-
end
1988-
end
1989-
end
1990-
19911936
describe '::TYPE_MAPPINGS' do
19921937
it 'returns the default mapping' do
19931938
expect(described_class::TYPE_MAPPINGS).to eq ::Mongoid::Fields::FieldTypes::DEFAULT_MAPPING

0 commit comments

Comments
 (0)