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

Merge pull request #15878 from sgrif/sg-null-column

Revert #15502, and add a deprecation warning instead.
This commit is contained in:
Rafael Mendonça França 2014-06-23 14:37:05 -03:00
commit 679626e4e1
5 changed files with 18 additions and 33 deletions

View file

@ -117,7 +117,8 @@
*Lauro Caetano*, *Carlos Antonio da Silva*
* Return a null column from `column_for_attribute` when no column exists.
* Deprecate returning `nil` from `column_for_attribute` when no column exists.
It will return a null object in Rails 5.0
*Sean Griffin*

View file

@ -104,11 +104,11 @@ module ActiveRecord
end
def association_key_type
@klass.column_for_attribute(association_key_name).type
@klass.type_for_attribute(association_key_name.to_s).type
end
def owner_key_type
@model.column_for_attribute(owner_key_name).type
@model.type_for_attribute(owner_key_name.to_s).type
end
def load_slices(slices)

View file

@ -186,8 +186,7 @@ module ActiveRecord
end
# Returns the column object for the named attribute.
# Returns a +ActiveRecord::ConnectionAdapters::NullColumn+ if the
# named attribute does not exist.
# Returns nil if the named attribute does not exist.
#
# class Person < ActiveRecord::Base
# end
@ -197,12 +196,17 @@ module ActiveRecord
# # => #<ActiveRecord::ConnectionAdapters::SQLite3Column:0x007ff4ab083980 @name="name", @sql_type="varchar(255)", @null=true, ...>
#
# person.column_for_attribute(:nothing)
# # => #<ActiveRecord::ConnectionAdapters::NullColumn:0xXXX @name=nil, @sql_type=nil, @cast_type=#<Type::Value>, ...>
# # => nil
def column_for_attribute(name)
name = name.to_s
columns_hash.fetch(name) do
ConnectionAdapters::NullColumn.new(name)
column = columns_hash[name.to_s]
if column.nil?
ActiveSupport::Deprecation.warn(<<-MESSAGE.strip_heredoc)
`column_for_attribute` will return a null object for non-existent columns
in Rails 5.0. If you would like to continue to receive `nil`, you should
instead call `model.class.columns_hash[name]`
MESSAGE
end
column
end
end

View file

@ -57,12 +57,6 @@ module ActiveRecord
end
end
end
class NullColumn < Column
def initialize(name)
super name, nil, Type::Value.new
end
end
end
# :startdoc:
end

View file

@ -80,24 +80,10 @@ class ReflectionTest < ActiveRecord::TestCase
assert_equal :integer, @first.column_for_attribute("id").type
end
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
assert_not column.number?
assert_not column.text?
assert_not column.binary?
def test_non_existent_columns_return_nil
assert_deprecated do
assert_nil @first.column_for_attribute("attribute_that_doesnt_exist")
end
def test_non_existent_columns_are_identity_types
column = @first.column_for_attribute("attribute_that_doesnt_exist")
object = Object.new
assert_equal object, column.type_cast_from_database(object)
assert_equal object, column.type_cast_from_user(object)
assert_equal object, column.type_cast_for_database(object)
end
def test_reflection_klass_for_nested_class_name