Skip to content

Commit 2d104c7

Browse files
committed
Add InvalidFieldTypeDefinition error + various cleanups
1 parent 6664977 commit 2d104c7

File tree

7 files changed

+122
-26
lines changed

7 files changed

+122
-26
lines changed

lib/config/locales/en.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,14 @@ en:
175175
\_\_end\n\n
176176
Refer to:
177177
https://docs.mongodb.com/mongoid/current/reference/fields/#custom-field-options"
178+
invalid_field_type_definition:
179+
message: "The field type definition of %{type_inspection} to %{klass_inspection} is invalid."
180+
summary: "In the field type definition, either field_type %{type_inspection} is not
181+
a Symbol or String, and/or klass %{klass_inspection} is not a Class or Module."
182+
resolution: "Please ensure you are specifying field_type as either a Symbol or String,
183+
and klass as a Class or Module.\n\n
184+
Refer to:
185+
https://docs.mongodb.com/mongoid/current/reference/fields/#custom-field-types"
178186
invalid_includes:
179187
message: "Invalid includes directive: %{klass}.includes(%{args})"
180188
summary: "Eager loading in Mongoid only supports providing arguments

lib/mongoid/errors.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
require "mongoid/errors/invalid_dependent_strategy"
1515
require "mongoid/errors/invalid_field"
1616
require "mongoid/errors/invalid_field_option"
17-
require "mongoid/errors/invalid_field_type"
17+
require "mongoid/errors/invalid_field_type_definition"
1818
require "mongoid/errors/invalid_find"
1919
require "mongoid/errors/invalid_includes"
2020
require "mongoid/errors/invalid_index"
@@ -55,6 +55,7 @@
5555
require "mongoid/errors/scope_overwrite"
5656
require "mongoid/errors/too_many_nested_attribute_records"
5757
require "mongoid/errors/unknown_attribute"
58+
require "mongoid/errors/unknown_field_type"
5859
require "mongoid/errors/unknown_model"
5960
require "mongoid/errors/unsaved_document"
6061
require "mongoid/errors/unsupported_javascript"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
module Mongoid
4+
module Errors
5+
6+
# This error is raised when trying to define a field type mapping with
7+
# invalid argument types.
8+
class InvalidFieldTypeDefinition < MongoidError
9+
10+
# Create the new error.
11+
#
12+
# @example Instantiate the error.
13+
# InvalidFieldTypeDefinition.new('number', 123)
14+
#
15+
# @param [ Object ] field_type The object which is expected to a be Symbol or String.
16+
# @param [ Object ] klass The object which is expected to be a Class or Module.
17+
def initialize(field_type, klass)
18+
super(
19+
compose_message('invalid_field_type_definition',
20+
type_inspection: field_type.inspect, klass_inspection: klass.inspect)
21+
)
22+
end
23+
end
24+
end
25+
end

lib/mongoid/fields.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module Fields
1616
Boolean = Mongoid::Boolean
1717

1818
# @deprecated
19-
TYPE_MAPPINGS = ::Mongoid::Fields::FieldTypes::DEFAULT_ALIASES
19+
TYPE_MAPPINGS = ::Mongoid::Fields::FieldTypes::DEFAULT_MAPPING
2020

2121
# Constant for all names of the _id field in a document.
2222
#
@@ -244,11 +244,13 @@ def configure(&block)
244244
# define_type :point, Point
245245
# end
246246
#
247-
# @param [ Symbol ] symbol the symbol identifier of the defined type
248-
# @param [ Class ] klass the class of the defined type, which must
249-
# include mongoize and related methods.
250-
def define_type(symbol, klass)
251-
Fields::FieldTypes.define(symbol, klass)
247+
# @param [ Symbol | String ] field_type the identifier of the
248+
# defined type. This identifier will be accessible as either a
249+
# string or a symbol regardless of the type passed to this method.
250+
# @param [ Module ] klass the class of the defined type, which must
251+
# include mongoize, demongoize, and evolve methods.
252+
def define_type(field_type, klass)
253+
Fields::FieldTypes.define_type(field_type, klass)
252254
end
253255

254256
# Stores the provided block to be run when the option name specified is

lib/mongoid/fields/field_types.rb

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ module Fields
99
# @api private
1010
module FieldTypes
1111

