提交 fb6df2ac 编写于 作者: S Sean Griffin

Extract an explicit type caster class

上级 7eed50c7
......@@ -122,6 +122,7 @@ def add_user_provided_columns(schema_columns)
end
def clear_caches_calculated_from_columns
@arel_table = nil
@attributes_builder = nil
@column_names = nil
@column_types = nil
......
......@@ -22,6 +22,7 @@
require 'active_record/explain_subscriber'
require 'active_record/relation/delegation'
require 'active_record/attributes'
require 'active_record/type_caster'
module ActiveRecord #:nodoc:
# = Active Record
......
......@@ -235,7 +235,7 @@ def ===(object)
# scope :published_and_commented, -> { published.and(self.arel_table[:comments_count].gt(0)) }
# end
def arel_table # :nodoc:
@arel_table ||= Arel::Table.new(table_name, type_caster: self)
@arel_table ||= Arel::Table.new(table_name, type_caster: type_caster)
end
# Returns the Arel engine.
......@@ -252,12 +252,6 @@ def predicate_builder # :nodoc:
@predicate_builder ||= PredicateBuilder.new(table_metadata)
end
def type_cast_for_database(attribute_name, value)
return value if value.is_a?(Arel::Nodes::BindParam)
type = type_for_attribute(attribute_name.to_s)
type.type_cast_for_database(value)
end
private
def relation # :nodoc:
......@@ -273,6 +267,10 @@ def relation # :nodoc:
def table_metadata # :nodoc:
TableMetadata.new(self, arel_table)
end
def type_caster # :nodoc:
TypeCaster::Map.new(self)
end
end
# New objects can be instantiated as either empty (pass no construction parameter) or pre-set with
......
......@@ -298,6 +298,7 @@ def reset_column_information
connection.schema_cache.clear_table_cache!(table_name) if table_exists?
@arel_engine = nil
@arel_table = nil
@column_names = nil
@column_types = nil
@content_columns = nil
......
......@@ -34,7 +34,7 @@ def associated_table(table_name)
association_klass = association.klass
arel_table = association_klass.arel_table
else
type_caster = ConnectionAdapterTypeCaster.new(klass.connection, table_name)
type_caster = TypeCaster::Connection.new(klass.connection, table_name)
association_klass = nil
arel_table = Arel::Table.new(table_name, type_caster: type_caster)
end
......@@ -50,35 +50,4 @@ def polymorphic_association?
attr_reader :klass, :arel_table, :association
end
class ConnectionAdapterTypeCaster
def initialize(connection, table_name)
@connection = connection
@table_name = table_name
end
def type_cast_for_database(attribute_name, value)
return value if value.is_a?(Arel::Nodes::BindParam)
type = type_for(attribute_name)
type.type_cast_for_database(value)
end
protected
attr_reader :connection, :table_name
private
def type_for(attribute_name)
if connection.schema_cache.table_exists?(table_name)
column_for(attribute_name).cast_type
else
Type::Value.new
end
end
def column_for(attribute_name)
connection.schema_cache.columns_hash(table_name)[attribute_name.to_s]
end
end
end
require 'active_record/type_caster/map'
require 'active_record/type_caster/connection'
module ActiveRecord
module TypeCaster
end
end
module ActiveRecord
module TypeCaster
class Connection
def initialize(connection, table_name)
@connection = connection
@table_name = table_name
end
def type_cast_for_database(attribute_name, value)
return value if value.is_a?(Arel::Nodes::BindParam)
type = type_for(attribute_name)
type.type_cast_for_database(value)
end
protected
attr_reader :connection, :table_name
private
def type_for(attribute_name)
if connection.schema_cache.table_exists?(table_name)
column_for(attribute_name).cast_type
else
Type::Value.new
end
end
def column_for(attribute_name)
connection.schema_cache.columns_hash(table_name)[attribute_name.to_s]
end
end
end
end
module ActiveRecord
module TypeCaster
class Map
def initialize(types)
@types = types
end
def type_cast_for_database(attr_name, value)
return value if value.is_a?(Arel::Nodes::BindParam)
type = types.type_for_attribute(attr_name.to_s)
type.type_cast_for_database(value)
end
protected
attr_reader :types
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册