Skip to content

Commit

Permalink
Deprecate Mongoid::Fields::TYPE_MAPPINGS
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyshields authored and p committed May 19, 2022
1 parent 7b9e174 commit 685ac5d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/mongoid/fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions lib/mongoid/fields/field_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ def define(type, klass)

delegate :delete, to: :mapping

private

# The memoized mapping of field type definitions to classes.
#
# @return [ ActiveSupport::HashWithIndifferentAccess<Symbol, Class> ] The memoized field mapping.
def mapping
@mapping ||= DEFAULT_MAPPING.dup
end

private

# Handles fallback for case where mapping does not contain the
# requested type.
#
Expand Down
17 changes: 17 additions & 0 deletions spec/mongoid/fields/field_types_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions spec/mongoid/fields_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 685ac5d

Please sign in to comment.