Browse Source

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
tags/2.6.0
Jean-Philippe Lang 10 years ago
parent
commit
8aeab43247

+ 11
- 2
app/controllers/groups_controller.rb View File

@@ -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

+ 2
- 3
app/views/groups/index.html.erb View File

@@ -3,7 +3,6 @@
</div>

<%= title l(:label_group_plural) %>

<% if @groups.any? %>
<table class="list groups">
<thead><tr>
@@ -13,9 +12,9 @@
</tr></thead>
<tbody>
<% @groups.each do |group| %>
<tr class="<%= cycle 'odd', 'even' %>">
<tr id="group-<%= group.id %>" class="<%= cycle 'odd', 'even' %>">
<td class="name"><%= link_to h(group), edit_group_path(group) %></td>
<td><%= group.users.size %></td>
<td class="user_count"><%= @user_count_by_group_id[group.id] || 0 %></td>
<td class="buttons"><%= delete_link group %></td>
</tr>
<% end %>

+ 6
- 0
test/functional/groups_controller_test.rb View File

@@ -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

Loading…
Cancel
Save