Skip to content

Commit

Permalink
Use transform_values in a few more places
Browse files Browse the repository at this point in the history
It's faster on Ruby 2.7+ since it avoids rehashing the keys.
  • Loading branch information
eugeneius committed Sep 8, 2020
1 parent 6c7d85e commit 7af59e1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
11 changes: 4 additions & 7 deletions activemodel/lib/active_model/errors.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions activesupport/lib/active_support/json/decoding.rb
Expand Up @@ -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
Expand Down

0 comments on commit 7af59e1

Please sign in to comment.