1 # redMine - project management software
2 # Copyright (C) 2006 Jean-Philippe Lang
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
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
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 class Enumeration < ActiveRecord::Base
19 default_scope :order => "#{Enumeration.table_name}.position ASC"
23 acts_as_list :scope => 'type = \'#{type}\''
25 acts_as_tree :order => 'position ASC'
27 before_destroy :check_integrity
29 validates_presence_of :name
30 validates_uniqueness_of :name, :scope => [:type, :project_id]
31 validates_length_of :name, :maximum => 30
33 # Backwards compatiblity named_scopes.
34 # Can be removed post-0.9
35 named_scope :priorities, :conditions => { :type => "IssuePriority" }, :order => 'position' do
36 ActiveSupport::Deprecation.warn("Enumeration#priorities is deprecated, use the IssuePriority class. (#{Redmine::Info.issue(3007)})")
38 find(:first, :conditions => { :is_default => true })
42 named_scope :document_categories, :conditions => { :type => "DocumentCategory" }, :order => 'position' do
43 ActiveSupport::Deprecation.warn("Enumeration#document_categories is deprecated, use the DocumentCategories class. (#{Redmine::Info.issue(3007)})")
45 find(:first, :conditions => { :is_default => true })
49 named_scope :activities, :conditions => { :type => "TimeEntryActivity" }, :order => 'position' do
50 ActiveSupport::Deprecation.warn("Enumeration#activities is deprecated, use the TimeEntryActivity class. (#{Redmine::Info.issue(3007)})")
52 find(:first, :conditions => { :is_default => true })
56 named_scope :values, lambda {|type| { :conditions => { :type => type }, :order => 'position' } } do
58 find(:first, :conditions => { :is_default => true })
61 # End backwards compatiblity named_scopes
63 named_scope :shared, :conditions => { :project_id => nil }
64 named_scope :active, :conditions => { :active => true }
67 # Creates a fake default scope so Enumeration.default will check
68 # it's type. STI subclasses will automatically add their own
69 # types to the finder.
70 if self.descends_from_active_record?
71 find(:first, :conditions => { :is_default => true, :type => 'Enumeration' })
74 find(:first, :conditions => { :is_default => true })
78 # Overloaded on concrete classes
83 # Backwards compatiblity. Can be removed post-0.9
85 ActiveSupport::Deprecation.warn("Enumeration#opt is deprecated, use the STI classes now. (#{Redmine::Info.issue(3007)})")
90 if is_default? && is_default_changed?
91 Enumeration.update_all("is_default = #{connection.quoted_false}", {:type => type})
95 # Overloaded on concrete classes
101 self.objects_count != 0
104 # Is this enumeration overiding a system level enumeration?
109 alias :destroy_without_reassign :destroy
111 # Destroy the enumeration
112 # If a enumeration is specified, objects are reassigned
113 def destroy(reassign_to = nil)
114 if reassign_to && reassign_to.is_a?(Enumeration)
115 self.transfer_relations(reassign_to)
117 destroy_without_reassign
121 position <=> enumeration.position
126 # Returns the Subclasses of Enumeration. Each Subclass needs to be
127 # required in development mode.
129 # Note: subclasses is protected in ActiveRecord
130 def self.get_subclasses
131 @@subclasses[Enumeration]
134 # Does the +new+ Hash override the previous Enumeration?
135 def self.overridding_change?(new, previous)
136 if (same_active_state?(new['active'], previous.active)) && same_custom_values?(new,previous)
143 # Does the +new+ Hash have the same custom values as the previous Enumeration?
144 def self.same_custom_values?(new, previous)
145 previous.custom_field_values.each do |custom_value|
146 if custom_value.value != new["custom_field_values"][custom_value.custom_field_id.to_s]
154 # Are the new and previous fields equal?
155 def self.same_active_state?(new, previous)
156 new = (new == "1" ? true : false)
157 return new == previous
162 raise "Can't delete enumeration" if self.in_use?
167 # Force load the subclasses in development mode
168 require_dependency 'time_entry_activity'
169 require_dependency 'document_category'
170 require_dependency 'issue_priority'