aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/type_caster/connection.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/type_caster/connection.rb')
-rw-r--r--activerecord/lib/active_record/type_caster/connection.rb28
1 files changed, 28 insertions, 0 deletions
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..7cf8181d8e
--- /dev/null
+++ b/activerecord/lib/active_record/type_caster/connection.rb
@@ -0,0 +1,28 @@
+# frozen_string_literal: true
+
+module ActiveRecord
+ module TypeCaster
+ class Connection # :nodoc:
+ def initialize(klass, table_name)
+ @klass = klass
+ @table_name = table_name
+ end
+
+ def type_cast_for_database(attribute_name, value)
+ return value if value.is_a?(Arel::Nodes::BindParam)
+ column = column_for(attribute_name)
+ connection.type_cast_from_column(column, value)
+ end
+
+ private
+ attr_reader :table_name
+ delegate :connection, to: :@klass
+
+ def column_for(attribute_name)
+ if connection.schema_cache.data_source_exists?(table_name)
+ connection.schema_cache.columns_hash(table_name)[attribute_name.to_s]
+ end
+ end
+ end
+ end
+end