Skip to content

Commit ddacedd

Browse files
authored
Merge pull request rails#41166 from kamipo/raise_unknown_type_error_on_definition_time
Raise unknown type error on the definition time
2 parents 1eeb26c + 26e1fe4 commit ddacedd

File tree

7 files changed

+18
-12
lines changed

7 files changed

+18
-12
lines changed

activemodel/test/cases/attributes_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,5 +142,11 @@ class GrandchildModelForAttributesTest < ChildModelForAttributesTest
142142
data.freeze
143143
assert_nothing_raised { data.freeze }
144144
end
145+
146+
test "unknown type error is raised" do
147+
assert_raise(ArgumentError) do
148+
ModelForAttributesTest.attribute :foo, :unknown
149+
end
150+
end
145151
end
146152
end

activerecord/lib/active_record/attributes.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,7 @@ def attribute(name, cast_type = nil, default: NO_DEFAULT_PROVIDED, **options, &b
214214

215215
case cast_type
216216
when Symbol
217-
type = cast_type
218-
cast_type = -> _ { Type.lookup(type, **options, adapter: Type.adapter_name_from(self)) }
217+
cast_type = Type.lookup(cast_type, **options, adapter: Type.adapter_name_from(self))
219218
when nil
220219
if (prev_cast_type, prev_default = attributes_to_define_after_schema_loads[name])
221220
default = prev_default if default == NO_DEFAULT_PROVIDED

activerecord/test/cases/adapters/postgresql/datatype_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class PostgresqlTime < ActiveRecord::Base
77
# Declare attributes to get rid from deprecation warnings on ActiveRecord 6.1
88
attribute :time_interval, :string
99
attribute :scaled_time_interval, :interval
10-
end
10+
end if current_adapter?(:PostgreSQLAdapter)
1111

1212
class PostgresqlOid < ActiveRecord::Base
1313
end

activerecord/test/cases/adapters/postgresql/geometric_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class PostgresqlPoint < ActiveRecord::Base
1616
attribute :legacy_x, :legacy_point
1717
attribute :legacy_y, :legacy_point
1818
attribute :legacy_z, :legacy_point
19-
end
19+
end if current_adapter?(:PostgreSQLAdapter)
2020

2121
def setup
2222
@connection = ActiveRecord::Base.connection

activerecord/test/cases/adapters/postgresql/interval_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class IntervalDataType < ActiveRecord::Base
1212
attribute :default_term, :interval
1313
attribute :all_terms, :interval, array: true
1414
attribute :legacy_term, :string
15-
end
15+
end if current_adapter?(:PostgreSQLAdapter)
1616

1717
class DeprecatedIntervalDataType < ActiveRecord::Base; end
1818

activerecord/test/cases/attributes_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,12 @@ def deserialize(*)
348348
assert_equal "foo", klass.new(no_type: "foo").no_type
349349
end
350350

351+
test "unknown type error is raised" do
352+
assert_raise(ArgumentError) do
353+
OverloadedType.attribute :foo, :unknown
354+
end
355+
end
356+
351357
test "immutable_strings_by_default changes schema inference for string columns" do
352358
with_immutable_strings do
353359
OverloadedType.reset_column_information

activerecord/test/cases/inheritance_test.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -597,17 +597,10 @@ def test_inheritance_new_with_subclass_as_default
597597

598598
class InheritanceAttributeMappingTest < ActiveRecord::TestCase
599599
setup do
600-
@old_registry = ActiveRecord::Type.registry
601-
ActiveRecord::Type.registry = ActiveRecord::Type.registry.dup
602-
ActiveRecord::Type.register :omg_sti, InheritanceAttributeMappingTest::OmgStiType
603600
Company.delete_all
604601
Sponsor.delete_all
605602
end
606603

607-
teardown do
608-
ActiveRecord::Type.registry = @old_registry
609-
end
610-
611604
class OmgStiType < ActiveRecord::Type::String
612605
def cast_value(value)
613606
if value =~ /\Aomg_(.+)\z/
@@ -624,6 +617,8 @@ def serialize(value)
624617
end
625618
end
626619

620+
ActiveRecord::Type.register :omg_sti, OmgStiType
621+
627622
class Company < ActiveRecord::Base
628623
self.table_name = "companies"
629624
attribute :type, :omg_sti

0 commit comments

Comments
 (0)