提交 3036c403 编写于 作者: Y Yves Senn

have an actual `NullColumn` object and update docs accordingly.

Follow up to #15438 and #15502.

/cc @sgrif
上级 31737e04
......@@ -195,8 +195,9 @@ def attribute_names
end
end
# Returns the column object for the named attribute. Returns +nil+ if the
# named attribute not exists.
# Returns the column object for the named attribute.
# Returns a +ActiveRecord::ConnectionAdapters::NullColumn+ if the
# named attribute does not exist.
#
# class Person < ActiveRecord::Base
# end
......@@ -206,11 +207,11 @@ def attribute_names
# # => #<ActiveRecord::ConnectionAdapters::SQLite3Column:0x007ff4ab083980 @name="name", @sql_type="varchar(255)", @null=true, ...>
#
# person.column_for_attribute(:nothing)
# # => #<ActiveRecord::ConnectionAdapters::Column:0xXXX @name=nil, @sql_type=nil, @cast_type=#<Type::Value>, ...>
# # => #<ActiveRecord::ConnectionAdapters::NullColumn:0xXXX @name=nil, @sql_type=nil, @cast_type=#<Type::Value>, ...>
def column_for_attribute(name)
name = name.to_s
columns_hash.fetch(name) do
ConnectionAdapters::Column.new(name, nil, Type::Value.new)
ConnectionAdapters::NullColumn.new(name)
end
end
end
......
......@@ -14,7 +14,10 @@ module ActiveRecord
module ConnectionAdapters # :nodoc:
extend ActiveSupport::Autoload
autoload :Column
autoload_at 'active_record/connection_adapters/column' do
autoload :Column
autoload :NullColumn
end
autoload :ConnectionSpecification
autoload_at 'active_record/connection_adapters/abstract/schema_definitions' do
......
......@@ -55,6 +55,12 @@ def extract_default(default)
type_cast(default)
end
end
class NullColumn < Column
def initialize(name)
super name, nil, Type::Value.new
end
end
end
# :startdoc:
end
......@@ -82,6 +82,7 @@ def test_integer_columns
def test_non_existent_columns_return_null_object
column = @first.column_for_attribute("attribute_that_doesnt_exist")
assert_instance_of ActiveRecord::ConnectionAdapters::NullColumn, column
assert_equal "attribute_that_doesnt_exist", column.name
assert_equal nil, column.sql_type
assert_equal nil, column.type
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册