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