12-
# The default mapping of field type symbol/string aliases to classes.
12+
# The default mapping of field type symbol/string identifiers to classes.
1313
#
1414
# @api private
15-
DEFAULT_ALIASES = {
15+
DEFAULT_MAPPING = {
1616
array: Array,
1717
big_decimal: BigDecimal,
1818
binary: BSON::Binary,
@@ -42,15 +42,15 @@ class << self
4242
# @example
4343
# Mongoid::FieldTypes.get(:point)
4444
#
45-
# @param [ Class | Symbol | String | Class ] field_type The field
46-
# type class or its string or symbol alias.
45+
# @param [ Module | Symbol | String ] field_type The field
46+
# type class or its string or symbol identifier.
4747
#
48-
# @return [ Class | nil ] The underlying field type class, or nil if
48+
# @return [ Module | nil ] The underlying field type class, or nil if
4949
# string or symbol was passed and it is not mapped to any class.
5050
def get(field_type)
5151
case field_type
52-
when Class
53-
type
52+
when Module
53+
field_type
5454
when Symbol, String
5555
mapping[field_type]
5656
else
@@ -61,21 +61,18 @@ def get(field_type)
6161
# Defines a field type mapping, for later use in field :type option.
6262
#
6363
# @example
64-
# Mongoid::FieldTypes.define(:point, Point)
64+
# Mongoid::FieldTypes.define_type(:point, Point)
6565
#
66-
# @param [ Symbol | String ] field_type_alias the string or symbol
67-
# alias of the defined type. The alias will be accessible as a string
68-
# or a symbol regardless of the type passed to this method.
66+
# @param [ Symbol | String ] field_type the identifier of the
67+
# defined type. This identifier will be accessible as either a
68+
# string or a symbol regardless of the type passed to this method.
6969
# @param [ Class ] klass the class of the defined type, which must
7070
# include mongoize, demongoize, and evolve methods.
71-
def define_alias(field_type_alias, klass)
72-
unless field_type_alias.is_a?(String) || field_type_alias.is_a?(Symbol)
73-
raise Mongoid::Errors::InvalidFieldTypeAliasName.new(field_type_alias)
71+
def define_type(field_type, klass)
72+
unless (field_type.is_a?(String) || field_type.is_a?(Symbol)) && klass.is_a?(Module)
73+
raise Mongoid::Errors::InvalidFieldTypeDefinition.new(field_type, klass)
7474
end
75-
unless klass.is_a?(Class)
76-
raise Mongoid::Errors::InvalidFieldTypeAliasValue.new(field_type_alias, klass)
77-
end
78-
mapping[field_type_alias] = klass
75+
mapping[field_type] = klass
7976
end
8077

8178
delegate :delete, to: :mapping
@@ -84,7 +81,7 @@ def define_alias(field_type_alias, klass)
8481
#
8582
# @return [ ActiveSupport::HashWithIndifferentAccess<Symbol, Class> ] The memoized field mapping.
8683
def mapping
87-
@mapping ||= DEFAULT_ALIASES.dup
84+
@mapping ||= DEFAULT_MAPPING.dup
8885
end
8986
end
9087
end
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# frozen_string_literal: true
2+
3+
require "spec_helper"
4+
5+
describe Mongoid::Errors::InvalidFieldTypeDefinition do
6+
7+
describe "#message" do
8+
9+
context 'when field_type is the wrong type' do
10+
let(:error) do
11+
described_class.new(123, Integer)
12+
end
13+
14+
it "contains the problem in the message" do
15+
expect(error.message).to include(
16+
"The field type definition of 123 to Integer is invalid."
17+
)
18+
end
19+
20+
it "contains the summary in the message" do
21+
expect(error.message).to include(
22+
"In the field type definition, either field_type 123 is not a Symbol"
23+
)
24+
end
25+
26+
it "contains the resolution in the message" do
27+
expect(error.message).to include(
28+
'Please ensure you are specifying field_type as either a Symbol'
29+
)
30+
end
31+
end
32+
33+
context 'when klass is the wrong type' do
34+
let(:error) do
35+
described_class.new('number', 123)
36+
end
37+
38+
it "contains the problem in the message" do
39+
expect(error.message).to include(
40+
'The field type definition of "number" to 123 is invalid.'
41+
)
42+
end
43+
44+
it "contains the summary in the message" do
45+
expect(error.message).to include(
46+
'In the field type definition, either field_type "number" is not a Symbol'
47+
)
48+
end
49+
50+
it "contains the resolution in the message" do
51+
expect(error.message).to include(
52+
'Please ensure you are specifying field_type as either a Symbol'
53+
)
54+
end
55+
end
56+
end
57+
end

spec/mongoid/errors/unknown_field_type_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
"Model 'Person' declares a field 'first_name' with an unknown type value :stringgy."
2727
)
2828
end
29+
30+
it "contains the resolution in the message" do
31+
expect(error.message).to include(
32+
'Please provide a known type value for the field.'
33+
)
34+
end
2935
end
3036

3137
context 'when type is a string' do

0 commit comments

Comments
 (0)