Skip to content

Commit c77d8ee

Browse files
committed
Requested fixes
1 parent 852cea6 commit c77d8ee

File tree

5 files changed

+23
-16
lines changed

5 files changed

+23
-16
lines changed

lib/config/locales/en.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,10 @@ en:
177177
https://docs.mongodb.com/mongoid/current/reference/fields/#custom-field-options"
178178
invalid_field_type:
179179
message: "Invalid field type %{type_inspection} for field '%{field}' on model '%{klass}'."
180-
summary: "Model '%{klass}' declares a field '%{field}' with an unknown :type value
180+
summary: "Model '%{klass}' declares a field '%{field}' with an unknown type value
181181
%{type_inspection}. This value is neither present in Mongoid's default type mapping,
182182
nor defined in a custom field type mapping."
183-
resolution: "Please provide a valid :type value for the field. If you
183+
resolution: "Please provide a valid type value for the field. If you
184184
meant to define a custom field type, please do so first as follows:\n\n
185185
\_\_Mongoid::Fields.configure do\n
186186
\_\_\_\_define_type %{type_inspection}, YourTypeClass

lib/mongoid/fields/field_types.rb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,23 @@ class << self
4343
# @example
4444
# Mongoid::FieldTypes.get(:point)
4545
#
46-
# @param [ Symbol | String ] value the identifier of the defined type
47-
def get(value)
48-
mapping[value] || handle_unmapped_type(value)
46+
# @param [ Symbol | String | Class | nil ] type the identifier of the defined
47+
# type, or a class to pass-through, or nil.
48+
# @return [ Class | nil ] The mapped type class, or nil.
49+
def get(type)
50+
mapping[type] || handle_unmapped_type(type)
4951
end
5052

5153
# Defines a field type mapping, for later use in field :type option.
5254
#
5355
# @example
5456
# Mongoid::FieldTypes.define(:point, Point)
5557
#
56-
# @param [ Symbol ] symbol the symbol identifier of the defined type
58+
# @param [ Symbol ] type the symbol identifier of the defined type
5759
# @param [ Class ] klass the class of the defined type, which must
58-
# include mongoize and related methods.
59-
def define(symbol, klass)
60-
mapping[symbol.to_sym] = klass
60+
# include mongoize, demongoize, and evolve methods.
61+
def define(type, klass)
62+
mapping[type] = klass
6163
end
6264

6365
delegate :delete, to: :mapping
@@ -74,6 +76,8 @@ def mapping
7476
# Handles fallback for case where mapping does not contain the
7577
# requested type.
7678
#
79+
# @param [ Symbol | String | Class | nil ] type the identifier of the defined
80+
# type, or a class, or nil.
7781
# @return [ Class | nil ] The class to use as a fallback, or nil.
7882
def handle_unmapped_type(type)
7983
return Object if type.nil?

spec/mongoid/errors/invalid_field_type_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
it "contains the summary in the message" do
2525
expect(error.message).to include(
26-
"Model 'Person' declares a field 'first_name' with an unknown :type value :stringgy."
26+
"Model 'Person' declares a field 'first_name' with an unknown type value :stringgy."
2727
)
2828
end
2929
end
@@ -41,14 +41,14 @@
4141

4242
it "contains the summary in the message" do
4343
expect(error.message).to include(
44-
%q,Model 'Person' declares a field 'first_name' with an unknown :type value "stringgy".,
44+
%q,Model 'Person' declares a field 'first_name' with an unknown type value "stringgy".,
4545
)
4646
end
4747
end
4848

4949
it "contains the resolution in the message" do
5050
expect(error.message).to include(
51-
'Please provide a valid :type value for the field.'
51+
'Please provide a valid type value for the field.'
5252
)
5353
end
5454
end

spec/mongoid/fields/field_types_spec.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
describe Mongoid::Fields::FieldTypes do
66

7-
after do
8-
described_class.instance_variable_set(:@mapping, described_class::DEFAULT_MAPPING.dup)
7+
around do |example|
8+
mapping = described_class::DEFAULT_MAPPING.dup
9+
described_class.instance_variable_set(:@mapping, mapping)
10+
example.run
11+
described_class.instance_variable_set(:@mapping, mapping)
912
end
1013

1114
describe '.get' do

spec/mongoid/fields_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,15 +369,15 @@
369369
it 'raises InvalidFieldType' do
370370
lambda do
371371
klass.field(:test, type: :bogus)
372-
end.should raise_error(Mongoid::Errors::InvalidFieldType, /declares a field 'test' with an unknown :type value :bogus/)
372+
end.should raise_error(Mongoid::Errors::InvalidFieldType, /declares a field 'test' with an unknown type value :bogus/)
373373
end
374374
end
375375

376376
context 'when using an unknown string' do
377377
it 'raises InvalidFieldType' do
378378
lambda do
379379
klass.field(:test, type: 'bogus')
380-
end.should raise_error(Mongoid::Errors::InvalidFieldType, /declares a field 'test' with an unknown :type value "bogus"/)
380+
end.should raise_error(Mongoid::Errors::InvalidFieldType, /declares a field 'test' with an unknown type value "bogus"/)
381381
end
382382
end
383383
end

0 commit comments

Comments
 (0)