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