Permalink
Browse files

Update for ActiveRecord#pluck so it type_casts the columns

  • Loading branch information...
1 parent f87d362 commit 4fa565a982b2ab0e236f4964d44be37333c32066 @jeroeningen committed May 31, 2012
@@ -156,29 +156,40 @@ def calculate(operation, column_name, options = {})
# # SELECT DATEDIFF(updated_at, created_at) FROM people
# # => ['0', '27761', '173']
#
- def pluck(*column_name)
- if column_name.size == 1 && column_name.first.is_a?(Symbol) && column_names.include?(column_name.first.to_s)
- column_name = "#{table_name}.#{column_name.first}"
- elsif column_name.is_a?(Array)
- column_name.flatten!
+ def pluck(*column_names)
+ column_names = column_names.flatten
+
+ if column_names.first.is_a?(Symbol) && self.column_names.include?(column_names.first.to_s)
+ if column_names.one?
+ column_names = "#{table_name}.#{column_names.first}"
+ else
+ column_names = column_names.collect{|column_name| "#{table_name}.#{column_name}"}
+ end
end
- result = klass.connection.select_all(select(column_name).arel, nil, bind_values)
-
- key = result.columns.first
- column = klass.column_types.fetch(key) {
- result.column_types.fetch(key) {
- Class.new { def type_cast(v); v; end }.new
+ result = klass.connection.select_all(select(column_names).arel, nil, bind_values)
+ keys = column_names.is_a?(Array) && !column_names.one? ? result.columns : [result.columns.first]
+
+ columns = keys.map do |key|
+ klass.column_types.fetch(key) {
+ result.column_types.fetch(key) {
+ Class.new { def type_cast(v); v; end }.new
+ }
}
- }
-
+ end
+
result.map do |attributes|
+ raise ArgumentError, "Pluck expects attributes parsed as an array: #{attributes.inspect}" if columns.one? != attributes.one?
if attributes.one?
value = klass.initialize_attributes(attributes).values.first
- column.type_cast(value)
+ columns.first.type_cast(value)
else
- column_name.collect{|c| attributes[c.to_s]}
+ values = klass.initialize_attributes(attributes).values
+
+ values.each_with_index.map do |value, i|
+ columns[i].type_cast(value)
+ end
end
end
end
@@ -493,8 +493,8 @@ def test_pluck_with_selection_clause
assert_equal [50 + 53 + 55 + 60], Account.pluck('SUM(DISTINCT(credit_limit)) as credit_limit')
end
- def test_pluck_expects_a_single_or_multiple_selection
- assert_nothing_raised(ArgumentError) { Account.pluck 'id, credit_limit' }
+ def test_pluck_expects_a_multiple_selection_as_array
+ assert_raise(ArgumentError) { Account.pluck 'id, credit_limit' }
end
def test_plucks_with_ids

0 comments on commit 4fa565a

Please sign in to comment.