##// END OF EJS Templates
Added project specific Enumeration overrides....
Eric Davis -
r2833:29301c8a381a
parent child
Show More
@@ -0,0 +1,11
1 class AddProjectToEnumerations < ActiveRecord::Migration
2 def self.up
3 add_column :enumerations, :project_id, :integer, :null => true, :default => nil
4 add_index :enumerations, :project_id
5 end
6
7 def self.down
8 remove_index :enumerations, :project_id
9 remove_column :enumerations, :project_id
10 end
11 end
@@ -0,0 +1,9
1 class AddParentIdToEnumerations < ActiveRecord::Migration
2 def self.up
3 add_column :enumerations, :parent_id, :integer, :null => true, :default => nil
4 end
5
6 def self.down
7 remove_column :enumerations, :parent_id
8 end
9 end
@@ -1,140 +1,148
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 belongs_to :project
20
19 21 acts_as_list :scope => 'type = \'#{type}\''
20 22 acts_as_customizable
23 acts_as_tree :order => 'position ASC'
21 24
22 25 before_destroy :check_integrity
23 26
24 27 validates_presence_of :name
25 28 validates_uniqueness_of :name, :scope => [:type]
26 29 validates_length_of :name, :maximum => 30
27 30
28 31 # Backwards compatiblity named_scopes.
29 32 # Can be removed post-0.9
30 33 named_scope :priorities, :conditions => { :type => "IssuePriority" }, :order => 'position' do
31 34 ActiveSupport::Deprecation.warn("Enumeration#priorities is deprecated, use the IssuePriority class. (#{Redmine::Info.issue(3007)})")
32 35 def default
33 36 find(:first, :conditions => { :is_default => true })
34 37 end
35 38 end
36 39
37 40 named_scope :document_categories, :conditions => { :type => "DocumentCategory" }, :order => 'position' do
38 41 ActiveSupport::Deprecation.warn("Enumeration#document_categories is deprecated, use the DocumentCategories class. (#{Redmine::Info.issue(3007)})")
39 42 def default
40 43 find(:first, :conditions => { :is_default => true })
41 44 end
42 45 end
43 46
44 47 named_scope :activities, :conditions => { :type => "TimeEntryActivity" }, :order => 'position' do
45 48 ActiveSupport::Deprecation.warn("Enumeration#activities is deprecated, use the TimeEntryActivity class. (#{Redmine::Info.issue(3007)})")
46 49 def default
47 50 find(:first, :conditions => { :is_default => true })
48 51 end
49 52 end
50 53
51 54 named_scope :values, lambda {|type| { :conditions => { :type => type }, :order => 'position' } } do
52 55 def default
53 56 find(:first, :conditions => { :is_default => true })
54 57 end
55 58 end
56 59 # End backwards compatiblity named_scopes
57 60
58 61 named_scope :all, :order => 'position'
59 62
60 63 named_scope :active, lambda {
61 64 {
62 65 :conditions => {:active => true},
63 66 :order => 'position'
64 67 }
65 68 }
66 69
67 70 def self.default
68 71 # Creates a fake default scope so Enumeration.default will check
69 72 # it's type. STI subclasses will automatically add their own
70 73 # types to the finder.
71 74 if self.descends_from_active_record?
72 75 find(:first, :conditions => { :is_default => true, :type => 'Enumeration' })
73 76 else
74 77 # STI classes are
75 78 find(:first, :conditions => { :is_default => true })
76 79 end
77 80 end
78 81
79 82 # Overloaded on concrete classes
80 83 def option_name
81 84 nil
82 85 end
83 86
84 87 # Backwards compatiblity. Can be removed post-0.9
85 88 def opt
86 89 ActiveSupport::Deprecation.warn("Enumeration#opt is deprecated, use the STI classes now. (#{Redmine::Info.issue(3007)})")
87 90 return OptName
88 91 end
89 92
90 93 def before_save
91 94 if is_default? && is_default_changed?
92 95 Enumeration.update_all("is_default = #{connection.quoted_false}", {:type => type})
93 96 end
94 97 end
95 98
96 99 # Overloaded on concrete classes
97 100 def objects_count
98 101 0
99 102 end
100 103
101 104 def in_use?
102 105 self.objects_count != 0
103 106 end
107
108 # Is this enumeration overiding a system level enumeration?
109 def is_override?
110 !self.parent.nil?
111 end
104 112
105 113 alias :destroy_without_reassign :destroy
106 114
107 115 # Destroy the enumeration
108 116 # If a enumeration is specified, objects are reassigned
109 117 def destroy(reassign_to = nil)
110 118 if reassign_to && reassign_to.is_a?(Enumeration)
111 119 self.transfer_relations(reassign_to)
112 120 end
113 121 destroy_without_reassign
114 122 end
115 123
116 124 def <=>(enumeration)
117 125 position <=> enumeration.position
118 126 end
119 127
120 128 def to_s; name end
121 129
122 130 # Returns the Subclasses of Enumeration. Each Subclass needs to be
123 131 # required in development mode.
124 132 #
125 133 # Note: subclasses is protected in ActiveRecord
126 134 def self.get_subclasses
127 135 @@subclasses[Enumeration]
128 136 end
129 137
130 138 private
131 139 def check_integrity
132 140 raise "Can't delete enumeration" if self.in_use?
133 141 end
134 142
135 143 end
136 144
137 145 # Force load the subclasses in development mode
138 146 require_dependency 'time_entry_activity'
139 147 require_dependency 'document_category'
140 148 require_dependency 'issue_priority'
@@ -1,89 +1,111
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 < ActiveSupport::TestCase
21 21 fixtures :enumerations, :issues, :custom_fields, :custom_values
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 40 def test_default
41 41 e = Enumeration.default
42 42 assert e.is_a?(Enumeration)
43 43 assert e.is_default?
44 44 assert_equal 'Default Enumeration', e.name
45 45 end
46 46
47 47 def test_create
48 48 e = Enumeration.new(:name => 'Not default', :is_default => false)
49 49 e.type = 'Enumeration'
50 50 assert e.save
51 51 assert_equal 'Default Enumeration', Enumeration.default.name
52 52 end
53 53
54 54 def test_create_as_default
55 55 e = Enumeration.new(:name => 'Very urgent', :is_default => true)
56 56 e.type = 'Enumeration'
57 57 assert e.save
58 58 assert_equal e, Enumeration.default
59 59 end
60 60
61 61 def test_update_default
62 62 e = Enumeration.default
63 63 e.update_attributes(:name => 'Changed', :is_default => true)
64 64 assert_equal e, Enumeration.default
65 65 end
66 66
67 67 def test_update_default_to_non_default
68 68 e = Enumeration.default
69 69 e.update_attributes(:name => 'Changed', :is_default => false)
70 70 assert_nil Enumeration.default
71 71 end
72 72
73 73 def test_change_default
74 74 e = Enumeration.find_by_name('Default Enumeration')
75 75 e.update_attributes(:name => 'Changed Enumeration', :is_default => true)
76 76 assert_equal e, Enumeration.default
77 77 end
78 78
79 79 def test_destroy_with_reassign
80 80 Enumeration.find(4).destroy(Enumeration.find(6))
81 81 assert_nil Issue.find(:first, :conditions => {:priority_id => 4})
82 82 assert_equal 5, Enumeration.find(6).objects_count
83 83 end
84 84
85 85 def test_should_be_customizable
86 86 assert Enumeration.included_modules.include?(Redmine::Acts::Customizable::InstanceMethods)
87 87 end
88 88
89 def test_should_belong_to_a_project
90 association = Enumeration.reflect_on_association(:project)
91 assert association, "No Project association found"
92 assert_equal :belongs_to, association.macro
93 end
94
95 def test_should_act_as_tree
96 enumeration = Enumeration.find(4)
97
98 assert enumeration.respond_to?(:parent)
99 assert enumeration.respond_to?(:children)
100 end
101
102 def test_is_override
103 # Defaults to off
104 enumeration = Enumeration.find(4)
105 assert !enumeration.is_override?
106
107 # Setup as an override
108 enumeration.parent = Enumeration.find(5)
109 assert enumeration.is_override?
110 end
89 111 end
General Comments 0
You need to be logged in to leave comments. Login now