From 8aeab43247f9c246edac565c25fa03d4b295504c Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sat, 24 May 2014 10:41:13 +0000 Subject: 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 --- app/controllers/groups_controller.rb | 13 +++++++++++-- app/views/groups/index.html.erb | 5 ++--- test/functional/groups_controller_test.rb | 6 ++++++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index 2dbcd8a1f..0cd4055e5 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 d48afe9f0..22c89eff4 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 35f0a4312..252472ab6 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 -- cgit v1.2.3
<%= link_to h(group), edit_group_path(group) %><%= group.users.size %><%= @user_count_by_group_id[group.id] || 0 %> <%= delete_link group %>