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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow nested pause blocks without resuming inner blocks #47

Merged
merged 1 commit into from
Jul 14, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/prosopite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,16 @@ def tc
end

def pause
tc[:prosopite_scan] = false

if block_given?
begin
previous = tc[:prosopite_scan]
tc[:prosopite_scan] = false
yield
ensure
tc[:prosopite_scan] = true
tc[:prosopite_scan] = previous
end
else
tc[:prosopite_scan] = false
end
end

Expand Down
27 changes: 27 additions & 0 deletions test/test_queries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,33 @@ def test_pause_with_a_block
assert_no_n_plus_ones
end

def test_nested_pause_blocks
# 10 chairs, 4 legs each
chairs = create_list(:chair, 10)
chairs.each { |c| create_list(:leg, 4, chair: c) }

Prosopite.scan

inner_result = nil
outer_result = Prosopite.pause do
inner_result = Prosopite.pause do
:result
end

Chair.last(20).each do |c|
c.legs.last
end

:outer_result
end

assert_equal(:result, inner_result)

assert_equal(:outer_result, outer_result)

assert_no_n_plus_ones
end

def test_pause_with_a_block_raising_error
Prosopite.scan

Expand Down