Permalink
Browse files

delegate attribute typecasting to the column

  • Loading branch information...
1 parent 321b4c8 commit 5dec3dd59c9f4b5c881860b0f629c8d6b3706f90 @tenderlove tenderlove committed Feb 6, 2012
@@ -43,23 +43,9 @@ def attribute=(attribute_name, value)
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 @@ def klass
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?

1 comment on commit 5dec3dd

+1 where it belongs.

Please sign in to comment.