2011-08-21 01:14:40 +00:00
|
|
|
# Redmine - project management software
|
2016-03-13 10:30:10 +00:00
|
|
|
# Copyright (C) 2006-2016 Jean-Philippe Lang
|
2007-03-12 17:59:02 +00: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 01:14:40 +00:00
|
|
|
#
|
2007-03-12 17:59:02 +00: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 01:14:40 +00:00
|
|
|
#
|
2007-03-12 17:59:02 +00: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 10:26:12 +00:00
|
|
|
include Redmine::SubclassFactory
|
|
|
|
|
|
2014-10-29 22:30:25 +00:00
|
|
|
default_scope lambda {order(:position)}
|
2011-08-21 01:14:40 +00:00
|
|
|
|
2009-10-21 22:34:34 +00:00
|
|
|
belongs_to :project
|
2011-08-21 01:14:40 +00:00
|
|
|
|
2016-04-17 06:57:20 +00:00
|
|
|
acts_as_positioned :scope => :parent_id
|
2009-10-21 22:34:22 +00:00
|
|
|
acts_as_customizable
|
2014-10-30 08:01:19 +00:00
|
|
|
acts_as_tree
|
2007-10-04 17:04:50 +00:00
|
|
|
|
2007-03-12 17:59:02 +00:00
|
|
|
before_destroy :check_integrity
|
2011-09-21 04:50:58 +00:00
|
|
|
before_save :check_default
|
2011-08-21 01:14:40 +00:00
|
|
|
|
2011-12-11 10:26:12 +00:00
|
|
|
attr_protected :type
|
|
|
|
|
|
2009-05-30 23:30:36 +00:00
|
|
|
validates_presence_of :name
|
2009-10-21 22:34:45 +00:00
|
|
|
validates_uniqueness_of :name, :scope => [:type, :project_id]
|
2007-07-16 17:16:49 +00:00
|
|
|
validates_length_of :name, :maximum => 30
|
2007-10-04 17:04:50 +00:00
|
|
|
|
2012-12-07 10:12:47 +00:00
|
|
|
scope :shared, lambda { where(:project_id => nil) }
|
2014-10-29 21:49:33 +00:00
|
|
|
scope :sorted, lambda { order(:position) }
|
2012-12-07 10:12:47 +00:00
|
|
|
scope :active, lambda { where(:active => true) }
|
2013-05-01 16:43:51 +00:00
|
|
|
scope :system, lambda { where(:project_id => nil) }
|
2012-05-25 19:48:22 +00:00
|
|
|
scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
|
2009-10-21 22:34:28 +00:00
|
|
|
|
2009-05-30 23:30:36 +00: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 19:48:22 +00:00
|
|
|
where(:is_default => true, :type => 'Enumeration').first
|
2009-05-30 23:30:36 +00:00
|
|
|
else
|
|
|
|
|
# STI classes are
|
2012-05-25 19:48:22 +00:00
|
|
|
where(:is_default => true).first
|
2009-05-30 23:30:36 +00:00
|
|
|
end
|
|
|
|
|
end
|
2011-08-21 01:14:40 +00:00
|
|
|
|
2009-05-30 23:30:36 +00:00
|
|
|
# Overloaded on concrete classes
|
2007-03-12 17:59:02 +00:00
|
|
|
def option_name
|
2009-05-30 23:30:36 +00:00
|
|
|
nil
|
|
|
|
|
end
|
2007-10-05 17:44:15 +00:00
|
|
|
|
2011-09-21 04:50:58 +00:00
|
|
|
def check_default
|
2008-12-12 13:32:39 +00:00
|
|
|
if is_default? && is_default_changed?
|
2014-01-08 04:57:38 +00:00
|
|
|
Enumeration.where({:type => type}).update_all({:is_default => false})
|
2008-12-12 13:32:39 +00:00
|
|
|
end
|
2007-10-05 17:44:15 +00:00
|
|
|
end
|
2011-08-21 01:14:40 +00:00
|
|
|
|
2009-05-30 23:30:36 +00:00
|
|
|
# Overloaded on concrete classes
|
2008-06-17 19:10:54 +00:00
|
|
|
def objects_count
|
2009-05-30 23:30:36 +00:00
|
|
|
0
|
2008-06-17 19:10:54 +00:00
|
|
|
end
|
2011-08-21 01:14:40 +00:00
|
|
|
|
2008-06-17 19:10:54 +00:00
|
|
|
def in_use?
|
|
|
|
|
self.objects_count != 0
|
|
|
|
|
end
|
2009-10-21 22:34:34 +00:00
|
|
|
|
2014-04-07 08:06:50 +00:00
|
|
|
# Is this enumeration overriding a system level enumeration?
|
2009-10-21 22:34:34 +00:00
|
|
|
def is_override?
|
|
|
|
|
!self.parent.nil?
|
|
|
|
|
end
|
2011-08-21 01:14:40 +00:00
|
|
|
|
2008-06-17 19:10:54 +00:00
|
|
|
alias :destroy_without_reassign :destroy
|
2011-08-21 01:14:40 +00:00
|
|
|
|
2008-06-17 19:10:54 +00: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-30 23:30:36 +00:00
|
|
|
self.transfer_relations(reassign_to)
|
2008-06-17 19:10:54 +00:00
|
|
|
end
|
|
|
|
|
destroy_without_reassign
|
|
|
|
|
end
|
2011-08-21 01:14:40 +00:00
|
|
|
|
2007-11-04 11:15:04 +00:00
|
|
|
def <=>(enumeration)
|
|
|
|
|
position <=> enumeration.position
|
|
|
|
|
end
|
2011-08-21 01:14:40 +00:00
|
|
|
|
2007-10-01 08:44:17 +00:00
|
|
|
def to_s; name end
|
2009-05-30 23:30:36 +00: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 18:23:58 +00:00
|
|
|
subclasses
|
2009-05-30 23:30:36 +00:00
|
|
|
end
|
2009-10-21 22:34:45 +00:00
|
|
|
|
|
|
|
|
# Does the +new+ Hash override the previous Enumeration?
|
2014-04-06 13:32:35 +00:00
|
|
|
def self.overriding_change?(new, previous)
|
2009-10-21 22:34:45 +00:00
|
|
|
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 01:14:40 +00:00
|
|
|
|
2009-10-21 22:34:45 +00: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 01:14:40 +00:00
|
|
|
|
2016-04-17 06:57:20 +00:00
|
|
|
private
|
2015-09-30 18:35:50 +00:00
|
|
|
|
2007-03-12 17:59:02 +00:00
|
|
|
def check_integrity
|
2014-12-21 12:39:48 +00:00
|
|
|
raise "Cannot delete enumeration" if self.in_use?
|
2006-06-28 18:11:03 +00:00
|
|
|
end
|
2009-05-30 23:30:36 +00:00
|
|
|
|
2016-04-17 06:57:20 +00:00
|
|
|
# Overrides Redmine::Acts::Positioned#set_default_position so that enumeration overrides
|
2015-09-30 18:35:50 +00:00
|
|
|
# get the same position as the overriden enumeration
|
2016-04-17 06:57:20 +00:00
|
|
|
def set_default_position
|
|
|
|
|
if position.nil? && parent
|
|
|
|
|
self.position = parent.position
|
|
|
|
|
end
|
|
|
|
|
super
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Overrides Redmine::Acts::Positioned#update_position so that overrides get the same
|
|
|
|
|
# position as the overriden enumeration
|
|
|
|
|
def update_position
|
|
|
|
|
super
|
|
|
|
|
if position_changed?
|
|
|
|
|
self.class.where.not(:parent_id => nil).update_all(
|
|
|
|
|
"position = coalesce((
|
|
|
|
|
select position
|
|
|
|
|
from (select id, position from enumerations) as parent
|
|
|
|
|
where parent_id = parent.id), 1)"
|
|
|
|
|
)
|
2015-09-30 18:35:50 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-04-17 06:57:20 +00:00
|
|
|
# Overrides Redmine::Acts::Positioned#remove_position so that enumeration overrides
|
2015-09-30 18:35:50 +00:00
|
|
|
# get the same position as the overriden enumeration
|
2016-04-17 06:57:20 +00:00
|
|
|
def remove_position
|
2015-09-30 18:35:50 +00:00
|
|
|
if parent_id.blank?
|
|
|
|
|
super
|
|
|
|
|
end
|
|
|
|
|
end
|
2006-06-28 18:11:03 +00:00
|
|
|
end
|
2009-05-30 23:30:36 +00:00
|
|
|
|
|
|
|
|
# Force load the subclasses in development mode
|
|
|
|
|
require_dependency 'time_entry_activity'
|
|
|
|
|
require_dependency 'document_category'
|
|
|
|
|
require_dependency 'issue_priority'
|