@@ -1,98 +1,96 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2013 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2013 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 EnumerationsController < ApplicationController |
|
18 | class EnumerationsController < ApplicationController | |
19 | layout 'admin' |
|
19 | layout 'admin' | |
20 |
|
20 | |||
21 | before_filter :require_admin, :except => :index |
|
21 | before_filter :require_admin, :except => :index | |
22 | before_filter :require_admin_or_api_request, :only => :index |
|
22 | before_filter :require_admin_or_api_request, :only => :index | |
23 | before_filter :build_new_enumeration, :only => [:new, :create] |
|
23 | before_filter :build_new_enumeration, :only => [:new, :create] | |
24 | before_filter :find_enumeration, :only => [:edit, :update, :destroy] |
|
24 | before_filter :find_enumeration, :only => [:edit, :update, :destroy] | |
25 | accept_api_auth :index |
|
25 | accept_api_auth :index | |
26 |
|
26 | |||
27 | helper :custom_fields |
|
27 | helper :custom_fields | |
28 |
|
28 | |||
29 | def index |
|
29 | def index | |
30 | respond_to do |format| |
|
30 | respond_to do |format| | |
31 | format.html |
|
31 | format.html | |
32 | format.api { |
|
32 | format.api { | |
33 | @klass = Enumeration.get_subclass(params[:type]) |
|
33 | @klass = Enumeration.get_subclass(params[:type]) | |
34 | if @klass |
|
34 | if @klass | |
35 | @enumerations = @klass.shared.sorted.all |
|
35 | @enumerations = @klass.shared.sorted.all | |
36 | else |
|
36 | else | |
37 | render_404 |
|
37 | render_404 | |
38 | end |
|
38 | end | |
39 | } |
|
39 | } | |
40 | end |
|
40 | end | |
41 | end |
|
41 | end | |
42 |
|
42 | |||
43 | def new |
|
43 | def new | |
44 | end |
|
44 | end | |
45 |
|
45 | |||
46 | def create |
|
46 | def create | |
47 | if request.post? && @enumeration.save |
|
47 | if request.post? && @enumeration.save | |
48 | flash[:notice] = l(:notice_successful_create) |
|
48 | flash[:notice] = l(:notice_successful_create) | |
49 | redirect_to enumerations_path |
|
49 | redirect_to enumerations_path | |
50 | else |
|
50 | else | |
51 | render :action => 'new' |
|
51 | render :action => 'new' | |
52 | end |
|
52 | end | |
53 | end |
|
53 | end | |
54 |
|
54 | |||
55 | def edit |
|
55 | def edit | |
56 | end |
|
56 | end | |
57 |
|
57 | |||
58 | def update |
|
58 | def update | |
59 | if request.put? && @enumeration.update_attributes(params[:enumeration]) |
|
59 | if request.put? && @enumeration.update_attributes(params[:enumeration]) | |
60 | flash[:notice] = l(:notice_successful_update) |
|
60 | flash[:notice] = l(:notice_successful_update) | |
61 | redirect_to enumerations_path |
|
61 | redirect_to enumerations_path | |
62 | else |
|
62 | else | |
63 | render :action => 'edit' |
|
63 | render :action => 'edit' | |
64 | end |
|
64 | end | |
65 | end |
|
65 | end | |
66 |
|
66 | |||
67 | def destroy |
|
67 | def destroy | |
68 | if !@enumeration.in_use? |
|
68 | if !@enumeration.in_use? | |
69 | # No associated objects |
|
69 | # No associated objects | |
70 | @enumeration.destroy |
|
70 | @enumeration.destroy | |
71 | redirect_to enumerations_path |
|
71 | redirect_to enumerations_path | |
72 | return |
|
72 | return | |
73 | elsif params[:reassign_to_id] |
|
73 | elsif params[:reassign_to_id].present? && (reassign_to = @enumeration.class.find_by_id(params[:reassign_to_id].to_i)) | |
74 | if reassign_to = @enumeration.class.find_by_id(params[:reassign_to_id]) |
|
74 | @enumeration.destroy(reassign_to) | |
75 | @enumeration.destroy(reassign_to) |
|
75 | redirect_to enumerations_path | |
76 | redirect_to enumerations_path |
|
76 | return | |
77 | return |
|
|||
78 | end |
|
|||
79 | end |
|
77 | end | |
80 | @enumerations = @enumeration.class.system.all - [@enumeration] |
|
78 | @enumerations = @enumeration.class.system.all - [@enumeration] | |
81 | end |
|
79 | end | |
82 |
|
80 | |||
83 | private |
|
81 | private | |
84 |
|
82 | |||
85 | def build_new_enumeration |
|
83 | def build_new_enumeration | |
86 | class_name = params[:enumeration] && params[:enumeration][:type] || params[:type] |
|
84 | class_name = params[:enumeration] && params[:enumeration][:type] || params[:type] | |
87 | @enumeration = Enumeration.new_subclass_instance(class_name, params[:enumeration]) |
|
85 | @enumeration = Enumeration.new_subclass_instance(class_name, params[:enumeration]) | |
88 | if @enumeration.nil? |
|
86 | if @enumeration.nil? | |
89 | render_404 |
|
87 | render_404 | |
90 | end |
|
88 | end | |
91 | end |
|
89 | end | |
92 |
|
90 | |||
93 | def find_enumeration |
|
91 | def find_enumeration | |
94 | @enumeration = Enumeration.find(params[:id]) |
|
92 | @enumeration = Enumeration.find(params[:id]) | |
95 | rescue ActiveRecord::RecordNotFound |
|
93 | rescue ActiveRecord::RecordNotFound | |
96 | render_404 |
|
94 | render_404 | |
97 | end |
|
95 | end | |
98 | end |
|
96 | end |
@@ -1,12 +1,12 | |||||
1 | <h2><%= l(@enumeration.option_name) %>: <%=h @enumeration %></h2> |
|
1 | <h2><%= l(@enumeration.option_name) %>: <%=h @enumeration %></h2> | |
2 |
|
2 | |||
3 | <%= form_tag({}, :method => :delete) do %> |
|
3 | <%= form_tag({}, :method => :delete) do %> | |
4 | <div class="box"> |
|
4 | <div class="box"> | |
5 | <p><strong><%= l(:text_enumeration_destroy_question, @enumeration.objects_count) %></strong></p> |
|
5 | <p><strong><%= l(:text_enumeration_destroy_question, @enumeration.objects_count) %></strong></p> | |
6 | <p><label for='reassign_to_id'><%= l(:text_enumeration_category_reassign_to) %></label> |
|
6 | <p><label for='reassign_to_id'><%= l(:text_enumeration_category_reassign_to) %></label> | |
7 | <%= select_tag 'reassign_to_id', (content_tag('option', "--- #{l(:actionview_instancetag_blank_option)} ---") + options_from_collection_for_select(@enumerations, 'id', 'name')) %></p> |
|
7 | <%= select_tag 'reassign_to_id', (content_tag('option', "--- #{l(:actionview_instancetag_blank_option)} ---", :value => '') + options_from_collection_for_select(@enumerations, 'id', 'name')) %></p> | |
8 | </div> |
|
8 | </div> | |
9 |
|
9 | |||
10 | <%= submit_tag l(:button_apply) %> |
|
10 | <%= submit_tag l(:button_apply) %> | |
11 | <%= link_to l(:button_cancel), enumerations_path %> |
|
11 | <%= link_to l(:button_cancel), enumerations_path %> | |
12 | <% end %> |
|
12 | <% end %> |
@@ -1,129 +1,136 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2013 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2013 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 EnumerationsControllerTest < ActionController::TestCase |
|
20 | class EnumerationsControllerTest < ActionController::TestCase | |
21 | fixtures :enumerations, :issues, :users |
|
21 | fixtures :enumerations, :issues, :users | |
22 |
|
22 | |||
23 | def setup |
|
23 | def setup | |
24 | @request.session[:user_id] = 1 # admin |
|
24 | @request.session[:user_id] = 1 # admin | |
25 | end |
|
25 | end | |
26 |
|
26 | |||
27 | def test_index |
|
27 | def test_index | |
28 | get :index |
|
28 | get :index | |
29 | assert_response :success |
|
29 | assert_response :success | |
30 | assert_template 'index' |
|
30 | assert_template 'index' | |
31 | end |
|
31 | end | |
32 |
|
32 | |||
33 | def test_index_should_require_admin |
|
33 | def test_index_should_require_admin | |
34 | @request.session[:user_id] = nil |
|
34 | @request.session[:user_id] = nil | |
35 | get :index |
|
35 | get :index | |
36 | assert_response 302 |
|
36 | assert_response 302 | |
37 | end |
|
37 | end | |
38 |
|
38 | |||
39 | def test_new |
|
39 | def test_new | |
40 | get :new, :type => 'IssuePriority' |
|
40 | get :new, :type => 'IssuePriority' | |
41 | assert_response :success |
|
41 | assert_response :success | |
42 | assert_template 'new' |
|
42 | assert_template 'new' | |
43 | assert_kind_of IssuePriority, assigns(:enumeration) |
|
43 | assert_kind_of IssuePriority, assigns(:enumeration) | |
44 | assert_tag 'input', :attributes => {:name => 'enumeration[type]', :value => 'IssuePriority'} |
|
44 | assert_tag 'input', :attributes => {:name => 'enumeration[type]', :value => 'IssuePriority'} | |
45 | assert_tag 'input', :attributes => {:name => 'enumeration[name]'} |
|
45 | assert_tag 'input', :attributes => {:name => 'enumeration[name]'} | |
46 | end |
|
46 | end | |
47 |
|
47 | |||
48 | def test_new_with_invalid_type_should_respond_with_404 |
|
48 | def test_new_with_invalid_type_should_respond_with_404 | |
49 | get :new, :type => 'UnknownType' |
|
49 | get :new, :type => 'UnknownType' | |
50 | assert_response 404 |
|
50 | assert_response 404 | |
51 | end |
|
51 | end | |
52 |
|
52 | |||
53 | def test_create |
|
53 | def test_create | |
54 | assert_difference 'IssuePriority.count' do |
|
54 | assert_difference 'IssuePriority.count' do | |
55 | post :create, :enumeration => {:type => 'IssuePriority', :name => 'Lowest'} |
|
55 | post :create, :enumeration => {:type => 'IssuePriority', :name => 'Lowest'} | |
56 | end |
|
56 | end | |
57 | assert_redirected_to '/enumerations' |
|
57 | assert_redirected_to '/enumerations' | |
58 | e = IssuePriority.find_by_name('Lowest') |
|
58 | e = IssuePriority.find_by_name('Lowest') | |
59 | assert_not_nil e |
|
59 | assert_not_nil e | |
60 | end |
|
60 | end | |
61 |
|
61 | |||
62 | def test_create_with_failure |
|
62 | def test_create_with_failure | |
63 | assert_no_difference 'IssuePriority.count' do |
|
63 | assert_no_difference 'IssuePriority.count' do | |
64 | post :create, :enumeration => {:type => 'IssuePriority', :name => ''} |
|
64 | post :create, :enumeration => {:type => 'IssuePriority', :name => ''} | |
65 | end |
|
65 | end | |
66 | assert_response :success |
|
66 | assert_response :success | |
67 | assert_template 'new' |
|
67 | assert_template 'new' | |
68 | end |
|
68 | end | |
69 |
|
69 | |||
70 | def test_edit |
|
70 | def test_edit | |
71 | get :edit, :id => 6 |
|
71 | get :edit, :id => 6 | |
72 | assert_response :success |
|
72 | assert_response :success | |
73 | assert_template 'edit' |
|
73 | assert_template 'edit' | |
74 | assert_tag 'input', :attributes => {:name => 'enumeration[name]', :value => 'High'} |
|
74 | assert_tag 'input', :attributes => {:name => 'enumeration[name]', :value => 'High'} | |
75 | end |
|
75 | end | |
76 |
|
76 | |||
77 | def test_edit_invalid_should_respond_with_404 |
|
77 | def test_edit_invalid_should_respond_with_404 | |
78 | get :edit, :id => 999 |
|
78 | get :edit, :id => 999 | |
79 | assert_response 404 |
|
79 | assert_response 404 | |
80 | end |
|
80 | end | |
81 |
|
81 | |||
82 | def test_update |
|
82 | def test_update | |
83 | assert_no_difference 'IssuePriority.count' do |
|
83 | assert_no_difference 'IssuePriority.count' do | |
84 | put :update, :id => 6, :enumeration => {:type => 'IssuePriority', :name => 'New name'} |
|
84 | put :update, :id => 6, :enumeration => {:type => 'IssuePriority', :name => 'New name'} | |
85 | end |
|
85 | end | |
86 | assert_redirected_to '/enumerations' |
|
86 | assert_redirected_to '/enumerations' | |
87 | e = IssuePriority.find(6) |
|
87 | e = IssuePriority.find(6) | |
88 | assert_equal 'New name', e.name |
|
88 | assert_equal 'New name', e.name | |
89 | end |
|
89 | end | |
90 |
|
90 | |||
91 | def test_update_with_failure |
|
91 | def test_update_with_failure | |
92 | assert_no_difference 'IssuePriority.count' do |
|
92 | assert_no_difference 'IssuePriority.count' do | |
93 | put :update, :id => 6, :enumeration => {:type => 'IssuePriority', :name => ''} |
|
93 | put :update, :id => 6, :enumeration => {:type => 'IssuePriority', :name => ''} | |
94 | end |
|
94 | end | |
95 | assert_response :success |
|
95 | assert_response :success | |
96 | assert_template 'edit' |
|
96 | assert_template 'edit' | |
97 | end |
|
97 | end | |
98 |
|
98 | |||
99 | def test_destroy_enumeration_not_in_use |
|
99 | def test_destroy_enumeration_not_in_use | |
100 | assert_difference 'IssuePriority.count', -1 do |
|
100 | assert_difference 'IssuePriority.count', -1 do | |
101 | delete :destroy, :id => 7 |
|
101 | delete :destroy, :id => 7 | |
102 | end |
|
102 | end | |
103 | assert_redirected_to :controller => 'enumerations', :action => 'index' |
|
103 | assert_redirected_to :controller => 'enumerations', :action => 'index' | |
104 | assert_nil Enumeration.find_by_id(7) |
|
104 | assert_nil Enumeration.find_by_id(7) | |
105 | end |
|
105 | end | |
106 |
|
106 | |||
107 | def test_destroy_enumeration_in_use |
|
107 | def test_destroy_enumeration_in_use | |
108 | assert_no_difference 'IssuePriority.count' do |
|
108 | assert_no_difference 'IssuePriority.count' do | |
109 | delete :destroy, :id => 4 |
|
109 | delete :destroy, :id => 4 | |
110 | end |
|
110 | end | |
111 | assert_response :success |
|
111 | assert_response :success | |
112 | assert_template 'destroy' |
|
112 | assert_template 'destroy' | |
113 | assert_not_nil Enumeration.find_by_id(4) |
|
113 | assert_not_nil Enumeration.find_by_id(4) | |
114 | assert_select 'select[name=reassign_to_id]' do |
|
114 | assert_select 'select[name=reassign_to_id]' do | |
115 | assert_select 'option[value=6]', :text => 'High' |
|
115 | assert_select 'option[value=6]', :text => 'High' | |
116 | end |
|
116 | end | |
117 | end |
|
117 | end | |
118 |
|
118 | |||
119 | def test_destroy_enumeration_in_use_with_reassignment |
|
119 | def test_destroy_enumeration_in_use_with_reassignment | |
120 | issue = Issue.where(:priority_id => 4).first |
|
120 | issue = Issue.where(:priority_id => 4).first | |
121 | assert_difference 'IssuePriority.count', -1 do |
|
121 | assert_difference 'IssuePriority.count', -1 do | |
122 | delete :destroy, :id => 4, :reassign_to_id => 6 |
|
122 | delete :destroy, :id => 4, :reassign_to_id => 6 | |
123 | end |
|
123 | end | |
124 | assert_redirected_to :controller => 'enumerations', :action => 'index' |
|
124 | assert_redirected_to :controller => 'enumerations', :action => 'index' | |
125 | assert_nil Enumeration.find_by_id(4) |
|
125 | assert_nil Enumeration.find_by_id(4) | |
126 | # check that the issue was reassign |
|
126 | # check that the issue was reassign | |
127 | assert_equal 6, issue.reload.priority_id |
|
127 | assert_equal 6, issue.reload.priority_id | |
128 | end |
|
128 | end | |
|
129 | ||||
|
130 | def test_destroy_enumeration_in_use_with_blank_reassignment | |||
|
131 | assert_no_difference 'IssuePriority.count' do | |||
|
132 | delete :destroy, :id => 4, :reassign_to_id => '' | |||
|
133 | end | |||
|
134 | assert_response :success | |||
|
135 | end | |||
129 | end |
|
136 | end |
General Comments 0
You need to be logged in to leave comments.
Login now