diff --git a/activerecord/lib/active_record/connection_adapters/column.rb b/activerecord/lib/active_record/connection_adapters/column.rb index 9557f41950dde84466757b5bead15a807e755480..107b18ffd2582868701ad378948e3d5d28b37bf9 100644 --- a/activerecord/lib/active_record/connection_adapters/column.rb +++ b/activerecord/lib/active_record/connection_adapters/column.rb @@ -18,7 +18,7 @@ module Format 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 @@ def klass 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 aba721eeced74e31672b13acb61e7f5e0b103c7a..151ebd9adb9963cec5bc31d0e1f0802e907c75df 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 @@ def self.extract_value_from_default(default) 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 a440de4d14dfabd7c58c577e351b4a8f8c1947c6..a3379831cb0b1a66d73bee5123ce831dac9c2fc3 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 Numeric # :nodoc: 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 506f402fefcc75e834b6faf62bfba592f020b8a0..ba8f360eb908738241b4ce138363f7fd812bbaca 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 @@ def type_cast(value) cast_value(value) unless value.nil? end + def type_cast_for_write(value) + value + end + def text? false end