##// END OF EJS Templates
Ability to remove enumerations (activities, priorities, document categories) that are in use. Associated objects can be reassigned to another value (#1467)....
Jean-Philippe Lang -
r1544:3c95f761e610
parent child
Show More
@@ -0,0 +1,12
1 <h2><%= l(@enumeration.option_name) %>: <%=h @enumeration %></h2>
2
3 <% form_tag({}) do %>
4 <div class="box">
5 <p><strong><%= l(:text_enumeration_destroy_question, @enumeration.objects_count) %></strong></p>
6 <p><%= l(:text_enumeration_category_reassign_to) %>
7 <%= select_tag 'reassign_to_id', ("<option>--- #{l(:actionview_instancetag_blank_option)} ---</option>" + options_from_collection_for_select(@enumerations, 'id', 'name')) %></p>
8 </div>
9
10 <%= submit_tag l(:button_apply) %>
11 <%= link_to l(:button_cancel), :controller => 'enumerations', :action => 'index' %>
12 <% end %>
@@ -0,0 +1,61
1 # redMine - project management software
2 # Copyright (C) 2006-2008 Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
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
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 require File.dirname(__FILE__) + '/../test_helper'
19 require 'enumerations_controller'
20
21 # Re-raise errors caught by the controller.
22 class EnumerationsController; def rescue_action(e) raise e end; end
23
24 class EnumerationsControllerTest < Test::Unit::TestCase
25 fixtures :enumerations, :issues, :users
26
27 def setup
28 @controller = EnumerationsController.new
29 @request = ActionController::TestRequest.new
30 @response = ActionController::TestResponse.new
31 @request.session[:user_id] = 1 # admin
32 end
33
34 def test_index
35 get :index
36 assert_response :success
37 assert_template 'list'
38 end
39
40 def test_destroy_enumeration_not_in_use
41 post :destroy, :id => 7
42 assert_redirected_to :controller => 'enumerations', :action => 'index'
43 assert_nil Enumeration.find_by_id(7)
44 end
45
46 def test_destroy_enumeration_in_use
47 post :destroy, :id => 4
48 assert_response :success
49 assert_template 'destroy'
50 assert_not_nil Enumeration.find_by_id(4)
51 end
52
53 def test_destroy_enumeration_in_use_with_reassignment
54 issue = Issue.find(:first, :conditions => {:priority_id => 4})
55 post :destroy, :id => 4, :reassign_to_id => 6
56 assert_redirected_to :controller => 'enumerations', :action => 'index'
57 assert_nil Enumeration.find_by_id(4)
58 # check that the issue was reassign
59 assert_equal 6, issue.reload.priority_id
60 end
61 end
@@ -0,0 +1,45
1 # redMine - project management software
2 # Copyright (C) 2006-2008 Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
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
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 require File.dirname(__FILE__) + '/../test_helper'
19
20 class EnumerationTest < Test::Unit::TestCase
21 fixtures :enumerations, :issues
22
23 def setup
24 end
25
26 def test_objects_count
27 # low priority
28 assert_equal 5, Enumeration.find(4).objects_count
29 # urgent
30 assert_equal 0, Enumeration.find(7).objects_count
31 end
32
33 def test_in_use
34 # low priority
35 assert Enumeration.find(4).in_use?
36 # urgent
37 assert !Enumeration.find(7).in_use?
38 end
39
40 def test_destroy_with_reassign
41 Enumeration.find(4).destroy(Enumeration.find(6))
42 assert_nil Issue.find(:first, :conditions => {:priority_id => 4})
43 assert_equal 5, Enumeration.find(6).objects_count
44 end
45 end
@@ -75,11 +75,20 class EnumerationsController < ApplicationController
75 75 end
76 76
77 77 def destroy
78 Enumeration.find(params[:id]).destroy
79 flash[:notice] = l(:notice_successful_delete)
80 redirect_to :action => 'list'
81 rescue
82 flash[:error] = "Unable to delete enumeration"
83 redirect_to :action => 'list'
78 @enumeration = Enumeration.find(params[:id])
79 if !@enumeration.in_use?
80 # No associated objects
81 @enumeration.destroy
82 redirect_to :action => 'index'
83 elsif params[:reassign_to_id]
84 if reassign_to = Enumeration.find_by_opt_and_id(@enumeration.opt, params[:reassign_to_id])
85 @enumeration.destroy(reassign_to)
86 redirect_to :action => 'index'
87 end
88 end
89 @enumerations = Enumeration.get_values(@enumeration.opt) - [@enumeration]
90 #rescue
91 # flash[:error] = 'Unable to delete enumeration'
92 # redirect_to :action => 'index'
84 93 end
85 94 end
@@ -24,10 +24,11 class Enumeration < ActiveRecord::Base
24 24 validates_uniqueness_of :name, :scope => [:opt]
25 25 validates_length_of :name, :maximum => 30
26 26
27 # Single table inheritance would be an option
27 28 OPTIONS = {
28 "IPRI" => :enumeration_issue_priorities,
29 "DCAT" => :enumeration_doc_categories,
30 "ACTI" => :enumeration_activities
29 "IPRI" => {:label => :enumeration_issue_priorities, :model => Issue, :foreign_key => :priority_id},
30 "DCAT" => {:label => :enumeration_doc_categories, :model => Document, :foreign_key => :category_id},
31 "ACTI" => {:label => :enumeration_activities, :model => TimeEntry, :foreign_key => :activity_id}
31 32 }.freeze
32 33
33 34 def self.get_values(option)
@@ -39,13 +40,32 class Enumeration < ActiveRecord::Base
39 40 end
40 41
41 42 def option_name
42 OPTIONS[self.opt]
43 OPTIONS[self.opt][:label]
43 44 end
44 45
45 46 def before_save
46 47 Enumeration.update_all("is_default = #{connection.quoted_false}", {:opt => opt}) if is_default?
47 48 end
48 49
50 def objects_count
51 OPTIONS[self.opt][:model].count(:conditions => "#{OPTIONS[self.opt][:foreign_key]} = #{id}")
52 end
53
54 def in_use?
55 self.objects_count != 0
56 end
57
58 alias :destroy_without_reassign :destroy
59
60 # Destroy the enumeration
61 # If a enumeration is specified, objects are reassigned
62 def destroy(reassign_to = nil)
63 if reassign_to && reassign_to.is_a?(Enumeration)
64 OPTIONS[self.opt][:model].update_all("#{OPTIONS[self.opt][:foreign_key]} = #{reassign_to.id}", "#{OPTIONS[self.opt][:foreign_key]} = #{id}")
65 end
66 destroy_without_reassign
67 end
68
49 69 def <=>(enumeration)
50 70 position <=> enumeration.position
51 71 end
@@ -54,13 +74,6 class Enumeration < ActiveRecord::Base
54 74
55 75 private
56 76 def check_integrity
57 case self.opt
58 when "IPRI"
59 raise "Can't delete enumeration" if Issue.find(:first, :conditions => ["priority_id=?", self.id])
60 when "DCAT"
61 raise "Can't delete enumeration" if Document.find(:first, :conditions => ["category_id=?", self.id])
62 when "ACTI"
63 raise "Can't delete enumeration" if TimeEntry.find(:first, :conditions => ["activity_id=?", self.id])
64 end
77 raise "Can't delete enumeration" if self.in_use?
65 78 end
66 79 end
@@ -1,7 +1,7
1 1 <h2><%=l(:label_enumerations)%></h2>
2 2
3 <% Enumeration::OPTIONS.each do |option, name| %>
4 <h3><%= l(name) %></h3>
3 <% Enumeration::OPTIONS.each do |option, params| %>
4 <h3><%= l(params[:label]) %></h3>
5 5
6 6 <% enumerations = Enumeration.get_values(option) %>
7 7 <% if enumerations.any? %>
@@ -16,6 +16,9
16 16 <%= link_to image_tag('1downarrow.png', :alt => l(:label_sort_lower)), {:action => 'move', :id => enumeration, :position => 'lower'}, :method => :post, :title => l(:label_sort_lower) %>
17 17 <%= link_to image_tag('2downarrow.png', :alt => l(:label_sort_lowest)), {:action => 'move', :id => enumeration, :position => 'lowest'}, :method => :post, :title => l(:label_sort_lowest) %>
18 18 </td>
19 <td align="center" style="width:10%;">
20 <%= link_to l(:button_delete), { :action => 'destroy', :id => enumeration }, :method => :post, :confirm => l(:text_are_you_sure), :class => "icon icon-del" %>
21 </td>
19 22 </tr>
20 23 <% end %>
21 24 </table>
@@ -624,3 +624,5 mail_subject_reminder: "%d issue(s) due in the next days"
624 624 text_user_wrote: '%s wrote:'
625 625 label_duplicated_by: duplicated by
626 626 setting_enabled_scm: Enabled SCM
627 text_enumeration_category_reassign_to: 'Reassign them to this value:'
628 text_enumeration_destroy_question: '%d objects are assigned to this value.'
@@ -629,3 +629,5 mail_subject_reminder: "%d issue(s) due in the next days"
629 629 text_user_wrote: '%s wrote:'
630 630 label_duplicated_by: duplicated by
631 631 setting_enabled_scm: Enabled SCM
632 text_enumeration_category_reassign_to: 'Reassign them to this value:'
633 text_enumeration_destroy_question: '%d objects are assigned to this value.'
@@ -626,3 +626,5 mail_subject_reminder: "%d issue(s) due in the next days"
626 626 text_user_wrote: '%s wrote:'
627 627 label_duplicated_by: duplicated by
628 628 setting_enabled_scm: Enabled SCM
629 text_enumeration_category_reassign_to: 'Reassign them to this value:'
630 text_enumeration_destroy_question: '%d objects are assigned to this value.'
@@ -625,3 +625,5 mail_subject_reminder: "%d issue(s) due in the next days"
625 625 text_user_wrote: '%s wrote:'
626 626 label_duplicated_by: duplicated by
627 627 setting_enabled_scm: Enabled SCM
628 text_enumeration_category_reassign_to: 'Reassign them to this value:'
629 text_enumeration_destroy_question: '%d objects are assigned to this value.'
@@ -599,6 +599,8 text_destroy_time_entries: Delete reported hours
599 599 text_assign_time_entries_to_project: Assign reported hours to the project
600 600 text_reassign_time_entries: 'Reassign reported hours to this issue:'
601 601 text_user_wrote: '%s wrote:'
602 text_enumeration_destroy_question: '%d objects are assigned to this value.'
603 text_enumeration_category_reassign_to: 'Reassign them to this value:'
602 604
603 605 default_role_manager: Manager
604 606 default_role_developper: Developer
@@ -627,3 +627,5 mail_subject_reminder: "%d issue(s) due in the next days"
627 627 text_user_wrote: '%s wrote:'
628 628 label_duplicated_by: duplicated by
629 629 setting_enabled_scm: Enabled SCM
630 text_enumeration_category_reassign_to: 'Reassign them to this value:'
631 text_enumeration_destroy_question: '%d objects are assigned to this value.'
@@ -624,3 +624,5 mail_subject_reminder: "%d issue(s) due in the next days"
624 624 text_user_wrote: '%s wrote:'
625 625 label_duplicated_by: duplicated by
626 626 setting_enabled_scm: Enabled SCM
627 text_enumeration_category_reassign_to: 'Reassign them to this value:'
628 text_enumeration_destroy_question: '%d objects are assigned to this value.'
@@ -599,6 +599,8 text_destroy_time_entries: Supprimer les heures
599 599 text_assign_time_entries_to_project: Reporter les heures sur le projet
600 600 text_reassign_time_entries: 'Reporter les heures sur cette demande:'
601 601 text_user_wrote: '%s a écrit:'
602 text_enumeration_destroy_question: 'Cette valeur est affectée à %d objets.'
603 text_enumeration_category_reassign_to: 'Réaffecter les objets à cette valeur:'
602 604
603 605 default_role_manager: Manager
604 606 default_role_developper: Développeur
@@ -624,3 +624,5 mail_subject_reminder: "%d issue(s) due in the next days"
624 624 text_user_wrote: '%s wrote:'
625 625 label_duplicated_by: duplicated by
626 626 setting_enabled_scm: Enabled SCM
627 text_enumeration_category_reassign_to: 'Reassign them to this value:'
628 text_enumeration_destroy_question: '%d objects are assigned to this value.'
@@ -625,3 +625,5 mail_subject_reminder: "%d feladat határidős az elkövetkező napokban"
625 625 text_user_wrote: '%s írta:'
626 626 label_duplicated_by: duplikálta
627 627 setting_enabled_scm: Forráskódkezelő (SCM) engedélyezése
628 text_enumeration_category_reassign_to: 'Reassign them to this value:'
629 text_enumeration_destroy_question: '%d objects are assigned to this value.'
@@ -624,3 +624,5 mail_subject_reminder: "%d issue(s) due in the next days"
624 624 text_user_wrote: '%s wrote:'
625 625 label_duplicated_by: duplicated by
626 626 setting_enabled_scm: Enabled SCM
627 text_enumeration_category_reassign_to: 'Reassign them to this value:'
628 text_enumeration_destroy_question: '%d objects are assigned to this value.'
@@ -625,3 +625,5 mail_subject_reminder: "%d issue(s) due in the next days"
625 625 text_user_wrote: '%s wrote:'
626 626 label_duplicated_by: duplicated by
627 627 setting_enabled_scm: Enabled SCM
628 text_enumeration_category_reassign_to: 'Reassign them to this value:'
629 text_enumeration_destroy_question: '%d objects are assigned to this value.'
@@ -624,3 +624,5 mail_subject_reminder: "%d issue(s) due in the next days"
624 624 text_user_wrote: '%s wrote:'
625 625 label_duplicated_by: duplicated by
626 626 setting_enabled_scm: Enabled SCM
627 text_enumeration_category_reassign_to: 'Reassign them to this value:'
628 text_enumeration_destroy_question: '%d objects are assigned to this value.'
@@ -626,3 +626,5 mail_subject_reminder: "%d issue(s) due in the next days"
626 626 text_user_wrote: '%s wrote:'
627 627 label_duplicated_by: duplicated by
628 628 setting_enabled_scm: Enabled SCM
629 text_enumeration_category_reassign_to: 'Reassign them to this value:'
630 text_enumeration_destroy_question: '%d objects are assigned to this value.'
@@ -625,3 +625,5 mail_subject_reminder: "%d issue(s) due in the next days"
625 625 text_user_wrote: '%s wrote:'
626 626 label_duplicated_by: duplicated by
627 627 setting_enabled_scm: Enabled SCM
628 text_enumeration_category_reassign_to: 'Reassign them to this value:'
629 text_enumeration_destroy_question: '%d objects are assigned to this value.'
@@ -625,3 +625,5 default_activity_development: Utvikling
625 625 enumeration_issue_priorities: Sakssprioriteringer
626 626 enumeration_doc_categories: Dokument-kategorier
627 627 enumeration_activities: Aktiviteter (tidssporing)
628 text_enumeration_category_reassign_to: 'Reassign them to this value:'
629 text_enumeration_destroy_question: '%d objects are assigned to this value.'
@@ -624,3 +624,5 mail_subject_reminder: "%d issue(s) due in the next days"
624 624 text_user_wrote: '%s wrote:'
625 625 label_duplicated_by: duplicated by
626 626 setting_enabled_scm: Enabled SCM
627 text_enumeration_category_reassign_to: 'Reassign them to this value:'
628 text_enumeration_destroy_question: '%d objects are assigned to this value.'
@@ -624,3 +624,5 mail_subject_reminder: "%d issue(s) due in the next days"
624 624 text_user_wrote: '%s wrote:'
625 625 label_duplicated_by: duplicated by
626 626 setting_enabled_scm: Enabled SCM
627 text_enumeration_category_reassign_to: 'Reassign them to this value:'
628 text_enumeration_destroy_question: '%d objects are assigned to this value.'
@@ -624,3 +624,5 mail_subject_reminder: "%d issue(s) due in the next days"
624 624 text_user_wrote: '%s wrote:'
625 625 label_duplicated_by: duplicated by
626 626 setting_enabled_scm: Enabled SCM
627 text_enumeration_category_reassign_to: 'Reassign them to this value:'
628 text_enumeration_destroy_question: '%d objects are assigned to this value.'
@@ -624,3 +624,5 mail_subject_reminder: "%d issue(s) due in the next days"
624 624 text_user_wrote: '%s wrote:'
625 625 label_duplicated_by: duplicated by
626 626 setting_enabled_scm: Enabled SCM
627 text_enumeration_category_reassign_to: 'Reassign them to this value:'
628 text_enumeration_destroy_question: '%d objects are assigned to this value.'
@@ -628,3 +628,5 mail_subject_reminder: "%d назначенных на вас задач в бл
628 628 text_user_wrote: '%s написал:'
629 629 label_duplicated_by: duplicated by
630 630 setting_enabled_scm: Enabled SCM
631 text_enumeration_category_reassign_to: 'Reassign them to this value:'
632 text_enumeration_destroy_question: '%d objects are assigned to this value.'
@@ -625,3 +625,5 mail_subject_reminder: "%d issue(s) due in the next days"
625 625 text_user_wrote: '%s wrote:'
626 626 label_duplicated_by: duplicated by
627 627 setting_enabled_scm: Enabled SCM
628 text_enumeration_category_reassign_to: 'Reassign them to this value:'
629 text_enumeration_destroy_question: '%d objects are assigned to this value.'
@@ -625,3 +625,5 mail_subject_reminder: "%d issue(s) due in the next days"
625 625 text_user_wrote: '%s wrote:'
626 626 label_duplicated_by: duplicated by
627 627 setting_enabled_scm: Enabled SCM
628 text_enumeration_category_reassign_to: 'Reassign them to this value:'
629 text_enumeration_destroy_question: '%d objects are assigned to this value.'
@@ -627,3 +627,5 mail_subject_reminder: "%d issue(s) due in the next days"
627 627 text_user_wrote: '%s wrote:'
628 628 label_duplicated_by: duplicated by
629 629 setting_enabled_scm: Enabled SCM
630 text_enumeration_category_reassign_to: 'Reassign them to this value:'
631 text_enumeration_destroy_question: '%d objects are assigned to this value.'
@@ -626,3 +626,5 mail_subject_reminder: "%d issue(s) due in the next days"
626 626 text_user_wrote: '%s wrote:'
627 627 label_duplicated_by: duplicated by
628 628 setting_enabled_scm: Enabled SCM
629 text_enumeration_category_reassign_to: 'Reassign them to this value:'
630 text_enumeration_destroy_question: '%d objects are assigned to this value.'
@@ -625,3 +625,5 default_activity_development: 開發
625 625 enumeration_issue_priorities: 項目優先權
626 626 enumeration_doc_categories: 文件分類
627 627 enumeration_activities: 活動 (時間追蹤)
628 text_enumeration_category_reassign_to: 'Reassign them to this value:'
629 text_enumeration_destroy_question: '%d objects are assigned to this value.'
@@ -625,3 +625,5 default_activity_development: 开发
625 625 enumeration_issue_priorities: 问题优先级
626 626 enumeration_doc_categories: 文档类别
627 627 enumeration_activities: 活动(时间跟踪)
628 text_enumeration_category_reassign_to: 'Reassign them to this value:'
629 text_enumeration_destroy_question: '%d objects are assigned to this value.'
General Comments 0
You need to be logged in to leave comments. Login now