debian-mirror-gitlab/app/controllers/concerns/group_tree.rb

50 lines
1.7 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
module GroupTree
# rubocop:disable Gitlab/ModuleWithInstanceVariables
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-03-17 18:26:18 +05:30
def render_group_tree(groups)
2018-11-08 19:23:39 +05:30
groups = groups.sort_by_attribute(@sort = params[:sort])
2018-03-17 18:26:18 +05:30
2018-11-08 19:23:39 +05:30
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])
2018-03-17 18:26:18 +05:30
respond_to do |format|
format.html
format.json do
serializer = GroupChildSerializer.new(current_user: current_user)
.with_pagination(request, response)
serializer.expand_hierarchy if params[:filter].present?
render json: serializer.represent(@groups)
end
end
# rubocop:enable Gitlab/ModuleWithInstanceVariables
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-11-08 19:23:39 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-11-08 19:23:39 +05:30
def filtered_groups_with_ancestors(groups)
filtered_groups = groups.search(params[:filter]).page(params[:page])
2019-02-13 22:33:31 +05:30
if Group.supports_nested_objects?
2018-11-08 19:23:39 +05:30
# 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.
2019-02-13 22:33:31 +05:30
Gitlab::ObjectHierarchy.new(Group.where(id: filtered_groups.select(:id)))
2018-11-08 19:23:39 +05:30
.base_and_ancestors
else
filtered_groups
end
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-03-17 18:26:18 +05:30
end