diff --git a/activemodel/test/cases/attributes_test.rb b/activemodel/test/cases/attributes_test.rb index 6ff5af09206..387005f56c4 100644 --- a/activemodel/test/cases/attributes_test.rb +++ b/activemodel/test/cases/attributes_test.rb @@ -142,5 +142,11 @@ module ActiveModel data.freeze assert_nothing_raised { data.freeze } end + + test "unknown type error is raised" do + assert_raise(ArgumentError) do + ModelForAttributesTest.attribute :foo, :unknown + end + end end end diff --git a/activerecord/lib/active_record/attributes.rb b/activerecord/lib/active_record/attributes.rb index 04a83d5dc4d..9e803fa58e9 100644 --- a/activerecord/lib/active_record/attributes.rb +++ b/activerecord/lib/active_record/attributes.rb @@ -214,8 +214,7 @@ module ActiveRecord case cast_type when Symbol - type = cast_type - cast_type = -> _ { Type.lookup(type, **options, adapter: Type.adapter_name_from(self)) } + cast_type = Type.lookup(cast_type, **options, adapter: Type.adapter_name_from(self)) when nil if (prev_cast_type, prev_default = attributes_to_define_after_schema_loads[name]) default = prev_default if default == NO_DEFAULT_PROVIDED diff --git a/activerecord/test/cases/adapters/postgresql/datatype_test.rb b/activerecord/test/cases/adapters/postgresql/datatype_test.rb index 2cfa35760df..ff9ac166a6f 100644 --- a/activerecord/test/cases/adapters/postgresql/datatype_test.rb +++ b/activerecord/test/cases/adapters/postgresql/datatype_test.rb @@ -7,7 +7,7 @@ class PostgresqlTime < ActiveRecord::Base # Declare attributes to get rid from deprecation warnings on ActiveRecord 6.1 attribute :time_interval, :string attribute :scaled_time_interval, :interval -end +end if current_adapter?(:PostgreSQLAdapter) class PostgresqlOid < ActiveRecord::Base end diff --git a/activerecord/test/cases/adapters/postgresql/geometric_test.rb b/activerecord/test/cases/adapters/postgresql/geometric_test.rb index f312b6e23d7..1e4f741f5fe 100644 --- a/activerecord/test/cases/adapters/postgresql/geometric_test.rb +++ b/activerecord/test/cases/adapters/postgresql/geometric_test.rb @@ -16,7 +16,7 @@ class PostgresqlPointTest < ActiveRecord::PostgreSQLTestCase attribute :legacy_x, :legacy_point attribute :legacy_y, :legacy_point attribute :legacy_z, :legacy_point - end + end if current_adapter?(:PostgreSQLAdapter) def setup @connection = ActiveRecord::Base.connection diff --git a/activerecord/test/cases/adapters/postgresql/interval_test.rb b/activerecord/test/cases/adapters/postgresql/interval_test.rb index 81f4e87181f..ca1fe4bc765 100644 --- a/activerecord/test/cases/adapters/postgresql/interval_test.rb +++ b/activerecord/test/cases/adapters/postgresql/interval_test.rb @@ -12,7 +12,7 @@ class PostgresqlIntervalTest < ActiveRecord::PostgreSQLTestCase attribute :default_term, :interval attribute :all_terms, :interval, array: true attribute :legacy_term, :string - end + end if current_adapter?(:PostgreSQLAdapter) class DeprecatedIntervalDataType < ActiveRecord::Base; end diff --git a/activerecord/test/cases/attributes_test.rb b/activerecord/test/cases/attributes_test.rb index 652d09ae9fa..efecf602f8b 100644 --- a/activerecord/test/cases/attributes_test.rb +++ b/activerecord/test/cases/attributes_test.rb @@ -348,6 +348,12 @@ module ActiveRecord assert_equal "foo", klass.new(no_type: "foo").no_type end + test "unknown type error is raised" do + assert_raise(ArgumentError) do + OverloadedType.attribute :foo, :unknown + end + end + test "immutable_strings_by_default changes schema inference for string columns" do with_immutable_strings do OverloadedType.reset_column_information diff --git a/activerecord/test/cases/inheritance_test.rb b/activerecord/test/cases/inheritance_test.rb index b316e6a6374..1a8ee5fcc62 100644 --- a/activerecord/test/cases/inheritance_test.rb +++ b/activerecord/test/cases/inheritance_test.rb @@ -597,17 +597,10 @@ end class InheritanceAttributeMappingTest < ActiveRecord::TestCase setup do - @old_registry = ActiveRecord::Type.registry - ActiveRecord::Type.registry = ActiveRecord::Type.registry.dup - ActiveRecord::Type.register :omg_sti, InheritanceAttributeMappingTest::OmgStiType Company.delete_all Sponsor.delete_all end - teardown do - ActiveRecord::Type.registry = @old_registry - end - class OmgStiType < ActiveRecord::Type::String def cast_value(value) if value =~ /\Aomg_(.+)\z/ @@ -624,6 +617,8 @@ class InheritanceAttributeMappingTest < ActiveRecord::TestCase end end + ActiveRecord::Type.register :omg_sti, OmgStiType + class Company < ActiveRecord::Base self.table_name = "companies" attribute :type, :omg_sti