2011-08-21 05:14:40 +04:00
|
|
|
# Redmine - project management software
|
2012-05-05 16:56:53 +04:00
|
|
|
# Copyright (C) 2006-2012 Jean-Philippe Lang
|
2007-03-12 20:59:02 +03:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
2011-08-21 05:14:40 +04:00
|
|
|
#
|
2007-03-12 20:59:02 +03:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
2011-08-21 05:14:40 +04:00
|
|
|
#
|
2007-03-12 20:59:02 +03:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
class Enumeration < ActiveRecord::Base
|
2011-12-11 14:26:12 +04:00
|
|
|
include Redmine::SubclassFactory
|
|
|
|
|
2009-11-21 15:00:49 +03:00
|
|
|
default_scope :order => "#{Enumeration.table_name}.position ASC"
|
2011-08-21 05:14:40 +04:00
|
|
|
|
2009-10-22 02:34:34 +04:00
|
|
|
belongs_to :project
|
2011-08-21 05:14:40 +04:00
|
|
|
|
2009-05-31 03:30:36 +04:00
|
|
|
acts_as_list :scope => 'type = \'#{type}\''
|
2009-10-22 02:34:22 +04:00
|
|
|
acts_as_customizable
|
2009-10-22 02:34:34 +04:00
|
|
|
acts_as_tree :order => 'position ASC'
|
2007-10-04 21:04:50 +04:00
|
|
|
|
2007-03-12 20:59:02 +03:00
|
|
|
before_destroy :check_integrity
|
2011-09-21 08:50:58 +04:00
|
|
|
before_save :check_default
|
2011-08-21 05:14:40 +04:00
|
|
|
|
2011-12-11 14:26:12 +04:00
|
|
|
attr_protected :type
|
|
|
|
|
2009-05-31 03:30:36 +04:00
|
|
|
validates_presence_of :name
|
2009-10-22 02:34:45 +04:00
|
|
|
validates_uniqueness_of :name, :scope => [:type, :project_id]
|
2007-07-16 21:16:49 +04:00
|
|
|
validates_length_of :name, :maximum => 30
|
2007-10-04 21:04:50 +04:00
|
|
|
|
2012-12-07 14:12:47 +04:00
|
|
|
scope :shared, lambda { where(:project_id => nil) }
|
|
|
|
scope :sorted, lambda { order("#{table_name}.position ASC") }
|
|
|
|
scope :active, lambda { where(:active => true) }
|
2012-05-25 23:48:22 +04:00
|
|
|
scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
|
2009-10-22 02:34:28 +04:00
|
|
|
|
2009-05-31 03:30:36 +04:00
|
|
|
def self.default
|
|
|
|
# Creates a fake default scope so Enumeration.default will check
|
|
|
|
# it's type. STI subclasses will automatically add their own
|
|
|
|
# types to the finder.
|
|
|
|
if self.descends_from_active_record?
|
2012-05-25 23:48:22 +04:00
|
|
|
where(:is_default => true, :type => 'Enumeration').first
|
2009-05-31 03:30:36 +04:00
|
|
|
else
|
|
|
|
# STI classes are
|
2012-05-25 23:48:22 +04:00
|
|
|
where(:is_default => true).first
|
2009-05-31 03:30:36 +04:00
|
|
|
end
|
|
|
|
end
|
2011-08-21 05:14:40 +04:00
|
|
|
|
2009-05-31 03:30:36 +04:00
|
|
|
# Overloaded on concrete classes
|
2007-03-12 20:59:02 +03:00
|
|
|
def option_name
|
2009-05-31 03:30:36 +04:00
|
|
|
nil
|
|
|
|
end
|
2007-10-05 21:44:15 +04:00
|
|
|
|
2011-09-21 08:50:58 +04:00
|
|
|
def check_default
|
2008-12-12 16:32:39 +03:00
|
|
|
if is_default? && is_default_changed?
|
2012-05-25 23:48:22 +04:00
|
|
|
Enumeration.update_all({:is_default => false}, {:type => type})
|
2008-12-12 16:32:39 +03:00
|
|
|
end
|
2007-10-05 21:44:15 +04:00
|
|
|
end
|
2011-08-21 05:14:40 +04:00
|
|
|
|
2009-05-31 03:30:36 +04:00
|
|
|
# Overloaded on concrete classes
|
2008-06-17 23:10:54 +04:00
|
|
|
def objects_count
|
2009-05-31 03:30:36 +04:00
|
|
|
0
|
2008-06-17 23:10:54 +04:00
|
|
|
end
|
2011-08-21 05:14:40 +04:00
|
|
|
|
2008-06-17 23:10:54 +04:00
|
|
|
def in_use?
|
|
|
|
self.objects_count != 0
|
|
|
|
end
|
2009-10-22 02:34:34 +04:00
|
|
|
|
|
|
|
# Is this enumeration overiding a system level enumeration?
|
|
|
|
def is_override?
|
|
|
|
!self.parent.nil?
|
|
|
|
end
|
2011-08-21 05:14:40 +04:00
|
|
|
|
2008-06-17 23:10:54 +04:00
|
|
|
alias :destroy_without_reassign :destroy
|
2011-08-21 05:14:40 +04:00
|
|
|
|
2008-06-17 23:10:54 +04:00
|
|
|
# Destroy the enumeration
|
|
|
|
# If a enumeration is specified, objects are reassigned
|
|
|
|
def destroy(reassign_to = nil)
|
|
|
|
if reassign_to && reassign_to.is_a?(Enumeration)
|
2009-05-31 03:30:36 +04:00
|
|
|
self.transfer_relations(reassign_to)
|
2008-06-17 23:10:54 +04:00
|
|
|
end
|
|
|
|
destroy_without_reassign
|
|
|
|
end
|
2011-08-21 05:14:40 +04:00
|
|
|
|
2007-11-04 14:15:04 +03:00
|
|
|
def <=>(enumeration)
|
|
|
|
position <=> enumeration.position
|
|
|
|
end
|
2011-08-21 05:14:40 +04:00
|
|
|
|
2007-10-01 12:44:17 +04:00
|
|
|
def to_s; name end
|
2009-05-31 03:30:36 +04:00
|
|
|
|
|
|
|
# Returns the Subclasses of Enumeration. Each Subclass needs to be
|
|
|
|
# required in development mode.
|
|
|
|
#
|
|
|
|
# Note: subclasses is protected in ActiveRecord
|
|
|
|
def self.get_subclasses
|
2012-02-24 22:23:58 +04:00
|
|
|
subclasses
|
2009-05-31 03:30:36 +04:00
|
|
|
end
|
2009-10-22 02:34:45 +04:00
|
|
|
|
|
|
|
# Does the +new+ Hash override the previous Enumeration?
|
|
|
|
def self.overridding_change?(new, previous)
|
|
|
|
if (same_active_state?(new['active'], previous.active)) && same_custom_values?(new,previous)
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Does the +new+ Hash have the same custom values as the previous Enumeration?
|
|
|
|
def self.same_custom_values?(new, previous)
|
|
|
|
previous.custom_field_values.each do |custom_value|
|
|
|
|
if custom_value.value != new["custom_field_values"][custom_value.custom_field_id.to_s]
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
2011-08-21 05:14:40 +04:00
|
|
|
|
2009-10-22 02:34:45 +04:00
|
|
|
# Are the new and previous fields equal?
|
|
|
|
def self.same_active_state?(new, previous)
|
|
|
|
new = (new == "1" ? true : false)
|
|
|
|
return new == previous
|
|
|
|
end
|
2011-08-21 05:14:40 +04:00
|
|
|
|
2007-03-12 20:59:02 +03:00
|
|
|
private
|
|
|
|
def check_integrity
|
2008-06-17 23:10:54 +04:00
|
|
|
raise "Can't delete enumeration" if self.in_use?
|
2006-06-28 22:11:03 +04:00
|
|
|
end
|
2009-05-31 03:30:36 +04:00
|
|
|
|
2006-06-28 22:11:03 +04:00
|
|
|
end
|
2009-05-31 03:30:36 +04:00
|
|
|
|
|
|
|
# Force load the subclasses in development mode
|
|
|
|
require_dependency 'time_entry_activity'
|
|
|
|
require_dependency 'document_category'
|
|
|
|
require_dependency 'issue_priority'
|