1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Allow type_for_attribute(:symbol)

Closes #31611.
This commit is contained in:
Beni Cherniavsky-Paskin 2018-01-01 17:13:39 +02:00
parent 5e4b70461d
commit bbcb1c9e6b
2 changed files with 16 additions and 1 deletions

View file

@ -361,8 +361,9 @@ module ActiveRecord
# it). # it).
# #
# +attr_name+ The name of the attribute to retrieve the type for. Must be # +attr_name+ The name of the attribute to retrieve the type for. Must be
# a string # a string or a symbol.
def type_for_attribute(attr_name, &block) def type_for_attribute(attr_name, &block)
attr_name = attr_name.to_s
if block if block
attribute_types.fetch(attr_name, &block) attribute_types.fetch(attr_name, &block)
else else

View file

@ -66,6 +66,9 @@ class ReflectionTest < ActiveRecord::TestCase
def test_column_string_type_and_limit def test_column_string_type_and_limit
assert_equal :string, @first.column_for_attribute("title").type assert_equal :string, @first.column_for_attribute("title").type
assert_equal :string, @first.column_for_attribute(:title).type
assert_equal :string, @first.type_for_attribute("title").type
assert_equal :string, @first.type_for_attribute(:title).type
assert_equal 250, @first.column_for_attribute("title").limit assert_equal 250, @first.column_for_attribute("title").limit
end end
@ -81,6 +84,9 @@ class ReflectionTest < ActiveRecord::TestCase
def test_integer_columns def test_integer_columns
assert_equal :integer, @first.column_for_attribute("id").type assert_equal :integer, @first.column_for_attribute("id").type
assert_equal :integer, @first.column_for_attribute(:id).type
assert_equal :integer, @first.type_for_attribute("id").type
assert_equal :integer, @first.type_for_attribute(:id).type
end end
def test_non_existent_columns_return_null_object def test_non_existent_columns_return_null_object
@ -89,6 +95,9 @@ class ReflectionTest < ActiveRecord::TestCase
assert_equal "attribute_that_doesnt_exist", column.name assert_equal "attribute_that_doesnt_exist", column.name
assert_nil column.sql_type assert_nil column.sql_type
assert_nil column.type assert_nil column.type
column = @first.column_for_attribute(:attribute_that_doesnt_exist)
assert_instance_of ActiveRecord::ConnectionAdapters::NullColumn, column
end end
def test_non_existent_types_are_identity_types def test_non_existent_types_are_identity_types
@ -98,6 +107,11 @@ class ReflectionTest < ActiveRecord::TestCase
assert_equal object, type.deserialize(object) assert_equal object, type.deserialize(object)
assert_equal object, type.cast(object) assert_equal object, type.cast(object)
assert_equal object, type.serialize(object) assert_equal object, type.serialize(object)
type = @first.type_for_attribute(:attribute_that_doesnt_exist)
assert_equal object, type.deserialize(object)
assert_equal object, type.cast(object)
assert_equal object, type.serialize(object)
end end
def test_reflection_klass_for_nested_class_name def test_reflection_klass_for_nested_class_name