1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

delegate attribute typecasting to the column

This commit is contained in:
Aaron Patterson 2012-02-06 15:51:39 -08:00
parent 321b4c8527
commit 5dec3dd59c
2 changed files with 17 additions and 16 deletions

View file

@ -43,23 +43,9 @@ module ActiveRecord
end end
def type_cast_attribute_for_write(column, value) def type_cast_attribute_for_write(column, value)
if column && column.number? return value unless column
convert_number_column_value(value)
else
value
end
end
def convert_number_column_value(value) column.type_cast_for_write value
if value == false
0
elsif value == true
1
elsif value.is_a?(String) && value.blank?
nil
else
value
end
end end
end end
end end

View file

@ -66,6 +66,21 @@ module ActiveRecord
end end
end end
# Casts a Ruby value to something appropriate for writing to the database.
def type_cast_for_write(value)
return value unless number?
if value == false
0
elsif value == true
1
elsif value.is_a?(String) && value.blank?
nil
else
value
end
end
# Casts value (which is a String) to an appropriate instance. # Casts value (which is a String) to an appropriate instance.
def type_cast(value) def type_cast(value)
return nil if value.nil? return nil if value.nil?