mirror of
https://github.com/redmine/redmine.git
synced 2025-11-07 13:55:52 +01:00
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
This commit is contained in:
@@ -26,9 +26,10 @@ class GroupsController < ApplicationController
|
|||||||
|
|
||||||
def index
|
def index
|
||||||
@groups = Group.sorted.all
|
@groups = Group.sorted.all
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.html
|
format.html {
|
||||||
|
@user_count_by_group_id = user_count_by_group_id
|
||||||
|
}
|
||||||
format.api
|
format.api
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -138,4 +139,12 @@ class GroupsController < ApplicationController
|
|||||||
rescue ActiveRecord::RecordNotFound
|
rescue ActiveRecord::RecordNotFound
|
||||||
render_404
|
render_404
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<%= title l(:label_group_plural) %>
|
<%= title l(:label_group_plural) %>
|
||||||
|
|
||||||
<% if @groups.any? %>
|
<% if @groups.any? %>
|
||||||
<table class="list groups">
|
<table class="list groups">
|
||||||
<thead><tr>
|
<thead><tr>
|
||||||
@@ -13,9 +12,9 @@
|
|||||||
</tr></thead>
|
</tr></thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
<% @groups.each do |group| %>
|
<% @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 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>
|
<td class="buttons"><%= delete_link group %></td>
|
||||||
</tr>
|
</tr>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|||||||
@@ -30,6 +30,12 @@ class GroupsControllerTest < ActionController::TestCase
|
|||||||
assert_template 'index'
|
assert_template 'index'
|
||||||
end
|
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
|
def test_show
|
||||||
get :show, :id => 10
|
get :show, :id => 10
|
||||||
assert_response :success
|
assert_response :success
|
||||||
|
|||||||
Reference in New Issue
Block a user