Skip to content

Commit 6ac6b0b

Browse files
authored
Merge pull request rails#48063 from miharekar/fix-nested-in-order-of
Only flatten first level to preserve nested
2 parents 3a1662f + ffa27f3 commit 6ac6b0b

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

activesupport/lib/active_support/core_ext/enumerable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def compact_blank
194194
# If the +series+ include keys that have no corresponding element in the Enumerable, these are ignored.
195195
# If the Enumerable has additional elements that aren't named in the +series+, these are not included in the result.
196196
def in_order_of(key, series)
197-
group_by(&key).values_at(*series).flatten.compact
197+
group_by(&key).values_at(*series).flatten(1).compact
198198
end
199199

200200
# Returns the sole item in the enumerable. If there are no items, or more

activesupport/test/core_ext/enumerable_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,11 @@ def test_in_order_of_preserves_duplicates
380380
assert_equal [ Payment.new(1), Payment.new(5), Payment.new(5) ], values.in_order_of(:price, [ 1, 5 ])
381381
end
382382

383+
def test_in_order_of_preserves_nested_elements
384+
values = [[:paid, { price: 1, currency: :eur }], [:opened, { price: 2, currency: :usd }]]
385+
assert_equal [[:opened, { price: 2, currency: :usd }], [:paid, { price: 1, currency: :eur }]], values.in_order_of(:first, [:opened, :paid])
386+
end
387+
383388
def test_sole
384389
expected_raise = Enumerable::SoleItemExpectedError
385390

0 commit comments

Comments
 (0)