##// END OF EJS Templates
Use subclasses method instead of class variable....
Jean-Philippe Lang -
r8857:528735b955ab
parent child
Show More
@@ -1,140 +1,140
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 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 include Redmine::SubclassFactory
19 include Redmine::SubclassFactory
20
20
21 default_scope :order => "#{Enumeration.table_name}.position ASC"
21 default_scope :order => "#{Enumeration.table_name}.position ASC"
22
22
23 belongs_to :project
23 belongs_to :project
24
24
25 acts_as_list :scope => 'type = \'#{type}\''
25 acts_as_list :scope => 'type = \'#{type}\''
26 acts_as_customizable
26 acts_as_customizable
27 acts_as_tree :order => 'position ASC'
27 acts_as_tree :order => 'position ASC'
28
28
29 before_destroy :check_integrity
29 before_destroy :check_integrity
30 before_save :check_default
30 before_save :check_default
31
31
32 attr_protected :type
32 attr_protected :type
33
33
34 validates_presence_of :name
34 validates_presence_of :name
35 validates_uniqueness_of :name, :scope => [:type, :project_id]
35 validates_uniqueness_of :name, :scope => [:type, :project_id]
36 validates_length_of :name, :maximum => 30
36 validates_length_of :name, :maximum => 30
37
37
38 named_scope :shared, :conditions => { :project_id => nil }
38 named_scope :shared, :conditions => { :project_id => nil }
39 named_scope :active, :conditions => { :active => true }
39 named_scope :active, :conditions => { :active => true }
40 named_scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
40 named_scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
41
41
42 def self.default
42 def self.default
43 # Creates a fake default scope so Enumeration.default will check
43 # Creates a fake default scope so Enumeration.default will check
44 # it's type. STI subclasses will automatically add their own
44 # it's type. STI subclasses will automatically add their own
45 # types to the finder.
45 # types to the finder.
46 if self.descends_from_active_record?
46 if self.descends_from_active_record?
47 find(:first, :conditions => { :is_default => true, :type => 'Enumeration' })
47 find(:first, :conditions => { :is_default => true, :type => 'Enumeration' })
48 else
48 else
49 # STI classes are
49 # STI classes are
50 find(:first, :conditions => { :is_default => true })
50 find(:first, :conditions => { :is_default => true })
51 end
51 end
52 end
52 end
53
53
54 # Overloaded on concrete classes
54 # Overloaded on concrete classes
55 def option_name
55 def option_name
56 nil
56 nil
57 end
57 end
58
58
59 def check_default
59 def check_default
60 if is_default? && is_default_changed?
60 if is_default? && is_default_changed?
61 Enumeration.update_all("is_default = #{connection.quoted_false}", {:type => type})
61 Enumeration.update_all("is_default = #{connection.quoted_false}", {:type => type})
62 end
62 end
63 end
63 end
64
64
65 # Overloaded on concrete classes
65 # Overloaded on concrete classes
66 def objects_count
66 def objects_count
67 0
67 0
68 end
68 end
69
69
70 def in_use?
70 def in_use?
71 self.objects_count != 0
71 self.objects_count != 0
72 end
72 end
73
73
74 # Is this enumeration overiding a system level enumeration?
74 # Is this enumeration overiding a system level enumeration?
75 def is_override?
75 def is_override?
76 !self.parent.nil?
76 !self.parent.nil?
77 end
77 end
78
78
79 alias :destroy_without_reassign :destroy
79 alias :destroy_without_reassign :destroy
80
80
81 # Destroy the enumeration
81 # Destroy the enumeration
82 # If a enumeration is specified, objects are reassigned
82 # If a enumeration is specified, objects are reassigned
83 def destroy(reassign_to = nil)
83 def destroy(reassign_to = nil)
84 if reassign_to && reassign_to.is_a?(Enumeration)
84 if reassign_to && reassign_to.is_a?(Enumeration)
85 self.transfer_relations(reassign_to)
85 self.transfer_relations(reassign_to)
86 end
86 end
87 destroy_without_reassign
87 destroy_without_reassign
88 end
88 end
89
89
90 def <=>(enumeration)
90 def <=>(enumeration)
91 position <=> enumeration.position
91 position <=> enumeration.position
92 end
92 end
93
93
94 def to_s; name end
94 def to_s; name end
95
95
96 # Returns the Subclasses of Enumeration. Each Subclass needs to be
96 # Returns the Subclasses of Enumeration. Each Subclass needs to be
97 # required in development mode.
97 # required in development mode.
98 #
98 #
99 # Note: subclasses is protected in ActiveRecord
99 # Note: subclasses is protected in ActiveRecord
100 def self.get_subclasses
100 def self.get_subclasses
101 @@subclasses[Enumeration]
101 subclasses
102 end
102 end
103
103
104 # Does the +new+ Hash override the previous Enumeration?
104 # Does the +new+ Hash override the previous Enumeration?
105 def self.overridding_change?(new, previous)
105 def self.overridding_change?(new, previous)
106 if (same_active_state?(new['active'], previous.active)) && same_custom_values?(new,previous)
106 if (same_active_state?(new['active'], previous.active)) && same_custom_values?(new,previous)
107 return false
107 return false
108 else
108 else
109 return true
109 return true
110 end
110 end
111 end
111 end
112
112
113 # Does the +new+ Hash have the same custom values as the previous Enumeration?
113 # Does the +new+ Hash have the same custom values as the previous Enumeration?
114 def self.same_custom_values?(new, previous)
114 def self.same_custom_values?(new, previous)
115 previous.custom_field_values.each do |custom_value|
115 previous.custom_field_values.each do |custom_value|
116 if custom_value.value != new["custom_field_values"][custom_value.custom_field_id.to_s]
116 if custom_value.value != new["custom_field_values"][custom_value.custom_field_id.to_s]
117 return false
117 return false
118 end
118 end
119 end
119 end
120
120
121 return true
121 return true
122 end
122 end
123
123
124 # Are the new and previous fields equal?
124 # Are the new and previous fields equal?
125 def self.same_active_state?(new, previous)
125 def self.same_active_state?(new, previous)
126 new = (new == "1" ? true : false)
126 new = (new == "1" ? true : false)
127 return new == previous
127 return new == previous
128 end
128 end
129
129
130 private
130 private
131 def check_integrity
131 def check_integrity
132 raise "Can't delete enumeration" if self.in_use?
132 raise "Can't delete enumeration" if self.in_use?
133 end
133 end
134
134
135 end
135 end
136
136
137 # Force load the subclasses in development mode
137 # Force load the subclasses in development mode
138 require_dependency 'time_entry_activity'
138 require_dependency 'time_entry_activity'
139 require_dependency 'document_category'
139 require_dependency 'document_category'
140 require_dependency 'issue_priority'
140 require_dependency 'issue_priority'
@@ -1,111 +1,122
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.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19
19
20 class EnumerationTest < ActiveSupport::TestCase
20 class EnumerationTest < ActiveSupport::TestCase
21 fixtures :enumerations, :issues, :custom_fields, :custom_values
21 fixtures :enumerations, :issues, :custom_fields, :custom_values
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 6, Enumeration.find(4).objects_count
28 assert_equal 6, 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
40 def test_default
41 e = Enumeration.default
41 e = Enumeration.default
42 assert e.is_a?(Enumeration)
42 assert e.is_a?(Enumeration)
43 assert e.is_default?
43 assert e.is_default?
44 assert_equal 'Default Enumeration', e.name
44 assert_equal 'Default Enumeration', e.name
45 end
45 end
46
46
47 def test_create
47 def test_create
48 e = Enumeration.new(:name => 'Not default', :is_default => false)
48 e = Enumeration.new(:name => 'Not default', :is_default => false)
49 e.type = 'Enumeration'
49 e.type = 'Enumeration'
50 assert e.save
50 assert e.save
51 assert_equal 'Default Enumeration', Enumeration.default.name
51 assert_equal 'Default Enumeration', Enumeration.default.name
52 end
52 end
53
53
54 def test_create_as_default
54 def test_create_as_default
55 e = Enumeration.new(:name => 'Very urgent', :is_default => true)
55 e = Enumeration.new(:name => 'Very urgent', :is_default => true)
56 e.type = 'Enumeration'
56 e.type = 'Enumeration'
57 assert e.save
57 assert e.save
58 assert_equal e, Enumeration.default
58 assert_equal e, Enumeration.default
59 end
59 end
60
60
61 def test_update_default
61 def test_update_default
62 e = Enumeration.default
62 e = Enumeration.default
63 e.update_attributes(:name => 'Changed', :is_default => true)
63 e.update_attributes(:name => 'Changed', :is_default => true)
64 assert_equal e, Enumeration.default
64 assert_equal e, Enumeration.default
65 end
65 end
66
66
67 def test_update_default_to_non_default
67 def test_update_default_to_non_default
68 e = Enumeration.default
68 e = Enumeration.default
69 e.update_attributes(:name => 'Changed', :is_default => false)
69 e.update_attributes(:name => 'Changed', :is_default => false)
70 assert_nil Enumeration.default
70 assert_nil Enumeration.default
71 end
71 end
72
72
73 def test_change_default
73 def test_change_default
74 e = Enumeration.find_by_name('Default Enumeration')
74 e = Enumeration.find_by_name('Default Enumeration')
75 e.update_attributes(:name => 'Changed Enumeration', :is_default => true)
75 e.update_attributes(:name => 'Changed Enumeration', :is_default => true)
76 assert_equal e, Enumeration.default
76 assert_equal e, Enumeration.default
77 end
77 end
78
78
79 def test_destroy_with_reassign
79 def test_destroy_with_reassign
80 Enumeration.find(4).destroy(Enumeration.find(6))
80 Enumeration.find(4).destroy(Enumeration.find(6))
81 assert_nil Issue.find(:first, :conditions => {:priority_id => 4})
81 assert_nil Issue.find(:first, :conditions => {:priority_id => 4})
82 assert_equal 6, Enumeration.find(6).objects_count
82 assert_equal 6, Enumeration.find(6).objects_count
83 end
83 end
84
84
85 def test_should_be_customizable
85 def test_should_be_customizable
86 assert Enumeration.included_modules.include?(Redmine::Acts::Customizable::InstanceMethods)
86 assert Enumeration.included_modules.include?(Redmine::Acts::Customizable::InstanceMethods)
87 end
87 end
88
88
89 def test_should_belong_to_a_project
89 def test_should_belong_to_a_project
90 association = Enumeration.reflect_on_association(:project)
90 association = Enumeration.reflect_on_association(:project)
91 assert association, "No Project association found"
91 assert association, "No Project association found"
92 assert_equal :belongs_to, association.macro
92 assert_equal :belongs_to, association.macro
93 end
93 end
94
94
95 def test_should_act_as_tree
95 def test_should_act_as_tree
96 enumeration = Enumeration.find(4)
96 enumeration = Enumeration.find(4)
97
97
98 assert enumeration.respond_to?(:parent)
98 assert enumeration.respond_to?(:parent)
99 assert enumeration.respond_to?(:children)
99 assert enumeration.respond_to?(:children)
100 end
100 end
101
101
102 def test_is_override
102 def test_is_override
103 # Defaults to off
103 # Defaults to off
104 enumeration = Enumeration.find(4)
104 enumeration = Enumeration.find(4)
105 assert !enumeration.is_override?
105 assert !enumeration.is_override?
106
106
107 # Setup as an override
107 # Setup as an override
108 enumeration.parent = Enumeration.find(5)
108 enumeration.parent = Enumeration.find(5)
109 assert enumeration.is_override?
109 assert enumeration.is_override?
110 end
110 end
111
112 def test_get_subclasses
113 classes = Enumeration.get_subclasses
114 assert_include IssuePriority, classes
115 assert_include DocumentCategory, classes
116 assert_include TimeEntryActivity, classes
117
118 classes.each do |klass|
119 assert_equal Enumeration, klass.superclass
120 end
121 end
111 end
122 end
General Comments 0
You need to be logged in to leave comments. Login now