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