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:
parent
321b4c8527
commit
5dec3dd59c
2 changed files with 17 additions and 16 deletions
|
@ -43,23 +43,9 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def type_cast_attribute_for_write(column, value)
|
||||
if column && column.number?
|
||||
convert_number_column_value(value)
|
||||
else
|
||||
value
|
||||
end
|
||||
end
|
||||
return value unless column
|
||||
|
||||
def convert_number_column_value(value)
|
||||
if value == false
|
||||
0
|
||||
elsif value == true
|
||||
1
|
||||
elsif value.is_a?(String) && value.blank?
|
||||
nil
|
||||
else
|
||||
value
|
||||
end
|
||||
column.type_cast_for_write value
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -66,6 +66,21 @@ module ActiveRecord
|
|||
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.
|
||||
def type_cast(value)
|
||||
return nil if value.nil?
|
||||
|
|
Loading…
Reference in a new issue