Browse files

Allow pre-casted values (other than nil) to pass through from calcula…

…tions un-touched

[#4514 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information...
1 parent 04fa5af commit 6dbc75fd7625c0248d269cc3127910cb4c888ed9 @brianmario brianmario committed with jeremy May 4, 2010
Showing with 6 additions and 3 deletions.
  1. +6 −3 activerecord/lib/active_record/calculations.rb
View
9 activerecord/lib/active_record/calculations.rb
@@ -294,12 +294,15 @@ def column_for(field)
end
def type_cast_calculated_value(value, column, operation = nil)
- operation = operation.to_s.downcase
- case operation
+ if value.is_a?(String) || value.nil?
+ case operation.to_s.downcase
when 'count' then value.to_i
when 'sum' then type_cast_using_column(value || '0', column)
- when 'avg' then value && (value.is_a?(Fixnum) ? value.to_f : value).to_d
+ when 'avg' then value && (value.is_a?(Fixnum) ? value.to_f : value).to_d
else type_cast_using_column(value, column)
+ end
+ else
+ value
end
end

0 comments on commit 6dbc75f

Please sign in to comment.