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 ...@@ -6,6 +6,12 @@ module API
def paginate(relation) def paginate(relation)
::Gitlab::Pagination::OffsetPagination.new(self).paginate(relation) ::Gitlab::Pagination::OffsetPagination.new(self).paginate(relation)
end 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 end
end end
...@@ -84,7 +84,6 @@ module API ...@@ -84,7 +84,6 @@ module API
def prepare_query(projects) def prepare_query(projects)
projects = reorder_projects(projects) projects = reorder_projects(projects)
projects = apply_filters(projects) projects = apply_filters(projects)
projects = paginate(projects)
projects, options = with_custom_attributes(projects) projects, options = with_custom_attributes(projects)
...@@ -105,6 +104,8 @@ module API ...@@ -105,6 +104,8 @@ module API
def prepare_and_present(project_relation) def prepare_and_present(project_relation)
projects, options = prepare_query(project_relation) projects, options = prepare_query(project_relation)
projects = paginate_and_retrieve!(projects)
# Refresh count caches # Refresh count caches
options[:with].execute_batch_counting(projects) options[:with].execute_batch_counting(projects)
......
...@@ -19,4 +19,18 @@ describe API::Helpers::Pagination do ...@@ -19,4 +19,18 @@ describe API::Helpers::Pagination do
expect(result).to eq(expected_result) expect(result).to eq(expected_result)
end end
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 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