Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only flatten first level to preserve nested #48063

Merged
merged 1 commit into from Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion activesupport/lib/active_support/core_ext/enumerable.rb
Expand Up @@ -194,7 +194,7 @@ def compact_blank
# If the +series+ include keys that have no corresponding element in the Enumerable, these are ignored.
# If the Enumerable has additional elements that aren't named in the +series+, these are not included in the result.
def in_order_of(key, series)
group_by(&key).values_at(*series).flatten.compact
group_by(&key).values_at(*series).flatten(1).compact
end

# Returns the sole item in the enumerable. If there are no items, or more
Expand Down
5 changes: 5 additions & 0 deletions activesupport/test/core_ext/enumerable_test.rb
Expand Up @@ -380,6 +380,11 @@ def test_in_order_of_preserves_duplicates
assert_equal [ Payment.new(1), Payment.new(5), Payment.new(5) ], values.in_order_of(:price, [ 1, 5 ])
end

def test_in_order_of_preserves_nested_elements
values = [[:paid, { price: 1, currency: :eur }], [:opened, { price: 2, currency: :usd }]]
assert_equal [[:opened, { price: 2, currency: :usd }], [:paid, { price: 1, currency: :eur }]], values.in_order_of(:first, [:opened, :paid])
end

def test_sole
expected_raise = Enumerable::SoleItemExpectedError

Expand Down