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

Move column_for_attribute into ModelSchema

`type_for_attribute` was added (extracted from `column_for_attribute`)
in `ModelSchema` at c93dbfe, I sometimes had expected that both
`column_for_attribute` and `type_for_attribute` exists in `ModelSchema`,
but `column_for_attribute` had existed in `AttributeMethods` even though
it's not very relevant to attribute methods (getting column object is no
longer useful for most end users since type object is separated from
column object, by the way).

So I've moved `column_for_attribute` to the same place with
`type_for_attribute` to less confusion a bit.
This commit is contained in:
Ryuta Kamizono 2020-07-28 17:26:04 +09:00
parent c0ea4321b9
commit 47aee7ff3b
2 changed files with 21 additions and 23 deletions

View file

@ -19,8 +19,6 @@ module ActiveRecord
include TimeZoneConversion
include Dirty
include Serialization
delegate :column_for_attribute, to: :class
end
RESTRICTED_CLASS_METHODS = %w(private public protected allocate new name parent superclass)
@ -188,26 +186,6 @@ module ActiveRecord
def _has_attribute?(attr_name) # :nodoc:
attribute_types.key?(attr_name)
end
# 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
#
# person = Person.new
# person.column_for_attribute(:name) # the result depends on the ConnectionAdapter
# # => #<ActiveRecord::ConnectionAdapters::Column: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>, ...>
def column_for_attribute(name)
name = name.to_s
columns_hash.fetch(name) do
ConnectionAdapters::NullColumn.new(name)
end
end
end
# A Person object with a name attribute can ask <tt>person.respond_to?(:name)</tt>,

View file

@ -141,7 +141,7 @@ module ActiveRecord
self.inheritance_column = "type"
self.ignored_columns = [].freeze
delegate :type_for_attribute, to: :class
delegate :type_for_attribute, :column_for_attribute, to: :class
initialize_load_schema_monitor
end
@ -400,6 +400,26 @@ module ActiveRecord
end
end
# 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
#
# person = Person.new
# person.column_for_attribute(:name) # the result depends on the ConnectionAdapter
# # => #<ActiveRecord::ConnectionAdapters::Column: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>, ...>
def column_for_attribute(name)
name = name.to_s
columns_hash.fetch(name) do
ConnectionAdapters::NullColumn.new(name)
end
end
# Returns a hash where the keys are column names and the values are
# default values when instantiating the Active Record object for this table.
def column_defaults