Skip to content

Commit 83d37c3

Browse files
committed
Add spec for InvalidFieldType
1 parent f7275ed commit 83d37c3

File tree

6 files changed

+105
-70
lines changed

6 files changed

+105
-70
lines changed

lib/config/locales/en.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ en:
186186
invalid_field_type:
187187
message: "Invalid field type :%{type} for field :%{field} on model '%{klass}'."
188188
summary: "Model '%{klass}' defines a field :%{field} with an unknown :type value
189-
'%{type}'. This value is neither present in Mongoid's default type mapping,
189+
:%{type}. This value is neither present in Mongoid's default type mapping,
190190
nor defined in a custom field type mapping."
191191
resolution: "Please provide a valid :type value for the field. If you
192192
meant to define a custom field type, please do so first as follows:\n\n

lib/mongoid/errors.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
require "mongoid/errors/invalid_dependent_strategy"
1616
require "mongoid/errors/invalid_field"
1717
require "mongoid/errors/invalid_field_option"
18+
require "mongoid/errors/invalid_field_type"
1819
require "mongoid/errors/invalid_find"
1920
require "mongoid/errors/invalid_includes"
2021
require "mongoid/errors/invalid_index"

lib/mongoid/field_types.rb

Lines changed: 0 additions & 67 deletions
This file was deleted.

lib/mongoid/fields.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require "mongoid/fields/foreign_key"
55
require "mongoid/fields/localized"
66
require "mongoid/fields/validators"
7+
require "mongoid/fields/field_types"
78

89
module Mongoid
910

@@ -199,7 +200,7 @@ def configure(&block)
199200
# type :point, Point
200201
# end
201202
def type(symbol, klass)
202-
Mongoid::FieldTypes.define(symbol, klass)
203+
Fields::FieldTypes.define(symbol, klass)
203204
end
204205

205206
# Stores the provided block to be run when the option name specified is
@@ -575,7 +576,7 @@ def field_for(name, options)
575576
end
576577

577578
def field_type_klass_for(field, type)
578-
klass = Mongoid::FieldTypes.get(type)
579+
klass = Fields::FieldTypes.get(type)
579580
return klass if klass
580581
raise Mongoid::Errors::InvalidFieldType.new(self.name, field, type)
581582
end

lib/mongoid/fields/field_types.rb

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# frozen_string_literal: true
2+
3+
module Mongoid
4+
module Fields
5+
6+
# Singleton module which contains a cache for field type definitions.
7+
# Custom field types can be configured.
8+
module FieldTypes
9+
extend self
10+
11+
# For fields defined with symbols use the correct class.
12+
DEFAULT_MAPPING = {
13+
array: Array,
14+
bigdecimal: BigDecimal,
15+
big_decimal: BigDecimal,
16+
binary: BSON::Binary,
17+
boolean: Mongoid::Boolean,
18+
date: Date,
19+
datetime: DateTime,
20+
date_time: DateTime,
21+
decimal128: BSON::Decimal128,
22+
double: Float,
23+
float: Float,
24+
hash: Hash,
25+
integer: Integer,
26+
object: Object,
27+
object_id: BSON::ObjectId,
28+
range: Range,
29+
regexp: Regexp,
30+
set: Set,
31+
string: String,
32+
stringified_symbol: Mongoid::StringifiedSymbol,
33+
symbol: Symbol,
34+
time: Time,
35+
time_with_zone: ActiveSupport::TimeWithZone
36+
}.with_indifferent_access.freeze
37+
38+
def get(value)
39+
mapping[value] || handle_unmapped_type(value)
40+
end
41+
42+
def define(symbol, klass)
43+
mapping[symbol] = klass
44+
end
45+
46+
delegate :delete, to: :mapping
47+
48+
def mapping
49+
@mapping ||= DEFAULT_MAPPING.dup
50+
end
51+
52+
def handle_unmapped_type(type)
53+
return Object if type.nil?
54+
55+
if type.is_a?(Module)
56+
symbol = type.name.demodulize.underscore
57+
Mongoid.logger.warn(
58+
"Using a Class (#{type}) as the field :type option is deprecated and will be removed in Mongoid 8.0. " +
59+
"Please use a Symbol (:#{symbol}) instead."
60+
)
61+
return Mongoid::Boolean if type.to_s == 'Boolean'
62+
return type
63+
end
64+
65+
nil
66+
end
67+
end
68+
end
69+
end
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
require "spec_helper"
4+
5+
describe Mongoid::Errors::InvalidFieldType do
6+
7+
describe "#message" do
8+
9+
let(:error) do
10+
described_class.new(Person, :first_name, :stringgy)
11+
end
12+
13+
it "contains the problem in the message" do
14+
expect(error.message).to include(
15+
"Invalid field type :stringgy for field :first_name on model 'Person'."
16+
)
17+
end
18+
19+
it "contains the summary in the message" do
20+
expect(error.message).to include(
21+
"Model 'Person' defines a field :first_name with an unknown :type value :stringgy."
22+
)
23+
end
24+
25+
it "contains the resolution in the message" do
26+
expect(error.message).to include(
27+
'Please provide a valid :type value for the field. If you meant to define'
28+
)
29+
end
30+
end
31+
end

0 commit comments

Comments
 (0)