1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Use destructured arguments when looping through pairs

Minor refactoring of looping behavior for aggregation
This commit is contained in:
Sean Griffin 2014-05-17 14:50:24 -06:00
parent 7359f8190d
commit 113f56d31d

View file

@ -230,8 +230,8 @@ module ActiveRecord
private private
def reader_method(name, class_name, mapping, allow_nil, constructor) def reader_method(name, class_name, mapping, allow_nil, constructor)
define_method(name) do define_method(name) do
if @aggregation_cache[name].nil? && (!allow_nil || mapping.any? {|pair| !read_attribute(pair.first).nil? }) if @aggregation_cache[name].nil? && (!allow_nil || mapping.any? {|key, _| !read_attribute(key).nil? })
attrs = mapping.collect {|pair| read_attribute(pair.first)} attrs = mapping.collect {|key, _| read_attribute(key)}
object = constructor.respond_to?(:call) ? object = constructor.respond_to?(:call) ?
constructor.call(*attrs) : constructor.call(*attrs) :
class_name.constantize.send(constructor, *attrs) class_name.constantize.send(constructor, *attrs)
@ -249,10 +249,10 @@ module ActiveRecord
end end
if part.nil? && allow_nil if part.nil? && allow_nil
mapping.each { |pair| self[pair.first] = nil } mapping.each { |key, _| self[key] = nil }
@aggregation_cache[name] = nil @aggregation_cache[name] = nil
else else
mapping.each { |pair| self[pair.first] = part.send(pair.last) } mapping.each { |key, value| self[key] = part.send(value) }
@aggregation_cache[name] = part.freeze @aggregation_cache[name] = part.freeze
end end
end end