2018-09-14 01:42:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-13 11:16:30 -04:00
|
|
|
module GroupTree
|
2017-11-22 02:50:36 -05:00
|
|
|
# rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-09-13 11:16:30 -04:00
|
|
|
def render_group_tree(groups)
|
2018-07-05 05:16:08 -04:00
|
|
|
groups = groups.sort_by_attribute(@sort = params[:sort])
|
2018-01-11 11:34:01 -05:00
|
|
|
|
2018-07-05 05:16:08 -04:00
|
|
|
groups = if params[:filter].present?
|
|
|
|
filtered_groups_with_ancestors(groups)
|
|
|
|
else
|
|
|
|
# If `params[:parent_id]` is `nil`, we will only show root-groups
|
|
|
|
groups.where(parent_id: params[:parent_id]).page(params[:page])
|
|
|
|
end
|
|
|
|
|
|
|
|
@groups = groups.with_selects_for_list(archived: params[:archived])
|
2017-09-13 11:16:30 -04:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
format.json do
|
|
|
|
serializer = GroupChildSerializer.new(current_user: current_user)
|
|
|
|
.with_pagination(request, response)
|
2017-09-14 03:01:29 -04:00
|
|
|
serializer.expand_hierarchy if params[:filter].present?
|
2017-09-13 11:16:30 -04:00
|
|
|
render json: serializer.represent(@groups)
|
|
|
|
end
|
|
|
|
end
|
2017-11-22 02:50:36 -05:00
|
|
|
# rubocop:enable Gitlab/ModuleWithInstanceVariables
|
2017-09-13 11:16:30 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-07-05 05:16:08 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-07-05 05:16:08 -04:00
|
|
|
def filtered_groups_with_ancestors(groups)
|
|
|
|
filtered_groups = groups.search(params[:filter]).page(params[:page])
|
|
|
|
|
2019-07-24 05:20:54 -04:00
|
|
|
# We find the ancestors by ID of the search results here.
|
|
|
|
# Otherwise the ancestors would also have filters applied,
|
|
|
|
# which would cause them not to be preloaded.
|
|
|
|
#
|
|
|
|
# Pagination needs to be applied before loading the ancestors to
|
|
|
|
# make sure ancestors are not cut off by pagination.
|
|
|
|
Gitlab::ObjectHierarchy.new(Group.where(id: filtered_groups.select(:id)))
|
|
|
|
.base_and_ancestors
|
2018-07-05 05:16:08 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-09-13 11:16:30 -04:00
|
|
|
end
|