From 7eed50c7208a1b605e6ad04e877a3cbeb7cc3434 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Mon, 29 Dec 2014 10:57:15 -0700 Subject: Rely on the injectable type caster for `arel_table` This API will require much less consuming code to change to accomodate the removal of automatic type casting from Arel. As long as the predicates are constructed using the `arel_table` off of an AR subclass, there will be no changes that need to happen. --- activerecord/lib/active_record/table_metadata.rb | 62 +++++------------------- 1 file changed, 13 insertions(+), 49 deletions(-) (limited to 'activerecord/lib/active_record/table_metadata.rb') diff --git a/activerecord/lib/active_record/table_metadata.rb b/activerecord/lib/active_record/table_metadata.rb index e60bf55021..4293c9704b 100644 --- a/activerecord/lib/active_record/table_metadata.rb +++ b/activerecord/lib/active_record/table_metadata.rb @@ -8,12 +8,6 @@ module ActiveRecord @association = association end - def type_cast_for_database(attribute_name, value) - return value if value.is_a?(Arel::Nodes::BindParam) || klass.nil? - type = klass.type_for_attribute(attribute_name.to_s) - Arel::Nodes::Quoted.new(type.type_cast_for_database(value)) - end - def resolve_column_aliases(hash) hash = hash.dup hash.keys.grep(Symbol) do |key| @@ -35,17 +29,17 @@ module ActiveRecord def associated_table(table_name) return self if table_name == arel_table.name - arel_table = Arel::Table.new(table_name) association = klass._reflect_on_association(table_name) if association && !association.polymorphic? association_klass = association.klass - end - - if association - TableMetadata.new(association_klass, arel_table, association) + arel_table = association_klass.arel_table else - ConnectionAdapterTable.new(klass.connection, arel_table) + type_caster = ConnectionAdapterTypeCaster.new(klass.connection, table_name) + association_klass = nil + arel_table = Arel::Table.new(table_name, type_caster: type_caster) end + + TableMetadata.new(association_klass, arel_table, association) end def polymorphic_association? @@ -57,56 +51,26 @@ module ActiveRecord attr_reader :klass, :arel_table, :association end - # FIXME: We want to get rid of this class. The connection adapter does not - # have sufficient knowledge about types, as they could be provided by or - # overriden by the ActiveRecord::Base subclass. The case where you reach this - # class is if you do a query like: - # - # Liquid.joins(molecules: :electrons) - # .where("molecules.name" => "something", "electrons.name" => "something") - # - # Since we don't know that we can get to electrons through molecules - class ConnectionAdapterTable # :nodoc: - def initialize(connection, arel_table) + class ConnectionAdapterTypeCaster + def initialize(connection, table_name) @connection = connection - @arel_table = arel_table + @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) - Arel::Nodes::Quoted.new(type.type_cast_for_database(value)) - end - - def resolve_column_aliases(hash) - hash - end - - def arel_attribute(column_name) - arel_table[column_name] - end - - def associated_with?(*) - false - end - - def associated_table(table_name) - arel_table = Arel::Table.new(table_name) - ConnectionAdapterTable.new(klass.connection, arel_table) - end - - def polymorphic_association? - false + type.type_cast_for_database(value) end protected - attr_reader :connection, :arel_table + attr_reader :connection, :table_name private def type_for(attribute_name) - if connection.schema_cache.table_exists?(arel_table.name) + if connection.schema_cache.table_exists?(table_name) column_for(attribute_name).cast_type else Type::Value.new @@ -114,7 +78,7 @@ module ActiveRecord end def column_for(attribute_name) - connection.schema_cache.columns_hash(arel_table.name)[attribute_name.to_s] + connection.schema_cache.columns_hash(table_name)[attribute_name.to_s] end end end -- cgit v1.2.3