##// END OF EJS Templates
Count users with a single query on group list (#16905)....
Jean-Philippe Lang -
r12874:8aeab43247f9
parent child
Show More
@@ -26,9 +26,10 class GroupsController < ApplicationController
26
26
27 def index
27 def index
28 @groups = Group.sorted.all
28 @groups = Group.sorted.all
29
30 respond_to do |format|
29 respond_to do |format|
31 format.html
30 format.html {
31 @user_count_by_group_id = user_count_by_group_id
32 }
32 format.api
33 format.api
33 end
34 end
34 end
35 end
@@ -138,4 +139,12 class GroupsController < ApplicationController
138 rescue ActiveRecord::RecordNotFound
139 rescue ActiveRecord::RecordNotFound
139 render_404
140 render_404
140 end
141 end
142
143 def user_count_by_group_id
144 h = User.joins(:groups).group('group_id').count
145 h.keys.each do |key|
146 h[key.to_i] = h.delete(key)
147 end
148 h
149 end
141 end
150 end
@@ -3,7 +3,6
3 </div>
3 </div>
4
4
5 <%= title l(:label_group_plural) %>
5 <%= title l(:label_group_plural) %>
6
7 <% if @groups.any? %>
6 <% if @groups.any? %>
8 <table class="list groups">
7 <table class="list groups">
9 <thead><tr>
8 <thead><tr>
@@ -13,9 +12,9
13 </tr></thead>
12 </tr></thead>
14 <tbody>
13 <tbody>
15 <% @groups.each do |group| %>
14 <% @groups.each do |group| %>
16 <tr class="<%= cycle 'odd', 'even' %>">
15 <tr id="group-<%= group.id %>" class="<%= cycle 'odd', 'even' %>">
17 <td class="name"><%= link_to h(group), edit_group_path(group) %></td>
16 <td class="name"><%= link_to h(group), edit_group_path(group) %></td>
18 <td><%= group.users.size %></td>
17 <td class="user_count"><%= @user_count_by_group_id[group.id] || 0 %></td>
19 <td class="buttons"><%= delete_link group %></td>
18 <td class="buttons"><%= delete_link group %></td>
20 </tr>
19 </tr>
21 <% end %>
20 <% end %>
@@ -30,6 +30,12 class GroupsControllerTest < ActionController::TestCase
30 assert_template 'index'
30 assert_template 'index'
31 end
31 end
32
32
33 def test_index_should_show_user_count
34 get :index
35 assert_response :success
36 assert_select 'tr#group-11 td.user_count', :text => '1'
37 end
38
33 def test_show
39 def test_show
34 get :show, :id => 10
40 get :show, :id => 10
35 assert_response :success
41 assert_response :success
General Comments 0
You need to be logged in to leave comments. Login now