Simplify validations_arry_for_column_names_and_attributes with map, each_with_index and Hash.

Hash[column_names.zip(attributes)] would be much clearer, but would leave nil values for missing attributes which doesn't match the existing behavior.
This commit is contained in:
Ben Woosley 2010-11-17 00:58:43 -08:00 committed by Zach Dennis
parent c9f246f4e2
commit 1a8082444b

View file

@ -336,13 +336,9 @@ class ActiveRecord::Base
# Returns an Array of Hashes for the passed in +column_names+ and +array_of_attributes+. # Returns an Array of Hashes for the passed in +column_names+ and +array_of_attributes+.
def validations_array_for_column_names_and_attributes( column_names, array_of_attributes ) # :nodoc: def validations_array_for_column_names_and_attributes( column_names, array_of_attributes ) # :nodoc:
arr = [] array_of_attributes.map do |attributes|
array_of_attributes.each do |attributes| Hash[attributes.each_with_index.map {|attr, c| [column_names[c], attr] }]
c = 0
hsh = attributes.inject( {} ){|hsh,attr| hsh[ column_names[c] ] = attr ; c+=1 ; hsh }
arr << hsh
end end
arr
end end
end end