##// END OF EJS Templates
Fixed: default flag removed when editing a default enumeration (#2327)....
Jean-Philippe Lang -
r2121:c6511849987c
parent child
Show More
@@ -1,79 +1,81
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006 Jean-Philippe Lang
2 # Copyright (C) 2006 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 Enumeration < ActiveRecord::Base
18 class Enumeration < ActiveRecord::Base
19 acts_as_list :scope => 'opt = \'#{opt}\''
19 acts_as_list :scope => 'opt = \'#{opt}\''
20
20
21 before_destroy :check_integrity
21 before_destroy :check_integrity
22
22
23 validates_presence_of :opt, :name
23 validates_presence_of :opt, :name
24 validates_uniqueness_of :name, :scope => [:opt]
24 validates_uniqueness_of :name, :scope => [:opt]
25 validates_length_of :name, :maximum => 30
25 validates_length_of :name, :maximum => 30
26
26
27 # Single table inheritance would be an option
27 # Single table inheritance would be an option
28 OPTIONS = {
28 OPTIONS = {
29 "IPRI" => {:label => :enumeration_issue_priorities, :model => Issue, :foreign_key => :priority_id},
29 "IPRI" => {:label => :enumeration_issue_priorities, :model => Issue, :foreign_key => :priority_id},
30 "DCAT" => {:label => :enumeration_doc_categories, :model => Document, :foreign_key => :category_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 "ACTI" => {:label => :enumeration_activities, :model => TimeEntry, :foreign_key => :activity_id}
32 }.freeze
32 }.freeze
33
33
34 def self.get_values(option)
34 def self.get_values(option)
35 find(:all, :conditions => {:opt => option}, :order => 'position')
35 find(:all, :conditions => {:opt => option}, :order => 'position')
36 end
36 end
37
37
38 def self.default(option)
38 def self.default(option)
39 find(:first, :conditions => {:opt => option, :is_default => true}, :order => 'position')
39 find(:first, :conditions => {:opt => option, :is_default => true}, :order => 'position')
40 end
40 end
41
41
42 def option_name
42 def option_name
43 OPTIONS[self.opt][:label]
43 OPTIONS[self.opt][:label]
44 end
44 end
45
45
46 def before_save
46 def before_save
47 Enumeration.update_all("is_default = #{connection.quoted_false}", {:opt => opt}) if is_default?
47 if is_default? && is_default_changed?
48 Enumeration.update_all("is_default = #{connection.quoted_false}", {:opt => opt})
49 end
48 end
50 end
49
51
50 def objects_count
52 def objects_count
51 OPTIONS[self.opt][:model].count(:conditions => "#{OPTIONS[self.opt][:foreign_key]} = #{id}")
53 OPTIONS[self.opt][:model].count(:conditions => "#{OPTIONS[self.opt][:foreign_key]} = #{id}")
52 end
54 end
53
55
54 def in_use?
56 def in_use?
55 self.objects_count != 0
57 self.objects_count != 0
56 end
58 end
57
59
58 alias :destroy_without_reassign :destroy
60 alias :destroy_without_reassign :destroy
59
61
60 # Destroy the enumeration
62 # Destroy the enumeration
61 # If a enumeration is specified, objects are reassigned
63 # If a enumeration is specified, objects are reassigned
62 def destroy(reassign_to = nil)
64 def destroy(reassign_to = nil)
63 if reassign_to && reassign_to.is_a?(Enumeration)
65 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}")
66 OPTIONS[self.opt][:model].update_all("#{OPTIONS[self.opt][:foreign_key]} = #{reassign_to.id}", "#{OPTIONS[self.opt][:foreign_key]} = #{id}")
65 end
67 end
66 destroy_without_reassign
68 destroy_without_reassign
67 end
69 end
68
70
69 def <=>(enumeration)
71 def <=>(enumeration)
70 position <=> enumeration.position
72 position <=> enumeration.position
71 end
73 end
72
74
73 def to_s; name end
75 def to_s; name end
74
76
75 private
77 private
76 def check_integrity
78 def check_integrity
77 raise "Can't delete enumeration" if self.in_use?
79 raise "Can't delete enumeration" if self.in_use?
78 end
80 end
79 end
81 end
@@ -1,45 +1,82
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2008 Jean-Philippe Lang
2 # Copyright (C) 2006-2008 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.dirname(__FILE__) + '/../test_helper'
18 require File.dirname(__FILE__) + '/../test_helper'
19
19
20 class EnumerationTest < Test::Unit::TestCase
20 class EnumerationTest < Test::Unit::TestCase
21 fixtures :enumerations, :issues
21 fixtures :enumerations, :issues
22
22
23 def setup
23 def setup
24 end
24 end
25
25
26 def test_objects_count
26 def test_objects_count
27 # low priority
27 # low priority
28 assert_equal 5, Enumeration.find(4).objects_count
28 assert_equal 5, Enumeration.find(4).objects_count
29 # urgent
29 # urgent
30 assert_equal 0, Enumeration.find(7).objects_count
30 assert_equal 0, Enumeration.find(7).objects_count
31 end
31 end
32
32
33 def test_in_use
33 def test_in_use
34 # low priority
34 # low priority
35 assert Enumeration.find(4).in_use?
35 assert Enumeration.find(4).in_use?
36 # urgent
36 # urgent
37 assert !Enumeration.find(7).in_use?
37 assert !Enumeration.find(7).in_use?
38 end
38 end
39
39
40 def test_default
41 e = Enumeration.default('IPRI')
42 assert e.is_a?(Enumeration)
43 assert e.is_default?
44 assert_equal 'Normal', e.name
45 end
46
47 def test_create
48 e = Enumeration.new(:opt => 'IPRI', :name => 'Very urgent', :is_default => false)
49 assert e.save
50 assert_equal 'Normal', Enumeration.default('IPRI').name
51 end
52
53 def test_create_as_default
54 e = Enumeration.new(:opt => 'IPRI', :name => 'Very urgent', :is_default => true)
55 assert e.save
56 assert_equal e, Enumeration.default('IPRI')
57 end
58
59 def test_update_default
60 e = Enumeration.default('IPRI')
61 e.update_attributes(:name => 'Changed', :is_default => true)
62 assert_equal e, Enumeration.default('IPRI')
63 end
64
65 def test_update_default_to_non_default
66 e = Enumeration.default('IPRI')
67 e.update_attributes(:name => 'Changed', :is_default => false)
68 assert_nil Enumeration.default('IPRI')
69 end
70
71 def test_change_default
72 e = Enumeration.find_by_name('Urgent')
73 e.update_attributes(:name => 'Urgent', :is_default => true)
74 assert_equal e, Enumeration.default('IPRI')
75 end
76
40 def test_destroy_with_reassign
77 def test_destroy_with_reassign
41 Enumeration.find(4).destroy(Enumeration.find(6))
78 Enumeration.find(4).destroy(Enumeration.find(6))
42 assert_nil Issue.find(:first, :conditions => {:priority_id => 4})
79 assert_nil Issue.find(:first, :conditions => {:priority_id => 4})
43 assert_equal 5, Enumeration.find(6).objects_count
80 assert_equal 5, Enumeration.find(6).objects_count
44 end
81 end
45 end
82 end
General Comments 0
You need to be logged in to leave comments. Login now