提交 7af59e16 编写于 作者: E Eugene Kenny

Use transform_values in a few more places

It's faster on Ruby 2.7+ since it avoids rehashing the keys.
上级 6c7d85ed
......@@ -317,12 +317,10 @@ def as_json(options = nil)
# person.errors.to_hash # => {:name=>["cannot be nil"]}
# person.errors.to_hash(true) # => {:name=>["name cannot be nil"]}
def to_hash(full_messages = false)
hash = {}
message_method = full_messages ? :full_message : :message
group_by_attribute.each do |attribute, errors|
hash[attribute] = errors.map(&message_method)
group_by_attribute.transform_values do |errors|
errors.map(&message_method)
end
hash
end
def to_h
......@@ -348,9 +346,8 @@ def messages
# Updating this hash would still update errors state for backward
# compatibility, but this behavior is deprecated.
def details
hash = {}
group_by_attribute.each do |attribute, errors|
hash[attribute] = errors.map(&:detail)
hash = group_by_attribute.transform_values do |errors|
errors.map(&:detail)
end
DeprecationHandlingDetailsHash.new(hash)
end
......
......@@ -63,8 +63,8 @@ def convert_dates_from(data)
when Array
data.map! { |d| convert_dates_from(d) }
when Hash
data.each do |key, value|
data[key] = convert_dates_from(value)
data.transform_values! do |value|
convert_dates_from(value)
end
else
data
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册