Class: GroupDescendantsFinder

Inherits:
Object
  • Object
show all
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.

When passing a ‘filter` param, the search is performed over all nested levels of the `parent_group`. All ancestors for a search result are loaded

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.

  filter: string - is aliased to `search` for consistency with the frontend
  archived: string - `only` or `true`.
                     `non_archived` is passed to the `ProjectFinder`s if none
                     was given.

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.reverse_merge(non_archived: params[:archived].blank?, not_aimed_for_deletion: true)
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
  # The children array might be extended with the ancestors of projects and
  # subgroups when filtering. In that case, take the maximum so the array does
  # not get limited otherwise, allow paginating through all results.
  #
  all_required_elements = children
  if params[:filter]
    all_required_elements |= ancestors_of_filtered_subgroups
    all_required_elements |= ancestors_of_filtered_projects
  end

  total_count = [all_required_elements.size, paginator.total_count].max

  Kaminari.paginate_array(all_required_elements, total_count: total_count)
end

#has_children?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'app/finders/group_descendants_finder.rb', line 47

def has_children?
  projects.any? || subgroups.any?
end