aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters
diff options
context:
space:
mode:
authorYves Senn <yves.senn@gmail.com>2014-05-26 15:22:09 +0200
committerYves Senn <yves.senn@gmail.com>2014-05-26 15:22:09 +0200
commitd075c84320fab51992a1ab7d020c62ff1bad0b4e (patch)
treef0ed59342e74f36f34a3c3b57edcbd5c938b1c34 /activerecord/lib/active_record/connection_adapters
parent7a2b37fbbbe5ef7dd9693bc7d4b404ec7e461fff (diff)
parentb5c4ef2c785ef634a81d7bb554e9b20f164be95b (diff)
downloadrails-d075c84320fab51992a1ab7d020c62ff1bad0b4e.tar.gz
rails-d075c84320fab51992a1ab7d020c62ff1bad0b4e.tar.bz2
rails-d075c84320fab51992a1ab7d020c62ff1bad0b4e.zip
Merge pull request #15307 from sgrif/sg-type-cast-for-write
Add an interface for type objects to control Ruby => SQL
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters')
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/quoting.rb9
-rw-r--r--activerecord/lib/active_record/connection_adapters/column.rb3
-rw-r--r--activerecord/lib/active_record/connection_adapters/type/value.rb4
3 files changed, 15 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb b/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb
index 75501852ed..0bd53a7eb0 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb
@@ -47,6 +47,15 @@ module ActiveRecord
return value.id
end
+ # FIXME: The only case we get an object other than nil or a real column
+ # is `SchemaStatements#add_column` with a PG array that has a non-empty default
+ # value. Is this really the only case? Are we missing tests for other types?
+ # We should have a real column object passed (or nil) here, and check for that
+ # instead
+ if column.respond_to?(:type_cast_for_database)
+ value = column.type_cast_for_database(value)
+ end
+
case value
when String, ActiveSupport::Multibyte::Chars
value = value.to_s
diff --git a/activerecord/lib/active_record/connection_adapters/column.rb b/activerecord/lib/active_record/connection_adapters/column.rb
index 42aabd6d7f..86232f9d3f 100644
--- a/activerecord/lib/active_record/connection_adapters/column.rb
+++ b/activerecord/lib/active_record/connection_adapters/column.rb
@@ -18,7 +18,8 @@ module ActiveRecord
alias :encoded? :coder
- delegate :type, :precision, :scale, :limit, :klass, :text?, :number?, :binary?, :type_cast_for_write, to: :cast_type
+ delegate :type, :precision, :scale, :limit, :klass, :text?, :number?, :binary?,
+ :type_cast_for_write, :type_cast_for_database, to: :cast_type
# Instantiates a new column in the table.
#
diff --git a/activerecord/lib/active_record/connection_adapters/type/value.rb b/activerecord/lib/active_record/connection_adapters/type/value.rb
index 54a3e9dd7a..9bbc249c2b 100644
--- a/activerecord/lib/active_record/connection_adapters/type/value.rb
+++ b/activerecord/lib/active_record/connection_adapters/type/value.rb
@@ -21,6 +21,10 @@ module ActiveRecord
value
end
+ def type_cast_for_database(value)
+ type_cast_for_write(value)
+ end
+
def text?
false
end