Skip to content

Commit

Permalink
Merge pull request #48063 from miharekar/fix-nested-in-order-of
Browse files Browse the repository at this point in the history
Only flatten first level to preserve nested
  • Loading branch information
byroot committed May 25, 2023
1 parent efe2741 commit 90474c6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activesupport/lib/active_support/core_ext/enumerable.rb
Expand Up @@ -245,7 +245,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 @@ -382,6 +382,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

0 comments on commit 90474c6

Please sign in to comment.