Skip to content

Commit 8715a66

Browse files
committed
Fix specs and refactor
1 parent ef41527 commit 8715a66

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

lib/mongoid/fields.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -745,19 +745,19 @@ def remove_defaults(name)
745745
post_processed_defaults.delete_one(name)
746746
end
747747

748-
def field_for(name, options)
748+
def field_for(field_name, options)
749749
opts = options.merge(klass: self)
750-
if type = options[:type]
751-
type = Fields::FieldTypes.get(type)
752-
unless type
753-
raise Mongoid::Errors::UnknownFieldType.new(self.name, name, type)
754-
end
755-
opts[:type] = type
756-
warn_unsupported_bson_type(type)
757-
end
758-
return Fields::Localized.new(name, opts) if options[:localize]
759-
return Fields::ForeignKey.new(name, opts) if options[:identity]
760-
Fields::Standard.new(name, opts)
750+
opts[:type] = get_field_type(field_name, options[:type])
751+
return Fields::Localized.new(field_name, opts) if options[:localize]
752+
return Fields::ForeignKey.new(field_name, opts) if options[:identity]
753+
Fields::Standard.new(field_name, opts)
754+
end
755+
756+
def get_field_type(field_name, raw_type)
757+
type = raw_type ? Fields::FieldTypes.get(raw_type) : Object
758+
raise Mongoid::Errors::UnknownFieldType.new(self.name, field_name, raw_type) unless type
759+
warn_unsupported_bson_type(type)
760+
type
761761
end
762762

763763
def warn_unsupported_bson_type(type)

lib/mongoid/fields/field_types.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ def get(field_type)
5353
module_field_type(field_type)
5454
when Symbol, String
5555
mapping[field_type]
56-
else
57-
raise Mongoid::Errors::UnknownFieldType.new(self.name, field, field_type)
5856
end
5957
end
6058

spec/mongoid/fields/field_types_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@
8585
context 'when value is nil' do
8686
let(:type) { nil }
8787

88-
it 'returns Object type' do
89-
is_expected.to eq Object
88+
it 'returns nil' do
89+
is_expected.to eq nil
9090
end
9191
end
9292
end

0 commit comments

Comments
 (0)