Skip to content

Commit 5d1fbc1

Browse files
geshwhocharkost
authored andcommitted
Allow nested pause blocks
1 parent f0d7718 commit 5d1fbc1

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

lib/prosopite.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,16 @@ def tc
4848
end
4949

5050
def pause
51-
tc[:prosopite_scan] = false
52-
5351
if block_given?
5452
begin
53+
previous = tc[:prosopite_scan]
54+
tc[:prosopite_scan] = false
5555
yield
5656
ensure
57-
tc[:prosopite_scan] = true
57+
tc[:prosopite_scan] = previous
5858
end
59+
else
60+
tc[:prosopite_scan] = false
5961
end
6062
end
6163

test/test_queries.rb

+27
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,33 @@ def test_pause_with_a_block
180180
assert_no_n_plus_ones
181181
end
182182

183+
def test_nested_pause_blocks
184+
# 10 chairs, 4 legs each
185+
chairs = create_list(:chair, 10)
186+
chairs.each { |c| create_list(:leg, 4, chair: c) }
187+
188+
Prosopite.scan
189+
190+
inner_result = nil
191+
outer_result = Prosopite.pause do
192+
inner_result = Prosopite.pause do
193+
:result
194+
end
195+
196+
Chair.last(20).each do |c|
197+
c.legs.last
198+
end
199+
200+
:outer_result
201+
end
202+
203+
assert_equal(:result, inner_result)
204+
205+
assert_equal(:outer_result, outer_result)
206+
207+
assert_no_n_plus_ones
208+
end
209+
183210
def test_pause_with_a_block_raising_error
184211
Prosopite.scan
185212

0 commit comments

Comments
 (0)