Commit 2eac1537 authored by Bob Van Landuyt's avatar Bob Van Landuyt

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

parent ca538899
...@@ -45,18 +45,15 @@ class GroupsController < Groups::ApplicationController ...@@ -45,18 +45,15 @@ class GroupsController < Groups::ApplicationController
end end
def show def show
setup_projects @children = GroupChildrenFinder.new(current_user, parent_group: @group, params: params).execute
@children = @children.page(params[:page])
respond_to do |format| respond_to do |format|
format.html format.html
format.json do
render json: {
html: view_to_html_string("dashboard/projects/_projects", locals: { projects: @projects })
}
end
format.atom do format.atom do
setup_projects
load_events load_events
render layout: 'xml.atom' render layout: 'xml.atom'
end end
......
...@@ -16,27 +16,38 @@ class GroupChildrenFinder ...@@ -16,27 +16,38 @@ class GroupChildrenFinder
# This allows us to fetch only the count without loading the objects. Unless # This allows us to fetch only the count without loading the objects. Unless
# the objects were already loaded. # the objects were already loaded.
def total_count def total_count
@total_count ||= if defined?(@children) @total_count ||= subgroup_count + project_count
children.size end
else
child_groups.count + projects.count def subgroup_count
end @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 end
private private
def children def children
@children ||= child_groups + projects @children ||= subgroups + projects
end end
def child_groups def subgroups
return Group.none unless Group.supports_nested_groups? return Group.none unless Group.supports_nested_groups?
return Group.none unless can?(current_user, :read_group, parent_group) return Group.none unless can?(current_user, :read_group, parent_group)
groups = GroupsFinder.new(current_user, groups = GroupsFinder.new(current_user,
parent: parent_group, parent: parent_group,
all_available: true, all_available: true).execute
all_children_for_parent: params[:filter_groups].present?).execute
groups = groups.search(params[:filter]) if params[:filter].present? groups = groups.search(params[:filter]) if params[:filter].present?
groups = groups.includes(:route).includes(:children) groups = groups.includes(:route).includes(:children)
......
- if children.any?
render children here
- else
.nothing-here-block No children found
%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
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
.groups-header{ class: container_class } .groups-header{ class: container_class }
.top-area .top-area
= render 'groups/show_nav'
.nav-controls .nav-controls
= render 'shared/projects/search_form' = render 'shared/projects/search_form'
= render 'shared/projects/dropdown' = render 'shared/projects/dropdown'
...@@ -16,4 +15,4 @@ ...@@ -16,4 +15,4 @@
= link_to new_project_path(namespace_id: @group.id), class: 'btn btn-new pull-right' do = link_to new_project_path(namespace_id: @group.id), class: 'btn btn-new pull-right' do
New Project New Project
= render "projects", projects: @projects = render "children", children: @children
...@@ -52,16 +52,16 @@ describe GroupChildrenFinder do ...@@ -52,16 +52,16 @@ describe GroupChildrenFinder do
describe '#total_count' do describe '#total_count' do
it 'counts the array children were already loaded' 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).not_to receive(:projects)
expect(finder.total_count).to eq(1) expect(finder.total_count).to eq(1)
end end
it 'performs a count without loading children when they are not loaded yet' do 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).to receive(:projects).and_call_original
expect(finder.total_count).to eq(2) expect(finder.total_count).to eq(2)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment