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