##// END OF EJS Templates
Redirect to custom field after create or update....
Jean-Philippe Lang -
r14364:a3efaceb8fd9
parent child
Show More
@@ -1,88 +1,88
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2015 Jean-Philippe Lang
2 # Copyright (C) 2006-2015 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 CustomFieldsController < ApplicationController
18 class CustomFieldsController < ApplicationController
19 layout 'admin'
19 layout 'admin'
20
20
21 before_filter :require_admin
21 before_filter :require_admin
22 before_filter :build_new_custom_field, :only => [:new, :create]
22 before_filter :build_new_custom_field, :only => [:new, :create]
23 before_filter :find_custom_field, :only => [:edit, :update, :destroy]
23 before_filter :find_custom_field, :only => [:edit, :update, :destroy]
24 accept_api_auth :index
24 accept_api_auth :index
25
25
26 def index
26 def index
27 respond_to do |format|
27 respond_to do |format|
28 format.html {
28 format.html {
29 @custom_fields_by_type = CustomField.all.group_by {|f| f.class.name }
29 @custom_fields_by_type = CustomField.all.group_by {|f| f.class.name }
30 }
30 }
31 format.api {
31 format.api {
32 @custom_fields = CustomField.all
32 @custom_fields = CustomField.all
33 }
33 }
34 end
34 end
35 end
35 end
36
36
37 def new
37 def new
38 @custom_field.field_format = 'string' if @custom_field.field_format.blank?
38 @custom_field.field_format = 'string' if @custom_field.field_format.blank?
39 @custom_field.default_value = nil
39 @custom_field.default_value = nil
40 end
40 end
41
41
42 def create
42 def create
43 if @custom_field.save
43 if @custom_field.save
44 flash[:notice] = l(:notice_successful_create)
44 flash[:notice] = l(:notice_successful_create)
45 call_hook(:controller_custom_fields_new_after_save, :params => params, :custom_field => @custom_field)
45 call_hook(:controller_custom_fields_new_after_save, :params => params, :custom_field => @custom_field)
46 redirect_to custom_fields_path(:tab => @custom_field.class.name)
46 redirect_to edit_custom_field_path(@custom_field)
47 else
47 else
48 render :action => 'new'
48 render :action => 'new'
49 end
49 end
50 end
50 end
51
51
52 def edit
52 def edit
53 end
53 end
54
54
55 def update
55 def update
56 if @custom_field.update_attributes(params[:custom_field])
56 if @custom_field.update_attributes(params[:custom_field])
57 flash[:notice] = l(:notice_successful_update)
57 flash[:notice] = l(:notice_successful_update)
58 call_hook(:controller_custom_fields_edit_after_save, :params => params, :custom_field => @custom_field)
58 call_hook(:controller_custom_fields_edit_after_save, :params => params, :custom_field => @custom_field)
59 redirect_to custom_fields_path(:tab => @custom_field.class.name)
59 redirect_to edit_custom_field_path(@custom_field)
60 else
60 else
61 render :action => 'edit'
61 render :action => 'edit'
62 end
62 end
63 end
63 end
64
64
65 def destroy
65 def destroy
66 begin
66 begin
67 @custom_field.destroy
67 @custom_field.destroy
68 rescue
68 rescue
69 flash[:error] = l(:error_can_not_delete_custom_field)
69 flash[:error] = l(:error_can_not_delete_custom_field)
70 end
70 end
71 redirect_to custom_fields_path(:tab => @custom_field.class.name)
71 redirect_to custom_fields_path(:tab => @custom_field.class.name)
72 end
72 end
73
73
74 private
74 private
75
75
76 def build_new_custom_field
76 def build_new_custom_field
77 @custom_field = CustomField.new_subclass_instance(params[:type], params[:custom_field])
77 @custom_field = CustomField.new_subclass_instance(params[:type], params[:custom_field])
78 if @custom_field.nil?
78 if @custom_field.nil?
79 render :action => 'select_type'
79 render :action => 'select_type'
80 end
80 end
81 end
81 end
82
82
83 def find_custom_field
83 def find_custom_field
84 @custom_field = CustomField.find(params[:id])
84 @custom_field = CustomField.find(params[:id])
85 rescue ActiveRecord::RecordNotFound
85 rescue ActiveRecord::RecordNotFound
86 render_404
86 render_404
87 end
87 end
88 end
88 end
@@ -1,238 +1,237
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2015 Jean-Philippe Lang
2 # Copyright (C) 2006-2015 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 require File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19
19
20 class CustomFieldsControllerTest < ActionController::TestCase
20 class CustomFieldsControllerTest < ActionController::TestCase
21 fixtures :custom_fields, :custom_values,
21 fixtures :custom_fields, :custom_values,
22 :custom_fields_projects, :custom_fields_trackers,
22 :custom_fields_projects, :custom_fields_trackers,
23 :roles, :users,
23 :roles, :users,
24 :members, :member_roles,
24 :members, :member_roles,
25 :groups_users,
25 :groups_users,
26 :trackers, :projects_trackers,
26 :trackers, :projects_trackers,
27 :enabled_modules,
27 :enabled_modules,
28 :projects, :issues,
28 :projects, :issues,
29 :issue_statuses,
29 :issue_statuses,
30 :issue_categories,
30 :issue_categories,
31 :enumerations,
31 :enumerations,
32 :workflows
32 :workflows
33
33
34 def setup
34 def setup
35 @request.session[:user_id] = 1
35 @request.session[:user_id] = 1
36 end
36 end
37
37
38 def test_index
38 def test_index
39 get :index
39 get :index
40 assert_response :success
40 assert_response :success
41 assert_template 'index'
41 assert_template 'index'
42 end
42 end
43
43
44 def test_new_without_type_should_render_select_type
44 def test_new_without_type_should_render_select_type
45 get :new
45 get :new
46 assert_response :success
46 assert_response :success
47 assert_template 'select_type'
47 assert_template 'select_type'
48 assert_select 'input[name=type]', CustomFieldsHelper::CUSTOM_FIELDS_TABS.size
48 assert_select 'input[name=type]', CustomFieldsHelper::CUSTOM_FIELDS_TABS.size
49 assert_select 'input[name=type][checked=checked]', 1
49 assert_select 'input[name=type][checked=checked]', 1
50 end
50 end
51
51
52 def test_new_should_work_for_each_customized_class_and_format
52 def test_new_should_work_for_each_customized_class_and_format
53 custom_field_classes.each do |klass|
53 custom_field_classes.each do |klass|
54 Redmine::FieldFormat.available_formats.each do |format_name|
54 Redmine::FieldFormat.available_formats.each do |format_name|
55 get :new, :type => klass.name, :custom_field => {:field_format => format_name}
55 get :new, :type => klass.name, :custom_field => {:field_format => format_name}
56 assert_response :success
56 assert_response :success
57 assert_template 'new'
57 assert_template 'new'
58 assert_kind_of klass, assigns(:custom_field)
58 assert_kind_of klass, assigns(:custom_field)
59 assert_equal format_name, assigns(:custom_field).format.name
59 assert_equal format_name, assigns(:custom_field).format.name
60 assert_select 'form#custom_field_form' do
60 assert_select 'form#custom_field_form' do
61 assert_select 'select#custom_field_field_format[name=?]', 'custom_field[field_format]'
61 assert_select 'select#custom_field_field_format[name=?]', 'custom_field[field_format]'
62 assert_select 'input[type=hidden][name=type][value=?]', klass.name
62 assert_select 'input[type=hidden][name=type][value=?]', klass.name
63 end
63 end
64 end
64 end
65 end
65 end
66 end
66 end
67
67
68 def test_new_should_have_string_default_format
68 def test_new_should_have_string_default_format
69 get :new, :type => 'IssueCustomField'
69 get :new, :type => 'IssueCustomField'
70 assert_response :success
70 assert_response :success
71 assert_equal 'string', assigns(:custom_field).format.name
71 assert_equal 'string', assigns(:custom_field).format.name
72 end
72 end
73
73
74 def test_new_issue_custom_field
74 def test_new_issue_custom_field
75 get :new, :type => 'IssueCustomField'
75 get :new, :type => 'IssueCustomField'
76 assert_response :success
76 assert_response :success
77 assert_template 'new'
77 assert_template 'new'
78 assert_select 'form#custom_field_form' do
78 assert_select 'form#custom_field_form' do
79 assert_select 'select#custom_field_field_format[name=?]', 'custom_field[field_format]' do
79 assert_select 'select#custom_field_field_format[name=?]', 'custom_field[field_format]' do
80 assert_select 'option[value=user]', :text => 'User'
80 assert_select 'option[value=user]', :text => 'User'
81 assert_select 'option[value=version]', :text => 'Version'
81 assert_select 'option[value=version]', :text => 'Version'
82 end
82 end
83 assert_select 'input[type=checkbox][name=?]', 'custom_field[project_ids][]', Project.count
83 assert_select 'input[type=checkbox][name=?]', 'custom_field[project_ids][]', Project.count
84 assert_select 'input[type=hidden][name=?]', 'custom_field[project_ids][]', 1
84 assert_select 'input[type=hidden][name=?]', 'custom_field[project_ids][]', 1
85 assert_select 'input[type=hidden][name=type][value=IssueCustomField]'
85 assert_select 'input[type=hidden][name=type][value=IssueCustomField]'
86 end
86 end
87 end
87 end
88
88
89 def test_new_time_entry_custom_field_should_not_show_trackers_and_projects
89 def test_new_time_entry_custom_field_should_not_show_trackers_and_projects
90 get :new, :type => 'TimeEntryCustomField'
90 get :new, :type => 'TimeEntryCustomField'
91 assert_response :success
91 assert_response :success
92 assert_template 'new'
92 assert_template 'new'
93 assert_select 'form#custom_field_form' do
93 assert_select 'form#custom_field_form' do
94 assert_select 'input[name=?]', 'custom_field[tracker_ids][]', 0
94 assert_select 'input[name=?]', 'custom_field[tracker_ids][]', 0
95 assert_select 'input[name=?]', 'custom_field[project_ids][]', 0
95 assert_select 'input[name=?]', 'custom_field[project_ids][]', 0
96 end
96 end
97 end
97 end
98
98
99 def test_default_value_should_be_an_input_for_string_custom_field
99 def test_default_value_should_be_an_input_for_string_custom_field
100 get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'string'}
100 get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'string'}
101 assert_response :success
101 assert_response :success
102 assert_select 'input[name=?]', 'custom_field[default_value]'
102 assert_select 'input[name=?]', 'custom_field[default_value]'
103 end
103 end
104
104
105 def test_default_value_should_be_a_textarea_for_text_custom_field
105 def test_default_value_should_be_a_textarea_for_text_custom_field
106 get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'text'}
106 get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'text'}
107 assert_response :success
107 assert_response :success
108 assert_select 'textarea[name=?]', 'custom_field[default_value]'
108 assert_select 'textarea[name=?]', 'custom_field[default_value]'
109 end
109 end
110
110
111 def test_default_value_should_be_a_checkbox_for_bool_custom_field
111 def test_default_value_should_be_a_checkbox_for_bool_custom_field
112 get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'bool'}
112 get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'bool'}
113 assert_response :success
113 assert_response :success
114 assert_select 'select[name=?]', 'custom_field[default_value]' do
114 assert_select 'select[name=?]', 'custom_field[default_value]' do
115 assert_select 'option', 3
115 assert_select 'option', 3
116 end
116 end
117 end
117 end
118
118
119 def test_default_value_should_not_be_present_for_user_custom_field
119 def test_default_value_should_not_be_present_for_user_custom_field
120 get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'user'}
120 get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'user'}
121 assert_response :success
121 assert_response :success
122 assert_select '[name=?]', 'custom_field[default_value]', 0
122 assert_select '[name=?]', 'custom_field[default_value]', 0
123 end
123 end
124
124
125 def test_new_js
125 def test_new_js
126 xhr :get, :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'list'}, :format => 'js'
126 xhr :get, :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'list'}, :format => 'js'
127 assert_response :success
127 assert_response :success
128 assert_template 'new'
128 assert_template 'new'
129 assert_equal 'text/javascript', response.content_type
129 assert_equal 'text/javascript', response.content_type
130
130
131 field = assigns(:custom_field)
131 field = assigns(:custom_field)
132 assert_equal 'list', field.field_format
132 assert_equal 'list', field.field_format
133 end
133 end
134
134
135 def test_new_with_invalid_custom_field_class_should_render_select_type
135 def test_new_with_invalid_custom_field_class_should_render_select_type
136 get :new, :type => 'UnknownCustomField'
136 get :new, :type => 'UnknownCustomField'
137 assert_response :success
137 assert_response :success
138 assert_template 'select_type'
138 assert_template 'select_type'
139 end
139 end
140
140
141 def test_create_list_custom_field
141 def test_create_list_custom_field
142 assert_difference 'CustomField.count' do
142 field = new_record(IssueCustomField) do
143 post :create, :type => "IssueCustomField",
143 post :create, :type => "IssueCustomField",
144 :custom_field => {:name => "test_post_new_list",
144 :custom_field => {:name => "test_post_new_list",
145 :default_value => "",
145 :default_value => "",
146 :min_length => "0",
146 :min_length => "0",
147 :searchable => "0",
147 :searchable => "0",
148 :regexp => "",
148 :regexp => "",
149 :is_for_all => "1",
149 :is_for_all => "1",
150 :possible_values => "0.1\n0.2\n",
150 :possible_values => "0.1\n0.2\n",
151 :max_length => "0",
151 :max_length => "0",
152 :is_filter => "0",
152 :is_filter => "0",
153 :is_required =>"0",
153 :is_required =>"0",
154 :field_format => "list",
154 :field_format => "list",
155 :tracker_ids => ["1", ""]}
155 :tracker_ids => ["1", ""]}
156 end
156 end
157 assert_redirected_to '/custom_fields?tab=IssueCustomField'
157 assert_redirected_to "/custom_fields/#{field.id}/edit"
158 field = IssueCustomField.find_by_name('test_post_new_list')
158 assert_equal "test_post_new_list", field.name
159 assert_not_nil field
160 assert_equal ["0.1", "0.2"], field.possible_values
159 assert_equal ["0.1", "0.2"], field.possible_values
161 assert_equal 1, field.trackers.size
160 assert_equal 1, field.trackers.size
162 end
161 end
163
162
164 def test_create_with_project_ids
163 def test_create_with_project_ids
165 assert_difference 'CustomField.count' do
164 assert_difference 'CustomField.count' do
166 post :create, :type => "IssueCustomField", :custom_field => {
165 post :create, :type => "IssueCustomField", :custom_field => {
167 :name => "foo", :field_format => "string", :is_for_all => "0", :project_ids => ["1", "3", ""]
166 :name => "foo", :field_format => "string", :is_for_all => "0", :project_ids => ["1", "3", ""]
168 }
167 }
169 assert_response 302
168 assert_response 302
170 end
169 end
171 field = IssueCustomField.order("id desc").first
170 field = IssueCustomField.order("id desc").first
172 assert_equal [1, 3], field.projects.map(&:id).sort
171 assert_equal [1, 3], field.projects.map(&:id).sort
173 end
172 end
174
173
175 def test_create_with_failure
174 def test_create_with_failure
176 assert_no_difference 'CustomField.count' do
175 assert_no_difference 'CustomField.count' do
177 post :create, :type => "IssueCustomField", :custom_field => {:name => ''}
176 post :create, :type => "IssueCustomField", :custom_field => {:name => ''}
178 end
177 end
179 assert_response :success
178 assert_response :success
180 assert_template 'new'
179 assert_template 'new'
181 end
180 end
182
181
183 def test_create_without_type_should_render_select_type
182 def test_create_without_type_should_render_select_type
184 assert_no_difference 'CustomField.count' do
183 assert_no_difference 'CustomField.count' do
185 post :create, :custom_field => {:name => ''}
184 post :create, :custom_field => {:name => ''}
186 end
185 end
187 assert_response :success
186 assert_response :success
188 assert_template 'select_type'
187 assert_template 'select_type'
189 end
188 end
190
189
191 def test_edit
190 def test_edit
192 get :edit, :id => 1
191 get :edit, :id => 1
193 assert_response :success
192 assert_response :success
194 assert_template 'edit'
193 assert_template 'edit'
195 assert_select 'input[name=?][value=?]', 'custom_field[name]', 'Database'
194 assert_select 'input[name=?][value=?]', 'custom_field[name]', 'Database'
196 end
195 end
197
196
198 def test_edit_invalid_custom_field_should_render_404
197 def test_edit_invalid_custom_field_should_render_404
199 get :edit, :id => 99
198 get :edit, :id => 99
200 assert_response 404
199 assert_response 404
201 end
200 end
202
201
203 def test_update
202 def test_update
204 put :update, :id => 1, :custom_field => {:name => 'New name'}
203 put :update, :id => 1, :custom_field => {:name => 'New name'}
205 assert_redirected_to '/custom_fields?tab=IssueCustomField'
204 assert_redirected_to '/custom_fields/1/edit'
206
205
207 field = CustomField.find(1)
206 field = CustomField.find(1)
208 assert_equal 'New name', field.name
207 assert_equal 'New name', field.name
209 end
208 end
210
209
211 def test_update_with_failure
210 def test_update_with_failure
212 put :update, :id => 1, :custom_field => {:name => ''}
211 put :update, :id => 1, :custom_field => {:name => ''}
213 assert_response :success
212 assert_response :success
214 assert_template 'edit'
213 assert_template 'edit'
215 end
214 end
216
215
217 def test_destroy
216 def test_destroy
218 custom_values_count = CustomValue.where(:custom_field_id => 1).count
217 custom_values_count = CustomValue.where(:custom_field_id => 1).count
219 assert custom_values_count > 0
218 assert custom_values_count > 0
220
219
221 assert_difference 'CustomField.count', -1 do
220 assert_difference 'CustomField.count', -1 do
222 assert_difference 'CustomValue.count', - custom_values_count do
221 assert_difference 'CustomValue.count', - custom_values_count do
223 delete :destroy, :id => 1
222 delete :destroy, :id => 1
224 end
223 end
225 end
224 end
226
225
227 assert_redirected_to '/custom_fields?tab=IssueCustomField'
226 assert_redirected_to '/custom_fields?tab=IssueCustomField'
228 assert_nil CustomField.find_by_id(1)
227 assert_nil CustomField.find_by_id(1)
229 assert_nil CustomValue.find_by_custom_field_id(1)
228 assert_nil CustomValue.find_by_custom_field_id(1)
230 end
229 end
231
230
232 def custom_field_classes
231 def custom_field_classes
233 files = Dir.glob(File.join(Rails.root, 'app/models/*_custom_field.rb')).map {|f| File.basename(f).sub(/\.rb$/, '') }
232 files = Dir.glob(File.join(Rails.root, 'app/models/*_custom_field.rb')).map {|f| File.basename(f).sub(/\.rb$/, '') }
234 classes = files.map(&:classify).map(&:constantize)
233 classes = files.map(&:classify).map(&:constantize)
235 assert classes.size > 0
234 assert classes.size > 0
236 classes
235 classes
237 end
236 end
238 end
237 end
General Comments 0
You need to be logged in to leave comments. Login now