diff options
author | Bob Van Landuyt <bob@vanlanduyt.co> | 2017-09-04 16:23:55 +0200 |
---|---|---|
committer | Bob Van Landuyt <bob@vanlanduyt.co> | 2017-10-04 22:46:49 +0200 |
commit | 063b5312111ccea62f84fa9f68a2262dc1f66e64 (patch) | |
tree | 44f6dbae88e55dc6c1e24ac6aa888e6cb2f3ea3a /app/finders | |
parent | 283e13b836f805af149c271088bc9305e0ce0ada (diff) | |
download | gitlab-ce-063b5312111ccea62f84fa9f68a2262dc1f66e64.tar.gz |
Add a separate finder for collecting children of groups
Diffstat (limited to 'app/finders')
-rw-r--r-- | app/finders/group_children_finder.rb | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/app/finders/group_children_finder.rb b/app/finders/group_children_finder.rb new file mode 100644 index 00000000000..d95dfa2a877 --- /dev/null +++ b/app/finders/group_children_finder.rb @@ -0,0 +1,54 @@ +class GroupChildrenFinder + include Gitlab::Allowable + + attr_reader :current_user, :parent_group, :params + + def initialize(current_user = nil, parent_group:, params: {}) + @current_user = current_user + @parent_group = parent_group + @params = params + end + + def execute + Kaminari.paginate_array(children) + end + + # This allows us to fetch only the count without loading the objects. Unless + # the objects were already loaded. + def total_count + @total_count ||= if defined?(@children) + children.size + else + child_groups.count + projects.count + end + end + + private + + def children + @children ||= child_groups + projects + end + + def child_groups + return Group.none unless Group.supports_nested_groups? + return Group.none unless can?(current_user, :read_group, parent_group) + + groups = GroupsFinder.new(current_user, + parent: parent_group, + all_available: true, + all_children_for_parent: params[:filter_groups].present?).execute + + groups = groups.search(params[:filter]) if params[:filter].present? + groups = groups.includes(:route).includes(:children) + groups.sort(params[:sort]) + end + + def projects + return Project.none unless can?(current_user, :read_group, parent_group) + + projects = GroupProjectsFinder.new(group: parent_group, params: params, current_user: current_user).execute + projects = projects.includes(:route) + projects = projects.search(params[:filter]) if params[:filter].present? + projects.sort(params[:sort]) + end +end |