Commit 50876d4c authored by Andreas Brandl's avatar Andreas Brandl

Explicitly retrieve data after pagination

The idea here is to make it explicit that at this point we are handling
an array and not a relation anymore.
parent 5566c021
......@@ -6,6 +6,12 @@ module API
def paginate(relation)
::Gitlab::Pagination::OffsetPagination.new(self).paginate(relation)
end
# This applies pagination and executes the query
# It always returns an array instead of an ActiveRecord relation
def paginate_and_retrieve!(relation)
paginate(relation).to_a
end
end
end
end
......@@ -84,7 +84,6 @@ module API
def prepare_query(projects)
projects = reorder_projects(projects)
projects = apply_filters(projects)
projects = paginate(projects)
projects, options = with_custom_attributes(projects)
......@@ -105,6 +104,8 @@ module API
def prepare_and_present(project_relation)
projects, options = prepare_query(project_relation)
projects = paginate_and_retrieve!(projects)
# Refresh count caches
options[:with].execute_batch_counting(projects)
......
......@@ -19,4 +19,18 @@ describe API::Helpers::Pagination do
expect(result).to eq(expected_result)
end
end
describe '#paginate_and_retrieve!' do
let(:relation) { double("relation") }
let(:paginated_result) { double }
let(:result) { double }
it 'applies pagination and returns an array' do
expect(subject).to receive(:paginate).with(relation).and_return(paginated_result)
expect(paginated_result).to receive(:to_a).and_return(result)
expect(subject.paginate_and_retrieve!(relation)).to eq(result)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment