Include pagination when rendering expanded hierarchies

This commit is contained in:
Bob Van Landuyt 2017-09-15 12:28:21 +02:00
parent 20a08965bc
commit 9781ac552d
3 changed files with 33 additions and 17 deletions

View file

@ -1,10 +1,12 @@
module WithPagination
attr_accessor :paginator
def with_pagination(request, response)
tap { @paginator = Gitlab::Serializer::Pagination.new(request, response) }
tap { self.paginator = Gitlab::Serializer::Pagination.new(request, response) }
end
def paginated?
@paginator.present?
paginator.present?
end
# super is `BaseSerializer#represent` here.

View file

@ -1,18 +1,20 @@
class GroupChildSerializer < BaseSerializer
include WithPagination
attr_reader :hierarchy_root
attr_reader :hierarchy_root, :should_expand_hierarchy
entity GroupChildEntity
def expand_hierarchy(hierarchy_root = nil)
@hierarchy_root = hierarchy_root
@expand_hierarchy = true
self
tap do
@hierarchy_root = hierarchy_root
@should_expand_hierarchy = true
end
end
def represent(resource, opts = {}, entity_class = nil)
if @expand_hierarchy
if should_expand_hierarchy
paginator.paginate(resource) if paginated?
represent_hierarchies(resource, opts)
else
super(resource, opts)
@ -33,15 +35,15 @@ class GroupChildSerializer < BaseSerializer
def represent_hierarchy(hierarchy, opts)
serializer = self.class.new(parameters)
result = if hierarchy.is_a?(Hash)
hierarchy.map do |parent, children|
serializer.represent(parent, opts)
.merge(children: Array.wrap(serializer.represent_hierarchy(children, opts)))
end
else
serializer.represent(hierarchy, opts)
end
result
if hierarchy.is_a?(Hash)
hierarchy.map do |parent, children|
serializer.represent(parent, opts)
.merge(children: Array.wrap(serializer.represent_hierarchy(children, opts)))
end
elsif hierarchy.is_a?(Array)
hierarchy.map { |child| serializer.represent_hierarchy(child, opts) }
else
serializer.represent(hierarchy, opts)
end
end
end

View file

@ -152,6 +152,10 @@ describe GroupsController do
describe 'GET #show' do
context 'pagination' do
before do
allow(Kaminari.config).to receive(:default_per_page).and_return(2)
end
context 'with only projects' do
let!(:other_project) { create(:project, :public, namespace: group) }
let!(:first_page_projects) { create_list(:project, Kaminari.config.default_per_page, :public, namespace: group ) }
@ -288,6 +292,14 @@ describe GroupsController do
expect(group_json['id']).to eq(public_subgroup.id)
expect(matched_group_json['id']).to eq(matched_group.id)
end
it 'includes pagination headers' do
2.times { |i| create(:group, :public, parent: public_subgroup, name: "filterme#{i}") }
get :children, id: group.to_param, filter: 'filter', per_page: 1, format: :json
expect(response).to include_pagination_headers
end
end
context 'queries per rendered element', :request_store do