##// END OF EJS Templates
Adds pagination to group list....
Jean-Philippe Lang -
r15374:58c3270a7d99
parent child
Show More
@@ -1,149 +1,154
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2016 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class GroupsController < ApplicationController
19 19 layout 'admin'
20 20
21 21 before_action :require_admin
22 22 before_action :find_group, :except => [:index, :new, :create]
23 23 accept_api_auth :index, :show, :create, :update, :destroy, :add_users, :remove_user
24 24
25 25 require_sudo_mode :add_users, :remove_user, :create, :update, :destroy, :edit_membership, :destroy_membership
26 26
27 27 helper :custom_fields
28 28 helper :principal_memberships
29 29
30 30 def index
31 31 respond_to do |format|
32 32 format.html {
33 @groups = Group.sorted.to_a
33 scope = Group.sorted
34 scope = scope.like(params[:name]) if params[:name].present?
35
36 @group_count = scope.count
37 @group_pages = Paginator.new @group_count, per_page_option, params['page']
38 @groups = scope.limit(@group_pages.per_page).offset(@group_pages.offset).to_a
34 39 @user_count_by_group_id = user_count_by_group_id
35 40 }
36 41 format.api {
37 42 scope = Group.sorted
38 43 scope = scope.givable unless params[:builtin] == '1'
39 44 @groups = scope.to_a
40 45 }
41 46 end
42 47 end
43 48
44 49 def show
45 50 respond_to do |format|
46 51 format.html
47 52 format.api
48 53 end
49 54 end
50 55
51 56 def new
52 57 @group = Group.new
53 58 end
54 59
55 60 def create
56 61 @group = Group.new
57 62 @group.safe_attributes = params[:group]
58 63
59 64 respond_to do |format|
60 65 if @group.save
61 66 format.html {
62 67 flash[:notice] = l(:notice_successful_create)
63 68 redirect_to(params[:continue] ? new_group_path : groups_path)
64 69 }
65 70 format.api { render :action => 'show', :status => :created, :location => group_url(@group) }
66 71 else
67 72 format.html { render :action => "new" }
68 73 format.api { render_validation_errors(@group) }
69 74 end
70 75 end
71 76 end
72 77
73 78 def edit
74 79 end
75 80
76 81 def update
77 82 @group.safe_attributes = params[:group]
78 83
79 84 respond_to do |format|
80 85 if @group.save
81 86 flash[:notice] = l(:notice_successful_update)
82 87 format.html { redirect_to(groups_path) }
83 88 format.api { render_api_ok }
84 89 else
85 90 format.html { render :action => "edit" }
86 91 format.api { render_validation_errors(@group) }
87 92 end
88 93 end
89 94 end
90 95
91 96 def destroy
92 97 @group.destroy
93 98
94 99 respond_to do |format|
95 100 format.html { redirect_to(groups_path) }
96 101 format.api { render_api_ok }
97 102 end
98 103 end
99 104
100 105 def new_users
101 106 end
102 107
103 108 def add_users
104 109 @users = User.not_in_group(@group).where(:id => (params[:user_id] || params[:user_ids])).to_a
105 110 @group.users << @users
106 111 respond_to do |format|
107 112 format.html { redirect_to edit_group_path(@group, :tab => 'users') }
108 113 format.js
109 114 format.api {
110 115 if @users.any?
111 116 render_api_ok
112 117 else
113 118 render_api_errors "#{l(:label_user)} #{l('activerecord.errors.messages.invalid')}"
114 119 end
115 120 }
116 121 end
117 122 end
118 123
119 124 def remove_user
120 125 @group.users.delete(User.find(params[:user_id])) if request.delete?
121 126 respond_to do |format|
122 127 format.html { redirect_to edit_group_path(@group, :tab => 'users') }
123 128 format.js
124 129 format.api { render_api_ok }
125 130 end
126 131 end
127 132
128 133 def autocomplete_for_user
129 134 respond_to do |format|
130 135 format.js
131 136 end
132 137 end
133 138
134 139 private
135 140
136 141 def find_group
137 142 @group = Group.find(params[:id])
138 143 rescue ActiveRecord::RecordNotFound
139 144 render_404
140 145 end
141 146
142 147 def user_count_by_group_id
143 148 h = User.joins(:groups).group('group_id').count
144 149 h.keys.each do |key|
145 150 h[key.to_i] = h.delete(key)
146 151 end
147 152 h
148 153 end
149 154 end
@@ -1,25 +1,28
1 1 <div class="contextual">
2 2 <%= link_to l(:label_group_new), new_group_path, :class => 'icon icon-add' %>
3 3 </div>
4 4
5 5 <%= title l(:label_group_plural) %>
6 6 <% if @groups.any? %>
7 <div class="autoscroll">
7 8 <table class="list groups">
8 9 <thead><tr>
9 10 <th><%=l(:label_group)%></th>
10 11 <th><%=l(:label_user_plural)%></th>
11 12 <th></th>
12 13 </tr></thead>
13 14 <tbody>
14 15 <% @groups.each do |group| %>
15 16 <tr id="group-<%= group.id %>" class="<%= cycle 'odd', 'even' %> <%= "builtin" if group.builtin? %>">
16 17 <td class="name"><%= link_to group, edit_group_path(group) %></td>
17 18 <td class="user_count"><%= (@user_count_by_group_id[group.id] || 0) unless group.builtin? %></td>
18 19 <td class="buttons"><%= delete_link group unless group.builtin? %></td>
19 20 </tr>
20 21 <% end %>
21 22 </tbody>
22 23 </table>
24 </div>
25 <span class="pagination"><%= pagination_links_full @group_pages, @group_count %></span>
23 26 <% else %>
24 27 <p class="nodata"><%= l(:label_no_data) %></p>
25 28 <% end %>
General Comments 0
You need to be logged in to leave comments. Login now