Fetch children using new finder for the show of a group.

This commit is contained in:
Bob Van Landuyt 2017-09-04 20:01:58 +02:00
parent ca538899b6
commit 2eac1537ad
6 changed files with 32 additions and 29 deletions

View file

@ -45,18 +45,15 @@ class GroupsController < Groups::ApplicationController
end
def show
setup_projects
@children = GroupChildrenFinder.new(current_user, parent_group: @group, params: params).execute
@children = @children.page(params[:page])
respond_to do |format|
format.html
format.json do
render json: {
html: view_to_html_string("dashboard/projects/_projects", locals: { projects: @projects })
}
end
format.atom do
setup_projects
load_events
render layout: 'xml.atom'
end

View file

@ -16,27 +16,38 @@ class GroupChildrenFinder
# 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
@total_count ||= subgroup_count + project_count
end
def subgroup_count
@subgroup_count ||= if defined?(@children)
children.count { |child| child.is_a?(Group) }
else
subgroups.count
end
end
def project_count
@project_count ||= if defined?(@children)
children.count { |child| child.is_a?(Project) }
else
projects.count
end
end
private
def children
@children ||= child_groups + projects
@children ||= subgroups + projects
end
def child_groups
def subgroups
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
all_available: true).execute
groups = groups.search(params[:filter]) if params[:filter].present?
groups = groups.includes(:route).includes(:children)

View file

@ -0,0 +1,4 @@
- if children.any?
render children here
- else
.nothing-here-block No children found

View file

@ -1,8 +0,0 @@
%ul.nav-links
= nav_link(page: group_path(@group)) do
= link_to group_path(@group) do
Projects
- if Group.supports_nested_groups?
= nav_link(page: subgroups_group_path(@group)) do
= link_to subgroups_group_path(@group) do
Subgroups

View file

@ -8,7 +8,6 @@
.groups-header{ class: container_class }
.top-area
= render 'groups/show_nav'
.nav-controls
= render 'shared/projects/search_form'
= render 'shared/projects/dropdown'
@ -16,4 +15,4 @@
= link_to new_project_path(namespace_id: @group.id), class: 'btn btn-new pull-right' do
New Project
= render "projects", projects: @projects
= render "children", children: @children

View file

@ -52,16 +52,16 @@ describe GroupChildrenFinder do
describe '#total_count' do
it 'counts the array children were already loaded' do
finder.instance_variable_set(:@children, [double])
finder.instance_variable_set(:@children, [build(:project)])
expect(finder).not_to receive(:child_groups)
expect(finder).not_to receive(:subgroups)
expect(finder).not_to receive(:projects)
expect(finder.total_count).to eq(1)
end
it 'performs a count without loading children when they are not loaded yet' do
expect(finder).to receive(:child_groups).and_call_original
expect(finder).to receive(:subgroups).and_call_original
expect(finder).to receive(:projects).and_call_original
expect(finder.total_count).to eq(2)