Skip to content

Commit

Permalink
Raise unknown type error on the definition time
Browse files Browse the repository at this point in the history
If unknow type is given for attribute (`attribute :foo, :unknown`),
unknown type error isn't raised on the definition time but runtime.

It should be raised on the definition time.
  • Loading branch information
kamipo committed Jan 19, 2021
1 parent 60e979d commit 26e1fe4
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 12 deletions.
6 changes: 6 additions & 0 deletions activemodel/test/cases/attributes_test.rb
Expand Up @@ -142,5 +142,11 @@ class GrandchildModelForAttributesTest < ChildModelForAttributesTest
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
3 changes: 1 addition & 2 deletions activerecord/lib/active_record/attributes.rb
Expand Up @@ -214,8 +214,7 @@ def attribute(name, cast_type = nil, default: NO_DEFAULT_PROVIDED, **options, &b

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
Expand Down
Expand Up @@ -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
Expand Down
Expand Up @@ -16,7 +16,7 @@ class PostgresqlPoint < ActiveRecord::Base
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
Expand Down
Expand Up @@ -12,7 +12,7 @@ class IntervalDataType < ActiveRecord::Base
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

Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/cases/attributes_test.rb
Expand Up @@ -348,6 +348,12 @@ def deserialize(*)
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
Expand Down
9 changes: 2 additions & 7 deletions activerecord/test/cases/inheritance_test.rb
Expand Up @@ -597,17 +597,10 @@ def test_inheritance_new_with_subclass_as_default

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/
Expand All @@ -624,6 +617,8 @@ def serialize(value)
end
end

ActiveRecord::Type.register :omg_sti, OmgStiType

class Company < ActiveRecord::Base
self.table_name = "companies"
attribute :type, :omg_sti
Expand Down

0 comments on commit 26e1fe4

Please sign in to comment.