##// END OF EJS Templates
Improve custom fields list performance (#24587)....
Jean-Philippe Lang -
r15687:9d1d3b825f1d
parent child
Show More
@@ -1,100 +1,102
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 CustomFieldsController < ApplicationController
19 19 layout 'admin'
20 20 self.main_menu = false
21 21
22 22 before_action :require_admin
23 23 before_action :build_new_custom_field, :only => [:new, :create]
24 24 before_action :find_custom_field, :only => [:edit, :update, :destroy]
25 25 accept_api_auth :index
26 26
27 27 def index
28 28 respond_to do |format|
29 29 format.html {
30 30 @custom_fields_by_type = CustomField.all.group_by {|f| f.class.name }
31 @custom_fields_projects_count =
32 IssueCustomField.where(is_for_all: false).joins(:projects).group(:custom_field_id).count
31 33 }
32 34 format.api {
33 35 @custom_fields = CustomField.all
34 36 }
35 37 end
36 38 end
37 39
38 40 def new
39 41 @custom_field.field_format = 'string' if @custom_field.field_format.blank?
40 42 @custom_field.default_value = nil
41 43 end
42 44
43 45 def create
44 46 if @custom_field.save
45 47 flash[:notice] = l(:notice_successful_create)
46 48 call_hook(:controller_custom_fields_new_after_save, :params => params, :custom_field => @custom_field)
47 49 redirect_to edit_custom_field_path(@custom_field)
48 50 else
49 51 render :action => 'new'
50 52 end
51 53 end
52 54
53 55 def edit
54 56 end
55 57
56 58 def update
57 59 @custom_field.safe_attributes = params[:custom_field]
58 60 if @custom_field.save
59 61 call_hook(:controller_custom_fields_edit_after_save, :params => params, :custom_field => @custom_field)
60 62 respond_to do |format|
61 63 format.html {
62 64 flash[:notice] = l(:notice_successful_update)
63 65 redirect_back_or_default edit_custom_field_path(@custom_field)
64 66 }
65 67 format.js { head 200 }
66 68 end
67 69 else
68 70 respond_to do |format|
69 71 format.html { render :action => 'edit' }
70 72 format.js { head 422 }
71 73 end
72 74 end
73 75 end
74 76
75 77 def destroy
76 78 begin
77 79 @custom_field.destroy
78 80 rescue
79 81 flash[:error] = l(:error_can_not_delete_custom_field)
80 82 end
81 83 redirect_to custom_fields_path(:tab => @custom_field.class.name)
82 84 end
83 85
84 86 private
85 87
86 88 def build_new_custom_field
87 89 @custom_field = CustomField.new_subclass_instance(params[:type])
88 90 if @custom_field.nil?
89 91 render :action => 'select_type'
90 92 else
91 93 @custom_field.safe_attributes = params[:custom_field]
92 94 end
93 95 end
94 96
95 97 def find_custom_field
96 98 @custom_field = CustomField.find(params[:id])
97 99 rescue ActiveRecord::RecordNotFound
98 100 render_404
99 101 end
100 102 end
@@ -1,30 +1,30
1 1 <table class="list custom_fields">
2 2 <thead><tr>
3 3 <th><%=l(:field_name)%></th>
4 4 <th><%=l(:field_field_format)%></th>
5 5 <th><%=l(:field_is_required)%></th>
6 6 <% if tab[:name] == 'IssueCustomField' %>
7 7 <th><%=l(:field_is_for_all)%></th>
8 8 <th><%=l(:label_used_by)%></th>
9 9 <% end %>
10 10 <th></th>
11 11 </tr></thead>
12 12 <tbody>
13 13 <% (@custom_fields_by_type[tab[:name]] || []).sort.each do |custom_field| -%>
14 14 <% back_url = custom_fields_path(:tab => tab[:name]) %>
15 15 <tr class="<%= cycle("odd", "even") %>">
16 16 <td class="name"><%= link_to custom_field.name, edit_custom_field_path(custom_field) %></td>
17 17 <td><%= l(custom_field.format.label) %></td>
18 18 <td><%= checked_image custom_field.is_required? %></td>
19 19 <% if tab[:name] == 'IssueCustomField' %>
20 20 <td><%= checked_image custom_field.is_for_all? %></td>
21 <td><%= l(:label_x_projects, :count => custom_field.projects.count) if custom_field.is_a? IssueCustomField and !custom_field.is_for_all? %></td>
21 <td><%= l(:label_x_projects, :count => @custom_fields_projects_count[custom_field.id]) if custom_field.is_a? IssueCustomField and !custom_field.is_for_all? %></td>
22 22 <% end %>
23 23 <td class="buttons">
24 24 <%= reorder_handle(custom_field, :url => custom_field_path(custom_field), :param => 'custom_field') %>
25 25 <%= delete_link custom_field_path(custom_field) %>
26 26 </td>
27 27 </tr>
28 28 <% end; reset_cycle %>
29 29 </tbody>
30 30 </table>
General Comments 0
You need to be logged in to leave comments. Login now