diff --git a/lib/mongoid/fields.rb b/lib/mongoid/fields.rb index 21657c507a..fd2d93e489 100644 --- a/lib/mongoid/fields.rb +++ b/lib/mongoid/fields.rb @@ -12,6 +12,9 @@ module Mongoid module Fields extend ActiveSupport::Concern + # @deprecated Use Mongoid::Fields::FieldTypes.mapping instead + TYPE_MAPPINGS = ::Mongoid::Fields::FieldTypes::DEFAULT_MAPPING + StringifiedSymbol = Mongoid::StringifiedSymbol Boolean = Mongoid::Boolean diff --git a/lib/mongoid/fields/field_types.rb b/lib/mongoid/fields/field_types.rb index f8ae682a8f..27f691ed7f 100644 --- a/lib/mongoid/fields/field_types.rb +++ b/lib/mongoid/fields/field_types.rb @@ -64,8 +64,6 @@ def define(type, klass) delegate :delete, to: :mapping - private - # The memoized mapping of field type definitions to classes. # # @return [ ActiveSupport::HashWithIndifferentAccess ] The memoized field mapping. @@ -73,6 +71,8 @@ def mapping @mapping ||= DEFAULT_MAPPING.dup end + private + # Handles fallback for case where mapping does not contain the # requested type. # diff --git a/spec/mongoid/fields/field_types_spec.rb b/spec/mongoid/fields/field_types_spec.rb index fde6256d6e..379ce06366 100644 --- a/spec/mongoid/fields/field_types_spec.rb +++ b/spec/mongoid/fields/field_types_spec.rb @@ -128,4 +128,21 @@ expect(described_class::DEFAULT_MAPPING[:integer]).to eq Integer end end + + describe '.mapping' do + + it 'returns the default mapping by default' do + expect(described_class.mapping).to eq described_class::DEFAULT_MAPPING + end + + it 'can add a type' do + described_class.define(:my_string, String) + expect(described_class.mapping[:my_string]).to eq(String) + end + + it 'can delete a default type' do + described_class.delete(:integer) + expect(described_class.mapping).to_not have_key(:integer) + end + end end diff --git a/spec/mongoid/fields_spec.rb b/spec/mongoid/fields_spec.rb index 10af3ad545..a46884798a 100644 --- a/spec/mongoid/fields_spec.rb +++ b/spec/mongoid/fields_spec.rb @@ -1838,4 +1838,10 @@ def self.model_name end end end + + describe '::TYPE_MAPPINGS' do + it 'returns the default mapping' do + expect(described_class::TYPE_MAPPINGS).to eq ::Mongoid::Fields::FieldTypes::DEFAULT_MAPPING + end + end end