Class: GroupDescendantsFinder

Inherits:
Object
  • Object
show all
Includes:
Gitlab::Utils::StrongMemoize
Defined in:
app/finders/group_descendants_finder.rb

Overview

GroupDescendantsFinder

Used to find and filter all subgroups and projects of a passed parent group visible to a specified user.

Arguments:

current_user: The user for which the children should be visible
parent_group: The group to find children of
params:
  Supports all params that the `ProjectsFinder` and `GroupProjectsFinder`
  support.

  active: boolean - filters for active descendants. When `false`, the search is performed over
                    all nested levels of the `parent group` and all inactive ancestors are loaded.
  filter: string - aliased to `search` for consistency with the frontend. When a filter is
                   passed, the search is performed over all nested levels of the `parent_group`.
                   All ancestors for a search result are loaded

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent_group:, current_user: nil, params: {}) ⇒ GroupDescendantsFinder

Returns a new instance of GroupDescendantsFinder.



25
26
27
28
29
# File 'app/finders/group_descendants_finder.rb', line 25

def initialize(parent_group:, current_user: nil, params: {})
  @current_user = current_user
  @parent_group = parent_group
  @params = params
end

Instance Attribute Details

#current_userObject (readonly)

Returns the value of attribute current_user.



23
24
25
# File 'app/finders/group_descendants_finder.rb', line 23

def current_user
  @current_user
end

#paramsObject (readonly)

Returns the value of attribute params.



23
24
25
# File 'app/finders/group_descendants_finder.rb', line 23

def params
  @params
end

#parent_groupObject (readonly)

Returns the value of attribute parent_group.



23
24
25
# File 'app/finders/group_descendants_finder.rb', line 23

def parent_group
  @parent_group
end

Instance Method Details

#executeObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/finders/group_descendants_finder.rb', line 31

def execute
  # First paginate and then include the ancestors of the filtered children to:
  # - Avoid truncating children or preloaded ancestors due to per_page limit
  # - Ensure correct pagination headers are returned
  all_required_elements = Kaminari.paginate_array(children, total_count: paginator.total_count)
                                  .page(page)

  preloaded_ancestors = []
  if search_descendants?
    preloaded_ancestors |= ancestors_of_filtered_subgroups
    preloaded_ancestors |= ancestors_of_filtered_projects
  end

  all_required_elements.concat(preloaded_ancestors - children)
end