Chris@0
|
1 # redMine - project management software
|
Chris@0
|
2 # Copyright (C) 2006 Jean-Philippe Lang
|
Chris@0
|
3 #
|
Chris@0
|
4 # This program is free software; you can redistribute it and/or
|
Chris@0
|
5 # modify it under the terms of the GNU General Public License
|
Chris@0
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@0
|
7 # of the License, or (at your option) any later version.
|
Chris@0
|
8 #
|
Chris@0
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 # GNU General Public License for more details.
|
Chris@0
|
13 #
|
Chris@0
|
14 # You should have received a copy of the GNU General Public License
|
Chris@0
|
15 # along with this program; if not, write to the Free Software
|
Chris@0
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@0
|
17
|
Chris@0
|
18 class Enumeration < ActiveRecord::Base
|
Chris@0
|
19 default_scope :order => "#{Enumeration.table_name}.position ASC"
|
Chris@0
|
20
|
Chris@0
|
21 belongs_to :project
|
Chris@0
|
22
|
Chris@0
|
23 acts_as_list :scope => 'type = \'#{type}\''
|
Chris@0
|
24 acts_as_customizable
|
Chris@0
|
25 acts_as_tree :order => 'position ASC'
|
Chris@0
|
26
|
Chris@0
|
27 before_destroy :check_integrity
|
Chris@0
|
28
|
Chris@0
|
29 validates_presence_of :name
|
Chris@0
|
30 validates_uniqueness_of :name, :scope => [:type, :project_id]
|
Chris@0
|
31 validates_length_of :name, :maximum => 30
|
Chris@0
|
32
|
Chris@0
|
33 named_scope :shared, :conditions => { :project_id => nil }
|
Chris@0
|
34 named_scope :active, :conditions => { :active => true }
|
Chris@507
|
35 named_scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
|
Chris@0
|
36
|
Chris@0
|
37 def self.default
|
Chris@0
|
38 # Creates a fake default scope so Enumeration.default will check
|
Chris@0
|
39 # it's type. STI subclasses will automatically add their own
|
Chris@0
|
40 # types to the finder.
|
Chris@0
|
41 if self.descends_from_active_record?
|
Chris@0
|
42 find(:first, :conditions => { :is_default => true, :type => 'Enumeration' })
|
Chris@0
|
43 else
|
Chris@0
|
44 # STI classes are
|
Chris@0
|
45 find(:first, :conditions => { :is_default => true })
|
Chris@0
|
46 end
|
Chris@0
|
47 end
|
Chris@0
|
48
|
Chris@0
|
49 # Overloaded on concrete classes
|
Chris@0
|
50 def option_name
|
Chris@0
|
51 nil
|
Chris@0
|
52 end
|
Chris@0
|
53
|
Chris@0
|
54 def before_save
|
Chris@0
|
55 if is_default? && is_default_changed?
|
Chris@0
|
56 Enumeration.update_all("is_default = #{connection.quoted_false}", {:type => type})
|
Chris@0
|
57 end
|
Chris@0
|
58 end
|
Chris@0
|
59
|
Chris@0
|
60 # Overloaded on concrete classes
|
Chris@0
|
61 def objects_count
|
Chris@0
|
62 0
|
Chris@0
|
63 end
|
Chris@0
|
64
|
Chris@0
|
65 def in_use?
|
Chris@0
|
66 self.objects_count != 0
|
Chris@0
|
67 end
|
Chris@0
|
68
|
Chris@0
|
69 # Is this enumeration overiding a system level enumeration?
|
Chris@0
|
70 def is_override?
|
Chris@0
|
71 !self.parent.nil?
|
Chris@0
|
72 end
|
Chris@0
|
73
|
Chris@0
|
74 alias :destroy_without_reassign :destroy
|
Chris@0
|
75
|
Chris@0
|
76 # Destroy the enumeration
|
Chris@0
|
77 # If a enumeration is specified, objects are reassigned
|
Chris@0
|
78 def destroy(reassign_to = nil)
|
Chris@0
|
79 if reassign_to && reassign_to.is_a?(Enumeration)
|
Chris@0
|
80 self.transfer_relations(reassign_to)
|
Chris@0
|
81 end
|
Chris@0
|
82 destroy_without_reassign
|
Chris@0
|
83 end
|
Chris@0
|
84
|
Chris@0
|
85 def <=>(enumeration)
|
Chris@0
|
86 position <=> enumeration.position
|
Chris@0
|
87 end
|
Chris@0
|
88
|
Chris@0
|
89 def to_s; name end
|
Chris@0
|
90
|
Chris@0
|
91 # Returns the Subclasses of Enumeration. Each Subclass needs to be
|
Chris@0
|
92 # required in development mode.
|
Chris@0
|
93 #
|
Chris@0
|
94 # Note: subclasses is protected in ActiveRecord
|
Chris@0
|
95 def self.get_subclasses
|
Chris@0
|
96 @@subclasses[Enumeration]
|
Chris@0
|
97 end
|
Chris@0
|
98
|
Chris@0
|
99 # Does the +new+ Hash override the previous Enumeration?
|
Chris@0
|
100 def self.overridding_change?(new, previous)
|
Chris@0
|
101 if (same_active_state?(new['active'], previous.active)) && same_custom_values?(new,previous)
|
Chris@0
|
102 return false
|
Chris@0
|
103 else
|
Chris@0
|
104 return true
|
Chris@0
|
105 end
|
Chris@0
|
106 end
|
Chris@0
|
107
|
Chris@0
|
108 # Does the +new+ Hash have the same custom values as the previous Enumeration?
|
Chris@0
|
109 def self.same_custom_values?(new, previous)
|
Chris@0
|
110 previous.custom_field_values.each do |custom_value|
|
Chris@0
|
111 if custom_value.value != new["custom_field_values"][custom_value.custom_field_id.to_s]
|
Chris@0
|
112 return false
|
Chris@0
|
113 end
|
Chris@0
|
114 end
|
Chris@0
|
115
|
Chris@0
|
116 return true
|
Chris@0
|
117 end
|
Chris@0
|
118
|
Chris@0
|
119 # Are the new and previous fields equal?
|
Chris@0
|
120 def self.same_active_state?(new, previous)
|
Chris@0
|
121 new = (new == "1" ? true : false)
|
Chris@0
|
122 return new == previous
|
Chris@0
|
123 end
|
Chris@0
|
124
|
Chris@0
|
125 private
|
Chris@0
|
126 def check_integrity
|
Chris@0
|
127 raise "Can't delete enumeration" if self.in_use?
|
Chris@0
|
128 end
|
Chris@0
|
129
|
Chris@0
|
130 end
|
Chris@0
|
131
|
Chris@0
|
132 # Force load the subclasses in development mode
|
Chris@0
|
133 require_dependency 'time_entry_activity'
|
Chris@0
|
134 require_dependency 'document_category'
|
Chris@0
|
135 require_dependency 'issue_priority'
|