Skip to content

Commit

Permalink
Merge pull request rails#41166 from kamipo/raise_unknown_type_error_o…
Browse files Browse the repository at this point in the history
…n_definition_time

Raise unknown type error on the definition time
  • Loading branch information
kamipo authored Jan 20, 2021
2 parents 1eeb26c + 26e1fe4 commit ddacedd
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 ddacedd

Please sign in to comment.