提交 470c4dfe 编写于 作者: R Rafael Mendonça França

Merge pull request #15208 from sgrif/sg-delegate-type-cast-for-write

Delegate `type_cast_for_write` to injected type object
......@@ -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?
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册