From 8aeab43247f9c246edac565c25fa03d4b295504c 2014-05-24 10:41:13 From: Jean-Philippe Lang Date: 2014-05-24 10:41:13 Subject: [PATCH] Count users with a single query on group list (#16905). git-svn-id: http://svn.redmine.org/redmine/trunk@13149 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index 2dbcd8a..0cd4055 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -26,9 +26,10 @@ class GroupsController < ApplicationController def index @groups = Group.sorted.all - respond_to do |format| - format.html + format.html { + @user_count_by_group_id = user_count_by_group_id + } format.api end end @@ -138,4 +139,12 @@ class GroupsController < ApplicationController rescue ActiveRecord::RecordNotFound render_404 end + + def user_count_by_group_id + h = User.joins(:groups).group('group_id').count + h.keys.each do |key| + h[key.to_i] = h.delete(key) + end + h + end end diff --git a/app/views/groups/index.html.erb b/app/views/groups/index.html.erb index d48afe9..22c89ef 100644 --- a/app/views/groups/index.html.erb +++ b/app/views/groups/index.html.erb @@ -3,7 +3,6 @@ <%= title l(:label_group_plural) %> - <% if @groups.any? %> @@ -13,9 +12,9 @@ <% @groups.each do |group| %> - + - + <% end %> diff --git a/test/functional/groups_controller_test.rb b/test/functional/groups_controller_test.rb index 35f0a43..252472a 100644 --- a/test/functional/groups_controller_test.rb +++ b/test/functional/groups_controller_test.rb @@ -30,6 +30,12 @@ class GroupsControllerTest < ActionController::TestCase assert_template 'index' end + def test_index_should_show_user_count + get :index + assert_response :success + assert_select 'tr#group-11 td.user_count', :text => '1' + end + def test_show get :show, :id => 10 assert_response :success
<%= link_to h(group), edit_group_path(group) %><%= group.users.size %><%= @user_count_by_group_id[group.id] || 0 %> <%= delete_link group %>