PostgreSQLAdapter#select_raw use each_with_index to avoid multiple array lookups

This commit is contained in:
Aaron Patterson
2010-07-12 17:49:12 -07:00
parent 8521cdf03d
commit 79e9f4a318

View File

@@ -952,7 +952,7 @@ module ActiveRecord
fields = res.fields
rows = results.map do |row|
hashed_row = {}
row.each_index do |cell_index|
row.each_with_index do |cell, cell_index|
# If this is a money type column and there are any currency symbols,
# then strip them off. Indeed it would be prettier to do this in
# PostgreSQLColumn.string_to_decimal but would break form input
@@ -962,15 +962,15 @@ module ActiveRecord
# cases to consider (note the decimal separators):
# (1) $12,345,678.12
# (2) $12.345.678,12
case column = row[cell_index]
case cell
when /^-?\D+[\d,]+\.\d{2}$/ # (1)
row[cell_index] = column.gsub(/[^-\d\.]/, '')
row[cell_index] = cell.gsub(/[^-\d\.]/, '')
when /^-?\D+[\d\.]+,\d{2}$/ # (2)
row[cell_index] = column.gsub(/[^-\d,]/, '').sub(/,/, '.')
row[cell_index] = cell.gsub(/[^-\d,]/, '').sub(/,/, '.')
end
end
hashed_row[fields[cell_index]] = column
hashed_row[fields[cell_index]] = cell
end
row
end