aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/lib/active_record/attributes.rb1
-rw-r--r--activerecord/lib/active_record/base.rb1
-rw-r--r--activerecord/lib/active_record/core.rb12
-rw-r--r--activerecord/lib/active_record/model_schema.rb1
-rw-r--r--activerecord/lib/active_record/table_metadata.rb33
-rw-r--r--activerecord/lib/active_record/type_caster.rb7
-rw-r--r--activerecord/lib/active_record/type_caster/connection.rb34
-rw-r--r--activerecord/lib/active_record/type_caster/map.rb19
8 files changed, 69 insertions, 39 deletions
diff --git a/activerecord/lib/active_record/attributes.rb b/activerecord/lib/active_record/attributes.rb
index 08f274fd42..aafb990bc1 100644
--- a/activerecord/lib/active_record/attributes.rb
+++ b/activerecord/lib/active_record/attributes.rb
@@ -122,6 +122,7 @@ module ActiveRecord
end
def clear_caches_calculated_from_columns
+ @arel_table = nil
@attributes_builder = nil
@column_names = nil
@column_types = nil
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 954d22f1d5..bb01231bca 100644
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -22,6 +22,7 @@ require 'active_record/log_subscriber'
require 'active_record/explain_subscriber'
require 'active_record/relation/delegation'
require 'active_record/attributes'
+require 'active_record/type_caster'
module ActiveRecord #:nodoc:
# = Active Record
diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb
index 51c90a9d9d..3f714397d2 100644
--- a/activerecord/lib/active_record/core.rb
+++ b/activerecord/lib/active_record/core.rb
@@ -235,7 +235,7 @@ module ActiveRecord
# 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 @@ module ActiveRecord
@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 @@ module ActiveRecord
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
diff --git a/activerecord/lib/active_record/model_schema.rb b/activerecord/lib/active_record/model_schema.rb
index d76dbb43d6..641512d323 100644
--- a/activerecord/lib/active_record/model_schema.rb
+++ b/activerecord/lib/active_record/model_schema.rb
@@ -298,6 +298,7 @@ module ActiveRecord
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
diff --git a/activerecord/lib/active_record/table_metadata.rb b/activerecord/lib/active_record/table_metadata.rb
index 4293c9704b..11e33e8dfe 100644
--- a/activerecord/lib/active_record/table_metadata.rb
+++ b/activerecord/lib/active_record/table_metadata.rb
@@ -34,7 +34,7 @@ module ActiveRecord
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 @@ module ActiveRecord
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
diff --git a/activerecord/lib/active_record/type_caster.rb b/activerecord/lib/active_record/type_caster.rb
new file mode 100644
index 0000000000..63ba10c289
--- /dev/null
+++ b/activerecord/lib/active_record/type_caster.rb
@@ -0,0 +1,7 @@
+require 'active_record/type_caster/map'
+require 'active_record/type_caster/connection'
+
+module ActiveRecord
+ module TypeCaster
+ end
+end
diff --git a/activerecord/lib/active_record/type_caster/connection.rb b/activerecord/lib/active_record/type_caster/connection.rb
new file mode 100644
index 0000000000..9e4a130b40
--- /dev/null
+++ b/activerecord/lib/active_record/type_caster/connection.rb
@@ -0,0 +1,34 @@
+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
diff --git a/activerecord/lib/active_record/type_caster/map.rb b/activerecord/lib/active_record/type_caster/map.rb
new file mode 100644
index 0000000000..03c9e8ff83
--- /dev/null
+++ b/activerecord/lib/active_record/type_caster/map.rb
@@ -0,0 +1,19 @@
+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