Skip to content

Commit

Permalink
delegate attribute typecasting to the column
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Feb 6, 2012
1 parent 321b4c8 commit 5dec3dd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
18 changes: 2 additions & 16 deletions activerecord/lib/active_record/attribute_methods/write.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions activerecord/lib/active_record/connection_adapters/column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down

1 comment on commit 5dec3dd

@fsvehla
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 where it belongs.

Please sign in to comment.