aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2014-05-20 18:42:14 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2014-05-20 18:42:14 -0300
commit470c4dfe2d06a428af159f8688eb432529c61c2e (patch)
tree6565a04ca310600d1461efa61519b6961ce194df
parentef00bb719641c93fd7a4670a00e300efbc2c571d (diff)
parent32218c10fa6072616ea01380bc33f10a89e157ba (diff)
downloadrails-470c4dfe2d06a428af159f8688eb432529c61c2e.tar.gz
rails-470c4dfe2d06a428af159f8688eb432529c61c2e.tar.bz2
rails-470c4dfe2d06a428af159f8688eb432529c61c2e.zip
Merge pull request #15208 from sgrif/sg-delegate-type-cast-for-write
Delegate `type_cast_for_write` to injected type object
-rw-r--r--activerecord/lib/active_record/connection_adapters/column.rb20
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/column.rb11
-rw-r--r--activerecord/lib/active_record/connection_adapters/type/numeric.rb9
-rw-r--r--activerecord/lib/active_record/connection_adapters/type/value.rb4
4 files changed, 14 insertions, 30 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/column.rb b/activerecord/lib/active_record/connection_adapters/column.rb
index 9557f41950..107b18ffd2 100644
--- a/activerecord/lib/active_record/connection_adapters/column.rb
+++ b/activerecord/lib/active_record/connection_adapters/column.rb
@@ -18,7 +18,7 @@ module ActiveRecord
alias :encoded? :coder
- delegate :type, :text?, :number?, :binary?, to: :cast_type
+ delegate :type, :text?, :number?, :binary?, :type_cast_for_write, to: :cast_type
# Instantiates a new column in the table.
#
@@ -60,24 +60,6 @@ module ActiveRecord
end
end
- # Casts a Ruby value to something appropriate for writing to the database.
- # Numeric columns will typecast boolean and string to appropriate numeric
- # values.
- def type_cast_for_write(value)
- return value unless number?
-
- case value
- when FalseClass
- 0
- when TrueClass
- 1
- when String
- value.presence
- else
- value
- end
- end
-
# Casts value to an appropriate instance.
def type_cast(value)
if encoded?
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/column.rb b/activerecord/lib/active_record/connection_adapters/postgresql/column.rb
index aba721eece..151ebd9adb 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql/column.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/column.rb
@@ -104,17 +104,6 @@ module ActiveRecord
end
end
- # Casts a Ruby value to something appropriate for writing to PostgreSQL.
- # see ActiveRecord::ConnectionAdapters::Class#type_cast_for_write
- # see ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::OID::Type
- def type_cast_for_write(value)
- if @oid_type.respond_to?(:type_cast_for_write)
- @oid_type.type_cast_for_write(value)
- else
- super
- end
- end
-
def accessor
@oid_type.accessor
end
diff --git a/activerecord/lib/active_record/connection_adapters/type/numeric.rb b/activerecord/lib/active_record/connection_adapters/type/numeric.rb
index a440de4d14..a3379831cb 100644
--- a/activerecord/lib/active_record/connection_adapters/type/numeric.rb
+++ b/activerecord/lib/active_record/connection_adapters/type/numeric.rb
@@ -5,6 +5,15 @@ module ActiveRecord
def number?
true
end
+
+ def type_cast_for_write(value)
+ case value
+ when true then 1
+ when false then 0
+ when ::String then value.presence
+ else super
+ end
+ end
end
end
end
diff --git a/activerecord/lib/active_record/connection_adapters/type/value.rb b/activerecord/lib/active_record/connection_adapters/type/value.rb
index 506f402fef..ba8f360eb9 100644
--- a/activerecord/lib/active_record/connection_adapters/type/value.rb
+++ b/activerecord/lib/active_record/connection_adapters/type/value.rb
@@ -8,6 +8,10 @@ module ActiveRecord
cast_value(value) unless value.nil?
end
+ def type_cast_for_write(value)
+ value
+ end
+
def text?
false
end